Search code examples
.netwindows-phone-7formatwavmicrophone

Getting a wav file from Windows Phone microphone


This question is not a duplicate of Get data file from microphone in windows phone 7 or any other referenced in it.

In Windows Phone, I record the microphone data in a following manner:

  1. I start a Dispatcher.Update timer to simulate an XNA game loop.
  2. I subscribe to Microphone.BufferReady and set a buffer duration. Then, I start the microphone.

    // Event handler for getting audio data when the buffer is full
    _microphone.BufferReady += MicrophoneBufferReady;
    
    // Get audio data in 1/2 second chunks
    _microphone.BufferDuration = TimeSpan.FromMilliseconds(100); 
    
  3. Then, each time I get a chunk of audio data from a mic, I record it to a data stream.

    // Retrieve audio data
    _microphone.GetData(_buffer);
    
    _stream.Write(_buffer, 0, _buffer.Length);
    
  4. Whenever the recording is over, the data stream is saved to an Isolated Storage as a file.

    var soundFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".wav";
    using (var isStore = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (var isfStream = new IsolatedStorageFileStream(soundFileName, FileMode.Create, isStore))
        {
            isfStream.Write(data.ToArray(), 0, data.ToArray().Length);
            isfStream.Close();
        }
    }
    

    Where data is a MemoryStream holding all the microphone buffers, that were collected during the recording.

The problem is, that the file actually saved isn't a wav file. If I copy it to a desktop using an ISETool, I can't play the sound.

How do I save the microphone recording to a file, so that it is saved in a .wav format?

P.S. I can play it on the device just fine if I read the data from IS and pack it into a SoundEffect instance.

_soundInstance = new SoundEffect(_data, Microphone.Default.SampleRate, AudioChannels.Mono).CreateInstance();

Solution

  • The format of the buffer is raw PCM. Adding the WAV header to the start of the file is al that is required. However the file is uncompressed and a better solution would be to zip the file on the device(to reduce its size) and send in to a web service for conversion to a compressed WAV format