BASS.NET API for the Un4seen BASS Audio LibraryWaveWriter ClassBASS.NET API for the Un4seen BASS Audio Library
Provides general methods to save an audio stream to a file in the WAVE file format.

Supported bit resolutions are 8-bit, 16-bit, 24-bit and 32-bit. Supported sample rates are 8000, 11025, 22050, 44100, 48000 or 96000 Hz. Multi channel wave files are also supported. Use 1=mono, 2=stereo...

Inheritance Hierarchy

SystemObject
  Un4seen.Bass.MiscWaveWriter

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

public sealed class WaveWriter

The WaveWriter type exposes the following members.

Constructors

  NameDescription
Public methodWaveWriter(String, Int32, Boolean)
Create a WaveWrite instance from a given BASS stream channel.
Public methodWaveWriter(String, Int32, Int32, Boolean)
Create a WaveWrite instance from a given BASS stream channel.
Public methodWaveWriter(String, Int32, Int32, Int32, Boolean)
Create a WaveWrite instance to save sample data to be processed to a wave file.
Top
Properties

  NameDescription
Public propertyBitsPerSample
Gets or Sets the target resolution (number of bits per sample) of the Wave file being created.

This will be either 8, 16, 24 or 32.

Public propertyFileName
Gets or Sets the fully referenced path and file name of the wave file to write to data to.
Public propertyNumChannels
Gets the number of channels of the Wave file being created (values between 1 and 9 should be fine).

E.g. 1=mono, 2=stereo...

Public propertyOrigResolution
Gets or Sets the original resolution (number of bits per sample) of the stream channel resp. sample data to process.

This should be either 8, 16 or 32.

This value is used in the Write(IntPtr, Int32) methods to automatically convert the bit resolution according to the target BitsPerSample value. E.g. set this value to 32, if you receive 32-bit float sample data in a DSP callback and if you want to call the Write(IntPtr, Int32) method there directly, but have specified a target resolution of 8, 16 or 24 bit.

Public propertySampleRate
Gets the sample rate of the Wave file being created.

e.g. 44100 for 44.1kHz. Typically one of the following: 11025, 22050, 44100, 48000, 96000.

Top
Methods

  NameDescription
Public methodClose
Closes the WaveWriter and underlying wave file and updates the wave header accordingly.
Public methodDispose
Disposes the current instance of the WaveWriter and all it's resources.
Protected methodFinalize
Finalization code.
(Overrides ObjectFinalize.)
Public methodCode exampleWrite(Byte, Int32)
Use this method to provide 8-bit sample data to the WaveWriter and write these samples to the wave file.
Public methodCode exampleWrite(Int16, Int32)
Use this method to provide 16-bit sample data to the WaveWriter and write these samples to the wave file.
Public methodCode exampleWrite(IntPtr, Int32)
Use this method to provide the sample data to the WaveWriter and write these samples to the wave file.
Public methodCode exampleWrite(Single, Int32)
Use this method to provide 32-bit sample data to the WaveWriter and write these samples to the wave file.
Public methodCode exampleWriteNoConvert
Use this method to provide sample data to the WaveWriter and write these samples to the wave file.
Top
Remarks

The WaveWriter class is an internal implementation of the BASS.NET API and does not require any additional extension or add-on to the BASS audio library.

The typical use of this class is as followed:

a) Create an instance of the class and define the frequency, channels and bitrate.

b) Call the Write(IntPtr, Int32) method subsequently to write sample data to the wave file.

c) Finally call Close to close and finish the wave file.

Note: Not all media players support playback of all formats, especially multi-channel (more than stereo) or high-resolution (greater 16-bit) wave files.

The following speaker assignment is used for multi-channel wave files (as defined/recommended by Microsoft):

#ChansSpeaker Assignment
1Mono: SPEAKER_FRONT_LEFT
2Stereo: SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT
32.1: SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT, SPEAKER_FRONT_CENTER
42.2: SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT, SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT
52.2.1: SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT, SPEAKER_FRONT_CENTER, SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT
65.1: SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT, SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY, SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT
75.1.1: SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT, SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY, SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT, SPEAKER_BACK_CENTER
87.1: SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT, SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY, SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT, SPEAKER_FRONT_LEFT_OF_CENTER, SPEAKER_FRONT_RIGHT_OF_CENTER
97.1.1: SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT, SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY, SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT, SPEAKER_FRONT_LEFT_OF_CENTER, SPEAKER_FRONT_RIGHT_OF_CENTER, SPEAKER_BACK_CENTER
> 9Undefined: SPEAKER_ALL

