Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
1.86/5 (4 votes)
See more:
Recently i am using NAudio Library, but i want to record the audio in .wav file in 32bits without using any library. I have googled for the solution but can't find the appropriate solution. All of the solution is based on NAudio, Bass etc.

Please help.
Thanks in advance!!!
Posted
Comments
Keith Barrow 2-Jun-14 11:41am    
Hi - is there any reason you want to do this? At some level you are either going to need a library - or write your own (which is going to be hard at a guess).
http://stackoverflow.com/questions/3694274/how-do-i-record-audio-with-c-wpf has an example, but it uses an MS library.
Rob Philpott 2-Jun-14 11:59am    
As Keith asks, why do you want to abandon the code that does exactly what you need? Also, you need to tell us what driver type you are using.

1 solution

It depends on what do you call "not using library". Isn't the OS API a kind of a library? :-) Anyway…

The simplest way (and very outdated but working one) is to use the Windows module "winmm.dll" via P/Invoke. Here is how:
C#
using System;
using System.Runtime.InteropServices;

//...

internal static class Mci {

    internal const string DllName = "winmm.dll";
    internal const string SoundRecordingDeviceId = "soundRecordingDevice"; // any valid identifier
    internal const string OpenCommandFormat = "open new type waveaudio alias {0}";
    internal const string RecordCommandFormat = "record {0}";
    internal const string PauseCommandFormat = "pause {0}";
    internal const string StopCommandFormat = "stop {0}";
    internal const string CloseCommandFormat = "close {0}";
    internal const string SaveCommandFormatFormat = @"save {0} ""{{0}}""";
    internal static readonly string OpenRecorderCommand = string.Format(OpenCommandFormat, SoundRecordingDeviceId);
    internal static readonly string RecordCommand = string.Format(RecordCommandFormat, SoundRecordingDeviceId);
    internal static readonly string PauseCommand = string.Format(PauseCommandFormat, SoundRecordingDeviceId);
    internal static readonly string StopCommand = string.Format(StopCommandFormat, SoundRecordingDeviceId);
    internal static readonly string CloseRecorderCommand = string.Format(CloseCommandFormat, SoundRecordingDeviceId);
    internal static readonly string SaveCommandFormat = string.Format(SaveCommandFormatFormat, SoundRecordingDeviceId);

    [DllImport(DllName)]
    private static extern long mciSendString(
        string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr oCallback);

    internal static void Open() {
        mciSendString(OpenRecorderCommand, null, 0, IntPtr.Zero);
    } //Open

    internal static void Record() {
        mciSendString(RecordCommand, null, 0, IntPtr.Zero);
    } //Record

    internal static void Pause() {
        mciSendString(PauseCommand, null, 0, IntPtr.Zero);
    } //Pause

    internal static void Stop() {
        mciSendString(StopCommand, null, 0, IntPtr.Zero);
    } //Stop

    internal static void Close() {
        mciSendString(CloseRecorderCommand, null, 0, IntPtr.Zero);
    } //Close

    internal static void SaveRecording(string fileName) {
        mciSendString(string.Format(SaveCommandFormat, fileName), null, 0, IntPtr.Zero);
    } //SaveRecording

}  // classs Mci

It's tested.

Important note: You need to call SaveRecording before calling Close. You first open the device, record/pause/resume… (to resume, call Record again) and stop, then you save the file and then you can open/record again.

This is kind of a lame to use string commands using this interface, but this is what I immediately have in my hands; and it works fast enough, so it's not a problem to use it. It can only record from a default recording device. The output file will be a WAV stereo file with "standard" wave parameters.

In many cases, you may need a lot more features. In this case, you would need to use the assembly "DirectD.DirectSound.dll". You can add the reference in the tab .NET of the "Add Reference" window, because this is the assembly put in the GAC. Using it is also pretty easy. Please start here:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb318690%28v=vs.85%29.aspx[^].

—SA
 
Share this answer
 
v8

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