How to process audio data from sound card output using Bass.NET

3.8k Views Asked by At

I want to capture and process data using Bass.NET using the BASS_ChannelGetData method. The examples I've seen that use this play audio files through the Bass.NET library and then sample that, however I wish to sample the data my soundcard outputs, so that I can capture and process audio data from third party audio players, for example Spotify.

Bass.BASS_ChannelGetData(handle, buffer, (int)BASSData.BASS_DATA_FFT256);

How would I get a handle that would allow me to process this data?

1

There are 1 best solutions below

1
On

Bass.BASS_RecordInit do return a handle but if you look closely at the documentation they do use it only for playing (actually starting) the record channel. Their code sample uses a callback to retrieve the audio samples.

Take a look at Bass.BASS_RecordStart Method documentation.

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;
}

Note that you should be able to use BASS_ChannelGetData inside that callback instead of Marshal.Copy.

Did you mean resample instead of sample ? If so then BassMix class will handle that job.