-
Notifications
You must be signed in to change notification settings - Fork 275
module: added Signalsmith time-stretching module. #1150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+11,862
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,60 @@ | ||
| <?xml version="1.0" encoding="UTF-8" ?> | ||
| <class name="SignalSmith" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd"> | ||
| <brief_description> | ||
| Performs time-stretching and pitch-shifting on raw audio buffers using Signalsmith. | ||
| </brief_description> | ||
| <description> | ||
| SignalSmith is a low-level audio processing utility which wraps the Signalsmith time-stretching library. It operates on raw interleaved floating-point PCM audio buffers and allows independent control of playback tempo and pitch. | ||
| </description> | ||
| <tutorials> | ||
| </tutorials> | ||
| <methods> | ||
| <method name="process"> | ||
| <return type="PackedFloat32Array" /> | ||
| <param index="0" name="input" type="PackedFloat32Array" /> | ||
| <description> | ||
| Processes a block of interleaved audio samples and returns a new buffer containing the time-stretched and pitch-shifted result. | ||
| The effective playback speed is determined by the ratio of input samples to output samples, as influenced by the current tempo setting. The input buffer must contain 32-bit floating-point PCM data and be interleaved according to the configured channel count. | ||
| </description> | ||
| </method> | ||
| <method name="reset"> | ||
| <return type="void" /> | ||
| <description> | ||
| Resets the internal processing state. | ||
| This should be called when restarting playback or discontinuously changing input streams. | ||
| </description> | ||
| </method> | ||
| <method name="set_channels"> | ||
| <return type="void" /> | ||
| <param index="0" name="channels" type="int" /> | ||
| <description> | ||
| Sets the number of audio channels. | ||
| Input and output buffers are expected to be interleaved according to this channel count. | ||
| </description> | ||
| </method> | ||
| <method name="set_pitch"> | ||
| <return type="void" /> | ||
| <param index="0" name="pitch" type="float" /> | ||
| <description> | ||
| Sets the pitch transpose factor. | ||
| A value of `1.0` leaves pitch unchanged. Values greater than `1.0` raise pitch, while values less than `1.0` lower pitch. | ||
| </description> | ||
| </method> | ||
| <method name="set_sample_rate"> | ||
| <return type="void" /> | ||
| <param index="0" name="rate" type="int" /> | ||
| <description> | ||
| Sets the sample rate, in Hz, used by the internal processing engine. | ||
| Changing the sample rate resets the internal state. | ||
| </description> | ||
| </method> | ||
| <method name="set_tempo"> | ||
| <return type="void" /> | ||
| <param index="0" name="tempo" type="float" /> | ||
| <description> | ||
| Sets the tempo multiplier used during processing. | ||
| This value influences the ratio between input and output buffer sizes during time-stretching. A value of `1.0` preserves the original tempo, values greater than `1.0` speed up playback, and values less than `1.0` slow it down. | ||
| </description> | ||
| </method> | ||
| </methods> | ||
| </class> |
This file contains hidden or 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,33 @@ | ||
| #!/usr/bin/env python | ||
| from misc.utility.scons_hints import * | ||
|
|
||
| Import("env") | ||
|
|
||
| signalsmith_env = env.Clone() | ||
|
|
||
| signalsmith_env.Append(CPPPATH=["#thirdparty"]) | ||
|
|
||
| cxx = env.subst("$CXX") | ||
| is_clang = "clang" in cxx | ||
| is_gcc = ("gcc" in cxx or "g++" in cxx) and not is_clang | ||
| is_msvc = ("cl" in cxx) and not is_clang | ||
|
|
||
| if is_gcc: | ||
| signalsmith_env.Append(CCFLAGS=["-Wno-class-memaccess"]) | ||
| signalsmith_env.Append(CCFLAGS=["-Wno-shadow"]) | ||
|
|
||
| # silence "hides previous declaration/member" warnings | ||
| if is_msvc: | ||
| signalsmith_env.Append( | ||
| CCFLAGS=[ | ||
| "/wd4456", # hides previous local declaration | ||
| "/wd4458", # hides class member | ||
| ] | ||
| ) | ||
|
|
||
| module_sources = [ | ||
| "register_types.cpp", | ||
| "signalsmith_module.cpp", | ||
| ] | ||
|
|
||
| signalsmith_env.add_source_files(env.modules_sources, module_sources) |
This file contains hidden or 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,6 @@ | ||
| def can_build(env, platform): | ||
| return True | ||
|
|
||
|
|
||
| def configure(env): | ||
| pass |
This file contains hidden or 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,49 @@ | ||
| /**************************************************************************/ | ||
| /* register_types.cpp */ | ||
| /**************************************************************************/ | ||
| /* This file is part of: */ | ||
| /* REDOT ENGINE */ | ||
| /* https://redotengine.org */ | ||
| /**************************************************************************/ | ||
| /* Copyright (c) 2024-present Redot Engine contributors */ | ||
| /* (see REDOT_AUTHORS.md) */ | ||
| /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | ||
| /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ | ||
| /* */ | ||
| /* 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 "register_types.h" | ||
| #include "core/object/class_db.h" | ||
| #include "signalsmith_module.h" | ||
|
|
||
| void initialize_signalsmith_module(ModuleInitializationLevel p_level) { | ||
| if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { | ||
| return; | ||
| } | ||
|
|
||
| ClassDB::register_class<SignalSmith>(); | ||
| } | ||
|
|
||
| void uninitialize_signalsmith_module(ModuleInitializationLevel p_level) { | ||
| if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { | ||
| return; | ||
| } | ||
| } |
This file contains hidden or 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,38 @@ | ||
| /**************************************************************************/ | ||
| /* register_types.h */ | ||
| /**************************************************************************/ | ||
| /* This file is part of: */ | ||
| /* REDOT ENGINE */ | ||
| /* https://redotengine.org */ | ||
| /**************************************************************************/ | ||
| /* Copyright (c) 2024-present Redot Engine contributors */ | ||
| /* (see REDOT_AUTHORS.md) */ | ||
| /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | ||
| /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ | ||
| /* */ | ||
| /* 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. */ | ||
| /**************************************************************************/ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "modules/register_module_types.h" | ||
|
|
||
| void initialize_signalsmith_module(ModuleInitializationLevel p_level); | ||
| void uninitialize_signalsmith_module(ModuleInitializationLevel p_level); |
This file contains hidden or 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,173 @@ | ||
| /**************************************************************************/ | ||
| /* signalsmith_module.cpp */ | ||
| /**************************************************************************/ | ||
| /* This file is part of: */ | ||
| /* REDOT ENGINE */ | ||
| /* https://redotengine.org */ | ||
| /**************************************************************************/ | ||
| /* Copyright (c) 2024-present Redot Engine contributors */ | ||
| /* (see REDOT_AUTHORS.md) */ | ||
| /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | ||
| /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ | ||
| /* */ | ||
| /* 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 "signalsmith_module.h" | ||
|
|
||
| #include "core/os/memory.h" | ||
|
|
||
| #include <cmath> | ||
| #include <vector> | ||
|
|
||
| void SignalSmith::_bind_methods() { | ||
| ClassDB::bind_method(D_METHOD("set_sample_rate", "rate"), &SignalSmith::set_sample_rate); | ||
| ClassDB::bind_method(D_METHOD("set_channels", "channels"), &SignalSmith::set_channels); | ||
| ClassDB::bind_method(D_METHOD("set_pitch", "pitch"), &SignalSmith::set_pitch); | ||
| ClassDB::bind_method(D_METHOD("set_tempo", "tempo"), &SignalSmith::set_tempo); | ||
| ClassDB::bind_method(D_METHOD("reset"), &SignalSmith::reset); | ||
| ClassDB::bind_method(D_METHOD("process", "input"), &SignalSmith::process); | ||
| } | ||
|
|
||
| SignalSmith::SignalSmith() { | ||
| stretch.presetDefault(channels, sample_rate); | ||
| } | ||
|
|
||
| SignalSmith::~SignalSmith() {} | ||
|
|
||
| void SignalSmith::set_sample_rate(int p_rate) { | ||
| if (p_rate < 1) { | ||
| return; | ||
| } | ||
|
|
||
| sample_rate = p_rate; | ||
| stretch.presetDefault(channels, sample_rate); | ||
| } | ||
|
|
||
| void SignalSmith::set_channels(int p_channels) { | ||
| if (p_channels < 1) { | ||
| return; | ||
| } | ||
|
|
||
| channels = p_channels; | ||
| stretch.presetDefault(channels, sample_rate); | ||
| } | ||
|
|
||
| void SignalSmith::set_pitch(float p_pitch) { | ||
| if (!(p_pitch > 0.0f)) { | ||
| return; | ||
| } | ||
|
|
||
| stretch.setTransposeFactor(p_pitch); | ||
| } | ||
|
|
||
| void SignalSmith::set_tempo(float p_tempo) { | ||
| if (!(p_tempo > 0.0f)) { | ||
| return; | ||
| } | ||
|
|
||
| tempo = p_tempo; | ||
| } | ||
|
|
||
| void SignalSmith::reset() { | ||
| stretch.reset(); | ||
| } | ||
|
|
||
| PackedFloat32Array SignalSmith::process(const PackedFloat32Array &input) { | ||
| PackedFloat32Array output; | ||
|
|
||
| if (channels < 1) { | ||
| return output; | ||
| } | ||
|
|
||
| const int total_samples = input.size(); | ||
|
|
||
| if (total_samples <= 0) { | ||
| return output; | ||
| } | ||
|
|
||
| if (total_samples % channels != 0) { | ||
| ERR_FAIL_V_MSG(output, "Input array size must be a multiple of channel count."); | ||
| } | ||
|
|
||
| const int input_frames = total_samples / channels; | ||
|
|
||
| if (input_frames <= 0) { | ||
| return output; | ||
| } | ||
|
|
||
| const float tf = (tempo > 0.0f) ? tempo : 1.0f; | ||
| int output_frames = (int)std::lround((double)input_frames / (double)tf); | ||
|
|
||
| if (output_frames < 0) { | ||
| output_frames = 0; | ||
| } | ||
|
|
||
| // Deinterleave | ||
| std::vector<std::vector<float>> in_ch; | ||
| in_ch.resize((size_t)channels); | ||
|
|
||
| for (int c = 0; c < channels; c++) { | ||
| in_ch[(size_t)c].resize((size_t)input_frames); | ||
| } | ||
|
|
||
| const float *src = input.ptr(); | ||
|
|
||
| for (int i = 0; i < input_frames; i++) { | ||
| const int base = i * channels; | ||
|
|
||
| for (int c = 0; c < channels; c++) { | ||
| in_ch[(size_t)c][(size_t)i] = src[base + c]; | ||
| } | ||
| } | ||
|
|
||
| // Output buffers | ||
| std::vector<std::vector<float>> out_ch; | ||
| out_ch.resize((size_t)channels); | ||
|
|
||
| for (int c = 0; c < channels; c++) { | ||
| out_ch[(size_t)c].assign((size_t)output_frames, 0.0f); | ||
| } | ||
|
|
||
| std::vector<float *> in_ptrs((size_t)channels, nullptr); | ||
| std::vector<float *> out_ptrs((size_t)channels, nullptr); | ||
|
|
||
| for (int c = 0; c < channels; c++) { | ||
| in_ptrs[(size_t)c] = in_ch[(size_t)c].data(); | ||
| out_ptrs[(size_t)c] = out_ch[(size_t)c].data(); | ||
| } | ||
|
|
||
| // Process: (inputs, inputSamples, outputs, outputSamples) | ||
| stretch.process(in_ptrs.data(), input_frames, out_ptrs.data(), output_frames); | ||
|
|
||
| // Interleave | ||
| output.resize(output_frames * channels); | ||
| float *dst = output.ptrw(); | ||
|
|
||
| for (int i = 0; i < output_frames; i++) { | ||
| const int base = i * channels; | ||
|
|
||
| for (int c = 0; c < channels; c++) { | ||
| dst[base + c] = out_ch[(size_t)c][(size_t)i]; | ||
| } | ||
| } | ||
|
|
||
| return output; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
signalsmith-stretch presetDefault internal state reset💡 Result:
presetDefault only configures block/interval and sample-rate; it does not clear the internal processing buffers. Call reset() to clear internal state/buffers after (or before) changing presets. [1][2]
Sources:
[1] signalsmith-stretch README (presetDefault / configure / reset).
[2] Signalsmith Audio project page (preset methods and "To clear the internal buffers: stretch.reset()").
Add
stretch.reset()after callingpresetDefault()in both methods.presetDefault()only configures block/interval and sample-rate—it does not clear internal processing buffers. Callreset()after changing the preset to clear the internal state and prevent stale data from affecting subsequent audio processing.🤖 Prompt for AI Agents