declare enum BUFFER_READY_STATES { NOT_READY = 0, READY = 1 } declare enum WORKER_STATES { NEW = 0, IDLE = 1, PROCESSING = 2 } export declare class State { /** * Create a new SharedArrayBuffer that backs the state object. * @param fullBufferLength The number of samples after which the worker will be activated. */ static createState(): State; constructor(state: SharedArrayBuffer); protected readonly state: Int32Array; /** * Export the storage buffer for the state as a SharedArrayBuffer. */ get stateBuffer(): SharedArrayBuffer; get bufferAvailable(): boolean; /** * Notifies! */ protected set bufferAvailable(value: BUFFER_READY_STATES); get bufferWriteHead(): number; set bufferWriteHead(value: number); get workerNew(): boolean; get workerIdle(): boolean; get workerProcessing(): boolean; protected set workerProcessing(value: WORKER_STATES); get generation(): number; get processorReadyGeneration(): number; protected set processorReadyGeneration(value: number); get processorCompleteGeneration(): number; private set processorCompleteGeneration(value); isProcessorReady(generation: number): boolean; isProcessorComplete(generation: number): boolean; /** * Called by the main thread when the processor has finished writing to the buffer. * The processor has no more samples to write. We trigger the buffer available state * to signal the worker to process the buffer. */ processorComplete(generation: number): void; /** * Called by the main thread when options have changed. * The generation is incremented. * The old processor should be discarded. * The worker should be updated. * @returns The new generation. */ reset(): number; matchesCurrentGeneration(generation: number): boolean; /** * Called by the main thread when it is waiting for the processor to be ready. */ waitForProcessorReady(generation: number): Promise; /** * Called by the main thread when it is waiting for the processor to be ready. */ waitForWorkerReady(): Promise; waitForWorkerIdle(): Promise; /** * Wait for the worker to transition into the PROCESSING state. * * Returns true if the worker became busy within the timeout, or false if the * timeout was reached without the worker starting processing. This prevents * the main thread from hanging indefinitely if the worker never transitions * to PROCESSING (e.g., worker crash or unhandled message). * * @param timeoutMs - Maximum time to wait in milliseconds. Defaults to 5000ms. */ waitForWorkerBusy(timeoutMs?: number): Promise; } export declare class ProcessorState extends State { /** * Called by the processor thread when it is ready to start processing. * This will signal the main thread to start rendering. * @param generation - The generation of the processor. * @returns true if the ready call is valid, false if the processor should abort. */ processorReady(generation: number): boolean; busyWaitForWorkerToProcessBuffer(generation: number, maxAttempts?: number): void; /** * called by the processor when it has finished writing to the buffer */ bufferReady(): void; } /** * Represents the state of a worker. */ export declare class WorkerState extends State { TIMEOUT: number; processorCanGenerate(generation: number): boolean; /** * Waits for the buffer to become available. * * This method can exit in the following scenarios: * - When the value is still the same as the expected value. * - When the value is different from the expected value. * - After the timeout period. * * In our first design we chose to never exit unless the value has changed. * However, long waits like that don't give us the opportunity to check for aborts. * * The tradeoff however, there is no guarantee that BUFFER_AVAILABLE is true when we exit. * @returns true if the buffer is available, false if the buffer is not available. */ waitForBuffer(): boolean; /** * Called by the worker when it has finished processing the buffer. * This will reset the buffer write head. * If any samples weren't consumed, they will be copied to the beginning of the buffer. * * @param sampleBuffer - The buffer containing the samples. * @param consumed - The number of samples consumed by the worker. */ bufferProcessed(sampleBuffer: Float32Array, consumed: number): void; /** * Called by the worker when it is processing the buffer. */ workerBusy(): void; /** * Called by the worker when it is idle. */ workerReady(): void; } export {};