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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A collection of SuperCollider plugins developed by Jeff Martin (www.jeffreymarti
## List of Plugins

### PV_PlayBufStretch
A phase vocoder STFT buffer player for time stretching, for use with PV_RecordBuf in the sc3plugins distribution. It incorporates two different optional phase locking algorithms.
A phase vocoder STFT buffer player for time stretching, for use with PV_RecordBuf in the sc3plugins distribution. It has four different phase interpolation modes--the traditional method, two phase locking strategies, and the RTPGHI algorithm.

### PV_CFreeze
A SuperCollider implementation of Jean-François Charles' Max patch for freezing [(the Max patch is available here)](https://newfloremusic.gumroad.com/).
Expand Down
6 changes: 5 additions & 1 deletion src/pv/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# Create the project library
add_library(pv MODULE pv.cpp)
add_library(hashtable STATIC hashtable.cpp)
add_library(peakFinder STATIC peakFinder.cpp)
add_library(pvCFreeze STATIC pvCFreeze.cpp)
add_library(pvOther STATIC pvOther.cpp)
add_library(pvStretch STATIC pvStretch.cpp)
set_target_properties(hashtable PROPERTIES POSITION_INDEPENDENT_CODE ON)
set_target_properties(peakFinder PROPERTIES POSITION_INDEPENDENT_CODE ON)
set_target_properties(pvCFreeze PROPERTIES POSITION_INDEPENDENT_CODE ON)
set_target_properties(pvOther PROPERTIES POSITION_INDEPENDENT_CODE ON)
set_target_properties(pvStretch PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_link_libraries(pvStretch PRIVATE hashtable peakFinder)
target_link_libraries(pv PRIVATE pvCFreeze pvOther pvStretch)

if(SUPERNOVA)
Expand Down
45 changes: 26 additions & 19 deletions src/pv/PV_PlayBufStretch.schelp
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ related:: Guides/FFT-Overview
Description::

PV_PlayBufStretch is a phase vocoder buffer player for time stretching. It differs from link::Classes/PV_BufRd::
in that it has a phase locking feature. Phase locking is a strategy that is used to reduce
"phasiness"--a reverberant sound caused by phase misalignment in adjacent bins.
The idea is that you want the phases of peak bins to dominate the phases of adjacent bins.
This makes sense intuitively because we know that in an FFT, the energy of a partial is spread
across several bins. PV_PlayBufStretch implements two classic phase locking algorithms--an
algorithm by Miller Puckette,footnote::Puckette, Miller. "Phase-locked vocoder." 1995 IEEE Workshop on Applications of Signal Processing to Audio and Acoustics (1995), 222-225.::
and a later peak-picking algorithm (called "identity phase locking")
by Jean Laroche and Mark Dolson.footnote::Laroche, Jean, and Mark Dolson. "Improved Phase Vocoder Time-Scale Modification of Audio." IEEE Transactions on Speech and Audio Processing, 7, no. 3 (1999), 323-332.::

You will want to choose the algorithm based on the sound it produces. In some cases, you may not
wish to use phase locking at all. Traditionally, one would use a hop size of 0.25 or less along with an
appropriate window (like the Hann window) in the STFT. Laroche and Dolson write that for identity
phase locking, a hop size of 0.5 is feasible.
in that it has four different phase interpolation modes:

list::
## Mode 0 is the classical phase vocoder.
## Mode 1 is a simple phase locking strategy devised by Miller Puckette footnote::Puckette, Miller. "Phase-locked vocoder." 1995 IEEE Workshop on Applications of Signal Processing to Audio and Acoustics (1995), 222-225.::.
## Mode 2 is a peak-picking phase locking strategy by Jean Laroche and Mark Dolson footnote::Laroche, Jean, and Mark Dolson. "Improved Phase Vocoder Time-Scale Modification of Audio." IEEE Transactions on Speech and Audio Processing, 7, no. 3 (1999), 323-332.::.
## Mode 3 is the real-time phase gradient heap integration (RTPGHI) algorithm by Zdeněk Průša and Nicki Holighaus footnote::Průša, Zdeněk and Nicki Holighaus. "Phase Vocoder Done Right."::.
::

In the classical phase vocoder, the phases of adjacent bins slowly drift out of alignment.
This causes a reverberant sound effect called "phasiness." Modes 1-3 of PV_PlayBufStretch
attempt to alleviate this problem by synchronizing the phases of adjacent bins to some extent.

You will want to choose the algorithm based on the sound it produces.
Traditionally, one would use a hop size of 0.25 or less along with an
appropriate window (like the Hann window) in the STFT.

classmethods::

Expand Down Expand Up @@ -47,9 +50,10 @@ result in time shrink. Negative rates result in backwards playback.
argument::loop
Whether (1.0) or not (0.0) to loop when the end of the stftBuffer is reached

argument::phaseLock
The phase locking strategy to adopt. 0.0 means no phase locking, 1.0 means Puckette-style
phase locking, and 2.0 means Laroche/Dolson identity phase locking.
argument::mode
The phase vocoder mode to adopt. 0 - classical phase vocoder, 1 - Puckette-style
phase locking, 2 - Laroche/Dolson identity phase locking, 3 - RTPGHI phase gradient algorithm.
The default is 3.

argument::peakRadius
The radius for the peak-picker in the Laroche/Dolson identity phase locking
Expand All @@ -58,6 +62,9 @@ strong::peakRadius:: bins to either side. This parameter can only be set on cons
and is clipped internally to the range [1, 32]. Laroche and Dolson suggest a radius of 2,
which is the default here.

argument::tol
The tolerance for the RTPGHI algorithm. It is clipped internally to between 0 and 1.

argument::doneAction
The action to take after playback is completed

Expand Down Expand Up @@ -86,21 +93,21 @@ SynthDef(\rec, {
}).add;

SynthDef(\play2, {
arg recBuf, rate=1, phaseLock=0, loop=0, startPos=0;
arg recBuf, rate=1, mode=0, tol=0.1, loop=0, startPos=0;
var sig, chain;
// Note that you must specify the appropriate hop size in FFTTrigger.
// Also note that the FFT size must be the same as the FFT size of
// the original analysis.
chain = FFTTrigger(LocalBuf(2048), 0.25, 1.0);
chain = PV_PlayBufStretch(chain, recBuf, startPos, rate, loop, phaseLock, 2.0, Done.freeSelf);
chain = PV_PlayBufStretch(chain, recBuf, startPos, rate, loop, mode, 2.0, tol, Done.freeSelf);
// Use the same window type that was used for analysis.
sig = IFFT(chain, 1);
Out.ar(0, Pan2.ar(sig));
}).add;
)

Synth(\rec, [\recBuf, y, \soundBuf, z]);
b = Synth(\play2, [\recBuf, y, \rate, 0.5, \loop, 1.0, \startPos, 0.0, \phaseLock, 2.0]);
b = Synth(\play2, [\recBuf, y, \rate, 0.5, \loop, 1.0, \startPos, 0.0, \mode, 3, \tol, 0.1]);

// A more unusual take. Here we modulate the playback rate, and it can go negative.
(
Expand Down
214 changes: 214 additions & 0 deletions src/pv/hashtable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
/*
File: hashtable.cpp
Author: Jeff Martin

Description:
A simple int hash set (table size must be power of 2)

Copyright © 2026 by Jeffrey Martin. All rights reserved.
Website: https://www.jeffreymartincomposer.com

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "hashtable.hpp"
#include <stdint.h>
#include <cmath>

// hash function from claude
// table bits is the power of 2 size of the table
static inline uint32_t hash32(uint32_t key, int table_bits) {
return (key * 2654435769u) >> (32 - table_bits);
}

FlexPlugins::IntHashSet::IntHashSet(double maxLoadFactor) :
m_table(nullptr),
m_rehashTable(nullptr),
m_originalAlloc(nullptr),
m_size(0),
m_maxSize(0),
m_tableBits(0),
m_currentLoadFactor(0),
m_hasTombstones(false)
{
if (maxLoadFactor > 0 && maxLoadFactor < 1) {
m_maxLoadFactor = maxLoadFactor;
} else {
m_maxLoadFactor = 0.7;
}
}

void FlexPlugins::IntHashSet::clear() {
m_size = 0;
m_hasTombstones = false;
m_currentLoadFactor = 0;
for (size_t i = 0; i < m_maxSize; i++) {
m_table[i].isTombstone = false;
m_table[i].isNull = true;
m_table[i].data = 0;
}
}

size_t FlexPlugins::IntHashSet::maxSize() const {
return m_maxSize;
}

size_t FlexPlugins::IntHashSet::size() const {
return m_size;
}

void FlexPlugins::IntHashSet::memLoad(void* mem, size_t nbytes) {
m_table = static_cast<IntHashSetNode*>(mem);
m_originalAlloc = mem;
m_maxSize = (nbytes / 2) / sizeof(IntHashSetNode);
// hash table size must be power of 2
m_tableBits = static_cast<size_t>(std::log2(static_cast<float>(m_maxSize)));
m_maxSize = 0x1 << m_tableBits;
// the upper half of the allocation will be the rehash table
m_rehashTable = m_table + m_maxSize;
for (size_t i = 0; i < m_maxSize; i++) {
m_table[i] = IntHashSetNode();
}
}

void* FlexPlugins::IntHashSet::memRetrieve() {
return m_originalAlloc;
}

size_t FlexPlugins::IntHashSet::memSize(size_t maxSize) const {
// need to allocate 2x size because we need second rehash table
return maxSize * 2 * sizeof(IntHashSetNode);
}

bool FlexPlugins::IntHashSet::insert(int data) {
if (m_currentLoadFactor > m_maxLoadFactor) {
if (m_hasTombstones) {
IntHashSetNode *swap = m_table;
m_table = m_rehashTable;
m_rehashTable = swap;
m_hasTombstones = false;
m_size = 0;
m_currentLoadFactor = 0;
for (size_t i = 0; i < m_maxSize; i++) {
m_table[i].data = 0;
m_table[i].isNull = true;
m_table[i].isTombstone = false;
}
for (size_t i = 0; i < m_maxSize; i++) {
if (!m_rehashTable[i].isNull) {
bool result = insert(m_rehashTable[i].data);
if (!result) return false;
}
}
}
if (m_currentLoadFactor > m_maxLoadFactor) {
return false;
}
}

// hash and insert the data
size_t idx = hash32(static_cast<uint32_t>(data), m_tableBits);
if (m_table[idx].isNull && !m_table[idx].isTombstone) {
m_table[idx].data = data;
m_table[idx].isNull = false;
m_size++;
m_currentLoadFactor += 1/static_cast<double>(m_maxSize);
return true;
} else if (m_table[idx].data == data && !m_table[idx].isTombstone) {
// no duplicate inserts allowed
return false;
} else {
// linear probing
for (size_t i = idx+1; i < m_maxSize; i++) {
if (m_table[i].isNull && !m_table[i].isTombstone) {
m_table[i].data = data;
m_table[i].isNull = false;
m_size++;
m_currentLoadFactor += 1/static_cast<double>(m_maxSize);
return true;
} else if (m_table[i].data == data && !m_table[i].isTombstone) {
// no duplicate inserts allowed
return false;
}
}
for (size_t i = 0; i < idx; i++) {
if (m_table[i].isNull && !m_table[i].isTombstone) {
m_table[i].data = data;
m_table[i].isNull = false;
m_size++;
m_currentLoadFactor += 1/static_cast<double>(m_maxSize);
return true;
} else if (m_table[i].data == data && !m_table[i].isTombstone) {
// no duplicate inserts allowed
return false;
}
}
return false;
}
}

int FlexPlugins::IntHashSet::ifind(int data) {
int idx = static_cast<int>(hash32(static_cast<uint32_t>(data), m_tableBits));
if (m_table[idx].isNull && !m_table[idx].isTombstone) {
return -1;
} else {
if (!m_table[idx].isNull && m_table[idx].data == data) {
return idx;
} else {
for (int i = idx+1; i < m_maxSize; i++) {
// the probing would have stopped here, so the item was not inserted
if (m_table[i].isNull) {
if (!m_table[i].isTombstone) {
return -1;
}
} else if (m_table[i].data == data) {
return i;
}
}
for (int i = 0; i < idx; i++) {
// the probing would have stopped here, so the item was not inserted
if (m_table[i].isNull) {
if (!m_table[i].isTombstone) {
return -1;
}
} else if (m_table[i].data == data) {
return i;
}
}
}
return -1;
}
}

bool FlexPlugins::IntHashSet::find(int data) {
if (ifind(data) >= 0) {
return true;
} else {
return false;
}
}

bool FlexPlugins::IntHashSet::remove(int data) {
int idx = ifind(data);
if (idx < 0) {
return false;
} else {
m_table[idx].data = 0;
m_table[idx].isNull = true;
m_table[idx].isTombstone = true;
m_hasTombstones = true;
m_size--;
return true;
}
}
Loading
Loading