mirror of
https://codeberg.org/uzu/strudel
synced 2026-07-22 13:13:10 -04:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6d73381985 | |||
| 7eb737006f | |||
| 8690f0e5d9 | |||
| 33ab0dc2c4 | |||
| ca6a976351 |
@@ -2170,6 +2170,8 @@ export const { speed } = registerControl('speed');
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
export const { stretch } = registerControl('stretch');
|
export const { stretch } = registerControl('stretch');
|
||||||
|
export const { pshift } = registerControl('pshift');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used in conjunction with `speed`, accepts values of "r" (rate, default behavior), "c" (cycles), or "s" (seconds). Using `unit "c"` means `speed` will be interpreted in units of cycles, e.g. `speed "1"` means samples will be stretched to fill a cycle. Using `unit "s"` means the playback speed will be adjusted so that the duration is the number of seconds specified by `speed`.
|
* Used in conjunction with `speed`, accepts values of "r" (rate, default behavior), "c" (cycles), or "s" (seconds). Using `unit "c"` means `speed` will be interpreted in units of cycles, e.g. `speed "1"` means samples will be stretched to fill a cycle. Using `unit "s"` means the playback speed will be adjusted so that the duration is the number of seconds specified by `speed`.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -451,6 +451,7 @@ export const superdough = async (value, t, hapDuration, cps = 0.5, cycle = 0.5)
|
|||||||
compressorKnee,
|
compressorKnee,
|
||||||
compressorAttack,
|
compressorAttack,
|
||||||
compressorRelease,
|
compressorRelease,
|
||||||
|
pshift,
|
||||||
} = value;
|
} = value;
|
||||||
|
|
||||||
delaytime = delaytime ?? cycleToSeconds(delaysync, cps);
|
delaytime = delaytime ?? cycleToSeconds(delaysync, cps);
|
||||||
@@ -530,7 +531,14 @@ export const superdough = async (value, t, hapDuration, cps = 0.5, cycle = 0.5)
|
|||||||
}
|
}
|
||||||
const chain = []; // audio nodes that will be connected to each other sequentially
|
const chain = []; // audio nodes that will be connected to each other sequentially
|
||||||
chain.push(sourceNode);
|
chain.push(sourceNode);
|
||||||
stretch !== undefined && chain.push(getWorklet(ac, 'phase-vocoder-processor', { pitchFactor: stretch }));
|
stretch !== undefined &&
|
||||||
|
chain.push(
|
||||||
|
getWorklet(ac, 'pitch-processor', { pitchFactor: stretch }, { processorOptions: { vocoderMode: true } }),
|
||||||
|
);
|
||||||
|
pshift !== undefined &&
|
||||||
|
chain.push(
|
||||||
|
getWorklet(ac, 'pitch-processor', { pitchFactor: pshift }, { processorOptions: { vocoderMode: false } }),
|
||||||
|
);
|
||||||
|
|
||||||
// gain stage
|
// gain stage
|
||||||
chain.push(gainNode(gain));
|
chain.push(gainNode(gain));
|
||||||
|
|||||||
@@ -569,57 +569,79 @@ function genHannWindow(length) {
|
|||||||
return hannCache.get(length);
|
return hannCache.get(length);
|
||||||
}
|
}
|
||||||
|
|
||||||
class PhaseVocoderProcessor extends OLAProcessor {
|
class PitchProcessor extends OLAProcessor {
|
||||||
static get parameterDescriptors() {
|
static get parameterDescriptors() {
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
name: 'pitchFactor',
|
name: 'pitchFactor',
|
||||||
defaultValue: 1.0,
|
defaultValue: 1,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
options.processorOptions = {
|
const parentOptions = {
|
||||||
|
...options,
|
||||||
|
processorOptions: {
|
||||||
blockSize: BUFFERED_BLOCK_SIZE,
|
blockSize: BUFFERED_BLOCK_SIZE,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
super(options);
|
super(parentOptions);
|
||||||
this.timeCursor = 0;
|
|
||||||
|
// if true, use spectral peak-finding to cluster strong bins ('stretch')
|
||||||
|
// else, use simple shift & interpolate ('pitch')
|
||||||
|
this.vocoderMode = options.processorOptions.vocoderMode ?? true;
|
||||||
|
|
||||||
this.fftSize = this.blockSize;
|
this.fftSize = this.blockSize;
|
||||||
this.invfftSize = 1 / this.fftSize;
|
this.invfftSize = 1 / this.fftSize;
|
||||||
this.hannWindow = genHannWindow(this.fftSize);
|
this.hannWindow = genHannWindow(this.fftSize);
|
||||||
// prepare FFT and pre-allocate buffers
|
// prepare FFT and pre-allocate buffers
|
||||||
|
this.nyquistBin = this.fftSize / 2;
|
||||||
|
this.hannWindow = genHannWindow(this.blockSize);
|
||||||
this.fft = new FFT(this.fftSize);
|
this.fft = new FFT(this.fftSize);
|
||||||
this.freqComplexBuffer = this.fft.createComplexArray();
|
this.freqComplexBuffer = this.fft.createComplexArray();
|
||||||
this.freqComplexBufferShifted = this.fft.createComplexArray();
|
this.freqComplexBufferShifted = this.fft.createComplexArray();
|
||||||
this.timeComplexBuffer = this.fft.createComplexArray();
|
this.timeComplexBuffer = this.fft.createComplexArray();
|
||||||
|
this.timeCursor = 0;
|
||||||
|
|
||||||
|
// for peak tracking in phase vocoder mode
|
||||||
|
if (this.vocoderMode) {
|
||||||
this.magnitudes = new Float32Array(this.fftSize / 2 + 1);
|
this.magnitudes = new Float32Array(this.fftSize / 2 + 1);
|
||||||
this.peakIndexes = new Int32Array(this.magnitudes.length);
|
this.peakIndexes = new Int32Array(this.magnitudes.length);
|
||||||
this.nbPeaks = 0;
|
this.nbPeaks = 0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
processOLA(inputs, outputs, parameters) {
|
processOLA(inputs, outputs, parameters) {
|
||||||
// no automation, take last value
|
// no automation, take last value
|
||||||
let pitchFactor = parameters.pitchFactor[parameters.pitchFactor.length - 1];
|
let pitchFactor = parameters.pitchFactor[parameters.pitchFactor.length - 1];
|
||||||
if (pitchFactor < 0) {
|
if (this.vocoderMode) {
|
||||||
pitchFactor = pitchFactor * 0.25;
|
pitchFactor = pitchFactor < 0 ? pitchFactor * 0.25 : pitchFactor;
|
||||||
|
pitchFactor += 1;
|
||||||
}
|
}
|
||||||
pitchFactor = Math.max(0, pitchFactor + 1);
|
pitchFactor = Math.max(0.01, pitchFactor);
|
||||||
for (let i = 0; i < this.nbInputs; i++) {
|
for (let i = 0; i < this.nbInputs; i++) {
|
||||||
for (let j = 0; j < inputs[i].length; j++) {
|
for (let j = 0; j < inputs[i].length; j++) {
|
||||||
const input = inputs[i][j];
|
const input = inputs[i][j];
|
||||||
const output = outputs[i][j];
|
const output = outputs[i][j];
|
||||||
this.applyHannWindow(input);
|
this.applyHannWindow(input);
|
||||||
this.fft.realTransform(this.freqComplexBuffer, input);
|
this.fft.realTransform(this.freqComplexBuffer, input);
|
||||||
|
|
||||||
|
if (this.vocoderMode) {
|
||||||
this.computeMagnitudes();
|
this.computeMagnitudes();
|
||||||
this.findPeaks();
|
this.findPeaks();
|
||||||
this.shiftPeaks(pitchFactor);
|
this.shiftPeaks(pitchFactor);
|
||||||
|
} else {
|
||||||
|
this.shiftSpectrum(pitchFactor);
|
||||||
|
}
|
||||||
|
|
||||||
this.fft.completeSpectrum(this.freqComplexBufferShifted);
|
this.fft.completeSpectrum(this.freqComplexBufferShifted);
|
||||||
this.fft.inverseTransform(this.timeComplexBuffer, this.freqComplexBufferShifted);
|
this.fft.inverseTransform(this.timeComplexBuffer, this.freqComplexBufferShifted);
|
||||||
this.fft.fromComplexArray(this.timeComplexBuffer, output);
|
this.fft.fromComplexArray(this.timeComplexBuffer, output);
|
||||||
this.applyHannWindow(output);
|
this.applyHannWindow(output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.timeCursor += this.hopSize;
|
this.timeCursor += this.hopSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -630,6 +652,47 @@ class PhaseVocoderProcessor extends OLAProcessor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
writeShiftedBin(destBin, valueReal, valueImag, phaseShiftReal, phaseShiftImag, accumulate) {
|
||||||
|
const shiftedReal = valueReal * phaseShiftReal - valueImag * phaseShiftImag;
|
||||||
|
const shiftedImag = valueReal * phaseShiftImag + valueImag * phaseShiftReal;
|
||||||
|
const destIndex = destBin * 2;
|
||||||
|
if (accumulate) {
|
||||||
|
this.freqComplexBufferShifted[destIndex] += shiftedReal;
|
||||||
|
this.freqComplexBufferShifted[destIndex + 1] += shiftedImag;
|
||||||
|
} else {
|
||||||
|
this.freqComplexBufferShifted[destIndex] = shiftedReal;
|
||||||
|
this.freqComplexBufferShifted[destIndex + 1] = shiftedImag;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Shift entire spectrum with simple resampling */
|
||||||
|
shiftSpectrum(pitchFactor) {
|
||||||
|
// zero-fill new spectrum
|
||||||
|
this.freqComplexBufferShifted.fill(0);
|
||||||
|
const nyquist = this.nyquistBin;
|
||||||
|
for (let destBin = 0; destBin <= nyquist; destBin++) {
|
||||||
|
const sourceBin = destBin / pitchFactor;
|
||||||
|
if (sourceBin > nyquist) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
const lower = ffloor(sourceBin);
|
||||||
|
const upper = lower + 1 > nyquist ? nyquist : lower + 1;
|
||||||
|
const t = sourceBin - lower;
|
||||||
|
const lowerIndex = lower * 2;
|
||||||
|
const upperIndex = upper * 2;
|
||||||
|
const realLower = this.freqComplexBuffer[lowerIndex];
|
||||||
|
const imagLower = this.freqComplexBuffer[lowerIndex + 1];
|
||||||
|
const realUpper = this.freqComplexBuffer[upperIndex];
|
||||||
|
const imagUpper = this.freqComplexBuffer[upperIndex + 1];
|
||||||
|
const real = lerp(realLower, realUpper, t);
|
||||||
|
const imag = lerp(imagLower, imagUpper, t);
|
||||||
|
const omegaDelta = TWO_PI * this.invfftSize * (destBin - sourceBin);
|
||||||
|
const phaseShiftReal = Math.cos(omegaDelta * this.timeCursor);
|
||||||
|
const phaseShiftImag = Math.sin(omegaDelta * this.timeCursor);
|
||||||
|
this.writeShiftedBin(destBin, real, imag, phaseShiftReal, phaseShiftImag, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Compute squared magnitudes for peak finding **/
|
/** Compute squared magnitudes for peak finding **/
|
||||||
computeMagnitudes() {
|
computeMagnitudes() {
|
||||||
let i = 0,
|
let i = 0,
|
||||||
@@ -701,20 +764,13 @@ class PhaseVocoderProcessor extends OLAProcessor {
|
|||||||
const indexImag = indexReal + 1;
|
const indexImag = indexReal + 1;
|
||||||
const valueReal = this.freqComplexBuffer[indexReal];
|
const valueReal = this.freqComplexBuffer[indexReal];
|
||||||
const valueImag = this.freqComplexBuffer[indexImag];
|
const valueImag = this.freqComplexBuffer[indexImag];
|
||||||
|
this.writeShiftedBin(binIndexShifted, valueReal, valueImag, phaseShiftReal, phaseShiftImag, true);
|
||||||
const valueShiftedReal = valueReal * phaseShiftReal - valueImag * phaseShiftImag;
|
|
||||||
const valueShiftedImag = valueReal * phaseShiftImag + valueImag * phaseShiftReal;
|
|
||||||
|
|
||||||
const indexShiftedReal = 2 * binIndexShifted;
|
|
||||||
const indexShiftedImag = indexShiftedReal + 1;
|
|
||||||
this.freqComplexBufferShifted[indexShiftedReal] += valueShiftedReal;
|
|
||||||
this.freqComplexBufferShifted[indexShiftedImag] += valueShiftedImag;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
registerProcessor('phase-vocoder-processor', PhaseVocoderProcessor);
|
registerProcessor('pitch-processor', PitchProcessor);
|
||||||
|
|
||||||
// Adapted from https://www.musicdsp.org/en/latest/Effects/221-band-limited-pwm-generator.html
|
// Adapted from https://www.musicdsp.org/en/latest/Effects/221-band-limited-pwm-generator.html
|
||||||
class PulseOscillatorProcessor extends AudioWorkletProcessor {
|
class PulseOscillatorProcessor extends AudioWorkletProcessor {
|
||||||
|
|||||||
Reference in New Issue
Block a user