Browse Source

Sound fixes (#363)

Pascal Peridont 7 năm trước cách đây
mục cha
commit
c3d219e42e
3 tập tin đã thay đổi với 21 bổ sung4 xóa
  1. 12 3
      hxd/snd/Manager.hx
  2. 1 0
      hxd/snd/openal/AudioTypes.hx
  3. 8 1
      hxd/snd/openal/Driver.hx

+ 12 - 3
hxd/snd/Manager.hx

@@ -55,6 +55,7 @@ class Manager {
 	// Automatically set the channel to streaming mode if its duration exceed this value.
 	public static var STREAM_DURATION            = 5.;
 	public static var STREAM_BUFFER_SAMPLE_COUNT = 44100;
+	public static var BUFFER_QUEUE_LENGTH        = 2;
 	public static var MAX_SOURCES                = 16;
 	public static var SOUND_BUFFER_CACHE_SIZE    = 256;
 
@@ -194,7 +195,7 @@ class Manager {
 				continue;
 			}
 
-			c.position += now - c.lastStamp;
+			c.position += Math.max(now - c.lastStamp, 0.0);
 			c.lastStamp = now;
 
 			var next = c.next; // save next, since we might release this channel
@@ -274,11 +275,19 @@ class Manager {
 			// sync channel position
 			c.sound    = s.buffers[0].sound;
 			c.duration = c.sound.getData().duration;
-			c.position = (s.start + driver.getPlayedSampleCount(s.handle)) / s.buffers[0].sampleRate;
+
+			var playedSamples = driver.getPlayedSampleCount(s.handle);
+			if (playedSamples < 0)  {
+				#if debug
+				throw "playedSamples should positive : bug in driver";
+				#end
+				playedSamples = 0;
+			}
+			c.position = (s.start + playedSamples) / s.buffers[0].sampleRate;
 			c.positionChanged = false;
 
 			// enqueue next buffers
-			if (s.buffers.length < 2) {
+			if (s.buffers.length < BUFFER_QUEUE_LENGTH) {
 				var b = s.buffers[s.buffers.length - 1];
 				if (!b.isEnd) {
 					// next stream buffer

+ 1 - 0
hxd/snd/openal/AudioTypes.hx

@@ -19,6 +19,7 @@ class BufferHandle {
 class SourceHandle {
 	public var inst           : Source;
 	public var sampleOffset   : Int;
+	public var playing        : Bool;
 	var nextAuxiliarySend     : Int;
 	var freeAuxiliarySends    : Array<Int>;
 	var effectToAuxiliarySend : Map<Effect, Int>;

+ 8 - 1
hxd/snd/openal/Driver.hx

@@ -85,10 +85,12 @@ class Driver implements hxd.snd.Driver {
 
 	public function playSource(source : SourceHandle) : Void {
 		AL.sourcePlay(source.inst);
+		source.playing = true;
 	}
 
 	public function stopSource(source : SourceHandle) : Void {
 		AL.sourceStop(source.inst);
+		source.playing = false;
 	}
 
 	public function setSourceVolume(source : SourceHandle, value : Float) : Void {
@@ -119,7 +121,10 @@ class Driver implements hxd.snd.Driver {
 	}
 
 	public function getPlayedSampleCount(source : SourceHandle) : Int {
-		return source.sampleOffset + AL.getSourcei(source.inst, AL.SAMPLE_OFFSET);
+		var v = source.sampleOffset + AL.getSourcei(source.inst, AL.SAMPLE_OFFSET);
+		if (v < 0) 
+			v = 0;
+		return v;
 	}
 
 	public function getProcessedBuffers(source : SourceHandle) : Int {
@@ -141,6 +146,8 @@ class Driver implements hxd.snd.Driver {
 			} else {
 				source.sampleOffset = 0;
 			}
+			if (source.playing) 
+				AL.sourcePlay(source.inst);
 		}
 		buffer.isEnd = endOfStream;
 	}