Main Menu

Playing Random Music

Started by XpMe_v1.2, May 20, 2005, 02:45:50 PM

Previous topic - Next topic

XpMe_v1.2

I'm now finishing a board game and I need music and a doc file to finish.
I have plenty of music I have written(ogg files). To avoid these
massive files and to keep all the files to the game to or at
6 megs. I did this...
To keep the board game files size small i'm considering the below
code I wrote.
To keep music playing files small for a game try this.
This will play random music.
Requires several SMALL music files(I use ogg files) to play
(read the source code).
Try wave files that uses a drum, another that plays rain,
another that plays a violin, and another playing a flute.
What ever mix you use will sound different wile playing.
Note that in code that I set panning to occur anywhere for
any playing music file(set when loaded only).
Give it a try.
If you make any major changes then let us see the
improvements that you do.
`---
`---start of code
`---
OpenScreen 320,200,32,1
TitleScreen "Playing Random Music"
`---
Type OggInfo
FileExt As String
 Volume As Integer
MaxFiles As Integer
MaxMusic As Integer
EndType
`---Music file extension   .ogg    may be changed to match your music file type
`---Oggs.MaxFiles count needs to be more than the Oggs.MaxMusic count to work correctly
Dim Oggs As OggInfo
Oggs.FileExt  = ".ogg"
Oggs.Volume   = 99
Oggs.MaxFiles = 6
Oggs.MaxMusic = 4
`---
Type OggText
Ogg     As String
InUse   As String
HiDelay As Integer
LoDelay As Integer
EndType
Dim OggMusic(Oggs.MaxFiles) As OggText
`---Music file names  CHANGE THESE names to match yours
`---Make sure the amount of names matches  Oggs.MaxFiles  count  or you will get an error while running
n = 1 : OggMusic(n).Ogg = "Cello"  : Inc n : OggMusic(n).Ogg = "Orc"     : Inc n : OggMusic(n).Ogg = "Perc"
Inc n : OggMusic(n).Ogg = "Shaker" : Inc n : OggMusic(n).Ogg = "Strings" : Inc n : OggMusic(n).Ogg = "Water"
`---Random delay to keep music playing at a random spacing
For t = 1 To Oggs.MaxMusic : OggMusic(t).HiDelay = Rnd(1000)+999 : OggMusic(t).LoDelay = Rnd(777) : Next
   n = 1 + Rnd(Oggs.MaxFiles-1) : LoadPlay(OggMusic(n).Ogg)
`---
`---MAIN LOOP
Do   : LoadPlay("")
      DisplayOggInformation()
      If FunctionKeys(4) = 1 Then LowerOggVol()
      If FunctionKeys(5) = 1 Then RaiseOggVol()
Sync
Loop : End
`---
`---If  Music number is not playing
`---If any Music playing name do not match a randomly selected name
`---Then  Loads a  non playing  Music number and starts playing it
Function LoadPlay(Og$)
                      If Len(Og$) > 0 Then k = 1 : Ooh$ = Og$ : Goto skipover
                      zz = Oggs.MaxMusic - 1 :  k = Rnd(zz) + 1
                      ff = Oggs.MaxFiles - 1 :  f = Rnd(ff) + 1
                      If GetSoundStatus(  k) = True
                      If GetSoundPlaying( k) = True Then Exitfunction
                      EndIf
                      z = -1 : Ooh$ = OggMusic(f).Ogg
For t = 0 To Oggs.MaxMusic
                      If t <> k
                      If Ooh$ = OggMusic(t).InUse Then z = t : ExitFor
                      EndIf
Next :                 If z > -1 Then Exitfunction
                    ` uses delay varibles to keep music playing random
                      If Rnd(OggMusic(k).HiDelay) <> OggMusic(k).LoDelay Then OggMusic(k).InUse = "" : Exitfunction
skipover:
                      LoadSound Ooh$ + Oggs.FileExt ,k
                        PlaySound k
                      SoundVolume k, Oggs.Volume
                         SoundPan k, Rnd(510) - 255
                         OggMusic(k).InUse = Ooh$
                        `reset the play delays
                         OggMusic(k).HiDelay = Rnd(1000)+999
                         OggMusic(k).LoDelay = Rnd(777)
