Pārlūkot izejas kodu

more sound api + bugfixes

ncannasse 10 gadi atpakaļ
vecāks
revīzija
c2441b265c
3 mainītis faili ar 23 papildinājumiem un 7 dzēšanām
  1. 18 5
      hxd/res/Sound.hx
  2. 2 0
      hxd/snd/NativeChannel.hx
  3. 3 2
      hxd/snd/Worker.hx

+ 18 - 5
hxd/res/Sound.hx

@@ -3,6 +3,7 @@ package hxd.res;
 class Sound extends Resource {
 class Sound extends Resource {
 
 
 	var data : hxd.snd.Data;
 	var data : hxd.snd.Data;
+	var channel : hxd.snd.Channel;
 	public var lastPlay(default, null) = 0.;
 	public var lastPlay(default, null) = 0.;
 
 
 	public function getData() : hxd.snd.Data {
 	public function getData() : hxd.snd.Data {
@@ -22,10 +23,25 @@ class Sound extends Resource {
 	}
 	}
 
 
 	public function dispose() {
 	public function dispose() {
+		stop();
 		data = null;
 		data = null;
 	}
 	}
 
 
-	public function play( loop = false ) {
+	public function stop() {
+		if( channel != null ) {
+			channel.stop();
+			channel = null;
+		}
+	}
+
+	public function play( loop = false, volume = 1. ) {
+		lastPlay = haxe.Timer.stamp();
+		return channel = getWorker().play(this, loop, volume);
+	}
+
+	static var defaultWorker : hxd.snd.Worker = null;
+
+	public static function getWorker() {
 		if( defaultWorker == null ) {
 		if( defaultWorker == null ) {
 			// don't use a native worker since we haven't setup it in our main()
 			// don't use a native worker since we haven't setup it in our main()
 			var old = hxd.Worker.ENABLE;
 			var old = hxd.Worker.ENABLE;
@@ -34,12 +50,9 @@ class Sound extends Resource {
 			defaultWorker.start();
 			defaultWorker.start();
 			hxd.Worker.ENABLE = old;
 			hxd.Worker.ENABLE = old;
 		}
 		}
-		lastPlay = haxe.Timer.stamp();
-		return defaultWorker.play(this, loop);
+		return defaultWorker;
 	}
 	}
 
 
-	static var defaultWorker : hxd.snd.Worker = null;
-
 	public static function startWorker() {
 	public static function startWorker() {
 		if( defaultWorker != null )
 		if( defaultWorker != null )
 			return true;
 			return true;

+ 2 - 0
hxd/snd/NativeChannel.hx

@@ -60,7 +60,9 @@ class NativeChannel {
 	function onFlashSample( event : flash.events.SampleDataEvent ) {
 	function onFlashSample( event : flash.events.SampleDataEvent ) {
 		var buf = event.data;
 		var buf = event.data;
 		buf.length = bufferSamples * 2 * 4;
 		buf.length = bufferSamples * 2 * 4;
+		buf.position = 0;
 		onSample(haxe.io.Float32Array.fromBytes(haxe.io.Bytes.ofData(buf)));
 		onSample(haxe.io.Float32Array.fromBytes(haxe.io.Bytes.ofData(buf)));
+		buf.position = bufferSamples * 2 * 4;
 	}
 	}
 	#end
 	#end
 
 

+ 3 - 2
hxd/snd/Worker.hx

@@ -32,6 +32,7 @@ class NativeChannelData {
 	public var tmpBuf : haxe.io.Bytes;
 	public var tmpBuf : haxe.io.Bytes;
 	public var channel : NativeChannel;
 	public var channel : NativeChannel;
 	public var channels : Array<Channel>;
 	public var channels : Array<Channel>;
+	public var busy : Bool;
 	public function new(w:Worker) {
 	public function new(w:Worker) {
 		channels = [];
 		channels = [];
 		tmpBuf = haxe.io.Bytes.alloc(w.bufferSamples * 4 * 2);
 		tmpBuf = haxe.io.Bytes.alloc(w.bufferSamples * 4 * 2);
@@ -72,7 +73,7 @@ class Worker extends hxd.Worker<Message> {
 
 
 	function cleanChannels() {
 	function cleanChannels() {
 		for( c in channels )
 		for( c in channels )
-			if( c.channels.length == 0 && c.channel != null ) {
+			if( c.channels.length == 0 && c.channel != null && !c.busy ) {
 				c.channel.stop();
 				c.channel.stop();
 				c.channel = null;
 				c.channel = null;
 			}
 			}
@@ -198,7 +199,7 @@ class Worker extends hxd.Worker<Message> {
 		var cmax = chan.channels.length;
 		var cmax = chan.channels.length;
 		for( i in 0...bufferSamples*2 )
 		for( i in 0...bufferSamples*2 )
 			out[i] = 0;
 			out[i] = 0;
-
+		chan.busy = cmax != 0;
 		while( cid < cmax ) {
 		while( cid < cmax ) {
 			var c = chan.channels[cid++];
 			var c = chan.channels[cid++];