Merge pull request #1510 from ninjamuffin99/pitch

Audio pitch change implementation
This commit is contained in:
player-03
2022-05-12 12:25:01 -04:00
committed by GitHub
4 changed files with 87 additions and 2 deletions

View File

@@ -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;

View File

@@ -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

View File

@@ -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)

View File

@@ -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();