Author Topic: How to capture audio from an input device and save it to WAV with BASS.NET?  (Read 45 times)

daniuchiha

  • Posts: 6
Hi everyone,

I'm interested in developing an audio recorder using BASS.NET that captures sound from an input device and saves it in WAV format with the following specifications:

Channels: 2 (Stereo)
Bit depth: 16 bits
Sample rate: 44.1 kHz
Bitrate: 1411 kbps
I have some questions on how to get started and would like some guidance from those who have experience with BASS.NET:

What DLLs do I need?

Do I need to download the BASS core library and other specific plugins?
How do I capture audio from an input device?

What is the best way to select the microphone or audio source?
How do I properly initialize recording in BASS.NET?
How do I save audio in WAV format with the required specifications?

Does BASS.NET allow recording directly to WAV or is an additional conversion step required?
If anyone has worked with these types of projects, I would greatly appreciate your help or guidance.

Ian @ un4seen

  • Administrator
  • Posts: 26254
I'm interested in developing an audio recorder using BASS.NET that captures sound from an input device and saves it in WAV format with the following specifications:

Channels: 2 (Stereo)
Bit depth: 16 bits
Sample rate: 44.1 kHz
Bitrate: 1411 kbps
I have some questions on how to get started and would like some guidance from those who have experience with BASS.NET:

What DLLs do I need?

Do I need to download the BASS core library and other specific plugins?

Yes, you will need at least BASS.DLL (in addition to the BASS.NET.DLL). I would recommend BASSENC.DLL too, for the WAV file writing part.

How do I capture audio from an input device?

You basically need to call BASS_RecordInit to initialize a device and BASS_RecordStart to start recording from it. You can provide a callback function that'll receive the captured data. The following example code is taken from BASS.Net's documentation for the BASS_RecordStart function:

Code: [Select]
private RECORDPROC _myRecProc; // make it global, so that the Garbage Collector can not remove it
...
Bass.BASS_RecordInit(-1);
_myRecProc = new RECORDPROC(MyRecording);
// start recording paused
int recChannel = Bass.BASS_RecordStart(44100, 2, BASSFlag.BASS_RECORD_PAUSE, _myRecProc, IntPtr.Zero);
...
// really start recording
Bass.BASS_ChannelPlay(recChannel, false);
...
// the recording callback
private bool MyRecording(int handle, IntPtr buffer, int length, IntPtr user)
{
  return true;
}

What is the best way to select the microphone or audio source?
How do I properly initialize recording in BASS.NET?

You can use BASS_RecordGetDeviceInfo to get information on the available devices, and then initialize the one you want (eg. one with BASS_DEVICE_TYPE_MICROPHONE) with BASS_RecordInit.

How do I save audio in WAV format with the required specifications?

Does BASS.NET allow recording directly to WAV or is an additional conversion step required?

You can use the BASSenc add-on for this, specifically the BASS_Encode_Start function with the BASS_ENCODE_PCM flag.

Please see the documentation for details on all of the mentioned functions.

radio42

  • Posts: 4842
An (but more complex example) example can be found in the 'Samples/CS/Encoder' folder when you use the full install from Bass.Net.
Instead of using a MP3 encoder you can directly use the above code from Ian.

In the BASS.NET helper class 'BroadCast' (Un4seen.Bass.Misc namespace) you will also find various example.
Also take a look to the EncoderWAV class. Here you'll find the following example:
http://www.bass.radio42.com/help/html/b4e242b5-8364-4a95-03ec-1be27f830f92.htm#
Code: [Select]
private RECORDPROC _recProc;

// init your recording device (we use the default device)
if ( !Bass.BASS_RecordInit(-1) )
  MessageBox.Show(this, "Bass_RecordInit error!" );
_recProc = new RECORDPROC(RecordingHandler);
// start recording at 44.1kHz, stereo (paused)
int recHandle = Bass.BASS_RecordStart(44100, 2, BASSFlag.BASS_RECORD_PAUSE, _recProc, IntPtr.Zero);
if (recHandle == Bass.FALSE)
  MessageBox.Show(this, "BASS_RecordStart error!" );

// setup the encoder
EncoderWAV w = new EncoderWAV(recHandle);
w.InputFile = null;
w.OutputFile = "testrec.wav";
w.Start(null, IntPtr.Zero, true); // start encoder paused
...
// now really start recording and encoding
w.Pause(false);
Bass.BASS_ChannelPlay(recHandle, false);
...
// your recording will be encoded until you call
w.Stop();

private bool RecordingHandler(int handle, IntPtr buffer, int length, IntPtr user)
{
  return true;
}

daniuchiha

  • Posts: 6
Thank you so much for the support and guidance. I will try it out and let you know how it goes.