discordjs / opus

Native opus bindings for node
MIT License
188 stars 53 forks source link

Cannot decode opus that came from openAI (Data corrupted) #149

Open MatthD opened 7 months ago

MatthD commented 7 months ago

Please describe the problem you are having in as much detail as possible:

Trying to decode opus data that come from open-ai tts

Include a reproducible code sample here, if possible:

import fs from "fs";
import path from "path";
import "openai/shims/node";
import OpenAI from "openai";
import { OpusEncoder } from "@discordjs/opus";

const openai = new OpenAI({
  apiKey: "***" // replace this with open-ai key,
});

const speechFile = path.resolve("./speech.ogg");
const speechFilePCM = path.resolve("./speech.pcm");

async function main() {
  const opusEncoder = new OpusEncoder(24000, 1);
  const opus = await openai.audio.speech.create({
    response_format: "opus",
    model: "tts-1",
    voice: "alloy",
    input:
      Hello I am a human trying to speech my voice",
  });
  const buffer = Buffer.from(await opus.arrayBuffer());
  const decoded = await fs.promises.writeFile(speechFile, buffer);

  const testDecodedPCM = opusEncoder.decode(buffer); // FAIL HERE
  // const decodedPCM = await fs.promises.writeFile(speechFilePCM, testDecodedPCM);
}

main();

Further details:

Qjuh commented 7 months ago

Are you sure the opus from OpenAI is encoded as 24000 bitrate?