0

I'm doing some simple simple programs with libsndfile and currently and trying to find a way to print silence in an audio file- other than muting the out of another file. Ostensibly, I'm making a drum machine. At the given moment, I am only able to write the length of an inputed audio file and append them onto each other if I so choose. Would love to have some more insight on this seemingly super simple task.. or a slap in the face if it is actually so simple.

Thanks!

  • I understand silence is zeros. I think my question was mainly, how would I read/write from an array I made rather than directly from a file? I now figure that I get rid of the while loop here: while ((readcount = sf_read_double (sndfile, data, BUFFER_LEN)) and instead pass in my own array using the SNDFILE* for the output file sf_write_double (SNDFILE sndfile, double *ptrToMyArray, sf_count_t frames); – pritchardsmith Jun 17 '13 at 01:37
  • Sorry I'm having trouble delineating code. I'm obviously new to this site. – pritchardsmith Jun 17 '13 at 01:41
  • @ Erik de Castro Lopo – pritchardsmith Jun 17 '13 at 01:42
  • Figured it out! Thanks again @ Eric de Castro Lopo ---------- ---------- { int size; double frames[sfinfo.frames]; size = (sizeof(frames)/sizeof(double)); printf("frames in audio file: %d\n", size); } – pritchardsmith Jun 17 '13 at 07:18

1 Answers1

1

Silence in an audio file is simply a bunch of consecutive zero valued samples.

To insert N frames of silence at the current write postion of a file is as simple as:

void sf_insert_silence (SNDFILE *file, int channels, int frames)
{ short silence [frames * channels];

  memset (silence, 0, sizeof (silence));
  sf_writef_short (sndfile, silence, frames);
}
  • thank you very much. I guess my next question would be how would I read a short sample into an array to be later inserted into a larger array of silence? Meaning, determine the length of audio one wants to write, and at certain intervals, read into the file the snare or hi hat sample in the array or being pointed to. When I try to clear space for an array of doubles for the frames in an audio file, I am unable to get the amount of doubles needed from sfinfo.frames because the return is sf_count_t. @ Erik de Castro Lopo – pritchardsmith Jun 17 '13 at 05:29
  • @pritchardsmith sf_count_t is basically just an integer type. This is C (or possibly C++) so converting from sf_count_t is trivial. Just use it lke you would any other number type. – Erik de Castro Lopo Jun 17 '13 at 21:11
  • @Eric de Castro Lopo when I used it as an int, I had errors about expected types. Though, after some fiddling it seemed like a type double and worked in the code when I used it that way. – pritchardsmith Jun 24 '13 at 03:41
  • Had it been 8bit audio, silence should be 128 instead of 0. – wyc Oct 24 '17 at 12:35