Click here to Skip to main content
11,798,866 members (73,594 online)
Click here to Skip to main content

MP3 Sound Recording Tool

, 2 Jan 2014 CPOL 62.5K 10.1K 80
Rate this:
Please Sign up or sign in to vote.
A small single dialog utility for recording from the sound card in MP3 format


Here is a single dialog utility for recording from the sound card in MP3 format. This is simply a repackaging of a console application described by rtybase in an article which appeared here on CodeProject a while ago.


I have put this utility together fairly quickly as a replacement for some DOS batch files which I used to drive the mp3_stream.exe with. I then soon realized that it would be useful to add a duration timer so that a recording can be ended whilst the computer is left unattended. I found this to be particularly useful when recording longer sessions, some of which can be over six hours long.

Using the Utility

The dialog shows the settings which represent the command line arguments for mp3_stream.exe, such as the volume, bit rate (in kilo bits per second) e.g. 128. The Device and line names are shown in dropdown list boxes. The total duration of the recording can be optionally specified, and a file name which the recording is to be saved as can be browsed for using the standard Windows Save File As dialog. All configuration settings are saved on closing down the utility, and are automatically reloaded the next time it is launched.

Windows 7, 8, and 8.1 Usability Notes

Since I first published this article, more and more people started using it in post-Windows XP operating systems, naturally. The issue encountered when using later versions of Windows is that the operating system both disables and hides the Stereo Mix device by default, even if it is available through the sound card's driver. Overcoming this issue is fairly straightforward, as follows:

  1. Right-click on the speaker icon in the system tray and select Recording devices (you can also bring up this dialog through Windows control panel)
  2. Right-click inside the dialog to bring up the context menu, and select Show Disabled Devices
  3. Again, right-click inside the dialog to bring up the context menu, and this time select Show Disconnected Devices
  4. The above two steps will make Stereo Mix visible, right-click on it and select Enable
  5. Close the dialog by pressing the OK button

Now the next time you launch the utility, you will be able to select the Stereo Mix device and Master Volume line.

It is also worth mentioning that the sound card's driver, as issued by the manufacturer (e.g. Realtek) may not always be installed by default on a newly purchased machine. In many cases, only the standard generic Windows driver is installed. This can be overcome easily by downloading the manufacturer's driver and installing it on the machine. It is then possible to carry out steps 1 to 5 above, and enjoy the benefits of recording from the sound card.

How the Code Works

mp3_stream.exe itself is a C++ application, which uses the LAME open source library to carry out the MP3 encoding. To communicate to LAME using a .NET application, one could go about this in several ways. For example, one could add a thin interface layer of managed C++ to the mp3_stream.exe source code and recompile it so that it can be accessed via .NET. Or, one could add a thin interface layer on the C# project side (using [DllImport]) to access the LAME DLL functionality directly.

However, mp3_stream.exe does in fact already provide an API which is perfectly accessible from .NET. This is perhaps not what one would normally think of as an API in a conventional sense, because it is simply the command line arguments supported by mp3_stream.exe. Nevertheless, it is a programmable interface which provides access to the desired functionality perfectly well. For example, to enumerate the sound cards supported by the system, mp3_stream.exe is invoked with a -device argument. To do this programmatically, System.Diagnostics.Process is used to spawn a mp3_stream.exe process providing it with the appropriate arguments. This is done by the Execute method, which in turn calls the InitiateMP3StreamProcess method:

private void InitiateMP3StreamProcess(Process proc, string arguments)
    proc.StartInfo.CreateNoWindow = true;
    proc.StartInfo.WorkingDirectory = Application.StartupPath;
    proc.StartInfo.FileName = "mp3_stream.exe";
    proc.StartInfo.Arguments = arguments;
    proc.StartInfo.UseShellExecute = false;
    proc.StartInfo.RedirectStandardOutput = true;
    proc.StartInfo.RedirectStandardError = true;
    proc.StartInfo.RedirectStandardInput = true;

