diff --git a/src/lime/_internal/backend/flash/FlashAudioSource.hx b/src/lime/_internal/backend/flash/FlashAudioSource.hx index c0b6dce55..1d1f10c9c 100644 --- a/src/lime/_internal/backend/flash/FlashAudioSource.hx +++ b/src/lime/_internal/backend/flash/FlashAudioSource.hx @@ -117,6 +117,17 @@ class FlashAudioSource return loops = value; } + public function getPitch():Float + { + lime.utils.Log.verbose("Pitch is not supported in Flash."); + return 1; + } + + public function setPitch(value:Float):Float + { + return getPitch(); + } + public function getPosition():Vector4 { position.x = channel.soundTransform.pan; diff --git a/src/lime/_internal/backend/html5/HTML5AudioSource.hx b/src/lime/_internal/backend/html5/HTML5AudioSource.hx index 51f6c9823..3c7e314f6 100644 --- a/src/lime/_internal/backend/html5/HTML5AudioSource.hx +++ b/src/lime/_internal/backend/html5/HTML5AudioSource.hx @@ -204,6 +204,25 @@ class HTML5AudioSource return loops = value; } + public function getPitch():Float + { + #if lime_howlerjs + return parent.buffer.__srcHowl.rate(); + #else + return 1; + #end + } + + public function setPitch(value:Float):Float + { + #if lime_howlerjs + parent.buffer.__srcHowl.rate(value); + #end + + return getPitch(); + } + + public function getPosition():Vector4 { #if lime_howlerjs diff --git a/src/lime/_internal/backend/native/NativeAudioSource.hx b/src/lime/_internal/backend/native/NativeAudioSource.hx index e8d6234db..e15e41360 100644 --- a/src/lime/_internal/backend/native/NativeAudioSource.hx +++ b/src/lime/_internal/backend/native/NativeAudioSource.hx @@ -387,6 +387,12 @@ class NativeAudioSource public function setCurrentTime(value:Int):Int { + // `setCurrentTime()` has side effects and is never safe to skip. + /* if (value == getCurrentTime()) + { + return value; + } */ + if (handle != null) { if (stream) @@ -425,7 +431,7 @@ class NativeAudioSource timer.stop(); } - var timeRemaining = getLength() - value; + var timeRemaining = Std.int((getLength() - value) / getPitch()); if (timeRemaining > 0) { @@ -484,7 +490,7 @@ class NativeAudioSource timer.stop(); } - var timeRemaining = value - getCurrentTime(); + var timeRemaining = Std.int((value - getCurrentTime()) / getPitch()); if (timeRemaining > 0) { @@ -506,6 +512,44 @@ class NativeAudioSource return loops = value; } + public function getPitch():Float + { + if (handle != null) + { + return AL.getSourcef(handle, AL.PITCH); + } + else + { + return 1; + } + } + + public function setPitch(value:Float):Float + { + if (playing && value != getPitch()) + { + if (timer != null) + { + timer.stop(); + } + + var timeRemaining = Std.int((getLength() - getCurrentTime()) / value); + + if (timeRemaining > 0) + { + timer = new Timer(timeRemaining); + timer.run = timer_onRun; + } + } + + if (handle != null) + { + AL.sourcef(handle, AL.PITCH, value); + } + + return value; + } + public function getPosition():Vector4 { if (handle != null) diff --git a/src/lime/media/AudioSource.hx b/src/lime/media/AudioSource.hx index 1bbdedac7..ab0992e4f 100644 --- a/src/lime/media/AudioSource.hx +++ b/src/lime/media/AudioSource.hx @@ -17,6 +17,7 @@ class AudioSource public var gain(get, set):Float; public var length(get, set):Int; public var loops(get, set):Int; + public var pitch(get, set):Float; public var offset:Int; public var position(get, set):Vector4; @@ -108,6 +109,16 @@ class AudioSource return __backend.setLoops(value); } + @:noCompletion private function get_pitch():Float + { + return __backend.getPitch(); + } + + @:noCompletion private function set_pitch(value:Float):Float + { + return __backend.setPitch(value); + } + @:noCompletion private function get_position():Vector4 { return __backend.getPosition();