EndFunction
`---Lower (all playing) music volume
Function LowerOggVol()
If Oggs.Volume > 0 Then Oggs.Volume = Oggs.Volume - 1   : For t = 1 To Oggs.MaxMusic : SoundVolume t, Oggs.Volume : Next
EndFunction
`---Raise (all playing) music volume
Function RaiseOggVol()
If Oggs.Volume < 255 Then Oggs.Volume = Oggs.Volume + 1 : For t = 1 To Oggs.MaxMusic : SoundVolume t, Oggs.Volume : Next
EndFunction
`---Shows which Music number  is  OR  is not  playing
`---Shows infomation of which function key to press to lower or raise (all playing) music volume
`---Shows infomation of (all playing) music volume
Function DisplayOggInformation()
Cls RGB(100,100,225)
x =  3  : y =   5          : Say( x,y ,9,9,9,  50,255, 50 ,"NOW PLAYING..." )
x =  3  : y =  30  
For t = 1 To Oggs.MaxMusic : Say( x,y ,9,9,9, 255,230,200 ,Str$(t) + ".  " + OggMusic(t).InUse )
         y = y + 20
Next    : y = 125          : Say( x,y ,9,9,9, 160,250,160 ,"PRESS Function Key 4 =  to LOWER VOLUME" )    
         y = 145          : Say( x,y ,9,9,9, 160,250,160 ,"PRESS Function Key 5 =  to RAISE VOLUME" )
         y = 175          : Say( x,y ,9,9,9, 255,240,100 ,"Music Volume (0-255) =  " + Str$(Oggs.Volume) )
EndFunction
`---
Function Say(x,y, r1,g1,b1 ,r2,g2,b2, Tx$)
Ink RGB(r1,g1,b1) : Text x+2 ,y+2 ,Tx$
Ink RGB(r2,g2,b2) : Text x   ,y   ,Tx$
EndFunction
`---
`---end of code
...XpMe v1.2

http://tilemusic.com/

XpMe_v1.2

Hear is a new version that allows you to select any directory
and change volume by using the mouse.
Just select any visible FILE to select the directory and it
will get the file list and randomly play the files.
Read the bottom of the program screen for more information.
>>>  pbdialogs.dll  <<< is required to be in the
directory of wherever you place this code.
Look in the subdirectory  PlayBasic\Projects\Examples\Dlls
for it. It should be there.    :blink:  

`---------------------------------
Global ScrWi = 320
Global ScrHi = 152
OpenScreen ScrWi,ScrHi,32,1
TitleScreen "Playing Random Music"
`---------------------------------
// Constants for FileDialogs
Constant FD_ReadOnly     = 1
Constant FD_OverwritePrompt     = 2
Constant FD_HideReadOnly    = 4
Constant FD_NoChangeDir     = 8
Constant FD_NoValidate     = 16
Constant FD_AllowMultiSelect       = 32
Constant FD_ExtensionDifferent    = 64
Constant FD_FileMustExist     = 128
Constant FD_CreatePrompt     = 256
Constant FD_NoNetworkButton     = 512
Constant FD_OldStyleDialog     = 1024
Constant FD_EnableSizing     = 2048
// Type For FileDialog
Type TFileDialog
  Title       As String
  Filter      As String
  FilterIndex As Integer
  FileName    As String
  InitialDir  As String
  Flags       As Integer
  DefaultExt  As String
