Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions doc/classes/SignalSmith.xml
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>
33 changes: 33 additions & 0 deletions modules/signalsmith/SCsub
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)
6 changes: 6 additions & 0 deletions modules/signalsmith/config.py
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
49 changes: 49 additions & 0 deletions modules/signalsmith/register_types.cpp
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;
}
}
38 changes: 38 additions & 0 deletions modules/signalsmith/register_types.h
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);
173 changes: 173 additions & 0 deletions modules/signalsmith/signalsmith_module.cpp
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);
}
Comment on lines +55 to +71
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 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 calling presetDefault() in both methods.

presetDefault() only configures block/interval and sample-rate—it does not clear internal processing buffers. Call reset() after changing the preset to clear the internal state and prevent stale data from affecting subsequent audio processing.

void SignalSmith::set_sample_rate(int p_rate) {
	if (p_rate < 1) {
		return;
	}

	sample_rate = p_rate;
	stretch.presetDefault(channels, sample_rate);
	stretch.reset();
}

void SignalSmith::set_channels(int p_channels) {
	if (p_channels < 1) {
		return;
	}

	channels = p_channels;
	stretch.presetDefault(channels, sample_rate);
	stretch.reset();
}
🤖 Prompt for AI Agents
In @modules/signalsmith/signalsmith_module.cpp around lines 55 - 71,
SignalSmith::set_sample_rate and SignalSmith::set_channels call
stretch.presetDefault(channels, sample_rate) but do not clear the internal
processing buffers; after each presetDefault call add a call to stretch.reset()
so the stretch instance is reinitialized (i.e., update both
SignalSmith::set_sample_rate and SignalSmith::set_channels to call
stretch.reset() immediately after stretch.presetDefault(...)).


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;
}
Loading
Loading