Add 'global_rate_scale' to the AudioServer

Closes #28953.
This commit is contained in:
Michael Alexsander Silva Dias 2019-06-02 20:01:42 -03:00
parent ddba2e7b47
commit 1c2ba35074
4 changed files with 50 additions and 1 deletions

View file

@ -177,6 +177,13 @@
<description> <description>
</description> </description>
</method> </method>
<method name="get_global_rate_scale">
<return type="float">
</return>
<description>
Returns the global rate scale at which audio is being played.
</description>
</method>
<method name="get_mix_rate" qualifiers="const"> <method name="get_mix_rate" qualifiers="const">
<return type="float"> <return type="float">
</return> </return>
@ -390,6 +397,15 @@
<description> <description>
</description> </description>
</method> </method>
<method name="set_global_rate_scale">
<return type="void">
</return>
<argument index="0" name="scale" type="float">
</argument>
<description>
Scales the rate at which audio is played (i.e. setting it to [code]0.5[/code] will make the audio be played twice as fast).
</description>
</method>
<method name="swap_bus_effects"> <method name="swap_bus_effects">
<return type="void"> <return type="void">
</return> </return>
@ -411,6 +427,16 @@
</description> </description>
</method> </method>
</methods> </methods>
<members>
<member name="bus_count" type="int" setter="set_bus_count" getter="get_bus_count">
Adds and removes buses to make the number of buses match [code]amount[/code].
</member>
<member name="device" type="string" setter="set_device" getter="get_device">
</member>
<member name="global_rate_scale" type="float" setter="set_global_rate_scale" getter="get_global_rate_scale">
Scales the rate at which audio is played (i.e. setting it to [code]0.5[/code] will make the audio be played twice as fast).
</member>
</members>
<signals> <signals>
<signal name="bus_layout_changed"> <signal name="bus_layout_changed">
<description> <description>

View file

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

View file

@ -944,6 +944,15 @@ bool AudioServer::is_bus_channel_active(int p_bus, int p_channel) const {
return buses[p_bus]->channels[p_channel].active; return buses[p_bus]->channels[p_channel].active;
} }
void AudioServer::set_global_rate_scale(float p_scale) {
global_rate_scale = p_scale;
}
float AudioServer::get_global_rate_scale() const {
return global_rate_scale;
}
void AudioServer::init_channels_and_buffers() { void AudioServer::init_channels_and_buffers() {
channel_count = get_channel_count(); channel_count = get_channel_count();
temp_buffer.resize(channel_count); temp_buffer.resize(channel_count);
@ -1352,6 +1361,9 @@ void AudioServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_bus_peak_volume_left_db", "bus_idx", "channel"), &AudioServer::get_bus_peak_volume_left_db); ClassDB::bind_method(D_METHOD("get_bus_peak_volume_left_db", "bus_idx", "channel"), &AudioServer::get_bus_peak_volume_left_db);
ClassDB::bind_method(D_METHOD("get_bus_peak_volume_right_db", "bus_idx", "channel"), &AudioServer::get_bus_peak_volume_right_db); ClassDB::bind_method(D_METHOD("get_bus_peak_volume_right_db", "bus_idx", "channel"), &AudioServer::get_bus_peak_volume_right_db);
ClassDB::bind_method(D_METHOD("set_global_rate_scale", "scale"), &AudioServer::set_global_rate_scale);
ClassDB::bind_method(D_METHOD("get_global_rate_scale"), &AudioServer::get_global_rate_scale);
ClassDB::bind_method(D_METHOD("lock"), &AudioServer::lock); ClassDB::bind_method(D_METHOD("lock"), &AudioServer::lock);
ClassDB::bind_method(D_METHOD("unlock"), &AudioServer::unlock); ClassDB::bind_method(D_METHOD("unlock"), &AudioServer::unlock);
@ -1372,6 +1384,10 @@ void AudioServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_bus_layout", "bus_layout"), &AudioServer::set_bus_layout); ClassDB::bind_method(D_METHOD("set_bus_layout", "bus_layout"), &AudioServer::set_bus_layout);
ClassDB::bind_method(D_METHOD("generate_bus_layout"), &AudioServer::generate_bus_layout); ClassDB::bind_method(D_METHOD("generate_bus_layout"), &AudioServer::generate_bus_layout);
ADD_PROPERTY(PropertyInfo(Variant::INT, "bus_count"), "set_bus_count", "get_bus_count");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "device"), "set_device", "get_device");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "global_rate_scale"), "set_global_rate_scale", "get_global_rate_scale");
ADD_SIGNAL(MethodInfo("bus_layout_changed")); ADD_SIGNAL(MethodInfo("bus_layout_changed"));
BIND_ENUM_CONSTANT(SPEAKER_MODE_STEREO); BIND_ENUM_CONSTANT(SPEAKER_MODE_STEREO);
@ -1396,6 +1412,7 @@ AudioServer::AudioServer() {
#endif #endif
mix_time = 0; mix_time = 0;
mix_size = 0; mix_size = 0;
global_rate_scale = 1;
} }
AudioServer::~AudioServer() { AudioServer::~AudioServer() {

View file

@ -180,6 +180,8 @@ private:
int channel_count; int channel_count;
int to_mix; int to_mix;
float global_rate_scale;
struct Bus { struct Bus {
StringName name; StringName name;
@ -338,6 +340,9 @@ public:
bool is_bus_channel_active(int p_bus, int p_channel) const; bool is_bus_channel_active(int p_bus, int p_channel) const;
void set_global_rate_scale(float p_scale);
float get_global_rate_scale() const;
virtual void init(); virtual void init();
virtual void finish(); virtual void finish();
virtual void update(); virtual void update();