mirror of
https://github.com/musix-org/musix-oss
synced 2024-12-23 13:03:18 +00:00
40 lines
1.1 KiB
Markdown
40 lines
1.1 KiB
Markdown
|
opusscript
|
||
|
==========
|
||
|
### JS bindings for libopus 1.2.1, ported with emscripten
|
||
|
|
||
|
```js
|
||
|
var opusscript = require("opusscript");
|
||
|
|
||
|
// 48kHz sampling rate, 20ms frame duration, stereo audio (2 channels)
|
||
|
var samplingRate = 48000;
|
||
|
var frameDuration = 20;
|
||
|
var channels = 2;
|
||
|
|
||
|
// Optimize encoding for audio. Available applications are VOIP, AUDIO, and RESTRICTED_LOWDELAY
|
||
|
var encoder = new opusscript(samplingRate, channels, opusscript.Application.AUDIO);
|
||
|
|
||
|
var frameSize = samplingRate * frameDuration / 1000;
|
||
|
|
||
|
// Get PCM data from somewhere and encode it into opus
|
||
|
var pcmData = new Buffer(pcmSource);
|
||
|
var encodedPacket = encoder.encode(pcmData, frameSize);
|
||
|
|
||
|
// Decode the opus packet back into PCM
|
||
|
var decodedPacket = encoder.decode(encodedPacket);
|
||
|
|
||
|
// Delete the encoder when finished with it (Emscripten does not automatically call C++ object destructors)
|
||
|
encoder.delete();
|
||
|
```
|
||
|
|
||
|
#### TypeScript
|
||
|
|
||
|
Since this module wasn't written for TypeScript, you need to use `import = require` syntax.
|
||
|
|
||
|
```ts
|
||
|
// Import using:
|
||
|
import OpusScript = require('opusscript');
|
||
|
|
||
|
// and NOT:
|
||
|
import OpusScript from 'opusscript';
|
||
|
```
|