Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8882839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:40:33+00:00 2026-06-14T20:40:33+00:00

I am trying to implement a piece of software that mix piano samples. I

  • 0

I am trying to implement a piece of software that mix piano samples. I want to create a wav file containing one sound, the other and a mix of both.

I trunc the samples at one second so I have the following :
[one second of sound 1][one second of sound 2][one second of soud 1 + sound 2]

The problem is that there is a weird sound artifact at each transition. Does somebody know where it comes from ?

Thanks in advance.

Here is the code I am using :

#include "stdafx.h"
#include <cstdlib>
#include <sndfile.h>


int _tmain(int argc, _TCHAR* argv[])
{
    SF_INFO sInfo1;
    SF_INFO sInfo2;
    SF_INFO sInfo3;

    SNDFILE *sFile1 = NULL;
    SNDFILE *sFile2 = NULL;
    SNDFILE *sFile3 = NULL;

    double *buff1;
    double *buff2;
    double *buff3;

    sf_count_t count1 = 0;
    sf_count_t count2 = 0;
    sf_count_t count3 = 0;

    buff1 = (double*)malloc(88200*sizeof(double));
    buff2 = (double*)malloc(88200*sizeof(double));
    buff3 = (double*)malloc(88200*sizeof(double));

    sInfo1.format = 0;
    sInfo2.format = 0;
    sFile1 = sf_open("C:/samples/mezzo forte/mcg_mf_022.wav", SFM_READ, &sInfo1);
    sFile2 = sf_open("C:/samples/mezzo forte/mcg_mf_046.wav", SFM_READ, &sInfo2);

    sInfo3 = sInfo2;
    sFile3 = sf_open("C:/samples/test1.wav", SFM_WRITE, &sInfo3);

    count1 = sf_read_double(sFile1, buff1, 88200);
    count2 = sf_read_double(sFile2, buff2, 88200);

    for(int i=0; i<88200; i++)
    {
        buff3[i] = buff1[i] + buff2[i] - ( buff1[i] * buff2[i] );
    }

    count1 = sf_write_double(sFile3, buff1, 88200);
    count2 = sf_write_double(sFile3, buff2, 88200);
    count3 = sf_write_double(sFile3, buff3, 88200);

    sf_close(sFile1);
    sf_close(sFile2);
    sf_close(sFile3);

    free(buff1);
    free(buff2);
    free(buff3);

    //getchar();
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:40:34+00:00Added an answer on June 14, 2026 at 8:40 pm

    This is not a libsndfile issue. This is a general audio synthesis question.

    Whenever you truncate a sample to an arbitrary value (e.g., 1 second), you can expect to hear (or see, if you were to load the resulting file in, say, Audacity and check-out the spectrogram and waveform at the transition boundaries) an artifact. This is because of the abrupt change in the sample waveform. I will skip an attempt at discussing the issues surrounding bandlimiting, and simply urge you to do a rapid fade-out of the samples rather than just truncating them. This forces your audio waveform to [rapidly] approach zero just before the transition– smoothly.

    You may find that you also need to fade-in (or cross-fade, if you overlap the smooth transitions) the next sample by weighting its first few samples by a value close to zero, and ramping that up [rapidly, or you’ll miss the attack] to full-scale. First, start with just doing a rapid fade-out before each transition, and only if you need to, worry about the fade-in. The implementation is the same (a sample scaling value that ramps-up or -down), but it’s the arbitrary truncation (ending) of the samples at 1 second that’s likely causing the most trouble.

    You’ll need to play with a few different parameters to see what works. E.g., you might want to start with a linear ramp-down rather than an exponential or parabolic decay function for simplicity. In any case, you will have to decide how many samples (or how many milliseconds) from the transition point to start scaling down the sample values.

    EDIT:

    I initially assumed that your mixing was fine, because you only asked about the transition artifacts. My answer addresses that. It is worthy of note, however, that I’ve no clue why you’re mixing into the buff3 exactly as you are, given your stated aim. If I understand correctly that you want to simply combine the two sounds into buff3, just add the other two corresponding samples together and ensure they don’t clip (i.e., exceed the range [-1.0, +1.0]). libsndfile automatically “guards” against clipping, but it’s only able to set, e.g., a sample value above 1.0 to 1.0– not ensure both audio waveforms are mixed equally.

    If the level on either of your two input sounds was high enough, the simple additive mix would clip, which would be another type of “artifact” (except likely to ruin the whole sound, so in this particular case, you probably would have noted as much). For general mixing, however, your loop would be:

    for(int i=0; i<88200; i++)
    {
        /* multiply sum of signals by factor slightly
           less than reciprocal of their count to guard
           also against floating-point error. */
    
        buff3[i] = (buff1[i] + buff2[i]) * 0.499;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take the following piece of javascript that is trying to implement a class: function
I'm trying to implement a piece of functionality that will let the user to
I am trying to implement SSE vectorization on a piece of code for which
Trying to implement 3-layer (not: tier, I just want to separate my project logically,
I am trying to implement a moving photo slider that is no jQuery ,
Let's say I'm trying to implement a Chess Game. I'd create my Chess classes
I want to implement some sort of lookup table in C++ that will act
I was trying to implement the Sorry! board game using C++ such that 4
I'm trying to implement a piece of code to synchronously start looped service in
I am trying to parse an XML file that is sitting on the sdCard

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.