From 2ffb6eef3f7eb401a2b23510b1cfaba446874851 Mon Sep 17 00:00:00 2001 From: Cameron Taylor Date: Sat, 18 Dec 2021 17:42:25 -0500 Subject: [PATCH 01/11] initial pitch stuff --- .../_internal/backend/native/NativeAudioSource.hx | 12 ++++++++++++ src/lime/media/AudioSource.hx | 13 +++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/lime/_internal/backend/native/NativeAudioSource.hx b/src/lime/_internal/backend/native/NativeAudioSource.hx index 693625cab..0baa34071 100644 --- a/src/lime/_internal/backend/native/NativeAudioSource.hx +++ b/src/lime/_internal/backend/native/NativeAudioSource.hx @@ -505,6 +505,18 @@ class NativeAudioSource return loops = value; } + public function getPitch():Float + { + return AL.getSourcef(handle, AL.PITCH); + } + + public function setPitch(value:Float):Float + { + AL.sourcef(handle, AL.PITCH, value); + + return getPitch(); + } + public function getPosition():Vector4 { if (handle != null) diff --git a/src/lime/media/AudioSource.hx b/src/lime/media/AudioSource.hx index 1bbdedac7..3a34cb71c 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; @@ -36,6 +37,8 @@ class AudioSource this.loops = loops; + pitch = 1; + if (buffer != null) { init(); @@ -108,6 +111,16 @@ class AudioSource return __backend.setLoops(value); } + private function get_pitch():Float + { + return __backend.getPitch(); + } + + private function set_pitch(value:Float):Float + { + return __backend.setPitch(value); + } + @:noCompletion private function get_position():Vector4 { return __backend.getPosition(); From b3dd46ee684a2199f6be54ef34694595948573e4 Mon Sep 17 00:00:00 2001 From: Cameron Taylor Date: Sat, 18 Dec 2021 17:51:33 -0500 Subject: [PATCH 02/11] HTML5 backend pitch --- .../_internal/backend/html5/HTML5AudioSource.hx | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/lime/_internal/backend/html5/HTML5AudioSource.hx b/src/lime/_internal/backend/html5/HTML5AudioSource.hx index 51f6c9823..2cd26ea6e 100644 --- a/src/lime/_internal/backend/html5/HTML5AudioSource.hx +++ b/src/lime/_internal/backend/html5/HTML5AudioSource.hx @@ -204,6 +204,20 @@ class HTML5AudioSource return loops = value; } + public function getPitch():Float + { + return parent.buffer.__srcHowl.rate(); + } + + public function setPitch(value:Float):Float + { + + parent.buffer.__srcHowl.rate(value); + + return getPitch(); + } + + public function getPosition():Vector4 { #if lime_howlerjs From cd2ab1600ef15c6b7f9d3ed35b770898fe4c3672 Mon Sep 17 00:00:00 2001 From: Cameron Taylor Date: Sat, 18 Dec 2021 18:05:32 -0500 Subject: [PATCH 03/11] following structure of other stuff --- src/lime/_internal/backend/html5/HTML5AudioSource.hx | 5 +++-- src/lime/media/AudioSource.hx | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lime/_internal/backend/html5/HTML5AudioSource.hx b/src/lime/_internal/backend/html5/HTML5AudioSource.hx index 2cd26ea6e..03b79c3d3 100644 --- a/src/lime/_internal/backend/html5/HTML5AudioSource.hx +++ b/src/lime/_internal/backend/html5/HTML5AudioSource.hx @@ -211,9 +211,10 @@ class HTML5AudioSource public function setPitch(value:Float):Float { - + #if lime_howlerjs parent.buffer.__srcHowl.rate(value); - + #end + return getPitch(); } diff --git a/src/lime/media/AudioSource.hx b/src/lime/media/AudioSource.hx index 3a34cb71c..e8bac2e63 100644 --- a/src/lime/media/AudioSource.hx +++ b/src/lime/media/AudioSource.hx @@ -111,12 +111,12 @@ class AudioSource return __backend.setLoops(value); } - private function get_pitch():Float + @:noCompletion private function get_pitch():Float { return __backend.getPitch(); } - private function set_pitch(value:Float):Float + @:noCompletion private function set_pitch(value:Float):Float { return __backend.setPitch(value); } From f65eedf28e34c28a633fcf60674e6246f939e1c4 Mon Sep 17 00:00:00 2001 From: player-03 Date: Wed, 29 Dec 2021 18:33:07 -0500 Subject: [PATCH 04/11] Calculate time remaining based on pitch --- .../backend/native/NativeAudioSource.hx | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/lime/_internal/backend/native/NativeAudioSource.hx b/src/lime/_internal/backend/native/NativeAudioSource.hx index 0baa34071..c1d7de086 100644 --- a/src/lime/_internal/backend/native/NativeAudioSource.hx +++ b/src/lime/_internal/backend/native/NativeAudioSource.hx @@ -424,7 +424,7 @@ class NativeAudioSource timer.stop(); } - var timeRemaining = getLength() - value; + var timeRemaining = Std.int((getLength() - getCurrentTime()) / getPitch()); if (timeRemaining > 0) { @@ -483,7 +483,7 @@ class NativeAudioSource timer.stop(); } - var timeRemaining = value - getCurrentTime(); + var timeRemaining = Std.int((getLength() - getCurrentTime()) / getPitch()); if (timeRemaining > 0) { @@ -514,6 +514,22 @@ class NativeAudioSource { AL.sourcef(handle, AL.PITCH, value); + if (playing) + { + if (timer != null) + { + timer.stop(); + } + + var timeRemaining = Std.int((getLength() - getCurrentTime()) / getPitch()); + + if (timeRemaining > 0) + { + timer = new Timer(timeRemaining); + timer.run = timer_onRun; + } + } + return getPitch(); } From 809ba2fd0c78f44c6c55bc955fb6e5154295df7f Mon Sep 17 00:00:00 2001 From: player-03 Date: Wed, 29 Dec 2021 18:36:20 -0500 Subject: [PATCH 05/11] Add pitch functions to `FlashAudioSource` Just placeholders, for now. --- src/lime/_internal/backend/flash/FlashAudioSource.hx | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/lime/_internal/backend/flash/FlashAudioSource.hx b/src/lime/_internal/backend/flash/FlashAudioSource.hx index c0b6dce55..7acf02b81 100644 --- a/src/lime/_internal/backend/flash/FlashAudioSource.hx +++ b/src/lime/_internal/backend/flash/FlashAudioSource.hx @@ -117,6 +117,16 @@ class FlashAudioSource return loops = value; } + public function getPitch():Float + { + return 1; + } + + public function setPitch(value:Float):Float + { + return getPitch(); + } + public function getPosition():Vector4 { position.x = channel.soundTransform.pan; From f46ed3dc7e6f6a1e9f4bdc538ffe4368dfa036ba Mon Sep 17 00:00:00 2001 From: player-03 Date: Fri, 7 Jan 2022 19:02:43 -0500 Subject: [PATCH 06/11] Standardize setters in `NativeAudioSource`. These setters now: (1) start by checking if anything changed, (2) use the new value in calculations, and (3) perform the assignment only at the end. --- .../backend/native/NativeAudioSource.hx | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/lime/_internal/backend/native/NativeAudioSource.hx b/src/lime/_internal/backend/native/NativeAudioSource.hx index c1d7de086..00ccc24fd 100644 --- a/src/lime/_internal/backend/native/NativeAudioSource.hx +++ b/src/lime/_internal/backend/native/NativeAudioSource.hx @@ -386,6 +386,11 @@ class NativeAudioSource public function setCurrentTime(value:Int):Int { + if (value == getCurrentTime()) + { + return value; + } + if (handle != null) { if (stream) @@ -424,7 +429,7 @@ class NativeAudioSource timer.stop(); } - var timeRemaining = Std.int((getLength() - getCurrentTime()) / getPitch()); + var timeRemaining = Std.int((getLength() - value) / getPitch()); if (timeRemaining > 0) { @@ -483,7 +488,7 @@ class NativeAudioSource timer.stop(); } - var timeRemaining = Std.int((getLength() - getCurrentTime()) / getPitch()); + var timeRemaining = Std.int((value - getCurrentTime()) / getPitch()); if (timeRemaining > 0) { @@ -512,16 +517,14 @@ class NativeAudioSource public function setPitch(value:Float):Float { - AL.sourcef(handle, AL.PITCH, value); - - if (playing) + if (playing && value != getPitch()) { if (timer != null) { timer.stop(); } - var timeRemaining = Std.int((getLength() - getCurrentTime()) / getPitch()); + var timeRemaining = Std.int((getLength() - getCurrentTime()) / value); if (timeRemaining > 0) { @@ -530,7 +533,9 @@ class NativeAudioSource } } - return getPitch(); + AL.sourcef(handle, AL.PITCH, value); + + return value; } public function getPosition():Vector4 From c328d0c85c6add8534ceb85f95497bd3bb44f798 Mon Sep 17 00:00:00 2001 From: player-03 Date: Fri, 7 Jan 2022 20:44:28 -0500 Subject: [PATCH 07/11] Make sure `NativeAudioSource.handle` is non-null. Using `getGain()` and `setGain()` as a template. Like gain, you won't be able to set pitch before calling `init()`. --- .../_internal/backend/native/NativeAudioSource.hx | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/lime/_internal/backend/native/NativeAudioSource.hx b/src/lime/_internal/backend/native/NativeAudioSource.hx index 00ccc24fd..5a3a2070a 100644 --- a/src/lime/_internal/backend/native/NativeAudioSource.hx +++ b/src/lime/_internal/backend/native/NativeAudioSource.hx @@ -512,7 +512,14 @@ class NativeAudioSource public function getPitch():Float { - return AL.getSourcef(handle, AL.PITCH); + if (handle != null) + { + return AL.getSourcef(handle, AL.PITCH); + } + else + { + return 1; + } } public function setPitch(value:Float):Float @@ -533,7 +540,10 @@ class NativeAudioSource } } - AL.sourcef(handle, AL.PITCH, value); + if (handle != null) + { + AL.sourcef(handle, AL.PITCH, value); + } return value; } From 8e46831823943095ee10c3b0064881fb96479b59 Mon Sep 17 00:00:00 2001 From: player-03 Date: Sat, 8 Jan 2022 12:01:23 -0500 Subject: [PATCH 08/11] Remove redundant code. --- src/lime/media/AudioSource.hx | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/lime/media/AudioSource.hx b/src/lime/media/AudioSource.hx index e8bac2e63..ab0992e4f 100644 --- a/src/lime/media/AudioSource.hx +++ b/src/lime/media/AudioSource.hx @@ -37,8 +37,6 @@ class AudioSource this.loops = loops; - pitch = 1; - if (buffer != null) { init(); From 07e65257c429c592644a45394d2c004bf04046be Mon Sep 17 00:00:00 2001 From: player-03 Date: Mon, 10 Jan 2022 07:46:04 -0500 Subject: [PATCH 09/11] Add missing `#if` tag. --- src/lime/_internal/backend/html5/HTML5AudioSource.hx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lime/_internal/backend/html5/HTML5AudioSource.hx b/src/lime/_internal/backend/html5/HTML5AudioSource.hx index 03b79c3d3..3c7e314f6 100644 --- a/src/lime/_internal/backend/html5/HTML5AudioSource.hx +++ b/src/lime/_internal/backend/html5/HTML5AudioSource.hx @@ -206,7 +206,11 @@ class HTML5AudioSource public function getPitch():Float { + #if lime_howlerjs return parent.buffer.__srcHowl.rate(); + #else + return 1; + #end } public function setPitch(value:Float):Float From 878982e3e2d480bcbeadbad828c09b7354d2297c Mon Sep 17 00:00:00 2001 From: player-03 Date: Wed, 26 Jan 2022 12:14:20 -0500 Subject: [PATCH 10/11] Never skip `setCurrentTime()`. --- src/lime/_internal/backend/native/NativeAudioSource.hx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lime/_internal/backend/native/NativeAudioSource.hx b/src/lime/_internal/backend/native/NativeAudioSource.hx index 5a3a2070a..95c9d5cb6 100644 --- a/src/lime/_internal/backend/native/NativeAudioSource.hx +++ b/src/lime/_internal/backend/native/NativeAudioSource.hx @@ -386,10 +386,11 @@ class NativeAudioSource public function setCurrentTime(value:Int):Int { - if (value == getCurrentTime()) + // `setCurrentTime()` has side effects and is never safe to skip. + /* if (value == getCurrentTime()) { return value; - } + } */ if (handle != null) { From cda9d3a4bb02b13b887b9d98c9965a1401402695 Mon Sep 17 00:00:00 2001 From: player-03 Date: Wed, 26 Jan 2022 12:20:10 -0500 Subject: [PATCH 11/11] Add missing feature warning to `FlashAudioSource`. --- src/lime/_internal/backend/flash/FlashAudioSource.hx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lime/_internal/backend/flash/FlashAudioSource.hx b/src/lime/_internal/backend/flash/FlashAudioSource.hx index 7acf02b81..1d1f10c9c 100644 --- a/src/lime/_internal/backend/flash/FlashAudioSource.hx +++ b/src/lime/_internal/backend/flash/FlashAudioSource.hx @@ -119,6 +119,7 @@ class FlashAudioSource public function getPitch():Float { + lime.utils.Log.verbose("Pitch is not supported in Flash."); return 1; }