public void Write(AudioEvent anEvent) { if (null == anEvent || anEvent.DataBuffer == null || anEvent.DataLength < 1) throw new ArgumentNullException("WaveSpeaker::Write(anEvent)"); try { // Get an available buffer WAVEHDR aHeader = fAvailableBuffers.Dequeue(); // Prepare the header UnprepareHeader(aHeader); PrepareHeader(aHeader); // copy the data to the buffer Marshal.Copy(anEvent.DataBuffer, 0, aHeader.lpData, anEvent.DataBuffer.Length); // Write it out to the device Write(aHeader); } catch (Exception e) { Console.WriteLine("WaveSpeaker::Write - {0}", e.Message); } }
public static AudioEvent CreateInstance(IntPtr data, int dataLength, double timeStamp, WAVEFORMATEX wfx) { if (data == IntPtr.Zero || dataLength < 1) throw new ArgumentException("data is null or empty"); AudioEvent anEvent = new AudioEvent(data, dataLength, timeStamp, wfx); return anEvent; }
void SendAudioEvent(AudioEvent anEvent) { BufferChunk chunk = new BufferChunk(anEvent.DataLength + 100); //public ushort cbSize; //public ushort wBitsPerSample; //public int nAvgBytesPerSec; //public short nBlockAlign; //public short nChannels; //public int nSamplesPerSec; //public short wFormatTag; chunk += (int)anEvent.WaveFormat.nChannels; chunk += (int)anEvent.WaveFormat.nSamplesPerSec; chunk += (int)anEvent.WaveFormat.wBitsPerSample; chunk += (int)anEvent.DataLength; chunk += anEvent.DataBuffer; fAudioChannel.Send(chunk); }
unsafe public static double GetAmplitudeAverage(AudioEvent anEvent) { GCHandle gcHandle = GCHandle.Alloc(anEvent.DataBuffer, GCHandleType.Pinned); double avg = 0; double sum = 0; if (16 == anEvent.WaveFormat.wBitsPerSample) { // Find the average assuming 'short' as the data type short* values = (short *)(gcHandle.AddrOfPinnedObject()); int numOfDatum = (int)anEvent.DataLength / 2; for (int i = 0; i < numOfDatum; i++) { sum += values[i]; } avg = sum / numOfDatum; } else if (8 == anEvent.WaveFormat.wBitsPerSample) { // Find the average assuming 'byte' as the data type byte * values = (byte*)gcHandle.AddrOfPinnedObject(); int numOfDatum = (int)anEvent.DataLength; for (int i = 0; i < numOfDatum; i++) { sum += Math.Abs(values[i] - 128); } avg = sum / numOfDatum; } gcHandle.Free(); return avg; }