Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio pitch change implementation #1510

Merged
merged 11 commits into from
May 12, 2022
10 changes: 10 additions & 0 deletions src/lime/_internal/backend/flash/FlashAudioSource.hx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ class FlashAudioSource
return loops = value;
}

public function getPitch():Float
{
return 1;
}

public function setPitch(value:Float):Float
{
player-03 marked this conversation as resolved.
Show resolved Hide resolved
return getPitch();
}

public function getPosition():Vector4
{
position.x = channel.soundTransform.pan;
Expand Down
15 changes: 15 additions & 0 deletions src/lime/_internal/backend/html5/HTML5AudioSource.hx
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,21 @@ class HTML5AudioSource
return loops = value;
}

public function getPitch():Float
{
return parent.buffer.__srcHowl.rate();
}

public function setPitch(value:Float):Float
{
#if lime_howlerjs
parent.buffer.__srcHowl.rate(value);
#end

return getPitch();
}


public function getPosition():Vector4
{
#if lime_howlerjs
Expand Down
37 changes: 35 additions & 2 deletions src/lime/_internal/backend/native/NativeAudioSource.hx
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,11 @@ class NativeAudioSource

public function setCurrentTime(value:Int):Int
{
if (value == getCurrentTime())
player-03 marked this conversation as resolved.
Show resolved Hide resolved
{
return value;
}

if (handle != null)
{
if (stream)
Expand Down Expand Up @@ -424,7 +429,7 @@ class NativeAudioSource
timer.stop();
}

var timeRemaining = getLength() - value;
var timeRemaining = Std.int((getLength() - value) / getPitch());

if (timeRemaining > 0)
{
Expand Down Expand Up @@ -483,7 +488,7 @@ class NativeAudioSource
timer.stop();
}

var timeRemaining = value - getCurrentTime();
var timeRemaining = Std.int((value - getCurrentTime()) / getPitch());

if (timeRemaining > 0)
{
Expand All @@ -505,6 +510,34 @@ class NativeAudioSource
return loops = value;
}

public function getPitch():Float
{
return AL.getSourcef(handle, AL.PITCH);
}

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

AL.sourcef(handle, AL.PITCH, value);

return value;
}

public function getPosition():Vector4
{
if (handle != null)
Expand Down
13 changes: 13 additions & 0 deletions src/lime/media/AudioSource.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -36,6 +37,8 @@ class AudioSource

this.loops = loops;

pitch = 1;
player-03 marked this conversation as resolved.
Show resolved Hide resolved

player-03 marked this conversation as resolved.
Show resolved Hide resolved
if (buffer != null)
{
init();
Expand Down Expand Up @@ -108,6 +111,16 @@ class AudioSource
return __backend.setLoops(value);
}

@:noCompletion private function get_pitch():Float
{
return __backend.getPitch();
player-03 marked this conversation as resolved.
Show resolved Hide resolved
}

@:noCompletion private function set_pitch(value:Float):Float
{
return __backend.setPitch(value);
player-03 marked this conversation as resolved.
Show resolved Hide resolved
}

@:noCompletion private function get_position():Vector4
{
return __backend.getPosition();
Expand Down