private void OnDataAvailable(object sender, NAudio.Wave.WaveInEventArgs args)
        {
            float avg = 0;
            float sum = 0;

            NAudio.Wave.WaveBuffer buff = new NAudio.Wave.WaveBuffer(args.Buffer);
            // interpret as 32 bit floating point audio
            for (int index = 0; index < args.BytesRecorded / 4; index++)
            {
                short sample = buff.ShortBuffer[index];

                // absolute value
                if (sample < 0)
                {
                    sample = (short)-sample;
                }
                // is this the max value?
                sum += sample;
            }

            avg = sum / (args.BytesRecorded / 4);

            Amani.Update(avg, slider_sensitivity.Value);
        }
示例#2
0
 public async Task<float[]> ReadSamplesAsync(int sampleCount)
 {
     int numBytesNeeded = sizeof(short) * sampleCount;
     byte[] b = await this.ReadFromBufferAsync(numBytesNeeded);
     short[] s = new NAudio.Wave.WaveBuffer(b).ShortBuffer;
     float[] f = new float[sampleCount];
     for (int i = 0; i < sampleCount; i++)
     {
         f[i] = (float)s[i];
     }
     return f;
 }