Examples

Writing a MP3 file to WAV:
VB
Dim stream As Integer = Bass.BASS_StreamCreateFile("test.mp3", 0, 0, BASSFlag.BASS_STREAM_DECODE)
Dim WW As New WaveWriter("test.wav", stream, True)
Dim data(32768 - 1) As Short
While Bass.BASS_ChannelIsActive(stream) = BASSActive.BASS_ACTIVE_PLAYING
  Dim length As Integer = Bass.BASS_ChannelGetData(stream, data, 32768)
  If length > 0 Then
    WW.Write(data, length)
  End If
End While
' finilize the wave file!
WW.Close()
Bass.BASS_StreamFree(stream)
int stream = Bass.BASS_StreamCreateFile("test.mp3", 0, 0, BASSFlag.BASS_STREAM_DECODE);
WaveWriter WW = new WaveWriter("test.wav", stream, true);
short[] data = new short[32768];
while (Bass.BASS_ChannelIsActive(stream) == BASSActive.BASS_ACTIVE_PLAYING)
{
  int length = Bass.BASS_ChannelGetData(stream, data, 32768);
  if (length > 0)
    WW.Write(data, length);
}
// finilize the wave file!
WW.Close();
Bass.BASS_StreamFree(stream);
Using a WaveWriter in a RECORDPROC to record 24-bit at 44.1kHz, stereo:
VB
Private _waveWriter As WaveWriter = Nothing ' make it global, so that the GC can not remove it
Private _myRecProc As RECORDPROC
Private _recHandle As Integer = 0
...
' start recording
_myRecProc = New RECORDPROC(AddressOf MyRecording)
_recHandle = Bass.BASS_RecordStart(44100, 2, 
                  BASSFlag.BASS_RECORD_PAUSE Or BASSFlag.BASS_SAMPLE_FLOAT, _myRecProc, IntPtr.Zero)
' create a WaveWriter using the _recHandle to set the freq. and channels, but write the wave at 24-bit
_waveWriter = New WaveWriter("test.wav", _recHandle, 24, True)
Bass.BASS_ChannelPlay(_recHandle, False)
...
' when finished recording call this!
If Not (_waveWriter Is Nothing) Then
  ' finilize the wave file!
  _waveWriter.Close()
End If
...
' the recording callback
Private Function MyRecording(handle As Integer, buffer As IntPtr, length As Integer, user As IntPtr) As Boolean
  ' we will get float sample data here
  ' so make sure the _waveWriter.OrigResolution property is set to 32
  ' this was automatically done, since we started recording with BASSFlag.BASS_SAMPLE_FLOAT
  _waveWriter.Write(buffer, length)
  Return True ' always continue recording
End Function
private WaveWriter _waveWriter = null; // make it global, so that the GC can not remove it
private RECORDPROC _myRecProc;
private int _recHandle = 0;
...
// start recording
_myRecProc = new RECORDPROC(MyRecording);
_recHandle = Bass.BASS_RecordStart(44100, 2, 
                  BASSFlag.BASS_RECORD_PAUSE | BASSFlag.BASS_SAMPLE_FLOAT, _myRecProc, IntPtr.Zero);
// create a WaveWriter using the _recHandle to set the freq. and channels, but write the wave at 24-bit
_waveWriter = new WaveWriter( "test.wav", _recHandle, 24, true);
Bass.BASS_ChannelPlay(_recHandle, false);
...
// when finished recording call this!
if (_waveWriter != null)
{
  // finilize the wave file!
  _waveWriter.Close();
}
...
// the recording callback
private bool MyRecording(int handle, IntPtr buffer, int length, IntPtr user)
{
  // we will get float sample data here
  // so make sure the _waveWriter.OrigResolution property is set to 32
  // this was automatically done, since we started recording with BASSFlag.BASS_SAMPLE_FLOAT
  _waveWriter.Write( buffer, length );
  return true; // always continue recording
}
See Also

Reference