This project has moved. For the latest updates, please go here.

sound buffer size

Jan 18, 2014 at 7:52 PM
can I increase my sound buffer size?

right now I am getting

17640 bytes coming in at a time.

then

i do this code:

int bytesRecorded = e.BytesRecorded;
    buffer1 = new double[(8192)];
    //  WriteToFile(buffer, bytesRecorded);
    int tempint = 0;
    for (int index = 0; index < 16384; index += 2)
    {

        short sample = (short)((buffer[index + 1] << 8) |
                                buffer[index + 0]);

        float sample32 = sample / 32768f;
        buffer1[tempint] = (double)sample32;
        tempint++;
and then I send it to my FFT

my FFT only works at 2^(number) samples at a time

so I can only do 8192 samples at a time

I wanted to do more samples at a time, so I can get an higher accuracy
Jan 21, 2014 at 2:28 PM
can anyone help me?
Jan 21, 2014 at 10:43 PM
Use a SampleChannel or ToSampleProvider to capture the audio samples using an event/callback function.

In the event/callback function you capture each sample (L and R values) one by one and add them to a List<> or an array with a size of 2048. When the sample count reaches 2048, send the data to the FFT and reset the array back to zero.
Jan 22, 2014 at 4:33 AM
K24A3

I do not understand...
can you upload some sample code?
also 2048 samples is less than 8192 samples
I want to do 65536 samples or more
Jan 22, 2014 at 5:50 AM
Edited Jan 22, 2014 at 5:59 AM
This is for playback, but recording should be a similar setup
Init()
{
    WaveStream _waveStream = new AudioFileReader(sAudioFile);
    SampleChannel _waveChannel =  new SampleChannel(_waveStream, true);

    MeteringSampleProvider _postVolumeMeter;
    _waveChannel.PreVolumeMeter += OnPreVolumeMeter;
    _postVolumeMeter = new MeteringSampleProvider(_waveChannel);
    _postVolumeMeter.StreamVolume += OnPostVolumeMeter;
}


List<float> FFTList = new FFTList();

// This will be called 65536 or more times
void OnPostVolumeMeter(object sender, StreamVolumeEventArgs e)
{
    FFTList.Add(e.MaxSampleValues[0]); // L
    FFTList.Add(e.MaxSampleValues[1]); // R

    if(FFTList.Count>=2048)
    {
        //process FFT Data.... 2048 samples at a time
        SendFFTDataToFFTControl(FFTList.Clone()); // YOUR FFT FUNCTION
        FFTList.Clear();
    }
}
&#43 is a plus symbol
Jan 22, 2014 at 5:05 PM
could you add some comments?
Jan 22, 2014 at 5:07 PM
_postVolumeMeter.StreamVolume += OnPostVolumeMeter;

is a event?
so does it happen when the microphone buffer is full?
Coordinator
Jan 22, 2014 at 7:43 PM
you can adjust the buffer sizes for recording (e.g you could increase latency), but with some types of recording device this is not necessarily possible or desirable. So the general approach suggested by K24A3 is the right one. However, it will not work as desired, since you are doing the FFT only on max values.

Put received samples into a buffer and when you have the desired amount, perform the FFT. If you look through the NAudio demo code, you'll see examples of this in action. I often call the class that does this a "SampleAggregator"
Jan 22, 2014 at 11:47 PM
MarthHeath

how do i change the buffer sizes?
Jan 23, 2014 at 5:44 AM
Specify it in the constructor. For example:
            _wavein = new WaveIn()
            {
                BufferMilliseconds = 100; // Default 100
            };  
Jan 23, 2014 at 1:44 PM
Edited Jan 23, 2014 at 2:31 PM
update

ok i just tried this

void Voice()
    {
        //Byte[] buffer = new Byte[10];
        // sound from the Mic

        int deviceNumber = 0;
        waveIn = new WaveIn();
        waveIn.BufferMilliseconds = 32768;
        waveIn.DeviceNumber = deviceNumber;
        waveIn.DataAvailable += waveIn_DataAvailable;
        waveIn.WaveFormat = new WaveFormat(44100, NAudio.Wave.WaveIn.GetCapabilities(deviceNumber).Channels);
        waveIn.StartRecording();


    }
and now I get now my buffer is 1485304.
but how?

I am sampling at 44100 Hz.
which is 2.267573e-5 S.
and
32768ms * 0.001S = 32.768S
so my buffer should only be 1445068.8 samples big
but it is 1485304 samples big?
I am using the debugger to check the size of my buffer
I did (32768 * 0.001) / (1/44100) = 1445068.8 samples
what math am I doing wrong ? or is C# rounding weirdly
and how do I get my buffer to be 44100 sizes big ?
Coordinator
Jan 23, 2014 at 2:35 PM
yes, I really would recommend steering clear of upping the buffer sizes beyond a max of 500ms. The SampleAggregator approach I mentioned above is a more reliable approach