Audio Improvements
Hi
Some improvements for the Audio API:
- Create the function "getCurrentTrack" - returns the current Music Track Playing ID or the function "isCurrentTrack(ID)" - compare the ID with the current Music Playing. The idea is find the actual music track playback.
- When calling the function me.audio.disable, all music tracks in playback must be stopped.
- Add volume and mute control to audio. The HTML5 audio has the atributes media . volume [ = value between 0 and 1 ] and media . muted [ = value true or false ]:
http://dev.w3.org/html5/spec/single-page.html#effective-media-volume http://html5doctor.com/html5-audio-the-state-of-play/
Thanks!
getCurrentTrackis super easy to implement. But... Playing devil's advocate for a moment, is there a reason your code is not able to keep track of which BGM was last played?- Second suggestion is also super simple to implement. I did something similar when I added
me.audio.unloadAll() - Accessing channel volume is a great idea.
Thanks.
yes, nice specially for the volume (I actually don't remember why I did not managed the volume in the first place), and on top of this, the Audio part of melonJS needs a small refresh anyway, like cleaning some code or adding missing audio format.
Hi parasyte, the idea in getCurrentTrack is facilitate the process of detecting if a background music is being played between the transition screens. For example, the Title Screen and Levels Selection Screen plays the same music, but the music is stop int the Game Screen. I currently use a variable for this, but as the audio API already has some private variables (current_track_id and current_track) this could be an extra feature in the engine ;)
Olivier thanks again for the melonJS engine and Jason, thanks for the optimizations, you are the guys!
Thanks!
Another request that should be part of this ticket: Support a callback for the canplay event in me.audio.load ... and somehow extend that to me.loader.load This is to support audio streaming.
Support for Web Audio API would be REALLY nice, because it's supported in iOS6, and will help to alleviate a lot of the limitations imposed by Mobile Safari's HTML5 audio support.
One word of warning: a user interaction is still necessary to "unmute" the Web Audio API. We will have to include something like a little "unmute" button in the corner of the screen, or "tap here to play" screen for Web Audio API-enabled mobile devices.
The API is also fully supported in desktop Chrome.
kind of related :
In case you did not you see the following, it's quite old now, and would need some clean-up, but it's add mod playing support to melonJS using the Audio API : https://github.com/obiot/melonJS-Plugins/tree/master/audio
(note : that's why we have a binary loader)
@obiot very cool! I remember seeing this in the old plugins repo. (I used the dialog plugin for my game) but didn't look through it until tonight. Just a quick peek, but it looks great!
@parasyte here is an example using it : https://github.com/shazz/Union-Demo-HTML5-Remake
Kind of off topic now, but: I found this: http://www.melonjs.org/demos/Union-Demo-HTML5-Remake/ But it links to the wrong file name.
GET http://www.melonjs.org/demos/Union-Demo-HTML5-Remake/data/music/Alloy_Run.ym 404 (Not Found) codef_music.js:79
The correct file name on the server is "Union%20Demo%20-%20Alloy%20Run.ym" :)
@ciangames @parasyte
Hi guys,
I added all the features requested in this ticket, except the callback for the canplay event.
Jason, on this last one, would mind further explain what you had in mind ?
thanks!
@obiot I don't remember exactly what I had in mind for audio streaming, but it was probably for supporting enabling better audio support on mobile. (Especially iOS 5 and older.)
A really important use for the canplay callback, even in modern browsers, is to reduce load times. Background music files are amongst the largest to download, so it would be much better to "stream" them if possible, instead of waiting for all of them to load during the loading screen.
The way to accomplish this is by supporting the canplay event, which according to MDN, "... is fired when the user agent can play the media, but estimates that not enough data has been loaded to play the media up to its end without having to stop for further buffering of content." This is actually perfect for streaming (playing while downloading) background music.
I'm in the mood for some melonJS hacking right now, so I'm going to commit a patch soon to address this. I'm thinking of adjusting the me.audio.load function to accept callback parameters for onload, onerror, and canplay. To start streaming music during a level, just do something like:
me.audio.load({
name : "bgmusic",
src : "http://www.example.com/bgmusic.ogg",
channel : 1
}, onload_cb, onerror_cb, function canplay_cb() {
me.audio.play("bgmusic", true);
});
In my original proposal, I suggested bubbling the canplay event to me.loader.load(), but I don't think that's wise. Keeping it within the audio module is cleaner.
That's a very very good idea, and furthermore it could also be an additional option for the audio type (i mean when passing the entry with name, path, #channels)
Glad that you are in this mood :)
Yeah? Could you explain more about that idea? (I'm on IRC right now if you like to discuss. Also respond here to keep a record! :D)
I'm out actually and replying from my mobile.
But my idea was to add one more parameter to the structure entry we pass to the preload or load function for the audio type and add something like a boolean ("stream"), that would cause the loader to register on "canPlay" instead of "canPlayThrough" for the selected entry.
@obiot I love that idea. :) Have a look at the last commit when you get a chance. If I understand right, the me.loader.load function should now work as you expect with the stream property on audio resources.
I also updated the doc comments with an example for streaming background music. It will be really cool to see more flashy loading screens with music now! :D
@parasyte awesome, thank you :)
I'll give it a proper look when i'm back tomorrow!
Hi @parasyte,
So i gave a further review to your last change, and was wondering why you actually kept the event registration on canPlayThrough if the stream parameter is set.
I'm maybe missnig a use case, but from my understanding that would allow not only to stream stuff using the load function, but as well to request it through the preloader (as both could be mixed as well).
soundclip.addEventListener((sound.stream === true) ?"canplay" :"canplaythrough", function(e) {
this.removeEventListener((sound.stream === true) ?"canplay" :"canplaythrough", arguments.callee, false);
soundLoaded.call(
me.audio,
sound.name,
sound.channel || 1,
onload_cb
);
}, false);
plus it makes the code much more simpler :)
I think soundLoaded should only be called when the audio element is fully loaded, right? I originally had something like your example (storing the result of the sound.stream check in a variable) but changed it because soundLoaded will create additional audio elements (one for each channel) using the same source. I don't know how well that works when the source file hasn't finished loading.
What do you think?
@parasyte good point, but the question is maybe more to think about the use case behind combining multiple channel and streaming, as if the idea is to stream music track, I don't think there is a need to create several channels for it (so maybe we could not create additional channel when stream is specified ?
Else I really think that allowing to combine regular audio elements loading (for SFX) and to stream music audio elements (level background music) would be great.
Ah, so we just declare that sound.stream and sound.channels are mutually exclusive.; You may have one or the other, or neither. But never both. I think that can be worked in, and would really simplify the code to get rid of one listener.
Another way to think about it is that we give stream priority in the case that both are set, because it will ideally make load times shorter. :)
var channels = sound.channel || 1;
var eventname = "canplaythrough";
if (sound.stream === true) {
channels = 1;
eventname = "canplay";
}
soundclip.addEventListener(eventname, function(e) {
this.removeEventListener(eventname, arguments.callee, false);
soundLoaded.call(
me.audio,
sound.name,
channels,
onload_cb
);
}, false);