private List<string> Execute(string command)
    Process devicesProc = new Process();
    InitiateMP3StreamProcess(devicesProc, command);

    List<string> response = new List<string>();
    string line;
    while ((line = devicesProc.StandardOutput.ReadLine()) != null)

    return response;

The method Execute is called with a string which specified the command line argument which needs to be passed to the mp3_stream.exe process:


This is used, in conjunction with using the -device argument, to iteratively populate the DeviceLines dictionary with all the available devices and lines:

private void PopulateDevices()
    List<string> devices = Execute("-devices");
    foreach (string device in devices)

        DeviceLines.Add(device, new List<string>());
        List<string> lines = Execute(string.Format("-device=\"{0}\"", device));
        foreach (string line in lines)

Saving and Retrieving the Configuration

For added convenience, the tool automatically saves its current configuration on exit. This configuration is then reloaded the next time the tool is launched. This is achieved by handling the form's FormClosing and Load events:

private void Record_FormClosing(object sender, FormClosingEventArgs e)

private void Record_Load(object sender, EventArgs e)

The SaveCurrentConfiguration and RestorePreviousConfiguration methods in turn use the Settings class to access the application configuration parameters:

private void RestorePreviousConfiguration()
    Settings config = Settings.Default;
    Volume.Text = config.Volume;
    BitRate.Text = config.BitRate;
    if (Devices.Items.Contains(config.Device))
        Devices.SelectedItem = config.Device;
    if (Lines.Items.Contains(config.Line))
        Lines.SelectedItem = config.Line;

private void SaveCurrentConfiguration()
    Settings config = Settings.Default;
    config.Volume = Volume.Text;
    config.BitRate = BitRate.Text;
    if (Devices.SelectedItem != null)
        config.Device = Devices.SelectedItem.ToString();
    if (Lines.SelectedItem != null)
        config.Line = Lines.SelectedItem.ToString();



Once configured and the Record button is pressed, the utility displays the time left for the recording to complete. This is done using a timer, which updates a progress bar and a textual display of time remaining:

private void UpdateTimeRemaining(TimeSpan timeSpan)
    TimeRemaining.Text = string.Format("Time remaining: {0}:{1}:{2}",
	timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);

private void Timer_Tick(object sender, EventArgs e)
    DateTime timeNow = DateTime.Now;
    if (timeNow >= EndTime)
        TimeSpan timeRemaining = EndTime - timeNow;
        TimeSpan timeElapsed = timeNow - StartTime;
        TimeSpan totalDuration = EndTime - StartTime;
        double percentageElapsed =
         (timeElapsed.TotalMilliseconds / totalDuration.TotalMilliseconds) * 100;
        if (percentageElapsed > 0)
            Progress.Value = (int)percentageElapsed;
            Progress.Value = 0;

The recording operation itself is invoked by the Click event handler of the Record button:

