Audio Channel Problem

Monkey Forums/Monkey Bug Reports/Audio Channel Problem

Midimaster(Posted 2013) [#1]
A old problem seems to be back:

On the Version V69 I can only use the Audio Channel 1-8, They are working perfect. Audio Channels 0 or 9...32 give no error, but the channelstate() return "0" and I get no sound on HTML5 target on FireFox 20.

here is a sample code:

Strict
Import mojo
Class Game Extends App

	Field Timer%
	Method OnCreate%()
		SetUpdateRate 15
		MySound.Create
		Return 0
	End	

	Method OnUpdate%()
		If KeyHit(KEY_ESCAPE) Then Error ""
		If TouchHit(0) MySound.SpielTon
		MySound.Check
		Return 0
	End	

	Method OnRender%()
		Cls 0,0,0
		DrawText "click 10 times",50,50
		Return 0
	End	
	
End

Function Main%()
	New Game
	Return 0
End



Class MySound 
	Global Klavier:Sound, ChannelStep%=0
	
	Field Channel%=0, ChannelTimer%=0

	Global Liste:List<MySound> = New List<MySound>

	Function Create:Void()
				Klavier=LoadSound("PianoW14.ogg")
	End
	
	Function SpielTon:Void()
		ChannelStep=(ChannelStep+1) Mod 12

		Local loc:MySound = New MySound
		loc.Channel=ChannelStep
		loc.ChannelTimer=Millisecs()
		Liste.AddLast loc

		StopChannel ChannelStep
		SetChannelVolume ChannelStep,1
		PlaySound Klavier,ChannelStep
	End
	
	Function Check:Void()
		For Local loc:MySound = Eachin Liste
			Local ChannelTimer%=loc.ChannelTimer
			Local Channel%=loc.Channel

local DebugText$="Ch. No:->" + Channel + " Started:" + ChannelTimer + " Now:" 
DebugText=DebugText + Millisecs() + " State:" + ChannelState(Channel)
Print DebugText

		Next
	End Function  
End


I think it has to do with the audio files. I always get this problem with my audio files, but Marks audio sample code runs perfect.
Here is the audio file. Rename it back to "PianoW14.ogg":

http://www.blitzforum.de/upload/file.php?id=12373


marksibly(Posted 2013) [#2]
Which targets? All? Just html5?

And if it's a problem with an audio file, there is little I can do to fix it - perhaps try importing/exporting it with an audio app?


Midimaster(Posted 2013) [#3]
Did you test it? I don't think it has to do do with a special audio file. It could be, that only the length of my audio files are different to your sample files.

It only occurs in HTML5. I did add you the audio file to please you testing the sample too. The code and the file of post #1 always produce the error. So it should be easy checking it.

Yesterday I also recognized that browsers have problems differing a new (audio) file from an old, when you hours before used a prior file with the same name. The game keeps on playing the old files, although the new ones are in the data folder.


dawlane(Posted 2013) [#4]
Yesterday I also recognized that browsers have problems differing a new (audio) file from an old, when you hours before used a prior file with the same name. The game keeps on playing the old files, although the new ones are in the data folder.
This issue you will find is most likely down to the browser caching files. Try clearing the browser cache to see if that clears it up.


Midimaster(Posted 2013) [#5]
thats, what i did... of course it worked...

But should'nt a monkey app in debug mode force the browser to reload the files each time new?

(but that behavior is only a minor priority. Can sombody please check the code above and the confirm the bug?)


dawlane(Posted 2013) [#6]
Well tested the code and I get similar results if I click the mouse button in quick succession, but if there's a slight delay between clicks I don't.

But should'nt a monkey app in debug mode force the browser to reload the files each time new?
You could add a meta tag to the head of a web page to stop caching, but even then it's a hit and miss depending on the browser. Here's a tutorial on caching.


Midimaster(Posted 2013) [#7]
Thats exactly, what i think. Mark's audio file are not long enough to produce the problem. If I use these 2sec files and I click very often, the problem increases.

But with the V69 I get the bugs even, when I click very slowly.


marksibly(Posted 2013) [#8]
Just had a look at the html5 audio code, and there is a 'max out' limit that prevents it loading the same sound more than 8 times. I added this because of a bug in firefox or opera or safari or something that caused the app to crash if too many sounds were simultaneously loaded. Html5 doesn't allow you to play the same sound multiple times, so each 'instance' of a sound playing through a unique channel must be independently loaded, which causes (hopefully that's now 'caused') internal browser caches to get confused/crash.

You could try getting rid of it - comment out/delete this line (557) in mojo.html5.js

	if( this.insts.length==8 ) return null;


This does appear to work here on my current Mac/Chome setup, but I'd need to test it on more browsers before making it an 'official' tweak. Still, I think it *should* be OK by now.

Unfortunately, the main problem is the html5 audio engine is just not very good, and there are a ton of workarounds and kludges in there to get it to work halfway decently on a wide range of browsers already. I would NOT really recommend the use of html5 audio for what you're doing, but it's your call. The good news is that there's apparently a new audio API on the way better suited for games etc that we'll hopefully be able to use soon.


Midimaster(Posted 2013) [#9]
thank you mark for clearifying the reason for this behavior. Now I use it as a "feature" and not a bug. I can wait for a future version, where a new and better audio api may get included in html5.

I'm not 100% sure, if this problem realy only occurs with "the same sound". I will test this. In the reality of my music games, I do not need often the same sound at the same time. But it can happen, that there are more than 8 different sounds together. Only sometime I need a second time the same sound, because the prior instance is still running.


Gerry Quinn(Posted 2013) [#10]
Why is HTML5 audio such a nightmare? It's like some saboteur decided to wreck the language prospects!

Everything else gets slowly standardised between browsers, or works after some fashion... but audio if anything is getting worse.


marksibly(Posted 2013) [#11]
> Why is HTML5 audio such a nightmare? It's like some saboteur decided to wreck the language prospects!

I think the main issue is it wasn't originally intended to be used the way games and realtime apps want to use it these days, in a much more 'programmable' environment.

The original designers probably didn't care much about latency and caching delays etc, and there's no concept of 'channel' in there so having to load sounds multiple times to 'fake' channels is a mess.

I do think it's improved a lot over time, but I also think html5 definitely needs a whole new realtime audio API.


Raz(Posted 2013) [#12]
Oh good, it's nice to see there's a reason for this, I ran in to this a while back when a sound gets repeated quickly.

In the end I just got my own sound manager to use one of 8 audio channels instead of the full 32.