백곡시스템

Qt – Playing Audio using the BASS library

출처: http://nandanbanerjee.com/tutorials/programming/playing-audio-using-the-bass-library

The BASS library is an audio library which provides a lot of functions, most importantly the playback of various types of audio files. The BASS library website (www.un4seen.com) contains a lot of code samples for various functions. In this tutorial, a very basic and efficient way of playing audio files is presented using a BASS Channel running on a separate thread and a basic explanation of the BASS library functions used for playing the audio.

As a general rule, all CPU intensive tasks other than the UI updates are performed on separate threads. The AudioThread class derived from QThread handles the task of creating a BASS channel and playing the audio.

In this tutorial, we will learn how to create a BASS audio channel, and then load and play audio files in it.

We start off by creating a new QtGui project and add a new class AudioThread with QThread as its base class. Include the header file “bass.h“. Create slots for Play, Pause, Resume, Stop and more as can be seen in the code below. Also we need to create a template for a function syncFunc which will be used to ascertain the end of the audio file. The code for the AudioThread class along with its header is given below.

We need to add the path of the header files as well as the library path in the project (*.pro) file.

After building the program in Windows, we could place the bass.dll in the executable folder or copy the bass.dll to the system32 folder or even add the path to bass.dll to the PATH environment variable. Any one will do but for the time being we will place the dll in the executable folder right beside the exe file.

All the detailed description about the BASS library functions have been very nicely explained in the BASS lib documentation. We’ll still look at some of the basic and tricky functions as it’ll help in understanding the code quickly. The BASS_Init function initializes an output device (in this case the default output device as the first parameter is 0) with a sampling rate of 44100 Hz (second parameter). The other parameters are all set to default values.The BASS_StreamCreateFile creates a sample stream from audio files like MP3 or WAV. The filepath (second parameter) is passed and all the other parameters are set to the default values. This function returns the handle to the audio stream. The BASS_STREAM_AUTOFREE essentially frees the stream when the audio playback ends. If the stream creation does not occur and the file is a MOD file, then the BASS_MusicLoad tries to load the MOD file and return the handle to the music. The BASS_ChannelPlay starts the playback of the audio. The BASS_ChannelSetSync sets up a synchronizer on a stream or MOD music. Here it is used to effectively identify the end of the audio playback using the BASS_SYNC_END flag and to trigger the syncFunc at the end of audio playback. All the other BASS lib functions are more or less self explanatory.

After this, we set up two signals to update the UI thread regarding the status of the audio being played, i.e., if it has reached the end of playback and to intimate the UI thread of the current track position of the audio file.

Finally, all that is required is a nice looking GUI (go crazy) and signal/slot connections to and from the AudioThread class.

 

댓글 남기기

Close Menu