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

Add pitch support for FlxSound #1465

Closed
wants to merge 8 commits into from
Closed
31 changes: 31 additions & 0 deletions flixel/system/FlxSound.hx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ class FlxSound extends FlxBasic
* Set volume to a value between 0 and 1 to change how this sound is.
*/
public var volume(get, set):Float;
#if ((cpp || neko) && openfl_legacy)
/**
* Set pitch, which also alters the playback speed. Default is 1.
*/
public var pitch(get, set):Float;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Default" would probably describe this better than "Normal".

#end
/**
* The position in runtime of the music playback.
*/
Expand Down Expand Up @@ -107,6 +113,12 @@ class FlxSound extends FlxBasic
* Internal tracker for volume.
*/
private var _volume:Float;
#if ((cpp || neko) && openfl_legacy)
/**
* Internal tracker for pitch.
*/
private var _pitch:Float = 1.0;
#end
/**
* Internal tracker for total volume adjustment.
*/
Expand Down Expand Up @@ -518,6 +530,9 @@ class FlxSound extends FlxBasic
_channel = _sound.play(time, numLoops, _transform);
if (_channel != null)
{
#if ((cpp || neko) && openfl_legacy)
pitch = _pitch;
#end
_channel.addEventListener(Event.SOUND_COMPLETE, stopped);
active = true;
}
Expand Down Expand Up @@ -629,6 +644,22 @@ class FlxSound extends FlxBasic
return Volume;
}


#if ((cpp || neko) && openfl_legacy)
private inline function get_pitch():Float
{
return _pitch;
}

private function set_pitch(v:Float):Float
{
if (_channel != null)
_channel.pitch = v;
return _pitch = v;
}
#end


private inline function get_pan():Float
{
return _transform.pan;
Expand Down