adamstark/AudioFile

Add documentation on how to fill a buffer with samples

ScienceMarc opened this issue · 3 comments

// 1. Create an AudioBuffer
// (BTW, AudioBuffer is just a vector of vectors)

AudioFile::AudioBuffer buffer;

// 2. Set to (e.g.) two channels
buffer.resize (2);

// 3. Set number of samples per channel
buffer[0].resize (100000);
buffer[1].resize (100000);

// 4. do something here to fill the buffer with samples

// 5. Put into the AudioFile object
bool ok = audioFile.setAudioBuffer (buffer);

Step 4 is way too vague. How do I actually add samples?

Hi Marc,

Thanks for getting in touch. I will perhaps update this for the next version, or maybe with some example code. But for now, here is what I would do at step 4 to fill a buffer with a sine wave... (i've added some extra variables for clarity):

int numChannels = 2;
int numSamplesPerChannel = 100000;
float sampleRate = 44100.f;
float frequency = 440;

for (int i = 0; i < numSamplesPerChannel; i++)
{
        float sample = sinf (2. * M_PI * ((float) i / sampleRate) * frequency) ;
        
        for (int channel = 0; channel < numChannels; channel++)
             buffer[channel][i] = sample * 0.5;
}

I haven't tested this - just wrote it here, but it should work. Hope this helps.

Adam

Thank you so much for your quick response. This worked.

I've now added this to the main documentation in the new version