opus icon indicating copy to clipboard operation
opus copied to clipboard

Cannot decode opus that came from openAI (Data corrupted)

Open MatthD opened this issue 2 years ago • 1 comments

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:

  • @discordjs/opus version: 0.9.0 Capture d’écran 2023-12-11 à 16 38 08

  • Node.js version: 16.20.1

  • Operating system: Mac os 14.1.2

  • Priority this issue should have – please be realistic and elaborate if possible:

MatthD avatar Dec 11 '23 15:12 MatthD

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

Qjuh avatar Dec 11 '23 16:12 Qjuh