From d68b1728044b9722e9d211e7d4680b07a45e27cb Mon Sep 17 00:00:00 2001 From: Maciej Makowski Date: Wed, 18 Dec 2024 10:12:40 +0100 Subject: [PATCH] feat: added ts interface for AnalyserNode --- .../src/core/AnalyserNode.ts | 37 +++++++++++++++++++ .../src/index.native.ts | 1 + packages/react-native-audio-api/src/index.ts | 34 +++++++++++++++++ .../react-native-audio-api/src/interfaces.ts | 13 +++++++ 4 files changed, 85 insertions(+) create mode 100644 packages/react-native-audio-api/src/core/AnalyserNode.ts diff --git a/packages/react-native-audio-api/src/core/AnalyserNode.ts b/packages/react-native-audio-api/src/core/AnalyserNode.ts new file mode 100644 index 00000000..4f585d69 --- /dev/null +++ b/packages/react-native-audio-api/src/core/AnalyserNode.ts @@ -0,0 +1,37 @@ +import { IAnalyserNode } from '../interfaces'; +import AudioNode from './AudioNode'; +import BaseAudioContext from './BaseAudioContext'; + +export default class AnalyserNode extends AudioNode { + fftSize: number; + readonly frequencyBinCount: number; + minDecibels: number; + maxDecibels: number; + smoothingTimeConstant: number; + + constructor(context: BaseAudioContext, analyser: IAnalyserNode) { + super(context, analyser); + + this.fftSize = analyser.fftSize; + this.frequencyBinCount = analyser.frequencyBinCount; + this.minDecibels = analyser.minDecibels; + this.maxDecibels = analyser.maxDecibels; + this.smoothingTimeConstant = analyser.smoothingTimeConstant; + } + + getFloatFrequencyData(array: number[]): void { + (this.node as IAnalyserNode).getFloatFrequencyData(array); + } + + getByteFrequencyData(array: number[]): void { + (this.node as IAnalyserNode).getByteFrequencyData(array); + } + + getFloatTimeDomainData(array: number[]): void { + (this.node as IAnalyserNode).getFloatTimeDomainData(array); + } + + getByteTimeDomainData(array: number[]): void { + (this.node as IAnalyserNode).getByteTimeDomainData(array); + } +} diff --git a/packages/react-native-audio-api/src/index.native.ts b/packages/react-native-audio-api/src/index.native.ts index e276dfde..ac281138 100644 --- a/packages/react-native-audio-api/src/index.native.ts +++ b/packages/react-native-audio-api/src/index.native.ts @@ -9,6 +9,7 @@ export { default as AudioBufferSourceNode } from './core/AudioBufferSourceNode'; export { default as AudioContext } from './core/AudioContext'; export { default as AudioDestinationNode } from './core/AudioDestinationNode'; export { default as AudioNode } from './core/AudioNode'; +export { default as AnalyserNode } from './core/AnalyserNode'; export { default as AudioParam } from './core/AudioParam'; export { default as AudioScheduledSourceNode } from './core/AudioScheduledSourceNode'; export { default as BaseAudioContext } from './core/BaseAudioContext'; diff --git a/packages/react-native-audio-api/src/index.ts b/packages/react-native-audio-api/src/index.ts index f9dfe049..1531f7a8 100644 --- a/packages/react-native-audio-api/src/index.ts +++ b/packages/react-native-audio-api/src/index.ts @@ -110,6 +110,40 @@ export class AudioNode { } } +export class AnalyserNode extends AudioNode { + readonly fftSize: number; + readonly frequencyBinCount: number; + readonly minDecibels: number; + readonly maxDecibels: number; + readonly smoothingTimeConstant: number; + + constructor(context: AudioContext, node: globalThis.AnalyserNode) { + super(context, node); + + this.fftSize = node.fftSize; + this.frequencyBinCount = node.frequencyBinCount; + this.minDecibels = node.minDecibels; + this.maxDecibels = node.maxDecibels; + this.smoothingTimeConstant = node.smoothingTimeConstant; + } + + public getByteFrequencyData(array: Uint8Array): void { + (this.node as globalThis.AnalyserNode).getByteFrequencyData(array); + } + + public getByteTimeDomainData(array: Uint8Array): void { + (this.node as globalThis.AnalyserNode).getByteTimeDomainData(array); + } + + public getFloatFrequencyData(array: Float32Array): void { + (this.node as globalThis.AnalyserNode).getFloatFrequencyData(array); + } + + public getFloatTimeDomainData(array: Float32Array): void { + (this.node as globalThis.AnalyserNode).getFloatTimeDomainData(array); + } +} + export class AudioScheduledSourceNode extends AudioNode { private hasBeenStarted: boolean = false; diff --git a/packages/react-native-audio-api/src/interfaces.ts b/packages/react-native-audio-api/src/interfaces.ts index 58a7637c..c6b09e0c 100644 --- a/packages/react-native-audio-api/src/interfaces.ts +++ b/packages/react-native-audio-api/src/interfaces.ts @@ -136,3 +136,16 @@ export interface IAudioParam { } export interface IPeriodicWave {} + +export interface IAnalyserNode extends IAudioNode { + fftSize: number; + readonly frequencyBinCount: number; + minDecibels: number; + maxDecibels: number; + smoothingTimeConstant: number; + + getFloatFrequencyData: (array: number[]) => void; + getByteFrequencyData: (array: number[]) => void; + getFloatTimeDomainData: (array: number[]) => void; + getByteTimeDomainData: (array: number[]) => void; +}