private void openToolStripMenuItem_Click(object sender, EventArgs e) { openFileDialog1.Filter = "Wave file|*.wav|Any file|*.*"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { try { mWaveFile = new WaveFile(openFileDialog1.FileName); wavePanel.WaveFile = mWaveFile; toolStripStatusLabel1.Text = mWaveFile.Label; playToolStripMenuItem.Enabled = true; WaveStream S = new WaveStream(openFileDialog1.FileName); if (S.Length <= 0) { throw new Exception("Invalid WAV file"); } mFormat = S.Format; if (mFormat.wFormatTag != (short)WaveFmts.Pcm && mFormat.wFormatTag != (short)WaveFmts.Float) { throw new Exception("Olny PCM files are supported"); } mAudioStream = S; } catch (Exception x) { MessageBox.Show(x.Message, "Error opening file", MessageBoxButtons.OK); } } }
private void ReadHeader() { BinaryReader Reader = new BinaryReader(m_Stream); if (ReadChunk(Reader) != "RIFF") { throw new Exception("Invalid file format"); } Reader.ReadInt32(); // File length minus first 8 bytes of RIFF description, we don't use it if (ReadChunk(Reader) != "WAVE") { throw new Exception("Invalid file format"); } if (ReadChunk(Reader) != "fmt ") { throw new Exception("Invalid file format"); } int len = Reader.ReadInt32(); if (len < 16) // bad format chunk length { throw new Exception("Invalid file format"); } m_Format = new WaveFmt(22050, 16, 2); // initialize to any format m_Format.wFormatTag = Reader.ReadInt16(); m_Format.nChannels = Reader.ReadInt16(); m_Format.nSamplesPerSec = Reader.ReadInt32(); m_Format.nAvgBytesPerSec = Reader.ReadInt32(); m_Format.nBlockAlign = Reader.ReadInt16(); m_Format.wBitsPerSample = Reader.ReadInt16(); // advance in the stream to skip the wave format block len -= 16; // minimum format size while (len > 0) { Reader.ReadByte(); len--; } // assume the data chunk is aligned while (m_Stream.Position < m_Stream.Length && ReadChunk(Reader) != "data") { ; } if (m_Stream.Position >= m_Stream.Length) { throw new Exception("Invalid file format"); } m_Length = Reader.ReadInt32(); m_DataPos = m_Stream.Position; Position = 0; }
public WaveOutPlayer(int device, WaveFmt format, int bufferSize, int bufferCount, BufferFillEventHandler fillProc) { m_zero = format.wBitsPerSample == 8 ? (byte)128 : (byte)0; m_FillProc = fillProc; WaveOutHelper.Try(WaveNative.waveOutOpen(out m_WaveOut, device, format, m_BufferProc, 0, WaveNative.CALLBACK_FUNCTION)); AllocateBuffers(bufferSize, bufferCount); m_Thread = new Thread(new ThreadStart(ThreadProc)); m_Thread.Start(); }
public static extern int waveOutOpen(out IntPtr hWaveOut, int uDeviceID, WaveFmt lpFormat, WaveDelegate dwCallback, int dwInstance, int dwFlags);