Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jgranick committed Oct 30, 2017
1 parent fd13469 commit b93f373
Showing 1 changed file with 41 additions and 28 deletions.
69 changes: 41 additions & 28 deletions lime/media/AudioBuffer.hx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package lime.media;


import haxe.crypto.Base64;
import haxe.io.Bytes;
import haxe.io.Path;
import haxe.crypto.Base64;
import lime._backend.native.NativeCFFI;
import lime.app.Future;
import lime.app.Promise;
import lime.media.codecs.vorbis.VorbisFile;
import lime.media.openal.AL;
import lime.media.openal.ALBuffer;
import lime.net.HTTPRequest;
import lime.utils.UInt8Array;
import lime.utils.Log;
import lime.utils.UInt8Array;

#if howlerjs
import lime.media.howlerjs.Howl;
Expand Down Expand Up @@ -55,8 +55,9 @@ class AudioBuffer {


public function new () {





}


Expand Down Expand Up @@ -88,39 +89,21 @@ class AudioBuffer {
#end


#if (js && html5 && howlerjs)
private static function __getCodec (bytes:Bytes):String {

var signature = bytes.getString(0, 4);
switch (signature) {
case "OggS": return "audio/ogg";
case "fLaC": return "audio/flac";
case "RIFF" if (bytes.getString(8, 4) == "WAVE"): return "audio/wav";
default: if (bytes.getString(0, 3) == "ID3" || bytes.getString(0, 2) == "ÿû") {

return "audio/mp3";

}
}

Log.error("Unsupported sound format");
return null;
}
#end


public static function fromBase64 (base64String:String):AudioBuffer {

if (base64String == null) return null;

#if (js && html5 && howlerjs)

// if base64String doesn't contain codec data, add it.
if (base64String.indexOf(",") == -1) {

base64String = "data:" + __getCodec (Base64.decode (base64String)) + ";base64," + base64String;

}

var audioBuffer = new AudioBuffer ();
audioBuffer.src = new Howl ({ src: [base64String], html5: true, preload: false });
audioBuffer.src = new Howl ({ src: [ base64String ], html5: true, preload: false });
return audioBuffer;

#elseif lime_console
Expand Down Expand Up @@ -169,10 +152,11 @@ class AudioBuffer {
public static function fromBytes (bytes:Bytes):AudioBuffer {

if (bytes == null) return null;

#if (js && html5 && howlerjs)

var audioBuffer = new AudioBuffer ();
audioBuffer.src = new Howl ({ src: ["data:" + __getCodec (bytes) + ";base64," + Base64.encode (bytes)], html5: true, preload: false });
audioBuffer.src = new Howl ({ src: [ "data:" + __getCodec (bytes) + ";base64," + Base64.encode (bytes) ], html5: true, preload: false });

return audioBuffer;

Expand Down Expand Up @@ -474,9 +458,38 @@ class AudioBuffer {
}


private static function __getCodec (bytes:Bytes):String {

var signature = bytes.getString (0, 4);

switch (signature) {

case "OggS": return "audio/ogg";
case "fLaC": return "audio/flac";
case "RIFF" if (bytes.getString (8, 4) == "WAVE"): return "audio/wav";
default:

if (bytes.getString (0, 3) == "ID3" || bytes.getString (0, 2) == "ÿû") {

return "audio/mp3";

}

}

Log.error ("Unsupported sound format");
return null;

}




// Get & Set Methods




private function get_src ():Dynamic {

#if (js && html5)
Expand Down Expand Up @@ -543,4 +556,4 @@ class AudioBuffer {
}


}
}

0 comments on commit b93f373

Please # to comment.