From 475b6ee59fa84278f44c3cb1aae0842de4ea0b47 Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Wed, 30 Jul 2014 12:37:22 -0700 Subject: [PATCH] Initial Flash and HTML5 audio contexts --- lime/media/AudioContext.hx | 4 +- lime/media/AudioSource.hx | 32 ++ lime/media/FlashAudioContext.hx | 218 +++++++++++- lime/media/HTML5AudioContext.hx | 609 +++++++++++++++++++++++++++++++- 4 files changed, 843 insertions(+), 20 deletions(-) create mode 100644 lime/media/AudioSource.hx diff --git a/lime/media/AudioContext.hx b/lime/media/AudioContext.hx index ae1b9d2b0..587bd71e4 100644 --- a/lime/media/AudioContext.hx +++ b/lime/media/AudioContext.hx @@ -10,9 +10,9 @@ import lime.media.WebAudioContext; enum AudioContext { OPENAL (al:ALAudioContext); - HTML5 (audio:HTML5AudioContext); + HTML5 (context:HTML5AudioContext); WEB (context:WebAudioContext); - FLASH (sound:FlashRenderContext); + FLASH (context:FlashAudioContext); CUSTOM (data:Dynamic); } \ No newline at end of file diff --git a/lime/media/AudioSource.hx b/lime/media/AudioSource.hx new file mode 100644 index 000000000..790ee3715 --- /dev/null +++ b/lime/media/AudioSource.hx @@ -0,0 +1,32 @@ +package lime.media; + + +#if js +import js.html.Audio; +#elseif flash +import flash.media.Sound; +#end + + +class AudioSource { + + + public var id:Int; + + #if js + public var src:Audio; + #elseif flash + public var src:Sound; + #else + public var src:Dynamic; + #end + + + public function new () { + + + + } + + +} \ No newline at end of file diff --git a/lime/media/FlashAudioContext.hx b/lime/media/FlashAudioContext.hx index 176094775..8bba450f5 100644 --- a/lime/media/FlashAudioContext.hx +++ b/lime/media/FlashAudioContext.hx @@ -1,12 +1,15 @@ -package lime.media; #if !flash +package lime.media; +import openfl.utils.UInt; + + +#if flash +import flash.media.Sound; +#end class FlashAudioContext { - // Need to come up with an API that allows for management of multiple Sound instances? - - public function new () { @@ -14,9 +17,204 @@ class FlashAudioContext { } -} - - -#else -typedef FlashAudioContext = flash.media.Sound; -#end \ No newline at end of file + public function createSource (stream:Dynamic /*URLRequest*/ = null, context:Dynamic /*SoundLoaderContext*/ = null):AudioSource { + + #if flash + var source = new AudioSource (); + source.src = new Sound (stream, context); + return source; + #else + return null; + #end + + } + + + public function getBytesLoaded (source:AudioSource):UInt { + + #if flash + if (source.src != null) { + + return source.src.bytesLoaded; + + } + #end + + return 0; + + } + + + public function getBytesTotal (source:AudioSource):Int { + + #if flash + if (source.src != null) { + + return source.src.bytesTotal; + + } + #end + + return 0; + + } + + + public function getID3 (source:AudioSource):Dynamic /*ID3Info*/ { + + #if flash + if (source.src != null) { + + return source.src.id3; + + } + #end + + return null; + + } + + + public function getIsBuffering (source:AudioSource):Bool { + + #if flash + if (source.src != null) { + + return source.src.isBuffering; + + } + #end + + return false; + + } + + + public function getIsURLInaccessible (source:AudioSource):Bool { + + #if flash + if (source.src != null) { + + return source.src.isURLInaccessible; + + } + #end + + return false; + + } + + + public function getLength (source:AudioSource):Float { + + #if flash + if (source.src != null) { + + return source.src.length; + + } + #end + + return false; + + } + + + public function getURL (source:AudioSource):String { + + #if flash + if (source.src != null) { + + return source.src.url; + + } + #end + + return null; + + } + + + public function close (source:AudioSource):Void { + + #if flash + if (source.src != null) { + + source.src.close (); + + } + #end + + } + + + public function extract (source:AudioSource, target:Dynamic /*flash.utils.ByteArray*/, length:Float, startPosition:Float = -1):Float { + + #if flash + if (source.src != null) { + + return source.src.extract (target, length, startPosition); + + } + #end + + return 0; + + } + + + public function load (source:AudioSource, stream:Dynamic /*flash.net.URLRequest*/, context:Dynamic /*SoundLoaderContext*/ = null):Void { + + #if flash + if (source.src != null) { + + source.src.load (stream, context); + + } + #end + + } + + + public function loadCompressedDataFromByteArray (source:AudioSource, bytes:Dynamic /*flash.utils.ByteArray*/, bytesLength:UInt):Void { + + #if flash + if (source.src != null) { + + source.src.loadCompressedDataFromByteArray (bytes, bytesLength); + + } + #end + + } + + + public function loadPCMFromByteArray (source:AudioSource, bytes:Dynamic /*flash.utils.ByteArray*/, samples:UInt, format:String = null, stereo:Bool = true, sampleRate:Float = 44100):Void { + + #if flash + if (source.src != null) { + + source.src.loadPCMFromByteArray (bytes, samples, format, stereo, sampleRate); + + } + #end + + } + + + public function play (source:AudioSource, startTime:Float = 0, loops:Int = 0, sndTransform:Dynamic /*SoundTransform*/ = null):Dynamic /*SoundChannel*/ { + + #if flash + if (source.src != null) { + + return source.src.play (startTime, loops, sndTransform); + + } + #end + + return null; + + } + + +} \ No newline at end of file diff --git a/lime/media/HTML5AudioContext.hx b/lime/media/HTML5AudioContext.hx index 219934738..85af94e6d 100644 --- a/lime/media/HTML5AudioContext.hx +++ b/lime/media/HTML5AudioContext.hx @@ -1,10 +1,23 @@ -package lime.media; #if !js +package lime.media; + + +#if js +import js.html.Audio; +#end class HTML5AudioContext { - // Need to come up with an API that allows for management of multiple Audio instances? + public inline var HAVE_CURRENT_DATA:Int = 2; + public inline var HAVE_ENOUGH_DATA:Int = 4; + public inline var HAVE_FUTURE_DATA:Int = 3; + public inline var HAVE_METADATA:Int = 1; + public inline var HAVE_NOTHING:Int = 0; + public inline var NETWORK_EMPTY:Int = 0; + public inline var NETWORK_IDLE:Int = 1; + public inline var NETWORK_LOADING:Int = 2; + public inline var NETWORK_NO_SOURCE:Int = 3; public function new () { @@ -14,9 +27,589 @@ class HTML5AudioContext { } -} - - -#else -typedef HTML5AudioContext = js.html.Audio; -#end \ No newline at end of file + public function canPlayType (source:AudioSource, type:String):String { + + #if js + if (source.src != null) { + + return source.src.canPlayType (type); + + } + #end + + return null; + + } + + + public function createSource (urlString:String = null):AudioSource { + + #if js + var source = new AudioSource (); + source.src = new Audio (urlString); + return source; + #else + return null; + #end + + } + + + public function getAudioDecodedByteCount (source:AudioSource):Int { + + #if js + if (source.src != null) { + + return source.src.audioDecodedByteCount; + + } + #end + + return 0; + + } + + + public function getAutoplay (source:AudioSource):Bool { + + #if js + if (source.src != null) { + + return source.src.autoplay; + + } + #end + + return false; + + } + + + public function getBuffered (source:AudioSource):Dynamic /*TimeRanges*/ { + + #if js + if (source.src != null) { + + return source.src.buffered; + + } + #end + + return null; + + } + + + public function getController (source:AudioSource):Dynamic /*MediaController*/ { + + #if js + if (source.src != null) { + + return source.src.controller; + + } + #end + + return null; + + } + + + public function getCurrentSrc (source:AudioSource):String { + + #if js + if (source.src != null) { + + return source.src.currentSrc; + + } + #end + + return null; + + } + + + public function getCurrentTime (source:AudioSource):Float { + + #if js + if (source.src != null) { + + return source.src.currentTime; + + } + #end + + return 0; + + } + + + public function getDefaultPlaybackRate (source:AudioSource):Float { + + #if js + if (source.src != null) { + + return source.src.defaultPlaybackRate; + + } + #end + + return 1; + + } + + + public function getDuration (source:AudioSource):Float { + + #if js + if (source.src != null) { + + return source.src.duration; + + } + #end + + return 0; + + } + + + public function getEnded (source:AudioSource):Bool { + + #if js + if (source.src != null) { + + return source.src.ended; + + } + #end + + return 0; + + } + + + public function getError (source:AudioSource):Dynamic /*MediaError*/ { + + #if js + if (source.src != null) { + + return source.src.error; + + } + #end + + return null; + + } + + + public function getInitialTime (source:AudioSource):Float { + + #if js + if (source.src != null) { + + return source.src.initialTime; + + } + #end + + return 0; + + } + + + public function getLoop (source:AudioSource):Bool { + + #if js + if (source.src != null) { + + return source.src.loop; + + } + #end + + return false; + + } + + + public function getMediaGroup (source:AudioSource):String { + + #if js + if (source.src != null) { + + return source.src.mediaGroup; + + } + #end + + return null; + + } + + + public function getMuted (source:AudioSource):Bool { + + #if js + if (source.src != null) { + + return source.src.muted; + + } + #end + + return false; + + } + + + public function getNetworkState (source:AudioSource):Int { + + #if js + if (source.src != null) { + + return source.src.networkState; + + } + #end + + return 0; + + } + + + public function getPaused (source:AudioSource):Bool { + + #if js + if (source.src != null) { + + return source.src.paused; + + } + #end + + return false; + + } + + + public function getPlaybackRate (source:AudioSource):Float { + + #if js + if (source.src != null) { + + return source.src.playbackRate; + + } + #end + + return 1; + + } + + + public function getPlayed (source:AudioSource):Dynamic /*TimeRanges*/ { + + #if js + if (source.src != null) { + + return source.src.played; + + } + #end + + return null; + + } + + + public function getPreload (source:AudioSource):Bool { + + #if js + if (source.src != null) { + + return source.src.preload; + + } + #end + + return false; + + } + + + public function getReadyState (source:AudioSource):Int { + + #if js + if (source.src != null) { + + return source.src.readyState; + + } + #end + + return false; + + } + + + public function getSeekable (source:AudioSource):Dynamic /*TimeRanges*/ { + + #if js + if (source.src != null) { + + return source.src.seekable; + + } + #end + + return null; + + } + + + public function getSeeking (source:AudioSource):Bool { + + #if js + if (source.src != null) { + + return source.src.seeking; + + } + #end + + return false; + + } + + + public function getSrc (source:AudioSource):String { + + #if js + if (source.src != null) { + + return source.src.src; + + } + #end + + return null; + + } + + + public function getStartTime (source:AudioSource):Float { + + #if js + if (source.src != null) { + + return source.src.playbackRate; + + } + #end + + return 0; + + } + + + public function getVolume (source:AudioSource):Float { + + #if js + if (source.src != null) { + + return source.src.volume; + + } + #end + + return 1; + + } + + + public function load (source:AudioSource):Void { + + #if js + if (source.src != null) { + + return source.src.load (); + + } + #end + + } + + + public function pause (source:AudioSource):Void { + + #if js + if (source.src != null) { + + return source.src.pause (); + + } + #end + + } + + + public function play (source:AudioSource):Void { + + #if js + if (source.src != null) { + + return source.src.play (); + + } + #end + + } + + + public function setAutoplay (source:AudioSource, value:Bool):Void { + + #if js + if (source.src != null) { + + source.src.autoplay = value; + + } + #end + + } + + + public function setController (source:AudioSource, value:Dynamic /*MediaController*/):Void { + + #if js + if (source.src != null) { + + source.src.controller = value; + + } + #end + + } + + + public function setCurrentTime (source:AudioSource, value:Float):Void { + + #if js + if (source.src != null) { + + source.src.currentTime = value; + + } + #end + + } + + + public function setDefaultPlaybackRate (source:AudioSource, value:Float):Void { + + #if js + if (source.src != null) { + + source.src.defaultPlaybackRate = value; + + } + #end + + } + + + public function setLoop (source:AudioSource, value:Bool):Void { + + #if js + if (source.src != null) { + + source.src.loop = value; + + } + #end + + } + + + public function setMediaGroup (source:AudioSource, value:String):Void { + + #if js + if (source.src != null) { + + source.src.mediaGroup = value; + + } + #end + + } + + + public function setMuted (source:AudioSource, value:Bool):Void { + + #if js + if (source.src != null) { + + source.src.muted = value; + + } + #end + + } + + + public function setPlaybackRate (source:AudioSource, value:Float):Void { + + #if js + if (source.src != null) { + + source.src.playbackRate = value; + + } + #end + + } + + + public function setPreload (source:AudioSource, value:Bool):Void { + + #if js + if (source.src != null) { + + source.src.preload = value; + + } + #end + + } + + + public function setSrc (source:AudioSource, value:String):Void { + + #if js + if (source.src != null) { + + source.src.src = value; + + } + #end + + } + + + public function setVolume (source:AudioSource, value:Float):Void { + + #if js + if (source.src != null) { + + source.src.volume = value; + + } + #end + + } + + +} \ No newline at end of file