TChannel question
BlitzMax Forums/BlitzMax Programming/TChannel question
| ||
Hi guys! I'm at a part in my project where its time to play music and I'm a little confused about channels. What I have so far is quite a few short sounds playing at various points with PlaySound. A few of these can play at the same time and its not a problem. The problem is getting music to also play at the same time - which comes on at random intervals - but not to play if its already playing. So if I use PlaySound to play music, it works. (I read that PlayMusic always loads from disk - pauses the game, so using PlaySound) The trouble is I don't know what default channel or whatever the PlaySound uses. I just load a bunch of sounds and use PlaySound. The little sounds are fine, even if they re-play on top of each other because they're so short - like a second or so. But the music file might be a half minute or so. So I end up with the music playing on top of itself which muffs everything up. I know about the ChannelPlaying command, but I don't know how to set up JUST the music on a different channel that will not stop my other sounds, so I can check if music is already playing before restarting. I figure it involves AllocChannel or TChannel or something, but not sure how to make this separate from all my little sounds, where I didn't set up any special channels. Any ideas on how I should go about this? Thanks in advance! -Rob |
| ||
The PlaySound Command returns a value: the Channel! So if you store this you have a pointer to check, what is your sound is currently doing: Global MySound:TSound=LoadSound("music.ogg") Global MusicChannel:TChannel=PlaySound(MySound) Repeat ... If MusicChannel.IsPlaying() Print "still playing" Else Print "has stopped" Endif Forever This way you can control Pan, Volume, Rate, ... and also stopp the channel. See "TChannel" manual *** EDITED **** thanks to Robby for finding a bug |
| ||
Thanks for the feedback. I think I'm a little confused as to the name of the loaded sound (or music) and the name of its channel? Not sure how the "load" sound fits in because when I try the above I get a can't convert string to TSound error. I figure its something like: Global combatmus:TSound=LoadSound("comba1.ogg") ?? Global TownMusic:TSound=LoadSound("town1mus.ogg") ?? Global MusicChannel:TSound=LoadSound("comba1.ogg") ?? Global MusicChannel:TChannel=PlaySound("comba1.ogg") ?? Then later in the program, something like this, play only if not already playing. If Not MusicChannel.Playing() Then PlaySound MusicChannel End If Also want to make sure my little sounds will come on when the music is playing. Still a little mixed up. Thanks, -Rob |
| ||
sorry, that was my mistake. I wrote it "on the fly"... Of course the PlaySound() command needs the TSound Type and not the file name: Global MySound:TSound=LoadSound("music.ogg") Global MusicChannel:TChannel=PlaySound(MySound) Repeat ... If MusicChannel.IsPlaying() Print "still playing" Else Print "has stopped" Endif Forever As long as a channel is playing, it will not be used for other sounds. So PlaySound() (always when it is called) returns a new generated Channel. You can force to use the same channel again: If Not MusicChannel.Playing() Then PlaySound MySound, MusicChannel End If or you can store the new channel in the old variable: If Not MusicChannel.Playing() Then MusicChannel= PlaySound(MySound) End If perphaps also this may work: Global MySound:TSound=LoadSound("music.ogg") Global MusicChannel:TChannel=CueSound(MySound) ' CueSound()!!!! Repeat ... If Not MusicChannel.Playing() Then ResumeChannel MusicChannel Endif Forever |