Weird Channel Bug?

BlitzMax Forums/BlitzMax Programming/Weird Channel Bug?

Grey Alien(Posted 2006) [#1]
This has been annoying me for about 3 hours now. I know there are other ways to play sounds, but this should work right?

Here's an exe with source and the sound fx: http://www.greyaliengames.com/misc/soundtest.zip [~500K)

Strict

Graphics 800,600,0

Global Explosion:TSound = LoadSound("explosion.wav")
Global Fire: TSound = LoadSound("fire.wav")
 
Global FChannel:TChannel = AllocChannel()
Global EChannel:TChannel = AllocChannel()

Repeat
	Cls
		DrawText("Press Z twice then quickly press X.",0,0)
		DrawText("The explosion noise is cut off.",0,15)
		DrawText("After that it works fine!",0,30)


		If KeyHit(KEY_Z) Then
			PlaySound(explosion,EChannel)
		EndIf
		If KeyHit(KEY_X) Then
			PlaySound(Fire,FChannel)
		EndIf
	Flip
Until KeyHit(KEY_ESCAPE)


What happens on my PC is that if I press Z twice then X, the X sound cuts off the Z sound EVEN THOUGH IT'S ON A DIFFERENT CHANNEL! What's going on? Naturally I'm very keen to hear if this is just happening on my PC or all PCs.

Actually, I am getting a worse, but related, problem in my mini game where after a couple of shots, my ship stops making firing noises at all even though it doesn't mix channels with the explosions. There really is no explanation except that using Channels in this way is Bugged in BMax, or my sound card (SB Audigy 2) is crazy, or I'm willing to accept there is something wrong with the above code of course, that would be the best thing :-)

oh and the same thing occurs if I change the code like this:
		If KeyHit(KEY_Z) Then
			EChannel = PlaySound(explosion,EChannel)
		EndIf
		If KeyHit(KEY_X) Then
			FChannel = PlaySound(Fire,FChannel)
		EndIf


and naturally it works fine like this:

If KeyHit(KEY_Z) Then
			EChannel = PlaySound(explosion)
		EndIf
		If KeyHit(KEY_X) Then
			FChannel = PlaySound(Fire)
		EndIf

but I don't want to do that as it wastes sound channels as a new one is made each time.


