Skip to content

Commit

Permalink
Add samples playback support
Browse files Browse the repository at this point in the history
  • Loading branch information
adamscott committed May 1, 2024
1 parent d282e4f commit 77c0bd9
Show file tree
Hide file tree
Showing 36 changed files with 1,295 additions and 2 deletions.
2 changes: 2 additions & 0 deletions core/config/project_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,8 @@ ProjectSettings::ProjectSettings() {
GLOBAL_DEF("animation/warnings/check_angle_interpolation_type_conflicting", true);

GLOBAL_DEF_BASIC(PropertyInfo(Variant::STRING, "audio/buses/default_bus_layout", PROPERTY_HINT_FILE, "*.tres"), "res://default_bus_layout.tres");
GLOBAL_DEF(PropertyInfo(Variant::INT, "audio/general/default_playback_type", PROPERTY_HINT_ENUM, "Stream,Sample"), 0);
GLOBAL_DEF(PropertyInfo(Variant::INT, "audio/general/default_playback_type.web", PROPERTY_HINT_ENUM, "Stream,Sample"), 1);
GLOBAL_DEF_RST("audio/general/text_to_speech", false);
GLOBAL_DEF_RST(PropertyInfo(Variant::FLOAT, "audio/general/2d_panning_strength", PROPERTY_HINT_RANGE, "0,2,0.01"), 0.5f);
GLOBAL_DEF_RST(PropertyInfo(Variant::FLOAT, "audio/general/3d_panning_strength", PROPERTY_HINT_RANGE, "0,2,0.01"), 0.5f);
Expand Down
9 changes: 9 additions & 0 deletions doc/classes/AudioSamplePlayback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="AudioSamplePlayback" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
</brief_description>
<description>
</description>
<tutorials>
</tutorials>
</class>
12 changes: 12 additions & 0 deletions doc/classes/AudioServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@
If [code]true[/code], the bus at index [param bus_idx] is in solo mode.
</description>
</method>
<method name="is_stream_registered_as_sample">
<return type="bool" />
<param index="0" name="stream" type="AudioStream" />
<description>
</description>
</method>
<method name="lock">
<return type="void" />
<description>
Expand All @@ -198,6 +204,12 @@
Moves the bus from index [param index] to index [param to_index].
</description>
</method>
<method name="register_stream_as_sample">
<return type="void" />
<param index="0" name="stream" type="AudioStream" />
<description>
</description>
</method>
<method name="remove_bus">
<return type="void" />
<param index="0" name="index" type="int" />
Expand Down
11 changes: 11 additions & 0 deletions doc/classes/AudioStreamPlayback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,16 @@
Overridable method. Called whenever the audio stream is mixed if the playback is active and [method AudioServer.set_enable_tagging_used_audio_streams] has been set to [code]true[/code]. Editor plugins may use this method to "tag" the current position along the audio stream and display it in a preview.
</description>
</method>
<method name="get_sample_playback" qualifiers="const">
<return type="AudioSamplePlayback" />
<description>
</description>
</method>
<method name="set_sample_playback">
<return type="void" />
<param index="0" name="playback_sample" type="AudioSamplePlayback" />
<description>
</description>
</method>
</methods>
</class>
2 changes: 2 additions & 0 deletions doc/classes/AudioStreamPlayer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
<member name="pitch_scale" type="float" setter="set_pitch_scale" getter="get_pitch_scale" default="1.0">
The audio's pitch and tempo, as a multiplier of the [member stream]'s sample rate. A value of [code]2.0[/code] doubles the audio's pitch, while a value of [code]0.5[/code] halves the pitch.
</member>
<member name="playback_type" type="int" setter="set_playback_type" getter="get_playback_type" enum="AudioServer.PlaybackType" default="0">
</member>
<member name="playing" type="bool" setter="_set_playing" getter="is_playing" default="false">
If [code]true[/code], this node is playing sounds. Setting this property has the same effect as [method play] and [method stop].
</member>
Expand Down
2 changes: 2 additions & 0 deletions doc/classes/AudioStreamPlayer2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
<member name="pitch_scale" type="float" setter="set_pitch_scale" getter="get_pitch_scale" default="1.0">
The pitch and the tempo of the audio, as a multiplier of the audio sample's sample rate.
</member>
<member name="playback_type" type="int" setter="set_playback_type" getter="get_playback_type" enum="AudioServer.PlaybackType" default="0">
</member>
<member name="playing" type="bool" setter="_set_playing" getter="is_playing" default="false">
If [code]true[/code], audio is playing or is queued to be played (see [method play]).
</member>
Expand Down
2 changes: 2 additions & 0 deletions doc/classes/AudioStreamPlayer3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@
<member name="pitch_scale" type="float" setter="set_pitch_scale" getter="get_pitch_scale" default="1.0">
The pitch and the tempo of the audio, as a multiplier of the audio sample's sample rate.
</member>
<member name="playback_type" type="int" setter="set_playback_type" getter="get_playback_type" enum="AudioServer.PlaybackType" default="0">
</member>
<member name="playing" type="bool" setter="_set_playing" getter="is_playing" default="false">
If [code]true[/code], audio is playing or is queued to be played (see [method play]).
</member>
Expand Down
5 changes: 5 additions & 0 deletions doc/classes/AudioStreamWAV.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
<link title="Runtime file loading and saving">$DOCS_URL/tutorials/io/runtime_file_loading_and_saving.html</link>
</tutorials>
<methods>
<method name="get_sample" qualifiers="const">
<return type="AudioSample" />
<description>
</description>
</method>
<method name="save_to_wav">
<return type="int" enum="Error" />
<param index="0" name="path" type="String" />
Expand Down
4 changes: 4 additions & 0 deletions doc/classes/ProjectSettings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,10 @@
The base strength of the panning effect for all [AudioStreamPlayer3D] nodes. The panning strength can be further scaled on each Node using [member AudioStreamPlayer3D.panning_strength]. A value of [code]0.0[/code] disables stereo panning entirely, leaving only volume attenuation in place. A value of [code]1.0[/code] completely mutes one of the channels if the sound is located exactly to the left (or right) of the listener.
The default value of [code]0.5[/code] is tuned for headphones. When using speakers, you may find lower values to sound better as speakers have a lower stereo separation compared to headphones.
</member>
<member name="audio/general/default_playback_type" type="int" setter="" getter="" default="0">
</member>
<member name="audio/general/default_playback_type.web" type="int" setter="" getter="" default="1">
</member>
<member name="audio/general/ios/mix_with_others" type="bool" setter="" getter="" default="false">
Sets the [url=https://developer.apple.com/documentation/avfaudio/avaudiosession/categoryoptions/1616611-mixwithothers]mixWithOthers[/url] option for the AVAudioSession on iOS. This will override the mix behavior, if the category is set to [code]Play and Record[/code], [code]Playback[/code], or [code]Multi Route[/code].
[code]Ambient[/code] always has this set per default.
Expand Down
32 changes: 32 additions & 0 deletions modules/minimp3/audio_stream_mp3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,22 @@ void AudioStreamPlaybackMP3::tag_used_streams() {
mp3_stream->tag_used(get_playback_position());
}

void AudioStreamPlaybackMP3::set_is_sample(bool p_is_sample) {
_is_sample = p_is_sample;
}

bool AudioStreamPlaybackMP3::get_is_sample() const {
return _is_sample;
}

Ref<AudioSamplePlayback> AudioStreamPlaybackMP3::get_sample_playback() const {
return sample_playback;
}

void AudioStreamPlaybackMP3::set_sample_playback(const Ref<AudioSamplePlayback> &p_playback) {
sample_playback = p_playback;
}

void AudioStreamPlaybackMP3::set_parameter(const StringName &p_name, const Variant &p_value) {
if (p_name == SNAME("looping")) {
if (p_value == Variant()) {
Expand Down Expand Up @@ -287,6 +303,20 @@ int AudioStreamMP3::get_bar_beats() const {
return bar_beats;
}

Ref<AudioSample> AudioStreamMP3::get_sample() const {
Ref<AudioSample> sample;
sample.instantiate();
sample->stream = this;
if (loop) {
sample->loop_mode = AudioSample::LoopMode::LOOP_FORWARD;
} else {
sample->loop_mode = AudioSample::LoopMode::LOOP_DISABLED;
}
sample->loop_begin = loop_offset;
sample->loop_end = 0;
return sample;
}

void AudioStreamMP3::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_data", "data"), &AudioStreamMP3::set_data);
ClassDB::bind_method(D_METHOD("get_data"), &AudioStreamMP3::get_data);
Expand All @@ -306,6 +336,8 @@ void AudioStreamMP3::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_bar_beats", "count"), &AudioStreamMP3::set_bar_beats);
ClassDB::bind_method(D_METHOD("get_bar_beats"), &AudioStreamMP3::get_bar_beats);

ClassDB::bind_method(D_METHOD("get_sample"), &AudioStreamMP3::get_sample);

ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_data", "get_data");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bpm", PROPERTY_HINT_RANGE, "0,400,0.01,or_greater"), "set_bpm", "get_bpm");
ADD_PROPERTY(PropertyInfo(Variant::INT, "beat_count", PROPERTY_HINT_RANGE, "0,512,1,or_greater"), "set_beat_count", "get_beat_count");
Expand Down
13 changes: 13 additions & 0 deletions modules/minimp3/audio_stream_mp3.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class AudioStreamPlaybackMP3 : public AudioStreamPlaybackResampled {

Ref<AudioStreamMP3> mp3_stream;

bool _is_sample = false;
Ref<AudioSamplePlayback> sample_playback;

protected:
virtual int _mix_internal(AudioFrame *p_buffer, int p_frames) override;
virtual float get_stream_sampling_rate() override;
Expand All @@ -74,6 +77,11 @@ class AudioStreamPlaybackMP3 : public AudioStreamPlaybackResampled {

virtual void tag_used_streams() override;

virtual void set_is_sample(bool p_is_sample) override;
virtual bool get_is_sample() const override;
virtual Ref<AudioSamplePlayback> get_sample_playback() const override;
virtual void set_sample_playback(const Ref<AudioSamplePlayback> &p_playback) override;

virtual void set_parameter(const StringName &p_name, const Variant &p_value) override;
virtual Variant get_parameter(const StringName &p_name) const override;

Expand Down Expand Up @@ -131,6 +139,11 @@ class AudioStreamMP3 : public AudioStream {

virtual bool is_monophonic() const override;

virtual bool can_be_sampled() const override {
return true;
};
virtual Ref<AudioSample> get_sample() const override;

virtual void get_parameter_list(List<Parameter> *r_parameters) override;

AudioStreamMP3();
Expand Down
7 changes: 7 additions & 0 deletions modules/minimp3/doc_classes/AudioStreamMP3.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
</description>
<tutorials>
</tutorials>
<methods>
<method name="get_sample" qualifiers="const">
<return type="AudioSample" />
<description>
</description>
</method>
</methods>
<members>
<member name="bar_beats" type="int" setter="set_bar_beats" getter="get_bar_beats" default="4">
</member>
Expand Down
30 changes: 30 additions & 0 deletions modules/vorbis/audio_stream_ogg_vorbis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,22 @@ void AudioStreamPlaybackOggVorbis::seek(double p_time) {
}
}

void AudioStreamPlaybackOggVorbis::set_is_sample(bool p_is_sample) {
_is_sample = p_is_sample;
}

bool AudioStreamPlaybackOggVorbis::get_is_sample() const {
return _is_sample;
}

Ref<AudioSamplePlayback> AudioStreamPlaybackOggVorbis::get_sample_playback() const {
return sample_playback;
}

void AudioStreamPlaybackOggVorbis::set_sample_playback(const Ref<AudioSamplePlayback> &p_playback) {
sample_playback = p_playback;
}

AudioStreamPlaybackOggVorbis::~AudioStreamPlaybackOggVorbis() {
if (block_is_allocated) {
vorbis_block_clear(&block);
Expand Down Expand Up @@ -517,6 +533,20 @@ void AudioStreamOggVorbis::get_parameter_list(List<Parameter> *r_parameters) {
r_parameters->push_back(Parameter(PropertyInfo(Variant::BOOL, "looping", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_CHECKABLE), Variant()));
}

Ref<AudioSample> AudioStreamOggVorbis::get_sample() const {
Ref<AudioSample> sample;
sample.instantiate();
sample->stream = this;
if (loop) {
sample->loop_mode = AudioSample::LoopMode::LOOP_FORWARD;
} else {
sample->loop_mode = AudioSample::LoopMode::LOOP_DISABLED;
}
sample->loop_begin = loop_offset;
sample->loop_end = 0;
return sample;
}

void AudioStreamOggVorbis::_bind_methods() {
ClassDB::bind_static_method("AudioStreamOggVorbis", D_METHOD("load_from_buffer", "buffer"), &AudioStreamOggVorbis::load_from_buffer);
ClassDB::bind_static_method("AudioStreamOggVorbis", D_METHOD("load_from_file", "path"), &AudioStreamOggVorbis::load_from_file);
Expand Down
13 changes: 13 additions & 0 deletions modules/vorbis/audio_stream_ogg_vorbis.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ class AudioStreamPlaybackOggVorbis : public AudioStreamPlaybackResampled {
Ref<OggPacketSequencePlayback> vorbis_data_playback;
Ref<AudioStreamOggVorbis> vorbis_stream;

bool _is_sample = false;
Ref<AudioSamplePlayback> sample_playback;

int _mix_frames(AudioFrame *p_buffer, int p_frames);
int _mix_frames_vorbis(AudioFrame *p_buffer, int p_frames);

Expand All @@ -100,6 +103,11 @@ class AudioStreamPlaybackOggVorbis : public AudioStreamPlaybackResampled {
virtual void set_parameter(const StringName &p_name, const Variant &p_value) override;
virtual Variant get_parameter(const StringName &p_name) const override;

virtual void set_is_sample(bool p_is_sample) override;
virtual bool get_is_sample() const override;
virtual Ref<AudioSamplePlayback> get_sample_playback() const override;
virtual void set_sample_playback(const Ref<AudioSamplePlayback> &p_playback) override;

AudioStreamPlaybackOggVorbis() {}
~AudioStreamPlaybackOggVorbis();
};
Expand Down Expand Up @@ -159,6 +167,11 @@ class AudioStreamOggVorbis : public AudioStream {

virtual void get_parameter_list(List<Parameter> *r_parameters) override;

virtual bool can_be_sampled() const override {
return true;
};
virtual Ref<AudioSample> get_sample() const override;

AudioStreamOggVorbis();
virtual ~AudioStreamOggVorbis();
};
Expand Down
Loading

0 comments on commit 77c0bd9

Please sign in to comment.