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
28 changes: 28 additions & 0 deletions flixel/system/FlxSound.hx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ 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;
/**
* Set pitch, which also alters the playback speed. Normal 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".

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


private inline function get_pitch():Float
{
return _pitch;
}

private function set_pitch(v:Float):Float
{
#if (cpp || neko)
_channel.pitch = v;
#else
FlxG.log.warn("Pitch is only supported on OpenAL targets (i.e. cpp & neko)");
Copy link
Member

Choose a reason for hiding this comment

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

Wondering if it wouldn't be better to only have this property exist for cpp and neko in the first place. That's how we handle most of the input stuff in FlxG for example. Then again. other features like post processing use this approach (except for the warn() call), so I guess we're not really consistent about it...

#end
return _pitch = v;
}


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