Click here to Skip to main content
15,798,592 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi guys i have the following code below to capture sound from a mic and save as a raw file, but i need help saving the raw file as either wav or mp3

this is my code...
public partial class IA_AudioPad : PhoneApplicationPage

        private Microphone mic = Microphone.Default;
        private MemoryStream stream;
        byte[] buffer;
        double msElapsed;

        private DispatcherTimer timer = new DispatcherTimer();

        private const string ConnectionString = @"isostore:/DBs/IAssist_DB.sdf";

        public IA_AudioPad()

            // Some necessary setup for our Microsoft.Xna.Framework.Audio.Microphone  
            mic.BufferDuration = TimeSpan.FromSeconds(1);
            buffer = new byte[mic.GetSampleSizeInBytes(mic.BufferDuration)];

            // Create the event handler.  I could have done an anonymous  
            // delegate here if I had so desired.  
            mic.BufferReady += handleBufferReady;

            timer.Interval = TimeSpan.FromMilliseconds(500);
            timer.Tick += new EventHandler(timer_Tick);

        private void handleBufferReady(object sender, EventArgs e)
            stream.Write(buffer, 0, buffer.Length);

        private void timer_Tick(object sender, EventArgs e)
            msElapsed += 500;
            TimeSpan span = TimeSpan.FromMilliseconds(msElapsed);
            Timer.Text = String.Format("{0:d2} : {1:d2} : {2:d2}", span.Hours, span.Minutes, span.Seconds);

        private void Record_Button_Click(object sender, EventArgs e)
            msElapsed = 0;
            stream = new MemoryStream();
            Rec_Txt.Text = "Recording Note...";
            SetButtonStates(false, true, false);

        private void Stop_Button_Click(object sender, EventArgs e)
            Rec_Txt.Text = "Save Note";
            SetButtonStates(false, false, true);

        private void Save_Btn_Click(object sender, EventArgs e)
            string filename = string.Format("Media/Audio/{0}", AudPadTitle_Txt.Text);
            writeFile(stream, filename);

        private void writeFile(MemoryStream s, string Filename)
            using (var userStore = IsolatedStorageFile.GetUserStoreForApplication())
                using (var file = userStore.OpenFile(Filename, FileMode.CreateNew))

1 solution

Did you try googling?

There is an open source project here:[^]

That is the source for a command line utility program that converts between all sorts of audio file formats (including raw to WAV and WAV to MP3 -- but not directly from raw to MP3).

Here's an article describing the use of the utility:[^]

Since it's an open source project you can download the source (which can be compiled for Windows) and use whatever part of it you need.

I imagine it's written in C++ -- but it's going to be a lot easier to link your C# program to some C++ code (native or managed) than it is going to be to write it yourself.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900