Author Topic: How to record sound and encode it to mp3? (C#!)  (Read 26075 times)

emmi

  • Posts: 1
Hi there,

I know my problem is not a new one, but I don't know how to record sound from soundcard and encode it to a mp3 file with C#. I read the code from the sample "encode", but that is only using wma for encoding.

Could anybody please tell me, which are the main steps of recording sounds? I didn't understand it...
Code examples would also be very helpful.

...and yes, I read the documentation...  ;)

Thanks in advance,
Emmi

ken

  • Posts: 739
Re: How to record sound and encode it to mp3? (C#!)
« Reply #1 on: 3 Sep '06 - 16:28 »
Have a look at the add-on "Bass.Net 2.3" there is some good C# exaples.

radio42

  • Posts: 4576
Re: How to record sound and encode it to mp3? (C#!)
« Reply #2 on: 3 Sep '06 - 17:59 »
Or have a look to the "Streaming" example.
It even sends the encoded data to a Streaming-Server....but it also shows the general use of the new BASS.NET internal "Encoder" framework.
To encode a recording input to a MP3 file....you just need to change the "Streaming" example code using the EncoderLAME piece...something like this:

Code: [Select]
// MP3
EncoderLAME lame = new EncoderLAME(_recHandle);
lame.InputFile = null; //STDIN (recording)
lame.OutputFile = "test.mp3";
lame.LAME_Bitrate = (int)EncoderLAME.BITRATE.kbps_128;
lame.LAME_Mode = EncoderLAME.LAMEMode.Mono;
lame.LAME_TargetSampleRate = (int)EncoderLAME.SAMPLERATE.Hz_22050;
lame.LAME_Quality = EncoderLAME.LAMEQuality.Quality;
lame.Start(null, 0);

Kenton

  • Guest
Re: How to record sound and encode it to mp3? (C#!)
« Reply #3 on: 27 Mar '07 - 01:39 »
Would you please look at the below code and let me know why the c:\test.mp3 file is not created. 
I have taken the example code right from the stream C# example.  From what I can tell it should be written with l.Stop() 
I confirmed the test.wav file is valid by uncommenting and playing the file.  The included examples with the Bass.Net are good for what they cover but they do not cover simple operations like recording from the microphone and saving to mp3.  The encoder example would be much better if an mp3 example was include or at least the realtime recording/encoding section should show how to save the encoded data to disk after pressing stop.

Please let me know what I am missing with the below code.  Also does anyone have an example of recording from a mic and saving to mp3.

// create the stream
            _stream = Bass.BASS_StreamCreateFile("c:\\test.wav", 0, 0, BASSStream.BASS_STREAM_DECODE);
            //_stream = Bass.BASS_StreamCreateFile("c:\\test.wav", 0, 0, BASSStream.BASS_SAMPLE_FLOAT | BASSStream.BASS_STREAM_PRESCAN);
            //Bass.BASS_ChannelPlay(_stream, false);
            EncoderLAME l = new EncoderLAME(_stream);
            l.InputFile = null;   //STDIN
            l.OutputFile = "c:\\test.mp3";
            l.LAME_Bitrate = (int)EncoderLAME.BITRATE.kbps_64;
            l.LAME_Mode = EncoderLAME.LAMEMode.Default;
            l.LAME_Quality = EncoderLAME.LAMEQuality.Quality;
            l.Start(null, 0);
            // encode the data
            byte[] encBuffer = new byte[65536]; // our dummy encoder buffer (32KB x 16-bit - size it as you like)
            while (Bass.BASS_ChannelIsActive(_stream) == (int)BASSActive.BASS_ACTIVE_PLAYING)
            {
                // get the decoded sample data
                int len = Bass.BASS_ChannelGetData(_stream, ref encBuffer[0], encBuffer.Length);
            }
            l.Stop();

            Bass.BASS_StreamFree(_stream);

radio42

  • Posts: 4576
Re: How to record sound and encode it to mp3? (C#!)
« Reply #4 on: 27 Mar '07 - 09:34 »
I just double checked it and the code worked absolutely fine here:
Code: [Select]
int _stream = Bass.BASS_StreamCreateFile( "test.wav", 0, 0, BASSStream.BASS_STREAM_DECODE);
EncoderLAME l = new EncoderLAME(_stream);
l.InputFile = null; //STDIN
l.OutputFile = "test.mp3";
l.LAME_Bitrate = (int)EncoderLAME.BITRATE.kbps_64;
l.LAME_Mode = EncoderLAME.LAMEMode.Default;
l.LAME_Quality = EncoderLAME.LAMEQuality.Quality;
if (l.Start(null, 0))
{
    // encode the data
    byte[] encBuffer = new byte[65536]; // our dummy encoder buffer (32KB x 16-bit - size it as you like)
    while ( Bass.BASS_ChannelIsActive(_stream) == (int)BASSActive.BASS_ACTIVE_PLAYING )
    {
        // get the decoded sample data
        int len = Bass.BASS_ChannelGetData(_stream, ref encBuffer[0], encBuffer.Length);
    }
    // finish
    l.Stop();
}

So there must be something else going wrong on your machine.
- did you call BASS_Init ?
- did you place bass.dll and bassenc.dll and lame.exe in your executable directory ?
- check, if l.Start is returning with TRUE


Regarding the encoding from a recording channel. This is absolutely the same:
Code: [Select]
EncoderLAME l = new EncoderLAME(_rechandle);
l.InputFile = null; //STDIN
l.OutputFile = "testrec.mp3";
l.LAME_Bitrate = (int)EncoderLAME.BITRATE.kbps_64;
l.LAME_Mode = EncoderLAME.LAMEMode.Default;
l.LAME_Quality = EncoderLAME.LAMEQuality.Quality;
l.Start(null, 0);

// your recording will be encoded until you call...
l.Stop();

Kenton

  • Guest
Re: How to record sound and encode it to mp3? (C#!)
« Reply #5 on: 27 Mar '07 - 15:51 »
Well, I am ashamed :-\  to say that I had not put the lame.exe file into the correct directory.  Now I am able to encode to disk.  Please have patience with me and help to solve the following problem.

I need to record from the microphone saving to memory
stop recording from microphone
encode to mp3 (or other well compressed codec)
play encoded sample just recorded, from memory
save sample to database
load sample from database
play sample loaded
(the last two I have accomplished)

I have samples already in a database blog field in mp3 format that I can load to memory and play from memory.
I do not understand how to encode to memory and play it back from memory.  To save to my DB blog field I need the sample in byte[].
Apparently this is supprted but it eludes me how to do it.

Here is the routine I am using to play my mp3 byte[] where buffer holds my mp3 encoded bytes

private void PlayWord(byte[] buffer)
{
    GCHandle hGC = GCHandle.Alloc(buffer, GCHandleType.Pinned);
    // create the stream (AddrOfPinnedObject delivers the necessary IntPtr)
    _stream = Bass.BASS_StreamCreateFile(hGC.AddrOfPinnedObject(), 0, buffer.Length, BASSStream.BASS_DEFAULT);

    if (buffer != null && buffer.Length > 0)
    {
        if (_stream != 0 && Bass.BASS_ChannelPlay(_stream, false))
        {
        }
        else
        {
            int error = Bass.BASS_ErrorGetCode();
            Console.WriteLine(Enum.GetName(typeof(BASSErrorCode), error));
        }
    }
    hGC.Free();
}

How do I use Base.Net to create a memory buffer of type byte with mp3 encoded bytes?

radio42

  • Posts: 4576
Re: How to record sound and encode it to mp3? (C#!)
« Reply #6 on: 27 Mar '07 - 17:27 »
When you call e.g. "l.Start" you might pass your own callback in which you are able to keep the encoded bytes in a dynamic buffer. When you finished encoding you might then have all the recorded and already encoded bytes ready for playback or storage.

Something like this should work (untested):
Code: [Select]
private ENCODEPROC _encProc;
private ArrayList _memBuffer = new ArrayList();

private bool StartRecording(int _rechandle)
{
    _encProc = new ENCODEPROC(EncodingCallback);

    EncoderLAME l = new EncoderLAME(_rechandle);
    l.InputFile = null; //STDIN
    l.OutputFile = null; //STDOUT
    l.LAME_Bitrate = (int)EncoderLAME.BITRATE.kbps_64;
    l.LAME_Mode = EncoderLAME.LAMEMode.Default;
    l.LAME_Quality = EncoderLAME.LAMEQuality.Quality;
    return l.Start(_encProc, 0);
}

private byte[] StopRecording()
{
    l.Stop();
    // now you might copy your dynamic arraylist to a flat byte[]
    return (byte[])_memBuffer.ToArray( typeof(byte) );   
}

unsafe private void EncodingCallback(int handle, int channel, IntPtr buffer, int length, int user)
{
    // here you receive the encoded data back and can put them into your memory buffer
    // e.g. like this:
    byte *data = (byte*)buffer;
    for (int a=0; a<length; a++)
    {
        _memBuffer.Add(data[a]);
    }
}

m4him

  • Posts: 4
Re: How to record sound and encode it to mp3? (C#!)
« Reply #7 on: 27 Mar '07 - 21:51 »
Thank you for the example  Your code idea worked fine.  I have attached a VS 2005 C# form that others may be able to use for recording/encoding to and from memory and preparing a memory stream for storing into a database blog field in mp3 format.


Windows Vista 32 bit, VS2005, Framework 3.0, Bass 2.3, Bass.Net 2.3.1.1