EndType
`---------------------------------
LoadDll "pbdialogs.dll",1
`---------------------------------
Type OggInfo
BkgImg     As Integer
Volume     As Integer
VolHiLi    As Integer
VolDelay   As Integer
Mute       As Integer
MuteHiLi   As Integer
MuteDelays As Integer
DirHiLi    As Integer
MaxFiles   As Integer
MaxMusic   As Integer
FilesDir   As String
CurrentDir As String
EndType
Dim Oggs As OggInfo
Oggs.Volume   = 100
Oggs.Mute     = 1
Oggs.MaxFiles = 32000
Oggs.MaxMusic = 4
Oggs.FilesDir = CurrentDir$()
Type OggText
Ogg     As String
InUse   As String
HiDelay As Integer
LoDelay As Integer
EndType
Dim OggMusic(Oggs.MaxFiles) As OggText
GetTheDir(Oggs.FilesDir)
`---Random delay to keep music playing at a random spacing
For t = 1 To Oggs.MaxMusic : OggMusic(t).HiDelay = Rnd(900)+500 : OggMusic(t).LoDelay = Rnd(300) : Next
BuildDisplayBKG()
`---------------------------------
`---MAIN LOOP
Do   : LoadPlay()
      ShowStuff()
Loop
`---------------------------------
`---If  Music number is Not playing
`---If any Music playing name do not match a randomly selected name
`---Then  Loads a  non playing  Music number and starts playing it
Function LoadPlay()
                      zz = Oggs.MaxMusic - 1 :  k = Rnd(zz) + 1
                      ff = Oggs.MaxFiles - 1 :  f = Rnd(ff) + 1
                      If GetSoundStatus(  k) = True
                      If GetSoundPlaying( k) = True Then Exitfunction
                      EndIf
                      z = -1 : Ooh$ = OggMusic(f).Ogg
For t = 0 To Oggs.MaxMusic
                      If t <> k
                      If Ooh$ = OggMusic(t).InUse Then z = t : ExitFor
                      EndIf
Next :                 If z > -1 Then Exitfunction
                    ` uses delay varibles to keep music playing random
                      If Rnd(OggMusic(k).HiDelay) <> OggMusic(k).LoDelay Then OggMusic(k).InUse = "" : Exitfunction
                      If FileSize(Ooh$) < 1 Then Exitfunction
                      LoadSound Ooh$ ,k
                      LoadSound Ooh$ ,k
                        PlaySound k
                        If Oggs.Mute = 0 Then SoundVolume k, 0
                        If Oggs.Mute = 1 Then SoundVolume k, Oggs.Volume
                         SoundPan k, Rnd(510) - 255
                         OggMusic(k).InUse = Ooh$
                        `reset the play delays
                         OggMusic(k).HiDelay = Rnd(900)+500
                         OggMusic(k).LoDelay = Rnd(300)
EndFunction
`---------------------------------Get music list
Function GetTheDir(pth$)
n = 0 : Fi$ = FirstFile$(pth$)
 While Fi$ <> ""
             If FileType(pth$ + Fi$) = 1
                     ext$ = GetFileExt$(pth$ + Fi$)
                     Select ext$
                     Case "mp3","ogg","wav","mid" : e = 1
                     EndSelect  : If e = 1 Then e = 0 : Inc n : OggMusic(n).Ogg = pth$ + Fi$ : Oggs.MaxFiles = n
             EndIf
                     Fi$ = NextFile$()
                      e$ = Make$(" ",36) + Left$(pth$,Len(pth$)-1) : Oggs.CurrentDir = Right$(e$,32)
 EndWhile
EndFunction
`---------------------------------Load a directory
Function GetTheFilesDirectory()
Dim MyFileDialog As TFileDialog
MyFileDialog.Title       = "To load a directory just click any file and then click open"
MyFileDialog.Filter      = "All files(*.*)|*.*|MP3 files(*.mp3)|*.mp3|OGG files(*.ogg)|*.ogg|WAV files(*.wav)|*.wav|MID files(*.mid)|*.mid"
MyFileDialog.FilterIndex = 1
MyFileDialog.FileName    = "test.wav"
MyFileDialog.Flags       = FD_EnableSizing + FD_FileMustExist
result = CallDll(1,"OpenFileDialog",MyFileDialog.TFileDialog)
If result <> 0
  Oggs.FilesDir = GetFolderName$(MyFileDialog.FileName)
          For t = 1 To 4 : If GetSoundStatus(t) = True Then StopSound t
          Next           : GetTheDir(Oggs.FilesDir)
EndIf
EndFunction
`---------------------------------Bottom of screen tip information
Function TextTips()
TipsCo = RGB(50,50,190)
Select MouseY()
Case  2  To 20
Select MouseX()
Case 0 To 53   : Tips$ = "Right click to select a new directory" : Oggs.DirHiLi = 1
     Select MouseX()
     Case 2 To 53
            If MouseButton() > 1
               GetTheFilesDirectory()
               DrawImage Oggs.BkgImg ,0,0,0 : Oggs.DirHiLi = 0
               GetTheDir(Oggs.FilesDir)
               DisplayOggInformation()
               Ink RGB(150,5,5) : Text 5,ScrHi-26 ,"RIGHT CLICK MOUSE TO CONTINUE"
               Sync
            EndIf
