Rantanen / node-opus

Opus bindings for Node.js
MIT License
79 stars 32 forks source link

"too large buffer" #84

Closed herenickname closed 5 years ago

herenickname commented 5 years ago

15360 node: ../../nan/nan.h:904: Nan::MaybeLocal Nan::CopyBuffer(const char*, uint32_t): Assertion `size <= imp::kMaxLength && "too large buffer"' failed. Aborted

console.log(rawAudio.byteLength)
const opusData = opus.encode(rawAudio)
Rantanen commented 5 years ago

node-opus is bindings to libopus (with some "easier to use from JS" benefits such as no need to pass the array length separately). You need to follow libopus requirements.

http://opus-codec.org/docs/opus_api-1.2/group__opus__encoder.html

[in] frame_size int: Number of samples per channel in the input signal. This must be an Opus frame size for the encoder's sampling rate. For example, at 48 kHz the permitted values are 120, 240, 480, 960, 1920, and 2880.
s4kh commented 4 years ago

So @ekifox how did you solve this?

herenickname commented 4 years ago

@s4kh I went the other way :)

import { OpusEncoder, Encoder as OpusEncoderStream } from 'node-opus'
import { Encoder as OggEncoder } from 'ogg'
import { WriteStream } from 'fs'

const opusEncoder = new OpusEncoder(48000)

export default function OpusFactory(audios: Buffer[], writeStream: WriteStream) {
    const opusEncodeStream = new OpusEncoderStream(48000, 1)
    const oggEncoder = new OggEncoder()

    opusEncodeStream.pipe(oggEncoder.stream())
    oggEncoder.pipe(writeStream)

    for (const part of audios) {
        const rawAudio = opusEncoder.decode(part)
        opusEncodeStream.write(rawAudio)
    }

    opusEncodeStream.end()
}