Streaming sound in C#
There are plenty of instances where playing sounds, music or virtually anything else sound related is an absolute necessity. The problem, however, is the availability in C#.NET is limited. Fear not, I have a solution!
The following post introduces the BASS.NET Api for streaming sound. In this tutorial you will learn how to:
- Load Mp3 files and play them
- Visual C# Express (sorry, Visual C# Express 2005 is no longer available)
- Bass.NET 2.3 (scroll down until you find the .NET 2.0 link)
- Un4seen Bass api (top download link for Windows)
Within the .NET development environment the availability of playing audio is limited. For one, the SoundPlayer that ships with the .NET framework will currently work with wav files (understandable because of license agreements). Once more with WMA you have to use the Windows Media Player control that ships as a Com object. Nevertheless, there are altenatives. As stated on there website:
BASS is an audio library for use in Windows and Mac OSX software. Its purpose is to provide developers with powerful and efficient sample, stream (MP3, MP2, MP1, OGG, WAV, AIFF, custom generated, and more via add-ons), MOD music (XM, IT, S3M, MOD, MTM, UMX), MO3 music (MP3/OGG compressed MODs), and recording functions. All in a tiny DLL, under 100KB* in size.
On Windows, BASS requires DirectX 3 or above for output, and takes advantage of DirectSound and DirectSound3D hardware accelerated drivers, when available. On OSX, BASS uses CoreAudio for output, and OSX 10.3 or above is recommended. Both PowerPC and Intel Macs are supported.
If you have everything downloaded and installed in order then you are ready to begin. Start by creating a new windows project.I've named my project BassAudio. Right-click on your project and add the following reference.
This dll should be wherever you downloaded it to on your machine. If you forgot to download it see the third download link I mentioned above.
Next, you will want to add whatever song you like. I'm using an Mp3 song that I have on my machine. Just make sure the file will copy to the output directory.
Now that everything has been setup. We can start coding to the api.
Within the Bass api there are a number of varying things you can do with audio devices. For instance, you can work with 3D audio including velocity, position of the emitting sound, position of the listener, doppler effects... and much more.
I recommend taking a look at all that's available to you in the Un4seen.Bass.Bass functions. The nice part is that the majority of all the methods in this api are within the Bass class.
Whenever you're ready, initialize the default device with -1 for the first parameter.
The second parameter is the sampling rate. I recommend looking up the definition on either Wikipedia or HowStuffWorks since I'd rather not explain this part. Basically your audio device has a rate at which it can sample whatever particular input you're working with. Speakers will sample your song at such a rate, or your microphone will sample the audio coming in at such a rate. 44100 is just about the best quality for sampling you can get.
Creating the stream is pretty easy. All you have to do is call the function:
The parameters to this function are pretty much self-explanatory when you read the comments. The first is the location of the file, the second parameter is the offset to begin the streaming from, the length is how long to stream (0 is all), the last parameter is the BassStream flags. I recommend taking a look at the comments in the enumerations. They are all very helpful.
Note: When you work with unmanaged code, you have to be sure you save your references; otherwise, the .NET garbage collector will destroy the reference. To do this, I've simply placed the int stream at the top of the class.
Add a PlaySong, StopSong, and a destructor to your Player class.
The destructor in this class makes sure that all the resources are let go from the unmanaged part of the bass api. Okay, now that we have a simple player setup, let's actually use it! :D
By the end of your form you might have something like this:
And there you have it. Hit F5, load your song and click play. You should now be hearing you song.
In this tutorial you learned how to use an alternative sound api to stream sound to a audio device of your choosing. You figured out what sampling meant and you created a nice reusable sound player. In the my next audio tutorial, I'll show you how to use BASS to show a list of all your audio devices. Then we'll get into some microphone recording later on. Leave and questions and comments here if you like.
Comments
I am looking for an example for playing input stream of WAV from C#.
Can you direct me to such ?
What if I've an application that is recording audio stream, and sending it to me over a tcp-channel for example. Assume I do not want to wait for loading the whole song before I start playing it, what then ? How do I play while keep waiting for more data to come ?
I've failed to far in searching for a solution to my question, and failed to find an implementation that will help me with my problem.
Regards,
Rani
Specifically, converting audio data from it's original byte stream into a playable type is quite simple with the Bass arena. I don't have a sample just yet, but until I have one up I suggest looking to the Un4seen forums for help.
- Thomas
http://www.microsoft.com/express/2005/download/default.aspx
You can download the 2005 versions of visual studio express
I am from Russia and am working on my small project with using of C#.NET (sorry for my English, it mught be some mistakes). And in this programm I need to work with sound (mp3, m3u and others), but, as you know, c# can play only wav-files with its standard library System.Media. That's why, I go to Google where I found your tutorials and read about bass.dll. I've downloaded it, but when I was trying to add it to my project there was a mistake: "A reference to 'some path\bass.dll' could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM componnent"
Maybe you can answer me, what exactly you've done for adding this library to yours project? I'll be very grateful to you)))
P.S. my e-mail: tonlo@mail.ru, and I work with Microsoft Visual C# 2008 Express Edition
Thank you
There are actually 2 libraries discussed here. One is the Bass.NET library (that's the one you want to add as a reference to your project) and the bass.dll library (which you need to include in the output directory of your project).
I can send you a screenshot if you need any help :)
You can download each here: http://www.un4seen.com/bass.html
Thanks again)))
Hello Tomas,
Thank you very much for recommending the BASS api, and it looks great!
Huh? My reply got cutted...
So basically, I only have 1 question: If I make a game with using this api, then to play it on other people computers, do they need to install BASS api as well?
Thanks,
Bass.NET also has a license, so depending on the kind of release you plan on having you may or may not need a license. If it's a non-commercial program then you are okay to use it on your own. I recommend reading the terms on the un4seen website just to make sure though.
Thanks alot for the quick response and easy solution!
We are not make profit, it is just a demo. lol
Anyway, we will check the terms.
Many thanks again!
I want to convert wav to mp3; how can i do this with bass?? also i am developing in .net compact framework.
Regards,
http://www.codeproject.com/KB/audio-video/cswavrec.aspx
Nevertheless, I have not discovered any encoder that will work with mp3 (besides LAME) that is supported on lesser platforms. The only C# library for working with the LAME encoder is the Bass.NET library (that I know of).
Thx Thomas !
Well what u say is true, actually i have already developed a recording aplication with wavein/waveout apis; the big problem is that i need those audio files in mp3 format(wich are recorded in wav format only :S).
The reason i need
thanks for the information..about base.net and Lame,
i was wondering about mixing streaming file with the recording one...
is it possible in C# ??
moreover i was developing asp.net web application using ur code but i m stuck in some problems..
do u have any other option to record music live ...
what ll be the logic behind recording and streaming music live..
and u have used base.net lib is there any other opensource library..which can work on asp.net and codec....
Can this be run under Windows Mobile x / Windows CE ?
Regards
What is the problem?
i included the Bass.net api in my project.. now when i refer the bass.dll from the Bass2.4, it is showing an error that no type libraries are found in the component.
can someone help me please!
Everything's fine after removing that flag.