norats:
            If MouseButton() > 1 Then Goto norats
     EndSelect
Case 54 To 320 : Tips$ = "the currently selected directory"
EndSelect
Case 22  To 40  : Tips$ = "music number 1"
Case 42  To 60  : Tips$ = "music number 2"
Case 62  To 80  : Tips$ = "music number 3"
Case 82  To 100 : Tips$ = "music number 4"
Case 102 To 120 : Tips$ = "music volume"
a = MouseButton()
Select MouseX()
Case   0 To 70 : Tips$ = "Left click to MUTE music volume(0)"      : Oggs.MuteHiLi = 1 : If a = 1 Then MUTEsOggVol()
Case 254 To 285: Tips$ = "Left/Right click to lower volume(0-255)" : Oggs.VolHiLi  = 1 : If a > 0 Then LowerOggVol(a)
Case 287 To 320: Tips$ = "Left/Right click to raise volume(0-255)" : Oggs.VolHiLi  = 2 : If a > 0 Then RaiseOggVol(a)
EndSelect
Case 122 To 140 : Tips$ = "this tip box"  : TipsCo = RGB(200,200,255)
EndSelect       : Ink TipsCo : Text 5,ScrHi-26 ,Tips$
EndFunction
`---MUTE (all playing) music volume
Function MUTEsOggVol()
If Oggs.MuteDelays < Timer()
                    Oggs.Mute = Oggs.Mute + 1     : Oggs.MuteDelays = Timer() + 300
                    If Oggs.Mute > 1 Then Oggs.Mute = 0
                    Select Oggs.Mute
                    Case 0 : muteVolume = 0
                    Case 1 : muteVolume = Oggs.Volume
                    EndSelect
                    For t = 1 To Oggs.MaxMusic    : SoundVolume t, muteVolume   : Next
EndIf
EndFunction
`---Lower (all playing) music volume
Function LowerOggVol©
If Oggs.Mute = 1
If Oggs.VolDelay < Timer()
                    Oggs.VolDelay = Timer() + 250 : v = 1 : If c > 1 Then v = 10
                    Oggs.Volume = Oggs.Volume - v : If Oggs.Volume < 0 Then Oggs.Volume = 0
                    For t = 1 To Oggs.MaxMusic : SoundVolume t, Oggs.Volume  : Next
EndIf
EndIf
EndFunction
`---Raise (all playing) music volume
Function RaiseOggVol©
If Oggs.Mute = 1
If Oggs.VolDelay < Timer()
                    Oggs.VolDelay = Timer() + 250 : v = 1 : If c > 1 Then v = 10
                    Oggs.Volume = Oggs.Volume + v : If Oggs.Volume > 255 Then Oggs.Volume = 255
                    For t = 1 To Oggs.MaxMusic: SoundVolume t, Oggs.Volume  : Next
EndIf
EndIf
EndFunction
`---prints raised text
Function Say(x,y, Tr1,Tg1,Tb1 ,Tr2,Tg2,Tb2 ,Txt$)
Ink RGB(Tr1,Tg1,Tb1) : Text x+1,y+1,Txt$ : Ink RGB(Tr2,Tg2,Tb2) : Text x ,y ,Txt$
EndFunction
`---text box
Function SayBox(x,y,w,d, r1,g1,b1 ,r2,g2,b2 ,Tr1,Tg1,Tb1 ,Tr2,Tg2,Tb2 ,Txt$, ri,dn)
BoxC x ,y  ,x+w ,y+d ,1,RGB(r1,g1,b1) : BoxC x+1,y+1,x+w-1,y+d-1 ,1,RGB(r2,g2,b2)
Ink RGB(Tr1,Tg1,Tb1) : Text x+ri+1,y+dn+1,Txt$ : Ink RGB(Tr2,Tg2,Tb2) : Text x+ri ,y+dn ,Txt$
EndFunction
`---get the background screen
Function BuildDisplayBKG()
Oggs.BkgImg = GetFreeImage() : CreateImage Oggs.BkgImg ,ScrWi,ScrHi :  RenderToImage Oggs.BkgImg : Cls RGB(100,100,225)
x = 2   : y = 2      :      SayBox(x   ,y,316,18, 15,15,15,120,120,190, 15,15,15,175,255,100 ,                    "" ,110,2)
x = 2   : y = 2      :      SayBox(x   ,y, 53,18, 15,15,15, 20, 20,255, 15,15,15,155,255,190 ,              "DIR..." ,3  ,2)
x = 2   : y = 22
For t = 1 To 4       :      SayBox(x   ,y, 23,18, 15,15,15,100,100,140, 15,15,15,255,255,255 ,           Str$(t)+"." ,5  ,2)
                           SayBox(x+22,y,294,18, 15,15,15,100,100,100, 15,15,15,255,255,255 ,                    "" ,5  ,2)
         y = y + 20
