Page 1 of 3

The Ultimate Guide To Use BASS.NET In Your .NET Application

Posted: Tue Jun 15, 2010 10:56 am
by frozerlaxegon

#1 - Getting Started - What Is BASS

Posted: Tue Jun 15, 2010 10:59 am
by frozerlaxegon
#1 - Getting Started(Whats Is BASS)
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.

#2 - Initialization[Edit 15/9/2010]

Posted: Tue Jun 15, 2010 11:04 am
by frozerlaxegon
#2 - Initialization
Before we play an audio file, we need to initialize BASS first by calling the BASS_Init flags.
Before initializing, you need to add BASS.NET.DLL as a reference to your project.
You also need to import BASS.NET into your project as well.
Add the following code before Public Class <Your Project Name>
Code: Select all
Imports System
Imports Un4seen.Bass
Imports Un4seen
Now you'll have to initialize BASS.
Add the following code under <Your Project Form Name>_Load :
Code: Select all
Un4seen.Bass.Bass.BASS_Init(-1, 44100, Un4seen.Bass.BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero)
*Important Part*
You guys need to download BASS.DLL and not just BASS.NET.DLL(.NET wrapper) as the wrapper refers to BASS.DLL as well, now, save your project by pressing the button with a diskette image and save your project somewhere, then, you drop BASS.DLL into
/Projects/<yourprojectnamehere>/bin/Debug/

#3 - Playing Audio File(MP3, WAV, WMA, Etc)

Posted: Tue Jun 15, 2010 11:13 am
by frozerlaxegon
#3 - Playing Audio File(MP3, WAV, WMA, Etc)
After the BASS_Init is called, you might want to play an audio file.
But first, you need to declare an integer because BASS converts files into integers, so add the following code under Public Class <YourFormNameHere> :
Code: Select all
Public strm As Integer = 0
This allows strm to be used by all forms in your project.
Use the following code to create a stream using BASS :
Code: Select all
strm = Bass.BASS_StreamCreateFile(Playlist.List.SelectedItem, 0, 0, BASSFlag.BASS_DEFAULT)
Now you've created the stream, you wan't BASS to play the stream.
Use the following code to play the stream :
Code: Select all
Bass.BASS_ChannelPlay(strm, False)
strm is the integer(which is the file to be played), false represents a Boolean value to restart the song(if the song is played half)
True : Yes, restart
False : No, resume from paused state
It'll be best for you to add a Boolean value to determine whether the song is paused or not.

#4 - Retrieving Position / Length

Posted: Tue Jun 15, 2010 11:30 am
by frozerlaxegon
#4 - Retrieving Position / Length
When your playing the audio, you might wan't to retrieve the audio length and current playback position.
But first, you need to declare 'timelapse' as an integer.
Add the following code udner Public Class <Your Project Name> :
Code: Select all
Dim TimeLapse As Integer = 0
Now, you wan't to get the most updated information(position and length), I'll recommend you to use a timer and set it's interval to 75.
Now you must declare 'tElapsed', 'tRemain', 'tLength', 'Pos', 'Len'
Add the following code to your timer :
Code: Select all
Dim tElapsed As Single
Dim tRemain As Single
Dim tLength As Single
Dim pos As Long
Dim len As Long
Now you wan't to make use of Timelapse you declared under Public Class.
Add the following code after the declarations :
Code: Select all
TimeLapse += 1
Now, you wan't to check is the file playing or is it stopped.
Using TimeLapse, we can check wether the file is playing or not.
Use the following code to check if it's playing and get the position / length :
Code: Select all
If TimeLapse = 1 Then
TimeLapse = 0
'Get the length
len = Bass.BASS_ChannelGetLength(strm)
'Get the position
pos = Bass.BASS_ChannelGetPosition(strm)
'Convert the length to seconds
tLength = Bass.BASS_ChannelBytes2Seconds(strm, len)
'Convert the position to seconds
tElapsed = Bass.BASS_ChannelBytes2Seconds(strm, pos)
'Get the remaining time
tRemain = tLength - tElapsed
'Convert the seconds to minutes and seconds and make the form's text the elapsed time and length
Me.Text = Un4seen.Bass.Utils.FixTimespan(tElapsed, "MMSS") & "/" & Un4seen.Bass.Utils.FixTimespan(tLength, "MMSS")
End If
This will return the current playback position and the full length of the song.
This will also make your form's text be the playback position / length of the song.
Since BASS returns the position and length in bytes, you must convert it to seconds using the BASS utilities

#5 - Wave Graph

Posted: Tue Jun 15, 2010 11:48 am
by frozerlaxegon
#5 - Wave Graph
As far as I know, this feature is only available for audio only, there's currently still no support for the chiptunes.
What you need :
A new form(let's name it Wave)
A picturebox on the form(let's name it PictureBoxWave)
A timer
Ok, let's get started.
You must first import BASS into the form.
Adding the following code before Public Class <Your Form Name> :
Code: Select all
Imports Un4seen.Bass
Now, let's declare 'WF' as the waveform.
Add the following code under Public Class <Your Form Name> :
Code: Select all
Private WF As Un4seen.Bass.Misc.WaveForm
Now, you must add a few subs to your project.
You must add the following subs :
MyWaveFormCallBack
DrawWavePosition
Add the following code under after declaring WF as the waveform :
Private Sub MyWaveFormCallback(ByVal framesDone As Integer, ByVal framesTotal As Integer, ByVal elapsedTime As TimeSpan, ByVal finished As Boolean)
If Not WF Is Nothing Then
Me.PictureBoxWave.BackgroundImage = WF.CreateBitmap(Me.PictureBoxWave.Width, Me.PictureBoxWave.Height, -1, -1, False)
End If
End Sub

Private Sub DrawWavePosition(ByVal pos As Long, ByVal len As Long)
If len = 0 Or pos < 0 Then
Me.PictureBoxWave.Image = Nothing
Return
End If
Dim bitmap As Bitmap = Nothing
Dim g As Graphics = Nothing
Dim p As Pen = Nothing
Dim bpp As Double = 0
bpp = len / CType(Me.PictureBoxWave.Width, Double) ' bytes per pixel
p = New Pen(Color.Red)
bitmap = New Bitmap(Me.PictureBoxWave.Width, Me.PictureBoxWave.Height)
g = Graphics.FromImage(bitmap)
g.Clear(Color.Black)
'position (x) where to draw the line, Integer)
Dim x As Integer = CType(Math.Round(pos / bpp), Double)
g.DrawLine(p, x, 0, x, Me.PictureBoxWave.Height - 1)
bitmap.MakeTransparent(Color.Black)
If Not p Is Nothing Then
p.Dispose()
End If
If Not g Is Nothing Then
g.Dispose()
End If
Me.PictureBoxWave.Image = bitmap
End Sub
Now, add the following code under <Your Form Name>_Load :
Code: Select all
WF = New Un4seen.Bass.Misc.WaveForm(Playlist.List.SelectedItem, New Un4seen.Bass.Misc.WAVEFORMPROC(AddressOf MyWaveFormCallback), Me)
WF.RenderStart(True, BASSFlag.BASS_DEFAULT)
Now, to get the position of the red line, add the following code under <Your Timer Name>_Tick :
Code: Select all
Dim pos As Long
Dim len As Long
Len = Bass.BASS_ChannelGetLength(musicbox.strm)
pos = Bass.BASS_ChannelGetPosition(musicbox.strm)
DrawWavePosition(pos, Len)

#6 - Visualization[Edit 15/9/2010]

Posted: Tue Jun 15, 2010 12:11 pm
by frozerlaxegon
#6 - Visualization
The changes for this one is so major I have to remake it.
The old one uses over 100 lines of codes this one only uses < 50 codes :shock:
So, this is the updated one, using Bass.Misc.
You'll also need a new form for this, and also a picture box(name it PicVis), and a Timer
So, the references you need to make are :
Code: Select all
Imports Un4seen.Bass.Misc
Now you need to declare the following as well as adding a function :
Code: Select all
   Dim vis As New Visuals
    Dim l As Integer = 1
    Private Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long
The integer is needed for changing the visualizations, the function is needed to hide the cursor.
Under Timer_Tick, you add the following code :
Code: Select all
 If l = 1 Then PicVis.Image = vis.CreateSpectrum(musicbox.strm, PicVis.Width, PicVis.Height, Color.Lime, Color.Red, Color.Black, False, False, False)
        If l = 2 Then PicVis.Image = vis.CreateSpectrumLine(musicbox.strm, PicVis.Width, PicVis.Height, Color.BlueViolet, Color.Purple, Color.Black, 2, 2, False, False, False)
        If l = 3 Then PicVis.Image = vis.CreateSpectrum(musicbox.strm, PicVis.Width, PicVis.Height, Color.Yellow, Color.Red, Color.Black, False, True, True)
        If l = 4 Then PicVis.Image = vis.CreateSpectrumLine(musicbox.strm, PicVis.Width, PicVis.Height, Color.Blue, Color.Red, Color.Black, 16, 4, False, False, False)
        If l = 5 Then PicVis.Image = vis.CreateSpectrumEllipse(musicbox.strm, PicVis.Width, PicVis.Height, Color.Green, Color.Yellow, Color.Black, 1, 2, False, False, False)
        If l = 6 Then PicVis.Image = vis.CreateSpectrumEllipse(musicbox.strm, PicVis.Width, PicVis.Height, Color.Violet, Color.SteelBlue, Color.Black, 2, 4, False, False, False)
        If l = 7 Then PicVis.Image = vis.CreateSpectrumDot(musicbox.strm, PicVis.Width, PicVis.Height, Color.Gold, Color.Yellow, Color.Black, 1, 0, False, False, False)
        If l = 8 Then PicVis.Image = vis.CreateSpectrumDot(musicbox.strm, PicVis.Width, PicVis.Height, Color.Orange, Color.Red, Color.Black, 2, 1, False, False, False)
        If l = 9 Then PicVis.Image = vis.CreateSpectrumLinePeak(musicbox.strm, PicVis.Width, PicVis.Height, Color.SeaGreen, Color.LightGreen, Color.Orange, Color.Black, 2, 1, 2, 10, False, False, False)
        If l = 10 Then PicVis.Image = vis.CreateSpectrumLinePeak(musicbox.strm, PicVis.Width, PicVis.Height, Color.GreenYellow, Color.RoyalBlue, Color.DarkOrange, Color.Black, 23, 5, 3, 5, False, False, False)
        If l = 11 Then PicVis.Image = vis.CreateSpectrumWave(musicbox.strm, PicVis.Width, PicVis.Height, Color.Chocolate, Color.DarkGoldenrod, Color.Black, 1, False, False, False)
        If l = 12 Then PicVis.Image = vis.CreateSpectrumBean(musicbox.strm, PicVis.Width, PicVis.Height, Color.Silver, Color.DarkGreen, Color.Black, 4, False, False, False)
        If l = 13 Then PicVis.Image = vis.CreateSpectrumText(musicbox.strm, PicVis.Width, PicVis.Height, Color.Chocolate, Color.DarkGoldenrod, Color.Black, "MusicBox.NET is Awes0me Like Fro", False, False, False)
        If l = 14 Then PicVis.Image = vis.CreateWaveForm(musicbox.strm, PicVis.Width, PicVis.Height, Color.Green, Color.Red, Color.Gray, Color.Black, 1, True, False, True)
The timer should determine what visualization should it shows according to the integer's value, musicbox.strm is the integer in the other form where you play music, change "musicbox" to your form's first name and change the colors to whatever color you want.
Now, you need to change the value of integer every time someone left click or right click on it, so you add the following code under PicVis_Click :
Code: Select all
 If Not l = 14 Then
            l = l + 1
        Else
            l = 1
        End If
If l, the integer is 14, which is the visualization number, then switch it back to one, else, add 1 to it so it switches to the next value.
Now, you might want to hide the cursor when your mouse go overs the visualizations, so add the following code under <YourVisualizationFormName>_Load :
Code: Select all
ShowCursor(False)
If you need the cursor to appear again, change False to True[/b]

#7 - High / Low Pitch

Posted: Tue Jun 15, 2010 12:18 pm
by frozerlaxegon
#7 - High / Low Pitch
We'll get the high / low pitch using two progressbar.
Before doing any code, add the following declaration under Public Class <Your Form Name> :
Code: Select all
Private RetVal As Integer = 0
Let's name the progressbar on the left pbleft and the progressbar on the right pbright.
Add this code into your timer(the same timer for the length and position) :
Code: Select all
pbleft.Value = Math.Abs(Un4seen.Bass.Utils.HighWord(RetVal) / 4)
pbright.Value = Math.Abs(Un4seen.Bass.Utils.LowWord(RetVal) / 4)

#8 - Playlist

Posted: Tue Jun 15, 2010 12:23 pm
by frozerlaxegon
#8 - Playlist(Optional)
This is optional, but whatever.
What your gonna need :
A new form(optional)
A listbox
Add the following code to your 'Add' button :
Code: Select all
Dim dlg As New OpenFileDialog
dlg.Filter = "MP3 Files (*.mp3)|*.mp3|Chiptunes (*.mo3;*.xm;*.mod;*.s3m;*.it;*.mtm;*.umx)|*.mo3; *.xm; *.mod; *.s3m; *.it; *.mtm; *.umx|WAV Files (*.wav; *.aif)|*.wav; *.aif |WMA Files (*.wma)|*.wma|OGG Files (*.ogg)|*.ogg|All Files (*.*)|*.*"
If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then
Me.List.Items.Add(dlg.FileName)
List.SelectedItem = dlg.FileName
End If
Now, add the following code under <Your Listbox Name>_SelectedItemChanged
Code: Select all
strm = Bass.BASS_StreamCreateFile(List.SelectedItem, 0, 0, BASSFlag.BASS_DEFAULT)

#9 - Playback Troubleshooting

Posted: Tue Jun 15, 2010 1:24 pm
by frozerlaxegon
#9 - Playback Troubleshooting
If you have problems such as two songs playing at the same time then
add the following code under Public Class <Your Form Name> :
Code: Select all
Dim Playing As Boolean
Then add the following code to your 'Play' button :
Code: Select all
If Playing = False Then
Playing = True
Else
Playing = False
End If
If Playing = True Then
Bass.Bass_Stop
Bass.Bass_Free
Else
'Do Your Play Code Here
End If
If you have problems such as opening a corrupted file then
add the following code after Bass.Bass_ChannelPlay(strm,false) :
Code: Select all
If strm = 0 Then msgbox "Error : Bla Bla Bla"