-
-
Notifications
You must be signed in to change notification settings - Fork 21.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AudioStreamRandomizer, replacing AudioStreamRandomPitch
Add additional randomization options.
- Loading branch information
Showing
8 changed files
with
651 additions
and
85 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<class name="AudioStreamRandomizer" inherits="AudioStream" version="4.0"> | ||
<brief_description> | ||
Wraps a pool of audio streams with pitch and volume shifting. | ||
</brief_description> | ||
<description> | ||
Picks a random AudioStream from the pool, depending on the playback mode, and applies random pitch shifting and volume shifting during playback. | ||
</description> | ||
<tutorials> | ||
</tutorials> | ||
<methods> | ||
<method name="add_stream"> | ||
<return type="void" /> | ||
<argument index="0" name="index" type="int" /> | ||
<description> | ||
Insert a stream at the specified index. | ||
</description> | ||
</method> | ||
<method name="get_stream" qualifiers="const"> | ||
<return type="AudioStream" /> | ||
<argument index="0" name="index" type="int" /> | ||
<description> | ||
Returns the stream at the specified index. | ||
</description> | ||
</method> | ||
<method name="get_stream_probability_weight" qualifiers="const"> | ||
<return type="float" /> | ||
<argument index="0" name="index" type="int" /> | ||
<description> | ||
Returns the probability weight associated with the stream at the given index. | ||
</description> | ||
</method> | ||
<method name="move_stream"> | ||
<return type="void" /> | ||
<argument index="0" name="index_from" type="int" /> | ||
<argument index="1" name="index_to" type="int" /> | ||
<description> | ||
Move a stream from one index to another. | ||
</description> | ||
</method> | ||
<method name="remove_stream"> | ||
<return type="void" /> | ||
<argument index="0" name="index" type="int" /> | ||
<description> | ||
Remove the stream at the specified index. | ||
</description> | ||
</method> | ||
<method name="set_stream"> | ||
<return type="void" /> | ||
<argument index="0" name="index" type="int" /> | ||
<argument index="1" name="stream" type="AudioStream" /> | ||
<description> | ||
Set the AudioStream at the specified index. | ||
</description> | ||
</method> | ||
<method name="set_stream_probability_weight"> | ||
<return type="void" /> | ||
<argument index="0" name="index" type="int" /> | ||
<argument index="1" name="weight" type="float" /> | ||
<description> | ||
Set the probability weight of the stream at the specified index. The higher this value, the more likely that the randomizer will choose this stream during random playback modes. | ||
</description> | ||
</method> | ||
</methods> | ||
<members> | ||
<member name="playback_mode" type="int" setter="set_playback_mode" getter="get_playback_mode" enum="AudioStreamRandomizer.PlaybackMode" default="0"> | ||
Controls how this AudioStreamRandomizer picks which AudioStream to play next. | ||
</member> | ||
<member name="random_pitch" type="float" setter="set_random_pitch" getter="get_random_pitch" default="1.1"> | ||
The intensity of random pitch variation. A value of 1 means no variation. | ||
</member> | ||
<member name="random_volume_offset_db" type="float" setter="set_random_volume_offset_db" getter="get_random_volume_offset_db" default="5.0"> | ||
The intensity of random volume variation. A value of 0 means no variation. | ||
</member> | ||
<member name="streams_count" type="int" setter="set_streams_count" getter="get_streams_count" default="0"> | ||
The number of streams in the stream pool. | ||
</member> | ||
</members> | ||
<constants> | ||
<constant name="PLAYBACK_RANDOM_NO_REPEATS" value="0" enum="PlaybackMode"> | ||
Pick a stream at random according to the probability weights chosen for each stream, but avoid playing the same stream twice in a row whenever possible. | ||
</constant> | ||
<constant name="PLAYBACK_RANDOM" value="1" enum="PlaybackMode"> | ||
Pick a stream at random according to the probability weights chosen for each stream. | ||
</constant> | ||
<constant name="PLAYBACK_SEQUENTIAL" value="2" enum="PlaybackMode"> | ||
Play streams in the order they appear in the stream pool. | ||
</constant> | ||
</constants> | ||
</class> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
editor/plugins/audio_stream_randomizer_editor_plugin.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/*************************************************************************/ | ||
/* audio_stream_randomizer_editor_plugin.cpp */ | ||
/*************************************************************************/ | ||
/* This file is part of: */ | ||
/* GODOT ENGINE */ | ||
/* https://godotengine.org */ | ||
/*************************************************************************/ | ||
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ | ||
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ | ||
/* */ | ||
/* Permission is hereby granted, free of charge, to any person obtaining */ | ||
/* a copy of this software and associated documentation files (the */ | ||
/* "Software"), to deal in the Software without restriction, including */ | ||
/* without limitation the rights to use, copy, modify, merge, publish, */ | ||
/* distribute, sublicense, and/or sell copies of the Software, and to */ | ||
/* permit persons to whom the Software is furnished to do so, subject to */ | ||
/* the following conditions: */ | ||
/* */ | ||
/* The above copyright notice and this permission notice shall be */ | ||
/* included in all copies or substantial portions of the Software. */ | ||
/* */ | ||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ | ||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ | ||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ | ||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ | ||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ | ||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ | ||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
/*************************************************************************/ | ||
|
||
#include "audio_stream_randomizer_editor_plugin.h" | ||
|
||
void AudioStreamRandomizerEditorPlugin::edit(Object *p_object) { | ||
} | ||
|
||
bool AudioStreamRandomizerEditorPlugin::handles(Object *p_object) const { | ||
return false; | ||
} | ||
|
||
void AudioStreamRandomizerEditorPlugin::make_visible(bool p_visible) { | ||
} | ||
|
||
void AudioStreamRandomizerEditorPlugin::_move_stream_array_element(Object *p_undo_redo, Object *p_edited, String p_array_prefix, int p_from_index, int p_to_pos) { | ||
UndoRedo *undo_redo = Object::cast_to<UndoRedo>(p_undo_redo); | ||
ERR_FAIL_COND(!undo_redo); | ||
|
||
AudioStreamRandomizer *randomizer = Object::cast_to<AudioStreamRandomizer>(p_edited); | ||
if (!randomizer) { | ||
return; | ||
} | ||
|
||
// Compute the array indices to save. | ||
int begin = 0; | ||
int end; | ||
if (p_array_prefix == "stream_") { | ||
end = randomizer->get_streams_count(); | ||
} else { | ||
ERR_FAIL_MSG("Invalid array prefix for AudioStreamRandomizer."); | ||
} | ||
if (p_from_index < 0) { | ||
// Adding new. | ||
if (p_to_pos >= 0) { | ||
begin = p_to_pos; | ||
} else { | ||
end = 0; // Nothing to save when adding at the end. | ||
} | ||
} else if (p_to_pos < 0) { | ||
// Removing. | ||
begin = p_from_index; | ||
} else { | ||
// Moving. | ||
begin = MIN(p_from_index, p_to_pos); | ||
end = MIN(MAX(p_from_index, p_to_pos) + 1, end); | ||
} | ||
|
||
#define ADD_UNDO(obj, property) undo_redo->add_undo_property(obj, property, obj->get(property)); | ||
// Save layers' properties. | ||
if (p_from_index < 0) { | ||
undo_redo->add_undo_method(randomizer, "remove_stream", p_to_pos < 0 ? randomizer->get_streams_count() : p_to_pos); | ||
} else if (p_to_pos < 0) { | ||
undo_redo->add_undo_method(randomizer, "add_stream", p_from_index); | ||
} | ||
|
||
List<PropertyInfo> properties; | ||
randomizer->get_property_list(&properties); | ||
for (PropertyInfo pi : properties) { | ||
if (pi.name.begins_with(p_array_prefix)) { | ||
String str = pi.name.trim_prefix(p_array_prefix); | ||
int to_char_index = 0; | ||
while (to_char_index < str.length()) { | ||
if (str[to_char_index] < '0' || str[to_char_index] > '9') { | ||
break; | ||
} | ||
to_char_index++; | ||
} | ||
if (to_char_index > 0) { | ||
int array_index = str.left(to_char_index).to_int(); | ||
if (array_index >= begin && array_index < end) { | ||
ADD_UNDO(randomizer, pi.name); | ||
} | ||
} | ||
} | ||
} | ||
#undef ADD_UNDO | ||
|
||
if (p_from_index < 0) { | ||
undo_redo->add_do_method(randomizer, "add_stream", p_to_pos); | ||
} else if (p_to_pos < 0) { | ||
undo_redo->add_do_method(randomizer, "remove_stream", p_from_index); | ||
} else { | ||
undo_redo->add_do_method(randomizer, "move_stream", p_from_index, p_to_pos); | ||
} | ||
} | ||
|
||
AudioStreamRandomizerEditorPlugin::AudioStreamRandomizerEditorPlugin(EditorNode *p_node) { | ||
EditorNode::get_singleton()->get_editor_data().add_move_array_element_function(SNAME("AudioStreamRandomizer"), callable_mp(this, &AudioStreamRandomizerEditorPlugin::_move_stream_array_element)); | ||
} | ||
|
||
AudioStreamRandomizerEditorPlugin::~AudioStreamRandomizerEditorPlugin() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/*************************************************************************/ | ||
/* audio_stream_randomizer_editor_plugin.h */ | ||
/*************************************************************************/ | ||
/* This file is part of: */ | ||
/* GODOT ENGINE */ | ||
/* https://godotengine.org */ | ||
/*************************************************************************/ | ||
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ | ||
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ | ||
/* */ | ||
/* Permission is hereby granted, free of charge, to any person obtaining */ | ||
/* a copy of this software and associated documentation files (the */ | ||
/* "Software"), to deal in the Software without restriction, including */ | ||
/* without limitation the rights to use, copy, modify, merge, publish, */ | ||
/* distribute, sublicense, and/or sell copies of the Software, and to */ | ||
/* permit persons to whom the Software is furnished to do so, subject to */ | ||
/* the following conditions: */ | ||
/* */ | ||
/* The above copyright notice and this permission notice shall be */ | ||
/* included in all copies or substantial portions of the Software. */ | ||
/* */ | ||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ | ||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ | ||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ | ||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ | ||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ | ||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ | ||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
/*************************************************************************/ | ||
|
||
#ifndef AUDIO_STREAM_RANDOMIZER_EDITOR_PLUGIN_H | ||
#define AUDIO_STREAM_RANDOMIZER_EDITOR_PLUGIN_H | ||
|
||
#include "editor/editor_node.h" | ||
#include "editor/editor_plugin.h" | ||
#include "servers/audio/audio_stream.h" | ||
|
||
class AudioStreamRandomizerEditorPlugin : public EditorPlugin { | ||
GDCLASS(AudioStreamRandomizerEditorPlugin, EditorPlugin); | ||
|
||
EditorNode *editor; | ||
|
||
private: | ||
void _move_stream_array_element(Object *p_undo_redo, Object *p_edited, String p_array_prefix, int p_from_index, int p_to_pos); | ||
|
||
public: | ||
virtual String get_name() const override { return "AudioStreamRandomizer"; } | ||
bool has_main_screen() const override { return false; } | ||
virtual void edit(Object *p_object) override; | ||
virtual bool handles(Object *p_object) const override; | ||
virtual void make_visible(bool p_visible) override; | ||
|
||
AudioStreamRandomizerEditorPlugin(EditorNode *p_node); | ||
~AudioStreamRandomizerEditorPlugin(); | ||
}; | ||
|
||
#endif // AUDIO_STREAM_RANDOMIZER_EDITOR_PLUGIN_H |
Oops, something went wrong.