|
| 1 | +/* |
| 2 | + * SoundTouch JS audio processing library |
| 3 | + * Copyright (c) Steve 'Cutter' Blades |
| 4 | + * |
| 5 | + * This library is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU Lesser General Public |
| 7 | + * License as published by the Free Software Foundation; either |
| 8 | + * version 3 of the License. |
| 9 | + * |
| 10 | + * This library is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + * Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public |
| 16 | + * License along with this library; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | + */ |
| 19 | + |
| 20 | +import type { SampleBuffer, SampleBufferType } from './SampleBuffer.js'; |
| 21 | +import type { StretchParameters } from './Stretch.js'; |
| 22 | + |
| 23 | +/** |
| 24 | + * Structural interface for a WSOLA time-stretch processing stage. |
| 25 | + * |
| 26 | + * @remarks |
| 27 | + * Implemented by the built-in `Stretch` class. Expose this interface as the type |
| 28 | + * for custom stretch implementations supplied via `SoundTouchOptions.stretchFactory` |
| 29 | + * so callers can swap in a phase vocoder or any other algorithm without subclassing. |
| 30 | + * |
| 31 | + * @example |
| 32 | + * const myFactory: StretchFactory = (sampleRate, opts) => new PhaseVocoderStretch(sampleRate, opts); |
| 33 | + * const st = new SoundTouch({ stretchFactory: myFactory }); |
| 34 | + */ |
| 35 | +export interface StretchPipe { |
| 36 | + /** Input buffer that feeds audio frames into the stretch stage. */ |
| 37 | + inputBuffer: SampleBuffer | null; |
| 38 | + /** Output buffer that the stretch stage writes processed frames into. */ |
| 39 | + outputBuffer: SampleBuffer | null; |
| 40 | + /** Tempo multiplier (1.0 = original speed). */ |
| 41 | + tempo: number; |
| 42 | + /** Minimum number of input frames required before the stage can produce output. */ |
| 43 | + readonly sampleReq: number; |
| 44 | + |
| 45 | + /** |
| 46 | + * Resets all internal state, including the mid-buffer. |
| 47 | + */ |
| 48 | + clear(): void; |
| 49 | + |
| 50 | + /** |
| 51 | + * Resets only the overlap mid-buffer without touching the sample buffers. |
| 52 | + */ |
| 53 | + clearMidBuffer(): void; |
| 54 | + |
| 55 | + /** |
| 56 | + * Runs one processing step, consuming frames from `inputBuffer` and writing to `outputBuffer`. |
| 57 | + */ |
| 58 | + process(): void; |
| 59 | + |
| 60 | + /** |
| 61 | + * Configures the WSOLA timing parameters. |
| 62 | + * |
| 63 | + * @param sampleRate Processing sample rate in Hz. |
| 64 | + * @param sequenceMs Sequence window length in ms; `0` = auto. |
| 65 | + * @param seekWindowMs Seek window length in ms; `0` = auto. |
| 66 | + * @param overlapMs Crossfade overlap length in ms. |
| 67 | + */ |
| 68 | + setParameters( |
| 69 | + sampleRate: number, |
| 70 | + sequenceMs: number, |
| 71 | + seekWindowMs: number, |
| 72 | + overlapMs: number, |
| 73 | + ): void; |
| 74 | + |
| 75 | + /** |
| 76 | + * Applies a partial set of WSOLA timing parameters without requiring all four values. |
| 77 | + * |
| 78 | + * @param params Partial timing parameters to update. |
| 79 | + */ |
| 80 | + setStretchParameters(params: StretchParameters): void; |
| 81 | + |
| 82 | + /** |
| 83 | + * Creates an independent copy with the same configuration and state. |
| 84 | + * @returns A new `StretchPipe` instance cloned from this one. |
| 85 | + */ |
| 86 | + clone(): StretchPipe; |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Options passed to a `StretchFactory` when `SoundTouch` creates its stretch stage. |
| 91 | + */ |
| 92 | +export interface StretchFactoryOptions { |
| 93 | + /** Factory for creating the sample buffers shared with the rest of the pipeline. */ |
| 94 | + sampleBufferFactory: () => SampleBuffer; |
| 95 | + /** Buffer strategy identifier (passed through from `SoundTouchOptions`). */ |
| 96 | + sampleBufferType: SampleBufferType; |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Factory function that creates a custom stretch processing stage. |
| 101 | + * |
| 102 | + * @remarks |
| 103 | + * Pass this to `SoundTouchOptions.stretchFactory` to replace the built-in WSOLA `Stretch` |
| 104 | + * with a custom implementation (e.g. a phase vocoder). |
| 105 | + * |
| 106 | + * @param sampleRate Processing sample rate in Hz. |
| 107 | + * @param options Additional options supplied by `SoundTouch`. |
| 108 | + * @returns A `StretchPipe` instance ready to be wired into the processing chain. |
| 109 | + */ |
| 110 | +export type StretchFactory = ( |
| 111 | + sampleRate: number, |
| 112 | + options: StretchFactoryOptions, |
| 113 | +) => StretchPipe; |
0 commit comments