Click here to Skip to main content
13,409,239 members (43,183 online)
Click here to Skip to main content
Add your own
alternative version


131 bookmarked
Posted 4 Sep 2002

nBASS: A sound libary for .NET

, 10 Nov 2002
Rate this:
Please Sign up or sign in to vote.
nBASS provides .NET users with sound functionality


Many times I have seen people asking in the forums for sound support for .NET. Unfortunately, we had to make do with difficult unmanaged dll's or use the media player interface. Then one day I came along a "little" sound library called BASS. This is a sound library that provides you with various static functions for playing, creating, streaming and recording sound. It has support for WAV/MP3/MP2/MP1/OGG sound formats as well a variety of popular MOD formats. With the addition of their WMA addon, the library supports WMA playback as well as WMA creation and network streaming. Having a VB.NET API on the BASS website, I used that as my starting point. NOTE: You must download the BASS and BASSWMA package from the BASS website and copy the 2 dll's to the application folder.


One of the big problems with consuming unmanaged code is the fact that you do not deal directly with objects, but rather with handles to undefined objects. But more on that later. Like I said my starting point was the VB.NET "API". All it is really is a mapping to all the functions provided by the BASS dll. Still that was too unfriendly for me. I then started rewriting all the functions over to C#, and adding more meaningful functions to be used from within .NET. In the code you will notice all unmanaged calls start with an underscore (_) and they usually will have a "friendly" public counterpart.

After remapping all the functions, I started working on the class hierarchy. Several options down the line, I finally came up with a solution that would work. This was essential as several of the static functions can be preformed on a variety of objects (handles). Basically, I created a base class (ChannelBase) and let my defined objects inherit from there or from an object already inheriting from  ChannelBase. In some cases like Stream and Music, I created an intermediate abstract class (AdvancedChannel) from which they inherited. All in all, the structure works very well and saves a lot on duplicate code. Unfortunately, when consuming unmanaged dll's there always seems to be a fair amount of duplicate code (obviously having the source to BASS would have simplified things, but I am not that lucky).

Having the structure in place, I started designing my own classes for managed use. Basically, I went by wrapping a class around a handle and "hiding" the handle from the user and let him/her rather work with an object directly. On object creation, a handle would be passed to the constructor and the resulting object would be returned. I opted not to have public constructors for my objects as most need to have BASS initialized first. So once a BASS class was created, further objects can be created from there, with the exception of the CD and Record class.  They are allowed to be instantiated with public constructors.

Having objects (with handles) made it possible for me to rewrite all unmanaged functions from:

static int UnmanFunc(IntPtr handle, int someparam);


void ManFunc(int someparam);

All "freeing" functions from BASS dll has also been implemented into their respective finalizers, so the implementer does not have to worry about freeing up unmanaged resources. I didn't go for the IDisposable model as it seemed to create more "work" than that is supposed to prevent. UPDATE: IDisposable has been implemented.

An exception model was also implemented, by using the static GetErrorCode() function provided. In other words, the user does not check for an error, but rather wait for an exception.

An event model was also implemented for when the "sound" stops playing. The marshalling required for this was quite tricky. You can have a look inside AdvancedChannel.

Finally, it came round to testing all the functions provided by the BASS dll. There were some really "weird" (from a C# perspective anyways) marshalling to be done to get the functions to behave like they should. I had to implement some classes to solve some problems, for example the ChannelAttributes class. This is really only a "placeholder" for some variables , making life alot easier when having to modify some settings. You can also have a look at the FX class to see how I handled the void * type from my code.


I have created various test projects to test various functions. Here are some screenshots:

CD (very basic):

Stream (with stereo visualization):

Music (with 3D positioning and EAX presets):

WMAStream (with stereo visualization, playing previously encoded WMA file):

WMAEncoder (encoding to network):


The implementation is very easy and a sound can be played with very little work.

<font size="2">BASS bass = new BASS(-1 , 44100, DeviceSetupFlags.Default, 
            new IntPtr(0)); //we use foreground window
bass.Start(); //we call this to INIT BASS.dll
Stream stream = bass.LoadStream(false, "test.mp3", 0, 0, StreamFlags.AutoFree);
stream.Play(true, StreamPlayFlags.Default);</font>

Please note: The BASS dll requires a valid window handle to function, so either pass IntPtr.Zero (foreground window) or the form's handle.


I have tried to do as much as possible for documentation, but it still lacks huge parts. I have included an nDoc generated help file with most of the documentation coming straight from the BASS dll's docs. I have tried to keep the functions as closely related to their static unmanaged counterparts.

I have since first public release also included a changelog as the project is still under heavy development, but mostly functional.


Playing sounds from within C# cant be easier than this. If you have any comments, suggestions, remarks, please feel free to contact me. This was my first project of such a kind, but I believe it was successful.


nBASS 0.9.5 (9 September 2002)

- Fixed bug in WMAStream.GetTags()
- Added IDisposable to classes (big thanks to Joel Mueller)
- ChannelBase and AdvancedChannel is now abstract (again, thanx to Joel)
- Change exceptions to show message when exception is thrown without having to do it manually :)

nBASS 0.9.6 (14 September 2002)

- Fixed Stream Tags. New tag properties available.
- WMAStream now directly inherites from AdvancedChannel.
- New ID3v1Tag class available.

nBASS (4 November 2002)

- CLSCompliant :)
- Support for BASS version 1.7+


This article, along with any associated source code and files, is licensed under The BSD License


About the Author

Software Developer
South Africa South Africa
No Biography provided

You may also be interested in...

Comments and Discussions

AnswerRe: how to initialise an nBass object with ver 1.8 Pin
leppie29-Nov-03 3:25
memberleppie29-Nov-03 3:25 
Questionwhat and how to reference nBASS? Pin
fguihen28-Nov-03 2:13
memberfguihen28-Nov-03 2:13 
AnswerRe: what and how to reference nBASS? Pin
leppie28-Nov-03 7:01
memberleppie28-Nov-03 7:01 
GeneralRe: what and how to reference nBASS? Pin
fguihen29-Nov-03 2:06
memberfguihen29-Nov-03 2:06 
GeneralV&#237;deo for Windows in C# Pin
Marcelo Marques Inácio25-Nov-03 5:21
memberMarcelo Marques Inácio25-Nov-03 5:21 
GeneralPlaying on more than one device Pin
linxpro124-Nov-03 18:43
memberlinxpro124-Nov-03 18:43 
GeneralRe: Playing on more than one device Pin
linxpro125-Nov-03 7:56
memberlinxpro125-Nov-03 7:56 
GeneralRe: Playing on more than one device Pin
leppie25-Nov-03 8:07
memberleppie25-Nov-03 8:07 
Hmm I didnt know 2.0 was out even! Im not even sure what compatiblilty there will be between the 2. If I get time (sure), I'll have a look at making a MC++ wrapper ofr BASS 2.0.

Cheers Smile | :)

<a TITLE="See my user info" href=>leppie::<a TITLE="Go to all articles written by me" href=>AllocCPArticle("Zee blog");
Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.

GeneralRe: Playing on more than one device Pin
leppie25-Nov-03 8:02
memberleppie25-Nov-03 8:02 
GeneralRe: Playing on more than one device Pin
linxpro126-Nov-03 9:47
memberlinxpro126-Nov-03 9:47 
QuestionCan you play a sound file from a embedded resource ? Pin
jtmtv184-Nov-03 1:27
memberjtmtv184-Nov-03 1:27 
AnswerRe: Can you play a sound file from a embedded resource ? Pin
leppie4-Nov-03 8:26
memberleppie4-Nov-03 8:26 
GeneralRe: Can you play a sound file from a embedded resource ? Pin
jtmtv184-Nov-03 10:53
memberjtmtv184-Nov-03 10:53 
GeneralUrgent Help Needed: Stream Length Error! Pin
dallyanzi17-Sep-03 11:12
memberdallyanzi17-Sep-03 11:12 
GeneralOld Version, please upgrade Pin
leppie17-Sep-03 11:20
memberleppie17-Sep-03 11:20 
GeneralRe: Old Version, please upgrade Pin
dallyanzi17-Sep-03 23:52
memberdallyanzi17-Sep-03 23:52 
GeneralSystem.FormatException being thrown Pin
Mattias Nordberg13-Jun-03 11:23
memberMattias Nordberg13-Jun-03 11:23 
GeneralRe: System.FormatException being thrown Pin
Mattias Nordberg13-Jun-03 11:41
memberMattias Nordberg13-Jun-03 11:41 
GeneralRe: System.FormatException being thrown Pin
leppie13-Jun-03 11:45
memberleppie13-Jun-03 11:45 
GeneralRe: System.FormatException being thrown Pin
leppie13-Jun-03 11:42
memberleppie13-Jun-03 11:42 
GeneralSome other mystery exception being thrown Pin
Anonymous11-May-03 12:19
sussAnonymous11-May-03 12:19 
GeneralRe: Some other mystery exception being thrown Pin
leppie11-May-03 15:12
memberleppie11-May-03 15:12 
QuestionHow to detect the soundcard. Pin
GriffonRL27-Apr-03 23:30
memberGriffonRL27-Apr-03 23:30 
GeneralDSP Pin
Seth Richards14-Apr-03 8:48
sussSeth Richards14-Apr-03 8:48 
GeneralStrange behaviour. Pin
sheppe14-Apr-03 7:16
membersheppe14-Apr-03 7:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180221.1 | Last Updated 11 Nov 2002
Article Copyright 2002 by leppie
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid