The Ultimate Guide To Use BASS.NET In Your .NET Application
Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
[Edit 15/9/2010]
#1 - Getting Started(What Is BASS)
#2 - Initialization
#3 - Playing Audio File(MP3, WAV, WMA, Etc)
#4 - Retrieving Position And Length
#5 - Wave Graph
#6 - Visualization
#7 - High / Low Pitch
#8 - Playlist
#9 - Playback Troubleshooting
#10 - Looping(Repeat)
#11 - Playing Chiptunes(Mod, XM, IT, Etc)
#12 - Free Output
#13 - Playing In Background
#14 - Controls(Play, Pause, Stop)
#15 - Example App
For this tutorial, you're gonna need :
BASS.DLL
BASS.NET.DLL
#1 - Getting Started(What Is BASS)
#2 - Initialization
#3 - Playing Audio File(MP3, WAV, WMA, Etc)
#4 - Retrieving Position And Length
#5 - Wave Graph
#6 - Visualization
#7 - High / Low Pitch
#8 - Playlist
#9 - Playback Troubleshooting
#10 - Looping(Repeat)
#11 - Playing Chiptunes(Mod, XM, IT, Etc)
#12 - Free Output
#13 - Playing In Background
#14 - Controls(Play, Pause, Stop)
#15 - Example App
For this tutorial, you're gonna need :
BASS.DLL
BASS.NET.DLL
Last edited by frozerlaxegon on Wed Sep 15, 2010 10:55 am, edited 3 times in total.
You've been RICKROLL'D
#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.
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.
You've been RICKROLL'D
#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>
Add the following code under <Your Project Form Name>_Load :
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
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
Now you'll have to initialize BASS.Imports System
Imports Un4seen.Bass
Imports Un4seen
Add the following code under <Your Project Form Name>_Load :
Code: Select all
*Important Part*Un4seen.Bass.Bass.BASS_Init(-1, 44100, Un4seen.Bass.BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero)
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/
Last edited by frozerlaxegon on Wed Sep 15, 2010 10:26 am, edited 3 times in total.
You've been RICKROLL'D
#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> :
Use the following code to create a stream using BASS :
Use the following code to play the stream :
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.
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
This allows strm to be used by all forms in your project.Public strm As Integer = 0
Use the following code to create a stream using BASS :
Code: Select all
Now you've created the stream, you wan't BASS to play the stream.strm = Bass.BASS_StreamCreateFile(Playlist.List.SelectedItem, 0, 0, BASSFlag.BASS_DEFAULT)
Use the following code to play the stream :
Code: Select all
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)Bass.BASS_ChannelPlay(strm, False)
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.
Last edited by frozerlaxegon on Wed Sep 15, 2010 10:34 am, edited 1 time in total.
You've been RICKROLL'D
#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> :
Now you must declare 'tElapsed', 'tRemain', 'tLength', 'Pos', 'Len'
Add the following code to your timer :
Add the following code after the declarations :
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 :
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
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
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.Dim TimeLapse As Integer = 0
Now you must declare 'tElapsed', 'tRemain', 'tLength', 'Pos', 'Len'
Add the following code to your timer :
Code: Select all
Now you wan't to make use of Timelapse you declared under Public Class.Dim tElapsed As Single
Dim tRemain As Single
Dim tLength As Single
Dim pos As Long
Dim len As Long
Add the following code after the declarations :
Code: Select all
Now, you wan't to check is the file playing or is it stopped.TimeLapse += 1
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
This will return the current playback position and the full length of the song.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 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
Last edited by frozerlaxegon on Wed Sep 15, 2010 10:36 am, edited 2 times in total.
You've been RICKROLL'D
#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> :
Add the following code under Public Class <Your Form Name> :
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
Now, let's declare 'WF' as the waveform.Imports Un4seen.Bass
Add the following code under Public Class <Your Form Name> :
Code: Select all
Now, add the following code under <Your Form Name>_Load :
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
Code: Select all
Now, to get the position of the red line, add the following code under <Your Timer Name>_Tick :
WF = New Un4seen.Bass.Misc.WaveForm(Playlist.List.SelectedItem, New Un4seen.Bass.Misc.WAVEFORMPROC(AddressOf MyWaveFormCallback), Me)
WF.RenderStart(True, BASSFlag.BASS_DEFAULT)
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)
Last edited by frozerlaxegon on Thu Jun 24, 2010 3:38 pm, edited 1 time in total.
You've been RICKROLL'D
#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 :
Under Timer_Tick, you add the following code :
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 :
Now, you might want to hide the cursor when your mouse go overs the visualizations, so add the following code under <YourVisualizationFormName>_Load :
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
Now you need to declare the following as well as adding a function :
Imports Un4seen.Bass.Misc
Code: Select all
The integer is needed for changing the visualizations, the function is needed to hide the cursor. Dim vis As New Visuals
Dim l As Integer = 1
Private Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long
Under Timer_Tick, you add the following code :
Code: Select all
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. 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)
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 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. If Not l = 14 Then
l = l + 1
Else
l = 1
End If
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
If you need the cursor to appear again, change False to True[/b]ShowCursor(False)
Last edited by frozerlaxegon on Wed Sep 15, 2010 10:55 am, edited 2 times in total.
You've been RICKROLL'D
#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> :
Add this code into your timer(the same timer for the length and position) :
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
Let's name the progressbar on the left pbleft and the progressbar on the right pbright.Private RetVal As Integer = 0
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)
You've been RICKROLL'D
#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 :
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
Now, add the following code under <Your Listbox Name>_SelectedItemChanged
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
Code: Select all
strm = Bass.BASS_StreamCreateFile(List.SelectedItem, 0, 0, BASSFlag.BASS_DEFAULT)
You've been RICKROLL'D
#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> :
add the following code after Bass.Bass_ChannelPlay(strm,false) :
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
Then add the following code to your 'Play' button :
Dim Playing As Boolean
Code: Select all
If you have problems such as opening a corrupted file thenIf 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
add the following code after Bass.Bass_ChannelPlay(strm,false) :
Code: Select all
If strm = 0 Then msgbox "Error : Bla Bla Bla"
You've been RICKROLL'D
Copyright Information
Copyright © Codenstuff.com 2020 - 2023