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

[SerializableAttribute]
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
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

VB
Imports Un4seen.Bass
...
Private _myStreamCreateUser As BASS_FILEPROCS
Private _fs As FileStream
...
Bass.BASS_Init(- 1, 44100, BASSInit.BASS_DEVICE_DEFAULT, Me.Handle)
' creating the user file callback delegates
_myStreamCreateUser = New BASS_FILEPROCS(
    New FILECLOSEPROC(AddressOf MyFileProcUserClose), 
    New FILELENPROC(AddressOf MyFileProcUserLength), 
    New FILEREADPROC(AddressOf MyFileProcUserRead), 
    New FILESEEKPROC(AddressOf 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
Dim stream As Integer = Bass.BASS_StreamCreateFileUser(BASSStreamSystem.STREAMFILE_NOBUFFER, BASSFlag.BASS_STREAM_PRESCAN Or BASSFlag.BASS_STREAM_AUTOFREE, _myStreamCreateUser, IntPtr.Zero)
' play the channel
Bass.BASS_ChannelPlay(stream, False)
...
Private Sub MyFileProcUserClose(user As IntPtr)
  If _fs Is Nothing Then
    Return
  End If 
  _fs.Close()
End Sub

Private Function MyFileProcUserLength(user As IntPtr) As Long
  If _fs Is Nothing Then
    Return 0L
  End If 
  Return _fs.Length   
End Function

Private Function MyFileProcUserRead(buffer As IntPtr, length As Integer, user As IntPtr) As Integer
  If _fs Is Nothing Then
    Return 0
  End If 
  Try
    ' at first we need to create a byte[] with the size of the requested length
    Dim data(length - 1) As Byte
    ' read the file into data
    Dim bytesread As Integer = _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
  End Try
End Function

Private Function MyFileProcUserSeek(offset As Long, user As IntPtr) As Boolean
  If _fs Is Nothing Then
    Return False
  End If 
  Try
    Dim pos As Long = _fs.Seek(offset, SeekOrigin.Begin)
    Return True
  Catch
    Return False
  End Try
End Function
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