Merge pull request #15254 from mrcdk/pitch_scale

Added pitch scale property to AudioStreamPlayer, AudioStreamPlayer2D and AudioStreamPlayer3D
This commit is contained in:
Rémi Verschelde 2018-02-01 14:26:27 +01:00 committed by GitHub
commit ff287d8341
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 57 additions and 9 deletions

View file

@ -54,7 +54,7 @@ void AudioStreamPlayer2D::_mix_audio() {
int buffer_size = mix_buffer.size();
//mix
stream_playback->mix(buffer, 1.0, buffer_size);
stream_playback->mix(buffer, pitch_scale, buffer_size);
//write all outputs
for (int i = 0; i < output_count; i++) {
@ -279,6 +279,13 @@ float AudioStreamPlayer2D::get_volume_db() const {
return volume_db;
}
void AudioStreamPlayer2D::set_pitch_scale(float p_pitch_scale) {
pitch_scale = p_pitch_scale;
}
float AudioStreamPlayer2D::get_pitch_scale() const {
return pitch_scale;
}
void AudioStreamPlayer2D::play(float p_from_pos) {
if (stream_playback.is_valid()) {
@ -419,6 +426,9 @@ void AudioStreamPlayer2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_volume_db", "volume_db"), &AudioStreamPlayer2D::set_volume_db);
ClassDB::bind_method(D_METHOD("get_volume_db"), &AudioStreamPlayer2D::get_volume_db);
ClassDB::bind_method(D_METHOD("set_pitch_scale", "pitch_scale"), &AudioStreamPlayer2D::set_pitch_scale);
ClassDB::bind_method(D_METHOD("get_pitch_scale"), &AudioStreamPlayer2D::get_pitch_scale);
ClassDB::bind_method(D_METHOD("play", "from_position"), &AudioStreamPlayer2D::play, DEFVAL(0.0));
ClassDB::bind_method(D_METHOD("seek", "to_position"), &AudioStreamPlayer2D::seek);
ClassDB::bind_method(D_METHOD("stop"), &AudioStreamPlayer2D::stop);
@ -448,6 +458,7 @@ void AudioStreamPlayer2D::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"), "set_stream", "get_stream");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "volume_db", PROPERTY_HINT_RANGE, "-80,24"), "set_volume_db", "get_volume_db");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "pitch_scale", PROPERTY_HINT_RANGE, "0.01,32,0.01"), "set_pitch_scale", "get_pitch_scale");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "_set_playing", "is_playing");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "is_autoplay_enabled");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_distance", PROPERTY_HINT_RANGE, "1,65536,1"), "set_max_distance", "get_max_distance");
@ -461,6 +472,7 @@ void AudioStreamPlayer2D::_bind_methods() {
AudioStreamPlayer2D::AudioStreamPlayer2D() {
volume_db = 0;
pitch_scale = 1.0;
autoplay = false;
setseek = -1;
active = false;

View file

@ -70,6 +70,7 @@ private:
volatile float setplay;
float volume_db;
float pitch_scale;
bool autoplay;
StringName bus;
@ -98,6 +99,9 @@ public:
void set_volume_db(float p_volume);
float get_volume_db() const;
void set_pitch_scale(float p_pitch_scale);
float get_pitch_scale() const;
void play(float p_from_pos = 0.0);
void seek(float p_seconds);
void stop();

View file

@ -57,18 +57,18 @@ void AudioStreamPlayer3D::_mix_audio() {
//mix
if (output_count > 0 || out_of_range_mode == OUT_OF_RANGE_MIX) {
float pitch_scale = 0.0;
float output_pitch_scale = 0.0;
if (output_count) {
//used for doppler, not realistic but good enough
for (int i = 0; i < output_count; i++) {
pitch_scale += outputs[i].pitch_scale;
output_pitch_scale += outputs[i].pitch_scale;
}
pitch_scale /= float(output_count);
output_pitch_scale /= float(output_count);
} else {
pitch_scale = 1.0;
output_pitch_scale = 1.0;
}
stream_playback->mix(buffer, pitch_scale, buffer_size);
stream_playback->mix(buffer, pitch_scale * output_pitch_scale, buffer_size);
}
//write all outputs
@ -607,6 +607,13 @@ float AudioStreamPlayer3D::get_max_db() const {
return max_db;
}
void AudioStreamPlayer3D::set_pitch_scale(float p_pitch_scale) {
pitch_scale = p_pitch_scale;
}
float AudioStreamPlayer3D::get_pitch_scale() const {
return pitch_scale;
}
void AudioStreamPlayer3D::play(float p_from_pos) {
if (stream_playback.is_valid()) {
@ -832,6 +839,9 @@ void AudioStreamPlayer3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_max_db", "max_db"), &AudioStreamPlayer3D::set_max_db);
ClassDB::bind_method(D_METHOD("get_max_db"), &AudioStreamPlayer3D::get_max_db);
ClassDB::bind_method(D_METHOD("set_pitch_scale", "pitch_scale"), &AudioStreamPlayer3D::set_pitch_scale);
ClassDB::bind_method(D_METHOD("get_pitch_scale"), &AudioStreamPlayer3D::get_pitch_scale);
ClassDB::bind_method(D_METHOD("play", "from_position"), &AudioStreamPlayer3D::play, DEFVAL(0.0));
ClassDB::bind_method(D_METHOD("seek", "to_position"), &AudioStreamPlayer3D::seek);
ClassDB::bind_method(D_METHOD("stop"), &AudioStreamPlayer3D::stop);
@ -885,6 +895,7 @@ void AudioStreamPlayer3D::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::REAL, "unit_db", PROPERTY_HINT_RANGE, "-80,80"), "set_unit_db", "get_unit_db");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "unit_size", PROPERTY_HINT_RANGE, "0.1,100,0.1"), "set_unit_size", "get_unit_size");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_db", PROPERTY_HINT_RANGE, "-24,6"), "set_max_db", "get_max_db");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "pitch_scale", PROPERTY_HINT_RANGE, "0.01,32,0.01"), "set_pitch_scale", "get_pitch_scale");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "_set_playing", "is_playing");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "is_autoplay_enabled");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_distance", PROPERTY_HINT_RANGE, "0,65536,1"), "set_max_distance", "get_max_distance");
@ -921,6 +932,7 @@ AudioStreamPlayer3D::AudioStreamPlayer3D() {
unit_size = 1;
attenuation_model = ATTENUATION_INVERSE_DISTANCE;
max_db = 3;
pitch_scale = 1.0;
autoplay = false;
setseek = -1;
active = false;

View file

@ -106,6 +106,7 @@ private:
float unit_db;
float unit_size;
float max_db;
float pitch_scale;
bool autoplay;
StringName bus;
@ -153,6 +154,9 @@ public:
void set_max_db(float p_boost);
float get_max_db() const;
void set_pitch_scale(float p_pitch_scale);
float get_pitch_scale() const;
void play(float p_from_pos = 0.0);
void seek(float p_seconds);
void stop();

View file

@ -45,7 +45,7 @@ void AudioStreamPlayer::_mix_internal(bool p_fadeout) {
}
//mix
stream_playback->mix(buffer, 1.0, buffer_size);
stream_playback->mix(buffer, pitch_scale, buffer_size);
//multiply volume interpolating to avoid clicks if this changes
float target_volume = p_fadeout ? -80.0 : volume_db;
@ -177,6 +177,13 @@ float AudioStreamPlayer::get_volume_db() const {
return volume_db;
}
void AudioStreamPlayer::set_pitch_scale(float p_pitch_scale) {
pitch_scale = p_pitch_scale;
}
float AudioStreamPlayer::get_pitch_scale() const {
return pitch_scale;
}
void AudioStreamPlayer::play(float p_from_pos) {
if (stream_playback.is_valid()) {
@ -297,6 +304,9 @@ void AudioStreamPlayer::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_volume_db", "volume_db"), &AudioStreamPlayer::set_volume_db);
ClassDB::bind_method(D_METHOD("get_volume_db"), &AudioStreamPlayer::get_volume_db);
ClassDB::bind_method(D_METHOD("set_pitch_scale", "pitch_scale"), &AudioStreamPlayer::set_pitch_scale);
ClassDB::bind_method(D_METHOD("get_pitch_scale"), &AudioStreamPlayer::get_pitch_scale);
ClassDB::bind_method(D_METHOD("play", "from_position"), &AudioStreamPlayer::play, DEFVAL(0.0));
ClassDB::bind_method(D_METHOD("seek", "to_position"), &AudioStreamPlayer::seek);
ClassDB::bind_method(D_METHOD("stop"), &AudioStreamPlayer::stop);
@ -320,6 +330,7 @@ void AudioStreamPlayer::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"), "set_stream", "get_stream");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "volume_db", PROPERTY_HINT_RANGE, "-80,24"), "set_volume_db", "get_volume_db");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "pitch_scale", PROPERTY_HINT_RANGE, "0.01,32,0.01"), "set_pitch_scale", "get_pitch_scale");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "_set_playing", "is_playing");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "is_autoplay_enabled");
ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_target", PROPERTY_HINT_ENUM, "Stereo,Surround,Center"), "set_mix_target", "get_mix_target");
@ -335,6 +346,7 @@ void AudioStreamPlayer::_bind_methods() {
AudioStreamPlayer::AudioStreamPlayer() {
mix_volume_db = 0;
pitch_scale = 1.0;
volume_db = 0;
autoplay = false;
setseek = -1;

View file

@ -54,6 +54,7 @@ private:
volatile bool active;
float mix_volume_db;
float pitch_scale;
float volume_db;
bool autoplay;
StringName bus;
@ -81,6 +82,9 @@ public:
void set_volume_db(float p_volume);
float get_volume_db() const;
void set_pitch_scale(float p_pitch_scale);
float get_pitch_scale() const;
void play(float p_from_pos = 0.0);
void seek(float p_seconds);
void stop();

View file

@ -46,9 +46,9 @@ void AudioStreamPlaybackResampled::_begin_resample() {
void AudioStreamPlaybackResampled::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
float target_rate = AudioServer::get_singleton()->get_mix_rate() * p_rate_scale;
float target_rate = AudioServer::get_singleton()->get_mix_rate();
uint64_t mix_increment = uint64_t((get_stream_sampling_rate() / double(target_rate)) * double(FP_LEN));
uint64_t mix_increment = uint64_t(((get_stream_sampling_rate() * p_rate_scale) / double(target_rate)) * double(FP_LEN));
for (int i = 0; i < p_frames; i++) {