Next
x = 2   : y = 103    :      SayBox(x   ,y, 72,18, 15,15,15, 20, 20,255, 15,15,15,155,255,190 ,                "Mute" ,3  ,2)
x = 73  : y = 103    :      SayBox(x   ,y,245,18, 15,15,15,120,120,190, 15,15,15,240,240,255 ,      "Music Volume =" ,17 ,2)

x = 254 : y = 103    :      SayBox(x   ,y, 31,18, 15,15,15, 20, 20,255, 15,15,15,155,255,190 ,                 "<<<" ,3  ,1)
x = 287 : y = 103    :      SayBox(x   ,y, 31,18, 15,15,15, 20, 20,255, 15,15,15,155,255,190 ,                 ">>>" ,3  ,1)
x = 2   : y = 124    :      SayBox(x   ,y,316,18, 15,15,15,100,100,225, 15,15,15,255,220,100 ,                    "" ,3  ,1)
RenderToScreen
EndFunction
`---Shows which Music number  is  OR  is not  playing
`---Shows buttons to press to mute or lower or raise (all playing) music volume
`---Shows infomation of (all playing) music volume
Function DisplayOggInformation()
If Oggs.DirHiLi = 1 Then Oggs.DirHiLi = 0 : x = 5   : y = 4      : Say(x   ,y, 15,15,15, 255,220,200 ,         "DIR..." )

x = 58    : y = 4 :                          Say(x ,y, 15,15,15, 240,240,255 ,    Right$(Oggs.CurrentDir,32) )
x = 7   : y = 24
For t = 1 To Oggs.MaxMusic
                          s$ = Make$(" ",36) + GetFileName$(OggMusic(t).InUse)
                             Say(x+20,y, 15,15,15, 255,255,255 ,Right$(s$,36) )
y = y + 20 : Next :                  Mr=155:Mg=255:Mb=190 : muted$ = "ON" : If Oggs.Mute = 1 Then muted$ = "off"
          If Oggs.MuteHiLi = 1 Then Mr=255:Mg=220:Mb=200
                                                        Say(x-2 ,y+1,15,15,15, Mr, Mg, Mb ,           "Mute" )
             Oggs.MuteHiLi = 0 : x = 5   : y = 105    : Say(x+39,y,  15,15,15, Mr, Mg, Mb ,           muted$ )
                                 x = 210 : y = 105    : Say(x   ,y, 15,15,15, 255,220,100 ,Str$(Oggs.Volume) )
If Oggs.VolHiLi = 1 Then Oggs.VolHiLi = 0 : x = 256 : y = 104    : Say(x   ,y, 15,15,15, 255,220,200 ,            "<<<" )
If Oggs.VolHiLi = 2 Then Oggs.VolHiLi = 0 : x = 289 : y = 104    : Say(x   ,y, 15,15,15, 255,220,200 ,            ">>>" )
EndFunction
`---
Function ShowStuff()
         DrawImage Oggs.BkgImg ,0,0,0
         DisplayOggInformation()
         TextTips() : Sync
EndFunction
`---
`---end of code
...XpMe v1.2

http://tilemusic.com/

XpMe_v1.2

LoadDll "pbdialogs.dll",1

should be located here

C:\Program Files\PlayBasic\Projects\Examples\Dlls\OpenFile


the below functions should look like this

Function LowerOggVol( c )

Function  RaiseOggVol( c )
...XpMe v1.2

http://tilemusic.com/

kevin

QuoteI'm now finishing a board game and I need music and a doc file to finish.

Awesome, another finnished game.  I'd love to see it when your ready.

XpMe_v1.2

QuoteAwesome, another finnished game.  I'd love to see it when your ready.

As soon as I test it some more and finish the doc. Not much left to do.
...XpMe v1.2

http://tilemusic.com/