Add types for audio-play and audio-context (#38119)

* Add types for audio-context and audio-play

* Test fixes

* More test fixes

* PR comments

* Remove excessive exporting
This commit is contained in:
Jeff Peterson 2019-09-06 16:25:07 -07:00 committed by Andrew Casey
parent b376aac021
commit 0f48f35cd2
8 changed files with 146 additions and 0 deletions

View File

@ -0,0 +1,11 @@
import getContext = require("audio-context");
const context = getContext();
const myContext = getContext(22000);
const yourOptions: getContext.Options = {
sampleRate: 44000,
offline: true,
length: 3000,
channels: 2
};
const yourContext = getContext(yourOptions);

34
types/audio-context/index.d.ts vendored Normal file
View File

@ -0,0 +1,34 @@
// Type definitions for audio-context 1.0
// Project: https://github.com/audiojs/audio-context
// Definitions by: Jeff Peterson <https://github.com/bdjeffyp>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
/**
* Options for your audio context:
* @param sampleRate if specified, will set the context sampleRate.
* @param latencyHint if specified, will control latency. One of 'balanced', 'playback', 'interaction' (default) or number.
* @param offline if specified, will create OfflineAudioContext.
* @param length if specified, will set number of frames for offline context.
* @param channels if specified, will set number of channels for offline context.
* @param contextAttributes any other options for the context.
*/
declare namespace getContext {
interface Options {
sampleRate?: number;
latencyHint?: string | number;
offline?: boolean;
length?: number;
channels?: number;
contextAttributes?: object;
}
}
/**
* Gets an audio context from your web browser.
* @param options Takes an Options object or just provide a sample rate.
* @returns the audio context or null if there was an error or not a web browser.
*/
declare function getContext(options?: getContext.Options | number): AudioContext | null;
export = getContext;

View File

@ -0,0 +1,24 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"audio-context-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

View File

@ -0,0 +1,6 @@
import audioPlay = require("audio-play");
const buffer = new AudioBuffer({length: 2, sampleRate: 22000});
const thisSound = audioPlay(buffer, { autoplay: true }, () => console.log('stopped!'));
thisSound.pause();
thisSound.play();

44
types/audio-play/index.d.ts vendored Normal file
View File

@ -0,0 +1,44 @@
// Type definitions for audio-play 2.2
// Project: https://github.com/audiojs/audio-play
// Definitions by: Jeff Peterson <https://github.com/bdjeffyp>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.0
/**
* Creates and plays/pauses a sound effect or music.
* @param buffer Handle to the audio buffer created by an audio loader
* @param how An options object that defines parameters for playback of the audio buffer
* @param cb A callback that is executed when playback stops
* @returns a handle to the player, which can then call pause and play functions
*/
declare function audioPlay(buffer: AudioBuffer, how: audioPlay.Options, cb: () => void): audioPlay.AudioPlayHandle;
declare namespace audioPlay {
interface AudioPlayHandle {
play: () => any;
pause: () => any;
}
/**
* Various options for audio playback
* @param start The timestamp at which to start the audio. Can be negative to start from end. (Default: 0)
* @param end The timestamp the audio ends at. (Default: length of audio buffer)
* @param autoplay Plays back the audio immediately upon loading. (Default: false)
* @param loop Continuously loops the buffer until paused. (Default: false)
* @param context Handle to an audio context. If not provided, one is provided for you.
* @param volume (not implemented) Playback the audio at a percentage of full volume. (Default: 1)
* @param detune (not implemented) Percentage of fine-tuning. (Default: 0)
* @param rate (not implemented) Playback rate, in percent, of the audio. (Default: 1)
*/
interface Options {
start?: number;
end?: number;
autoplay?: boolean;
loop?: boolean;
rate?: number;
detune?: number;
volume?: number;
context?: AudioContext;
}
}
export = audioPlay;

View File

@ -0,0 +1,25 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true
},
"files": [
"index.d.ts",
"audio-play-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }