BASS.NET API for the Un4seen BASS Audio LibraryBassBASS_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.12.0
Syntax

[DllImportAttribute("bass.dll", CharSet = CharSet.Unicode)]
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

When using the default device (device = -1), BASS_RecordGetDevice can be used to find out which device it was mapped to. On Windows, it'll always be the first device.

ERROR CODEDescription
BASS_ERROR_DXA sufficient version of DirectX is not installed.
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.

Platform-specific

Recording support requires DirectX 5 (or above) on Windows. 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.

Examples

VB
Private _myRecProc As RECORDPROC ' make it global, so that the GC can not remove it
Private _byteswritten As Integer = 0
Private _recbuffer() As Byte ' local recording buffer
...
If Bass.BASS_RecordInit(-1) Then
  _myRecProc = New RECORDPROC(AddressOf MyRecording)
  Dim recHandle As Integer = Bass.BASS_RecordStart(44100, 2, BASSFlag.BASS_RECORD_PAUSE, _myRecProc, IntPtr.Zero)
  ...
  ' start recording
  Bass.BASS_ChannelPlay(recHandle, False)
End If
...
Private Function MyRecording(handle As Integer, buffer As IntPtr, length As Integer, user As IntPtr) As Boolean
  Dim cont As Boolean = True
  If length > 0 AndAlso buffer <> IntPtr.Zero Then
    ' increase the rec buffer as needed
    If _recbuffer Is Nothing OrElse _recbuffer.Length < length Then
      _recbuffer = New Byte(length) {}
    End If
    ' 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 Then
      cont = False ' stop recording
    End If
  End If
  Return cont
End Function
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):
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