Skip to content

Commit

Permalink
Add AudioStreamRandomizer, replacing AudioStreamRandomPitch
Browse files Browse the repository at this point in the history
Add additional randomization options.
  • Loading branch information
ellenhp authored and akien-mga committed Feb 8, 2022
1 parent ba1024f commit 41a158a
Show file tree
Hide file tree
Showing 8 changed files with 651 additions and 85 deletions.
19 changes: 0 additions & 19 deletions doc/classes/AudioStreamRandomPitch.xml

This file was deleted.

90 changes: 90 additions & 0 deletions doc/classes/AudioStreamRandomizer.xml
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>
2 changes: 2 additions & 0 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
#include "editor/plugins/animation_tree_editor_plugin.h"
#include "editor/plugins/asset_library_editor_plugin.h"
#include "editor/plugins/audio_stream_editor_plugin.h"
#include "editor/plugins/audio_stream_randomizer_editor_plugin.h"
#include "editor/plugins/camera_3d_editor_plugin.h"
#include "editor/plugins/canvas_item_editor_plugin.h"
#include "editor/plugins/collision_polygon_2d_editor_plugin.h"
Expand Down Expand Up @@ -7013,6 +7014,7 @@ EditorNode::EditorNode() {
add_editor_plugin(memnew(TextureLayeredEditorPlugin(this)));
add_editor_plugin(memnew(Texture3DEditorPlugin(this)));
add_editor_plugin(memnew(AudioStreamEditorPlugin(this)));
add_editor_plugin(memnew(AudioStreamRandomizerEditorPlugin(this)));
add_editor_plugin(memnew(AudioBusesEditorPlugin(audio_bus_editor)));
add_editor_plugin(memnew(Skeleton3DEditorPlugin(this)));
add_editor_plugin(memnew(SkeletonIK3DEditorPlugin(this)));
Expand Down
119 changes: 119 additions & 0 deletions editor/plugins/audio_stream_randomizer_editor_plugin.cpp
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() {}
57 changes: 57 additions & 0 deletions editor/plugins/audio_stream_randomizer_editor_plugin.h
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
Loading

0 comments on commit 41a158a

Please sign in to comment.