NAudio converts an input byte array into an array of two pairs

I am very new to NAudio, and I need to convert the input sample buffer from the input device to a doubling array that ranges from -1 to 1.

I create an input device as follows:

WaveIn inputDevice = new WaveIn();

//change the input device to the one i want to receive audio from  
inputDevice.DeviceNumber = 1;

//change the wave format to what i want it to be.  
inputDevice.WaveFormat = new WaveFormat(24000, 16, 2);

//set up the event handlers  
inputDevice.DataAvailable += 
   new EventHandler<WaveInEventArgs>(inputDevice_DataAvailable);
inputDevice.RecordingStopped += 
   new EventHandler(inputDevice_RecordingStopped);  

//start the device recording  
inputDevice.StartRecording();

Now when the callback 'inputDevice_DataAvailable' is called, I get an audio data buffer. I need to convert this data into an array of two pairs that represent volume levels between -1 and 1. If someone can help me, it will be great.

+3
source share
1 answer

, , 16- . WaveBuffer NAudio, . 32768, / .

    void waveIn_DataAvailable(object sender, WaveInEventArgs e)
    {
        byte[] buffer = e.Buffer;

        for (int index = 0; index < e.BytesRecorded; index += 2)
        {
            short sample = (short)((buffer[index + 1] << 8) |
                                    buffer[index]);
            float sample32 = sample / 32768f;                
        }
    }
+2

Source: https://habr.com/ru/post/1769461/


All Articles