This document explains how to use the Piper TTS API from other Visual Studio Code extensions.
To use the Piper TTS API in your extension, you need to:
- Add
sethmiller.piper-ttsas an extension dependency in yourpackage.json:
"extensionDependencies": [
"sethmiller.piper-tts"
]- Access the API in your extension's code:
import * as vscode from 'vscode';
// Type definition for the Piper TTS API
interface PiperTTSApi {
readText(text: string): Promise<void>;
stopPlayback(): void;
selectVoice(): Promise<void>;
}
async function usePiperTTS() {
// Get the Piper TTS extension
const piperExtension = vscode.extensions.getExtension('sethmiller.piper-tts');
if (!piperExtension) {
vscode.window.showErrorMessage('Piper TTS extension is not installed');
return;
}
// Activate the extension if it's not already activated
if (!piperExtension.isActive) {
await piperExtension.activate();
}
// Get the API from the extension
const piperApi = piperExtension.exports as PiperTTSApi;
// Now you can use the API
try {
await piperApi.readText('Hello, world!');
} catch (error) {
console.error('Error using Piper TTS:', error);
}
}The Piper TTS API provides the following methods:
Reads the provided text aloud using the currently selected voice.
text: The text to be read aloud- Returns a Promise that resolves when the text has been fully read or rejects if an error occurs
Stops any currently playing text-to-speech.
Opens a quick pick menu for the user to select a voice for text-to-speech.
- Returns a Promise that resolves when the user has selected a voice or dismissed the menu