0

I'm trying to take a wav file and create gaps of silence in it at 1-second intervals. The gaps of silence aren't for pausing the file and then playing it again, they serve a 'mute' function, so if the input wav file was 10 seconds long then the output file would be 10 seconds long too.

I have access to all properties of the input wav file such as buffer size, I'm afraid I just have no idea how to go about reading from said file in this context. My best guess is something like this:

int gapLength = 1 * sampleRate;

for (int f = 0; f < numOfSamples; f++) {
    if (f <= gapLength) {
        buffer[f] = silence
    }
    else {
        buffer[f] = sample[f] from audio input
    } 
}

sf_writef_double(sndFile, buffer, numOfSamples);

Going off this, I need to find out how to have the for loop check for the gap again after the first time, and some way to obtain sample[f] from the input wav file - maybe I could iterate through both wav files at the same time? Any advice at all would be a big help.

  • 2
    This question contains multiple questions, and needs to be simplified. It provides a code example that outlines a specific need for periodic silencing of audio data. Yet, on closer reading the question is actually about how to read WAV file format. You must decide exactly what you are asking about, if you want to receive focused answers. – paddy May 18 '20 at 02:44

2 Answers2

1

If you have a buffer with all your samples already filled, you can just use the modulo operator to determine where you are at in a full cycle:

int gapLength = 1 * sampleRate;
int repeatLength = 2 * gapLength;

for (int f = 0; f < numOfSamples; f++) {
    if (f % repeatLength < gapLength) {
        buffer[f] = silence;
    }
}

Note that I've split this into two parts: a length for the "gap" and a length for the whole repeating cycle. It assumes that the first part of this cycle contains the silence, but it's straight forward to change that. I'm sure you get the idea.

paddy
  • 60,864
  • 6
  • 61
  • 103
0

The first thing I would do is write a libsndfile-based .wav-file copying program -- i.e. it sf_open()'s a .wav file for reading, and creates/sf_open()'s a second .wav file for writing (with the same parameters as the input file), and sf_readf()'s the frames of audio from the input file and sf_writef()'s them directly out to the output file.

If it's working correctly, after you run, it you should have a second file that sounds identical to the original .wav file it read in.

Once that's working, it's just a matter of inserting some code into the read/write loop such that some of the frames you read into RAM are overwritten by 0.0f's before they are written out again.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234