Grey Alien(Posted 2006) [#2]
What really confuses me is that in .audio.bmx, PLaySound is this:

Function PlaySound:TChannel( sound:TSound,channel:TChannel=Null )
	Return sound.Play( channel )
End Function


and TSound's Play method is this:
	Method Play:TChannel( alloced_channel:TChannel=Null )
		Return New TChannel
	End Method

All it appears to do is return a channel. So, like where is all the playing code etc, what does it do with alloced_channel? Where is the *real* source? ...


Who was John Galt?(Posted 2006) [#3]
I can confirm the problem on my machine. 'Sounds' like a bug.


Eikon(Posted 2006) [#4]
Sounds like the thing I described to you earlier, where even setting channel volume on my music would affect other sounds.


Grey Alien(Posted 2006) [#5]
This is great news that other people get the same bug. Also my example is very simple for Mark/Skidracer whoever to fix hopefully. I think there is a problem with AllocChannel where sometimes it must point to an existing channel or there's some kind of overlap. It's weird, like I said.

Basically the long and short of this is that, this sort of thing worked fine in BlitzPlus and allowed you to control channels well. I don't really like the idea of "throwing away" channels by calling PlaySound and not monitoring the returned channel. Besides, then you can get way too many sounds at once and it's too lound. Using channels you can control this and even auto-balance all the channels.


Yan(Posted 2006) [#6]
All it appears to do is return a channel. So, like where is all the playing code etc, what does it do with alloced_channel? Where is the *real* source? ...
Arrrrrrrgh...Not again... ;op

It's in FreeAudioAudio.bmx!

TSound, TChannel and TAudioDriver, make up the 'Null' driver (remember?).

If FreeAudio can initialise, TFreeAudioSound (which extends TSound), TFreeAudioChannel (which extends TChannel) and TFreeAudioAudioDriver (which extends TAudioDriver) are used via 'audio_driver:TAudioDriver' (which is global). All the magic takes place in TSound.Load().

I can't be arsed to explain it fully, just read the code man! ;op


Grey Alien(Posted 2006) [#7]
OK Thanks Ian, I don't think I fully understood before. I've found the source now.

Seems the Max Channels is 4096 in freeaudio.cpp:
#define MAXCHANNELS 4096


wonder if the bug is anything to do with this comment in the cpp file:

// 2006.01.17 fixed recycling of stopped channels

Or maybe it's always been there, really need an older version to test it on, if I could be bothered.

Problem with that C code is there aren't any comments so it's not exactly easy to figure our what's going on without sitting down for half a day and staring at it!


Sensenwerk(Posted 2006) [#8]
Just want to say that I've had exactly the same bug in my current project. Hope this will be corrected soon. :-\


Grey Alien(Posted 2006) [#9]
It is fixed now. Try a Sync mods.


Sensenwerk(Posted 2006) [#10]
I did (after updating to 1.20 of course) but the problem 's still the same. : (


Grey Alien(Posted 2006) [#11]
hmm, you mean if you download the zip file at the top of this post (just to get the sound effects), then recompile the source at the top of post, you get the 2nd explosion noise cut off still? This was fixed for me for sure.

Or are you reporting a similar problem in your own code, because if so, perhaps you have found another bug OR your own code is bugged, maybe?


Sensenwerk(Posted 2006) [#12]
similar problem, own code:

i've got a type called sound, controlling every sound/music played in my game:

Field MaxSChannels:Byte = 10;
Field SoundChannel:TChannel[MaxSChannels];

Field MusicChannel:TChannel;

So basically just 10 different channels for sound and one for music.

So in my menue i start a music, and when you switch over some buttons a sound is played (using my sound channels) and the music continues playing correctly.
But when I start the game an other music is started. Now when I fire a shot the shootsound is played and the music 's stopped immediately.
Strange... seems like the second time I start the music, it is played in an other channel, used by my sounds so that they 'overwrite' the current music. I just donno why that's the case.


Grey Alien(Posted 2006) [#13]
that does sound like a problem I was having, hmm. But mine are fixed now. It *could* be a bug in your code I guess too. Try to simplify it all into a mini program that shows the same fault, this it what I did and it let to a fix. Also if you ahe a bug, it might help you pinpoint it.


Grey Alien(Posted 2006) [#14]
Actually I'm about to write a new piece of channel code so I'll post if that doesn't work as it should.


Sensenwerk(Posted 2006) [#15]
Well I tried this and that, and finally fixed that problem thoug I don't really understand why this works only this way. I got this method:
	Method PlayM( MusicNr:Byte )
	
		If ChannelPlaying( MusicChannel )
			StopChannel( MusicChannel );
		EndIf
		MusicChannel = AllocChannel( ); 'I added this line and that solved my problem
		MusicChannel = PlaySound( Music[MusicNr], MusicChannel );	
		
	EndMethod


I actually *dislike* code that works without absolutly understanding it. I mean I allocated the channel before, so why do I have to alloc again to prevent overwriting by my other channels. |confused| Anyway, nice to have it working.


Wiebo(Posted 2006) [#16]
I'm having these problems too. I hope someone can find something cos I've given up


Grey Alien(Posted 2006) [#17]
Sensenwerk: You should only have to alloc the channel once at the start of the program, try it when it's declared like this:

Global MusicChannel:TChannel = AllocChannel() '(or this could be a field, whatever suits you)

then in your PlayM method remove the alloc channel thing and have only this line (notice the assignment IS NOT needed):

PlaySound(Music[MusicNr], MusicChannel)

and it should work finem MusicChannel is passed in and reused, it doens't need to be reassigned. This is what I'm doing and my sound doesn't overwrite it.


Sensenwerk(Posted 2006) [#18]
Well, yes the assignment is a relict of my various tests. It doesn't matter although it's unnecessary.

And as I wrote "I mean I allocated the channel before" you can see that I allocated it correctly on the startup of my type Sound, and in my case IT IS NEEDED to be reassigned because if I don't do so, the music is interrupted by my sounds.


Grey Alien(Posted 2006) [#19]
that's odd for sure. Anyway try removing the assignment before PlaySound as it shouldn't be there.


Grey Alien(Posted 2006) [#20]
Sensenwerk and Wiebo: I have isolated the problem, it's with stopchannel. See this thread in the bugs forum http://www.blitzbasic.com/Community/posts.php?topic=59301


Wiebo(Posted 2006) [#21]
Thanks M8