소스 검색

added Driver.hasFeature detection and automatic master volume handling

ncannasse 6 년 전
부모
커밋
4b069b8f9b
3개의 변경된 파일24개의 추가작업 그리고 9개의 파일을 삭제
  1. 5 0
      hxd/snd/Driver.hx
  2. 5 2
      hxd/snd/Manager.hx
  3. 14 7
      hxd/snd/openal/Driver.hx

+ 5 - 0
hxd/snd/Driver.hx

@@ -19,7 +19,12 @@ class EffectDriver<T> {
 	public function unbind  (e : T, source : SourceHandle) : Void {};
 	public function unbind  (e : T, source : SourceHandle) : Void {};
 }
 }
 
 
+enum DriverFeature {
+	MasterVolume;
+}
+
 interface Driver {
 interface Driver {
+	public function hasFeature           (d : DriverFeature) : Bool;
 	public function setMasterVolume      (value : Float) : Void;
 	public function setMasterVolume      (value : Float) : Void;
 	public function setListenerParams    (position : h3d.Vector, direction : h3d.Vector, up : h3d.Vector, ?velocity : h3d.Vector) : Void;
 	public function setListenerParams    (position : h3d.Vector, direction : h3d.Vector, up : h3d.Vector, ?velocity : h3d.Vector) : Void;
 
 

+ 5 - 2
hxd/snd/Manager.hx

@@ -90,6 +90,7 @@ class Manager {
 	var soundBufferMap    : Map<String, Buffer>;
 	var soundBufferMap    : Map<String, Buffer>;
 	var freeStreamBuffers : Array<Buffer>;
 	var freeStreamBuffers : Array<Buffer>;
 	var effectGC          : Array<Effect>;
 	var effectGC          : Array<Effect>;
+	var hasMasterVolume   : Bool;
 
 
 	private function new() {
 	private function new() {
 		try {
 		try {
@@ -103,6 +104,7 @@ class Manager {
 		}
 		}
 
 
 		masterVolume       = 1.0;
 		masterVolume       = 1.0;
+		hasMasterVolume    = driver.hasFeature(MasterVolume);
 		masterSoundGroup   = new SoundGroup  ("master");
 		masterSoundGroup   = new SoundGroup  ("master");
 		masterChannelGroup = new ChannelGroup("master");
 		masterChannelGroup = new ChannelGroup("master");
 		listener           = new Listener();
 		listener           = new Listener();
@@ -404,11 +406,12 @@ class Manager {
 		// --------------------------------------------------------------------
 		// --------------------------------------------------------------------
 
 
 		var usedEffects : Effect = null;
 		var usedEffects : Effect = null;
+		var gain = hasMasterVolume ? 1. : masterVolume;
 		for (s in sources) {
 		for (s in sources) {
 			var c = s.channel;
 			var c = s.channel;
 			if (c == null) continue;
 			if (c == null) continue;
 
 
-			var v = c.currentVolume;
+			var v = c.currentVolume * gain;
 			if (s.volume != v) {
 			if (s.volume != v) {
 				s.volume = v;
 				s.volume = v;
 				driver.setSourceVolume(s.handle, v);
 				driver.setSourceVolume(s.handle, v);
@@ -474,7 +477,7 @@ class Manager {
 		listener.direction.normalize();
 		listener.direction.normalize();
 		listener.up.normalize();
 		listener.up.normalize();
 
 
-		driver.setMasterVolume(masterVolume);
+		if( hasMasterVolume ) driver.setMasterVolume(masterVolume);
 		driver.setListenerParams(listener.position, listener.direction, listener.up, listener.velocity);
 		driver.setListenerParams(listener.position, listener.direction, listener.up, listener.velocity);
 
 
 		driver.update();
 		driver.update();

+ 14 - 7
hxd/snd/openal/Driver.hx

@@ -1,6 +1,7 @@
 package hxd.snd.openal;
 package hxd.snd.openal;
 
 
 import hxd.snd.openal.AudioTypes;
 import hxd.snd.openal.AudioTypes;
+import hxd.snd.Driver.DriverFeature;
 
 
 #if hlopenal
 #if hlopenal
 import openal.AL;
 import openal.AL;
@@ -35,6 +36,12 @@ class Driver implements hxd.snd.Driver {
 			throw "could not init openAL Driver";
 			throw "could not init openAL Driver";
 	}
 	}
 
 
+	public function hasFeature( f : DriverFeature ) {
+		return switch( f ) {
+		case MasterVolume: true;
+		}
+	}
+
 	public function getTmpBytes(size) {
 	public function getTmpBytes(size) {
 		if (tmpBytes.length < size) tmpBytes = haxe.io.Bytes.alloc(size);
 		if (tmpBytes.length < size) tmpBytes = haxe.io.Bytes.alloc(size);
 		return tmpBytes;
 		return tmpBytes;
@@ -110,7 +117,7 @@ class Driver implements hxd.snd.Driver {
 		bytes.setInt32(0, buffer.inst.toInt());
 		bytes.setInt32(0, buffer.inst.toInt());
 		AL.deleteBuffers(1, bytes);
 		AL.deleteBuffers(1, bytes);
 	}
 	}
-	
+
 	public function setBufferData(buffer : BufferHandle, data : haxe.io.Bytes, size : Int, format : Data.SampleFormat, channelCount : Int, samplingRate : Int) : Void {
 	public function setBufferData(buffer : BufferHandle, data : haxe.io.Bytes, size : Int, format : Data.SampleFormat, channelCount : Int, samplingRate : Int) : Void {
 		var alFormat = switch (format) {
 		var alFormat = switch (format) {
 			case UI8 : channelCount == 1 ? AL.FORMAT_MONO8  : AL.FORMAT_STEREO8;
 			case UI8 : channelCount == 1 ? AL.FORMAT_MONO8  : AL.FORMAT_STEREO8;
@@ -126,7 +133,7 @@ class Driver implements hxd.snd.Driver {
 
 
 	public function getPlayedSampleCount(source : SourceHandle) : Int {
 	public function getPlayedSampleCount(source : SourceHandle) : Int {
 		var v = source.sampleOffset + AL.getSourcei(source.inst, AL.SAMPLE_OFFSET);
 		var v = source.sampleOffset + AL.getSourcei(source.inst, AL.SAMPLE_OFFSET);
-		if (v < 0) 
+		if (v < 0)
 			v = 0;
 			v = 0;
 		return v;
 		return v;
 	}
 	}
@@ -134,7 +141,7 @@ class Driver implements hxd.snd.Driver {
 	public function getProcessedBuffers(source : SourceHandle) : Int {
 	public function getProcessedBuffers(source : SourceHandle) : Int {
 		return AL.getSourcei(source.inst, AL.BUFFERS_PROCESSED);
 		return AL.getSourcei(source.inst, AL.BUFFERS_PROCESSED);
 	}
 	}
-	
+
 	public function queueBuffer(source : SourceHandle, buffer : BufferHandle, sampleStart : Int, endOfStream : Bool) : Void {
 	public function queueBuffer(source : SourceHandle, buffer : BufferHandle, sampleStart : Int, endOfStream : Bool) : Void {
 		var bytes = getTmpBytes(4);
 		var bytes = getTmpBytes(4);
 		bytes.setInt32(0, buffer.inst.toInt());
 		bytes.setInt32(0, buffer.inst.toInt());
@@ -150,12 +157,12 @@ class Driver implements hxd.snd.Driver {
 			} else {
 			} else {
 				source.sampleOffset = 0;
 				source.sampleOffset = 0;
 			}
 			}
-			if (source.playing) 
+			if (source.playing)
 				AL.sourcePlay(source.inst);
 				AL.sourcePlay(source.inst);
 		}
 		}
 		buffer.isEnd = endOfStream;
 		buffer.isEnd = endOfStream;
 	}
 	}
-	
+
 	public function unqueueBuffer(source : SourceHandle, buffer : BufferHandle) : Void {
 	public function unqueueBuffer(source : SourceHandle, buffer : BufferHandle) : Void {
 		var bytes = getTmpBytes(4);
 		var bytes = getTmpBytes(4);
 		bytes.setInt32(0, buffer.inst.toInt());
 		bytes.setInt32(0, buffer.inst.toInt());
@@ -168,10 +175,10 @@ class Driver implements hxd.snd.Driver {
 		if (buffer.isEnd) source.sampleOffset = 0;
 		if (buffer.isEnd) source.sampleOffset = 0;
 		else source.sampleOffset += samples;
 		else source.sampleOffset += samples;
 	}
 	}
-	
+
 	public function update() : Void {
 	public function update() : Void {
 	}
 	}
-	
+
 	public function dispose() : Void {
 	public function dispose() : Void {
 		ALC.makeContextCurrent(null);
 		ALC.makeContextCurrent(null);
 		ALC.destroyContext(context);
 		ALC.destroyContext(context);