2

I'm recording sound in c#, but not able to save it... Please have a look at this part of the code:

private void Stop()
{
    if (m_Player != null)
        try
        {
            m_Player.Dispose();
        }
        finally
        {
            m_Player = null;
        }
    if (m_Recorder != null)
        try
        {
            m_Recorder.Dispose();
        }
        finally
        {
            m_Recorder = null;
        }
    m_Fifo.Flush(); // clear all pending data
}

private void Start()
{
    Stop();
    try
    {
        WaveLib.WaveFormat fmt = new WaveLib.WaveFormat(44100, 16, 2);
        m_Player = new WaveLib.WaveOutPlayer(-1, fmt, 16384, 3, new WaveLib.BufferFillEventHandler(Filler));
        m_Recorder = new WaveLib.WaveInRecorder(-1, fmt, 16384, 3, new WaveLib.BufferDoneEventHandler(DataArrived));
    }
    catch
    {
        Stop();
    }
}
faceman
  • 1,318
  • 11
  • 20
Engineer
  • 161
  • 1
  • 5
  • 17
  • You should post your code for `DataArrived` too, there you copy received data to a `byte` array and then you can write it to a stream. Example (assuming `buffer` is allocated and `stream` is open): `Marshal.Copy(data, buffer, 0, size); stream.Write(m_RecBuffer, 0, size);` – Adriano Repetti Mar 06 '13 at 11:58
  • Side question...why don't you use [NAudio](http://naudio.codeplex.com/)??? It's more than a thin wrapper around winmm – Adriano Repetti Mar 06 '13 at 12:00
  • I dint get you, please can you make the changes in the code... And I have very less knowledge about NAudio too... Thanks... – Engineer Mar 07 '13 at 15:52
  • Probably you have 80% done but please **post code of DataArrived method** too so we can check and update it (it's where you have to put few lines required for saving). – Adriano Repetti Mar 07 '13 at 16:09
  • private void DataArrived(IntPtr data, int size) { if (m_RecBuffer == null || m_RecBuffer.Length < size) m_RecBuffer = new byte[size]; System.Runtime.InteropServices.Marshal.Copy(data, m_RecBuffer, 0, size); m_Fifo.Write(m_RecBuffer, 0, m_RecBuffer.Length); } – Engineer Mar 08 '13 at 17:13
  • And for filler: private void Filler(IntPtr data, int size) { if (m_PlayBuffer == null || m_PlayBuffer.Length < size) m_PlayBuffer = new byte[size]; if (m_Fifo.Length >= size) m_Fifo.Read(m_PlayBuffer, 0, size); else for (int i = 0; i < m_PlayBuffer.Length; i++) m_PlayBuffer[i] = 0; System.Runtime.InteropServices.Marshal.Copy(m_PlayBuffer, 0, data, size); } – Engineer Mar 08 '13 at 17:24
  • It looks right then the only question is...what m_Fifo is? a stream? Is it a MemoryStream (then write it to disk too!) or a FileStream (then...what do you have there?). – Adriano Repetti Mar 08 '13 at 18:22
  • private WaveLib.FifoStream m_Fifo = new WaveLib.FifoStream(); – Engineer Mar 09 '13 at 15:27
  • @Adriano hey buddy, please can you help me out with saving it... – Engineer Mar 10 '13 at 11:35
  • And do we need to include a throw anywhere? – Engineer Mar 10 '13 at 13:55
  • FifoStream is an in-memory stream, what you write there won't go to the disk (moreover its implementation isn't so great if you use it in a multithreading environment). Just replace it with a _plain_ FileStream and your data will be written to disk. – Adriano Repetti Mar 11 '13 at 08:21
  • @Adriano, please can you show in the code how? – Engineer Mar 13 '13 at 12:27
  • Try this: http://speasy.googlecode.com/svn-history/r56/trunk/SpEasy/frmMain.cs (well it sends via TCP but you simply have to replace it with an hypothetical _AppendAllBytes_ function that mimics AppendAllText but with a byte[] buffer) – Adriano Repetti Mar 13 '13 at 12:36
  • @Adriano, did not work, please see if you can make any additions or changes to my code... – Engineer Mar 13 '13 at 16:37
  • or if you have any working code of recording sound and saving it, please can you send it to me? I can tell you my email address... – Engineer Mar 13 '13 at 16:55
  • "did not work"? Wrong data? Nothing did happen? Exceptions? Post everything here, I'm sure with **all** required info many people will help you here! – Adriano Repetti Mar 13 '13 at 17:11

0 Answers0