BASS.NET API for the Un4seen BASS Audio Library

BASS_FILEPROCS Class

BASS.NET API for the Un4seen BASS Audio Library
Inheritance Hierarchy

SystemObject
  Un4seen.BassBASS_FILEPROCS

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

[SerializableAttribute]
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public sealed class BASS_FILEPROCS

The BASS_FILEPROCS type exposes the following members.

Constructors

  NameDescription
Public methodBASS_FILEPROCS
Default constructor taking the callback delegates.
Top
Fields

  NameDescription
Public fieldclose
Callback function to close the file.
Public fieldlength
Callback function to get the file length.
Public fieldread
Callback function to read from the file.
Public fieldseek
Callback function to seek in the file. Not used by buffered file streams.
Top
Remarks

A copy is made of the procs callback function table, so it does not have to persist beyond this function call. This means it is not required to pin the 'procs' instance, but it is still required to keep a reference as long as BASS uses the callback delegates in order to prevent the callbacks from being garbage collected.
Examples

using Un4seen.Bass;
...
private BASS_FILEPROCS _myStreamCreateUser;
private FileStream _fs;
...
Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, this.Handle);
// creating the user file callback delegates
_myStreamCreateUser = new BASS_FILEPROCS(
    new FILECLOSEPROC(MyFileProcUserClose), 
    new FILELENPROC(MyFileProcUserLength), 
    new FILEREADPROC(MyFileProcUserRead), 
    new FILESEEKPROC(MyFileProcUserSeek));
// open the file...
_fs = File.OpenRead("test.mp3");
// create the stream (the PRESCAN flag shows you what BASS is doing at the beginning to scan the entire file)
// if that generates to much output for you, you can simply remove it
int stream = Bass.BASS_StreamCreateFileUser(BASSStreamSystem.STREAMFILE_NOBUFFER, BASSFlag.BASS_STREAM_PRESCAN | BASSFlag.BASS_STREAM_AUTOFREE, _myStreamCreateUser, IntPtr.Zero);
// play the channel
Bass.BASS_ChannelPlay(stream, false);
...
private void MyFileProcUserClose(IntPtr user)
{
  if (_fs == null)
    return;
  _fs.Close();
  Console.WriteLine("File Closed");
}

private long MyFileProcUserLength(IntPtr user)
{
  if (_fs == null)
    return 0L;
  return _fs.Length;
}

private int MyFileProcUserRead(IntPtr buffer, int length, IntPtr user)
{
  if (_fs == null)
    return 0;
  try
  {
    // at first we need to create a byte[] with the size of the requested length
    byte[] data = new byte[length];
    // read the file into data
    int bytesread = _fs.Read(data, 0, length);
    // and now we need to copy the data to the buffer
    // we write as many bytes as we read via the file operation
    Marshal.Copy(data, 0, buffer, bytesread);
    return bytesread;
  }
  catch { return 0; }
}

private bool MyFileProcUserSeek(long offset, IntPtr user)
{
  if (_fs == null)
    return false;
  try
  {
    long pos = _fs.Seek(offset, SeekOrigin.Begin);
    return true;
  }
  catch
  {
    return false;
  }
}
See Also

Reference