import { OpenAI } from "https://esm.town/v/yawnxyz/OpenAI";
import { fetch } from "https://esm.town/v/std/fetch";
import { getUrl } from "https://esm.town/v/yawnxyz/download";
import { blobby } from "https://esm.town/v/yawnxyz/blobby";
export const LANGUAGE_CODES = {
english: "en",
spanish: "es",
chinese: "zh",
french: "fr",
german: "de",
japanese: "ja",
portuguese: "pt",
russian: "ru",
italian: "it",
korean: "ko",
arabic: "ar",
hindi: "hi",
turkish: "tr",
vietnamese: "vi",
polish: "pl",
ukrainian: "uk",
thai: "th",
dutch: "nl",
indonesian: "id",
czech: "cs",
swedish: "sv",
romanian: "ro",
greek: "el",
hungarian: "hu",
danish: "da",
finnish: "fi",
norwegian: "no",
hebrew: "he",
malay: "ms",
filipino: "fil",
};
export class AudioManager {
constructor(apiKey=null, uploadFunction = null, downloadFunction = null) {
this.openai = new OpenAI(apiKey);
this.uploadFunction = uploadFunction || this.blobUpload;
}
async blobUpload(buffer, key) {
try {
console.log("[uploading audio to blob:]", key);
await blobby.upload(key, buffer);
return getUrl(key);
} catch (e) {
console.error("blobUpload errors:", e);
return null;
}
};
async speechToTranscript(audioUrl, options = {}) {
const buffer = await this.getAudioBuffer(audioUrl);
const defaultOptions = {
file: buffer,
model: "whisper-1",
};
const mergedOptions = { ...defaultOptions, ...options };
const transcription = await this.openai.audio.transcriptions.create(mergedOptions);
return transcription;
}
async speechToTranslation(audioUrl, options = {}) {
const buffer = await this.getAudioBuffer(audioUrl);
const defaultOptions = {
file: buffer,
model: "whisper-1",
};
const mergedOptions = { ...defaultOptions, ...options };
const translation = await this.openai.audio.translations.create(mergedOptions);
return translation;
}
async textToSpeech(text, options = {}) {
const defaultOptions = {
model: "tts-1",
voice: "alloy",
input: text,
};
const mergedOptions = { ...defaultOptions, ...options };
const speech = await this.openai.audio.speech.create(mergedOptions);
const arrayBuffer = await speech.arrayBuffer();
const blob = new Blob([arrayBuffer], { type: "audio/mpeg" });
return { speech, blob, arrayBuffer};
}