private void StartRecording_Click(object sender, EventArgs e)
    string args = 	string.Format
	("-device=\"{0}\" -line=\"{1}\" -v={2} -br={3} -sr=32000",

    RecordingProc = new Process();
    InitiateMP3StreamProcess(RecordingProc, args);
    StartRecording.Enabled = false;
    StopRecording.Enabled = true;

    // Use timed recording if user has specified a duration,
    // otherwise do not initiate the timer and simply return.
    TimeSpan recordingDuration = new TimeSpan(Duration.Value.Hour,
    if(recordingDuration.TotalSeconds == 0)

    StartTime = DateTime.Now;
    EndTime = StartTime.Add(recordingDuration).AddSeconds(TimerFudgeFactor);

You may have noticed TimerFudgeFactor and wondered why it is needed. I found that the total time of recordings made using mp3_stream.exe is always shorter than expected by around 3 seconds. This could be an artefact of the encoding process throwing away a small chunk of data at the end of its buffer for some reason. TimerFudgeFactor has a value of 3 (seconds) and seems to work reasonably well for short and long recording alike.

Feature Requests

A few people got in touch, since this article was first published, and asked if the utility could be enhanced to add support for new features. I will add here new subsections each time a new feature or enhancement has been incorporated. Please feel free to keep your requests coming. I will endeavour to accommodate as many requests as possible, just as soon as I can.

Auto File Names

A popular request was to add an enhancement so that the same file name can be used more than once. A common way of using the utility, it appears, is to keep the utility up and running and perform multiple recordings in succession. Having to keep specifying a new file name, in between recordings, breaks the flow of this usage pattern. And so, many enhancement requests were centred around how to make this possible.

One suggestion was for the audio content to be appended to the existing file, thus growing it in length. A potential issue with this suggestion is the way in which some audio players (e.g. Windows Media Player) seem to cache the length of an audio file the first time it is opened, or added to their internal libraries. This appears to prevent such players from recognizing that the length of the audio file has changed, and so will stop playing the track once its original length has elapsed.

Another suggestion was to overwrite a pre-existing file with the new one, though this has many complications. For example, the user may not have intended for this to happen. Even when overwriting is intentional, it can still be problematic. For instance, the existing file might have happened to be opened by another player or utility, and so it would not be possible to overwrite it at that point in time.

A solution to many of these complications is to automatically generate a new file name, based on the original file name. It is also useful to minimize the chance of accidentally generating a file name which might clash with some other existing file name.

The utility now has a new feature which auto generates a new file name if the one specified by the user already exists. For example, if the user specified test7.mp3 as the file name but a file with that name already existed, the utility will now save the new recording to a file called, for example, test7_auto_named_2013_12_29_14_08_36_372.mp3. The auto generated name adds the suffix _auto_named_ followed by the date and time the name was generated. The sequence for the date and time information is: year, month, day, hour, minutes, seconds, and milliseconds. This should help to minimize the chance of unintended file name clashes.

And Finally...

I hope that you find this utility useful. I have certainly had made a lot of use out of the original mp3_stream.exe application, and thought it would be good to contribute back to CodeProject by posting these usability improvements.


  • 6th February, 2010: Initial post
  • 18th September, 2011: Added "Windows 7 Usability Notes" section
  • 29th December, 2013: Added "Feature Requests" section and its "Auto File Names" subsection
  • 29th March, 2015: Updated screenshot, and updated "Windows 7, 8, and 8.1 Usability Notes" section


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


About the Author

firas sawaf
Software Developer
United Kingdom United Kingdom
I work in financial software. When programming for fun, I like solving problems involving some maths or a puzzle of some kind.

You may also be interested in...

Comments and Discussions

QuestionTransferring music in MP3 from a keyboard to my computer (Windows 7) Pin
Member 1169366215-May-15 6:07
memberMember 1169366215-May-15 6:07 
AnswerRe: Transferring music in MP3 from a keyboard to my computer (Windows 7) Pin
firas sawaf15-May-15 6:59
memberfiras sawaf15-May-15 6:59 
Questionw8.1 Pin
Member 1110099422-Sep-14 5:44
memberMember 1110099422-Sep-14 5:44 
AnswerRe: w8.1 Pin
firas sawaf22-Sep-14 6:36
memberfiras sawaf22-Sep-14 6:36 
QuestionBecause I can not write to Windows XP? Pin
Member 1046547411-Feb-14 10:33
memberMember 1046547411-Feb-14 10:33 
AnswerRe: Because I can not write to Windows XP? Pin
firas sawaf11-Feb-14 23:28
memberfiras sawaf11-Feb-14 23:28 
GeneralRe: Because I can not write to Windows XP? Pin
Member 1046547413-Feb-14 4:10
memberMember 1046547413-Feb-14 4:10 
GeneralRe: Because I can not write to Windows XP? Pin
firas sawaf13-Feb-14 11:00
memberfiras sawaf13-Feb-14 11:00 
QuestionDoesn't Download Pin
Clinton Gallagher1-Jan-14 10:42
professionalClinton Gallagher1-Jan-14 10:42 
AnswerRe: Doesn't Download Pin
firas sawaf1-Jan-14 14:48
memberfiras sawaf1-Jan-14 14:48 
GeneralRe: Doesn't Download Pin
Clinton Gallagher2-Jan-14 6:09
professionalClinton Gallagher2-Jan-14 6:09 
AnswerRe: Doesn't Download Pin
firas sawaf2-Jan-14 5:47
memberfiras sawaf2-Jan-14 5:47 
GeneralMy vote of 4 Pin
fredatcodeproject30-Dec-13 13:50
memberfredatcodeproject30-Dec-13 13:50 
GeneralRe: My vote of 4 Pin
firas sawaf31-Dec-13 2:08
memberfiras sawaf31-Dec-13 2:08 
GeneralRe: My vote of 4 Pin
fredatcodeproject31-Dec-13 5:20
memberfredatcodeproject31-Dec-13 5:20 
QuestionUpdated utility and append MP3 file Pin
Member 794828611-Jan-13 12:07
memberMember 794828611-Jan-13 12:07 
AnswerRe: Updated utility and append MP3 file Pin
firas sawaf11-Jan-13 21:21
memberfiras sawaf11-Jan-13 21:21 
AnswerRe: Updated utility and append MP3 file Pin
firas sawaf30-Dec-13 5:57
memberfiras sawaf30-Dec-13 5:57 code Pin
newcoder19-Aug-12 11:43
membernewcoder19-Aug-12 11:43 
Generalnot working Pin
louis sawaf26-Sep-11 9:55
memberlouis sawaf26-Sep-11 9:55 
GeneralRe: not working Pin
firas sawaf26-Sep-11 9:57
memberfiras sawaf26-Sep-11 9:57 
GeneralRe: not working Pin
louis sawaf26-Sep-11 9:58
memberlouis sawaf26-Sep-11 9:58 
GeneralRe: not working Pin
firas sawaf26-Sep-11 9:59
memberfiras sawaf26-Sep-11 9:59 
GeneralRe: not working Pin
louis sawaf26-Sep-11 10:00
memberlouis sawaf26-Sep-11 10:00 
Questionsame but in Pin
elpaez15-Sep-11 10:59
memberelpaez15-Sep-11 10:59 
GeneralReplace Save Pin
crimsonfire0325-Apr-11 11:28
membercrimsonfire0325-Apr-11 11:28 
GeneralRe: Replace Save Pin
firas sawaf18-Sep-11 9:17
memberfiras sawaf18-Sep-11 9:17 
GeneralRe: Replace Save Pin
firas sawaf1-Jan-14 3:38
memberfiras sawaf1-Jan-14 3:38 
GeneralMixer support Pin
ashruuk4-Apr-10 21:08
memberashruuk4-Apr-10 21:08 
GeneralRe: Mixer support Pin
firas sawaf7-Apr-10 16:41
memberfiras sawaf7-Apr-10 16:41 
Generalwin7 Support Pin
ashruuk4-Apr-10 21:06
memberashruuk4-Apr-10 21:06 
GeneralRe: win7 Support Pin
firas sawaf7-Apr-10 16:40
memberfiras sawaf7-Apr-10 16:40 
GeneralRe: win7 Support Pin
Member 359177311-Sep-10 10:53
memberMember 359177311-Sep-10 10:53 
GeneralRe: win7 Support Pin
elpaez15-Sep-11 23:15
memberelpaez15-Sep-11 23:15 
GeneralRe: win7 Support Pin
Member 359177316-Sep-11 13:30
memberMember 359177316-Sep-11 13:30 
GeneralRe: win7 Support Pin
firas sawaf18-Sep-11 9:12
memberfiras sawaf18-Sep-11 9:12 
GeneralRe: win7 Support Pin
elpaez15-Sep-11 10:58
memberelpaez15-Sep-11 10:58 
GeneralGreat Pin
Ashish Basran9-Feb-10 18:52
memberAshish Basran9-Feb-10 18:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151002.1 | Last Updated 2 Jan 2014
Article Copyright 2010 by firas sawaf
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid