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

Adding pitch support #2564

Merged
merged 10 commits into from
Sep 29, 2022
24 changes: 15 additions & 9 deletions flixel/system/FlxSound.hx
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,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 (sys && openfl_legacy)
Cheemsandfriends marked this conversation as resolved.
Show resolved Hide resolved
#if FLX_PITCH
/**
* Set pitch, which also alters the playback speed. Default is 1.
*/
public var pitch(get, set):Float;
#end

/**
* The position in runtime of the music playback in milliseconds.
* If set while paused, changes only come into effect after a `resume()` call.
Expand Down Expand Up @@ -177,14 +175,12 @@ class FlxSound extends FlxBasic
* Internal tracker for sound length, so that length can still be obtained while a sound is paused, because _sound becomes null.
*/
var _length:Float = 0;

#if (sys && openfl_legacy)
#if FLX_PITCH
/**
* Internal tracker for pitch.
*/
var _pitch:Float = 1.0;
#end

/**
* Internal tracker for total volume adjustment.
*/
Expand Down Expand Up @@ -432,6 +428,9 @@ class FlxSound extends FlxBasic
updateTransform();
exists = true;
onComplete = OnComplete;
#if FLX_PITCH
pitch = 1;
#end
_length = (_sound == null) ? 0 : _sound.length;
endTime = _length;
return this;
Expand Down Expand Up @@ -610,7 +609,7 @@ class FlxSound extends FlxBasic
_channel = _sound.play(_time, 0, _transform);
if (_channel != null)
{
#if (sys && openfl_legacy)
#if FLX_PITCH
pitch = _pitch;
#end
_channel.addEventListener(Event.SOUND_COMPLETE, stopped);
Expand Down Expand Up @@ -736,17 +735,24 @@ class FlxSound extends FlxBasic
updateTransform();
return Volume;
}

#if (sys && openfl_legacy)
#if FLX_PITCH
inline function get_pitch():Float
{
return _pitch;
}


function set_pitch(v:Float):Float
{
if (_channel != null)
#if openfl_legacy
_channel.pitch = v;
#else
@:privateAccess
if (_channel.__source != null)
_channel.__source.pitch = v;
Copy link
Member

Choose a reason for hiding this comment

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

getting errors here:

.../FlxSound.hx:752: characters 17-25 : flash.media.SoundChannel has no field __source
.../FlxSound.hx:753: characters 14-22 : flash.media.SoundChannel has no field __source

There must be a problem with the FLX_PITCH definition

#end

return _pitch = v;
}
#end
Expand Down
10 changes: 9 additions & 1 deletion flixel/system/macros/FlxDefines.hx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ private enum UserDefines
FLX_4_LEGACY_COLLISION;
/* Simplifies FlxPoint but can increase GC frequency */
FLX_NO_POINT_POOL;
FLX_NO_PITCH;
}

/**
Expand Down Expand Up @@ -54,6 +55,7 @@ private enum HelperDefines
FLX_ACCELEROMETER;
FLX_DRAW_QUADS;
FLX_POINT_POOL;
FLX_PITCH;
}

class FlxDefines
Expand Down Expand Up @@ -141,7 +143,13 @@ class FlxDefines

if (!defined(FLX_NO_SOUND_SYSTEM) && !defined(FLX_NO_SOUND_TRAY))
define(FLX_SOUND_TRAY);


if (defined(FLX_NO_SOUND_SYSTEM) || #if openfl_legacy !defined("sys") #elseif (lime >= "8.0.0") defined("flash") #end)
define(FLX_NO_PITCH);

if (!defined(FLX_NO_PITCH))
define(FLX_PITCH);

if ((!defined("openfl_legacy") && !defined("flash")) || defined("flash11_8"))
define(FLX_GAMEINPUT_API);
else if (!defined("openfl_next") && (defined("cpp") || defined("neko")))
Expand Down