react-native-audio-api
Version:
react-native-audio-api provides system for controlling audio in React Native environment compatible with Web Audio API specification
123 lines (100 loc) • 3.37 kB
text/typescript
import type { ShareableWorkletCallback } from '../interfaces';
import { NativeAudioAPIModule } from '../specs';
// Hint: Copied from react-native-worklets
// Doesn't really matter what is inside, just need a unique type
interface WorkletRuntime {
__hostObjectWorkletRuntime: never;
readonly name: string;
}
interface IWorkletsModule {
makeShareableCloneRecursive: (
workletCallback: ShareableWorkletCallback
) => ShareableWorkletCallback;
createWorkletRuntime: (runtimeName: string) => WorkletRuntime;
}
class AudioAPIModule {
public supportedWorkletsVersion = ['0.6.0', '0.6.1'];
constructor() {
// Important! Verify and import worklets first
// otherwise the native module installation might crash
// if react-native-worklets is not imported before audio-api
this.
if (this.
return;
}
if (!NativeAudioAPIModule) {
throw new Error(
`Failed to install react-native-audio-api: The native module could not be found.`
);
}
NativeAudioAPIModule.install();
}
try {
const workletsPackage = require('react-native-worklets');
const workletsPackageJson = require('react-native-worklets/package.json');
this.
this.
this.
workletsPackageJson.version
);
if (this.
this.
}
return this.
} catch {
this.
return false;
}
}
return (
global.createAudioContext != null &&
global.createOfflineAudioContext != null &&
global.createAudioRecorder != null &&
global.createAudioDecoder != null &&
global.createAudioStretcher != null &&
global.AudioEventEmitter != null
);
}
get workletsModule(): IWorkletsModule | null {
return this.
}
/**
* Indicates whether react-native-worklets are installed in matching version,
* for usage with react-native-audio-api.
*/
get canUseWorklets(): boolean {
return this.
}
/** Returns the installed worklets version or 'unknown'. */
get workletsVersion(): string {
return this.
}
/**
* Indicates whether react-native-worklets are installed, regardless of
* version support. Useful for asserting compatibility.
*/
get areWorkletsAvailable(): boolean {
return this.
}
/**
* Indicates whether the installed react-native-worklets version is supported.
* Useful for asserting compatibility.
*/
get isWorkletsVersionSupported(): boolean {
// Note: if areWorkletsAvailable is true, canUseWorklets is equivalent to version check
return this.
}
public createAudioRuntime(): WorkletRuntime | null {
if (!this.
return null;
}
return this.
}
}
export default new AudioAPIModule();