BASS.NET API for the Un4seen BASS Audio Library

BassBASS_RecordInit Method

BASS.NET API for the Un4seen BASS Audio Library
Initializes a recording device.

Namespace:  Un4seen.Bass
Assembly:  Bass.Net (in Bass.Net.dll) Version: 2.4.17.5
Syntax

[DllImportAttribute("bass")]
public static bool BASS_RecordInit(
	int device
)

Parameters

device
Type: SystemInt32
The device to use... -1 = default device, 0 = first. BASS_RecordGetDeviceInfo(Int32, BASS_DEVICEINFO) or BASS_RecordGetDeviceCount can be used to get the total number of devices.

Return Value

Type: Boolean
If successful, is returned, else is returned. Use BASS_ErrorGetCode to get the error code.
Remarks

This function must be successfully called before using the recording features.

Simultaneously using multiple devices is supported in the BASS API via a context switching system - instead of there being an extra "device" parameter in the function calls, the device to be used is set prior to calling the functions. BASS_RecordSetDevice(Int32) is used to switch the current recording device. When successful, BASS_RecordInit(Int32) automatically sets the current thread's device to the one that was just initialized

The default device (device = -1) always maps to the 'Default' device unless that has been disabled via the BASS_CONFIG_REC_DEFAULT config option. BASS_RecordGetDevice can be used afterwards to confirm which device is being used.

Platform-specific: On Windows, WASAPI or DirectSound may be used, depending on the BASS_CONFIG_REC_WASAPI config option setting. On Linux, a 'Default' device is hardcoded to device number 0, which uses the default input set in the ALSA config; that could map directly to one of the other devices or it could use ALSA plugins.

ERROR CODEDescription
BASS_ERROR_DEVICEdevice is invalid.
BASS_ERROR_ALREADYThe device has already been initialized. BASS_RecordFree must be called before it can be initialized again.
BASS_ERROR_DRIVERThere is no available device driver.

Examples

private RECORDPROC _myRecProc; // make it global, so that the GC can not remove it
private int _byteswritten = 0;
private byte[] _recbuffer; // local recording buffer
...
if ( Bass.BASS_RecordInit(-1) )
{
  _myRecProc = new RECORDPROC(MyRecording);
  int recHandle = Bass.BASS_RecordStart(44100, 2, BASSFlag.BASS_RECORD_PAUSE, _myRecProc, IntPtr.Zero);
  ...
  // start recording
  Bass.BASS_ChannelPlay(recHandle, false);
}
...
private bool MyRecording(int handle, IntPtr buffer, int length, IntPtr user)
{
  bool cont = true;
  if (length > 0 && buffer != IntPtr.Zero)
  {
    // increase the rec buffer as needed
    if (_recbuffer == null || _recbuffer.Length < length)
      _recbuffer = new byte[length];
    // copy from managed to unmanaged memory
    Marshal.Copy(buffer, _recbuffer, 0, length);
    _byteswritten += length;
    // write to file
    ...
    // stop recording after a certain amout (just to demo)
    if (_byteswritten > 800000)
      cont = false; // stop recording
  }
  return cont;
}
If you are into C# you might also use an unsafe codeblock with native pointer access (which might be must faster than the above - depending on what you are doing with the data):
C#
private unsafe bool MyRecording(int handle, IntPtr buffer, int length, IntPtr user)
{
  bool cont = true;
  if (length > 0 && buffer != IntPtr.Zero)
  {
    // assuming 16-bit sample data here
    short *data = (short*)buffer;
    ...

    // stop recording after a certain amout (just to demo)
    if (_byteswritten > 800000)
      cont = false; // stop recording
  }
  return cont;
}
See Also

Reference