JS script to use gpt audio generation model in your notes through templater

enjoy :slight_smile:

const fs = require('fs');

function generateUUID() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}

const audio = async (conf, dir) => {
    const openAIKey = conf.openai;
    
    document.body.style.cursor = "wait";
    const selection = window.getSelection().toString();
    const url = "https://api.openai.com/v1/audio/speech";
    
    const response = await fetch(url, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            "Authorization": `Bearer ${openAIKey}`
        },
        body: JSON.stringify({
            model: "tts-1",
            voice: "onyx",
            input: selection,
            response_format: "mp3"
        })
    });

    if (!response.ok) {
        const text = await response.text();
        throw new Error(`HTTP error! status: ${response.status} ${text}`);
    }

    const audioBuffer = await response.arrayBuffer();
    const audioTitle = generateUUID();
    const folder = dir.replace(/[^/]+$/, '');

    await app.vault.adapter.writeBinary(
        `${folder}/${audioTitle}.mp3`, 
        audioBuffer
    );

    document.body.style.cursor = "default";

    return `![[${audioTitle}.mp3]]`;
}

module.exports = audio;
3 Likes

Nice clean approach. Another approach using the client side OpenAI API: