メインコンテンツへ移動

OpenAI

Mastra の OpenAIVoice クラスは、OpenAI モデルを使用した Text-to-Speech および Speech-to-Text 機能を提供します。

使用例
使用例への直接リンク

import { OpenAIVoice } from '@mastra/voice-openai'

// Initialize with default configuration using environment variables
const voice = new OpenAIVoice()

// Or initialize with specific configuration
const voiceWithConfig = new OpenAIVoice({
speechModel: {
name: 'tts-1-hd',
apiKey: 'your-openai-api-key',
},
listeningModel: {
name: 'whisper-1',
apiKey: 'your-openai-api-key',
},
speaker: 'alloy', // Default voice
})

// Convert text to speech
const audioStream = await voice.speak('Hello, how can I help you?', {
speaker: 'nova', // Override default voice
speed: 1.2, // Adjust speech speed
})

// Convert speech to text
const text = await voice.listen(audioStream, {
filetype: 'mp3',
})

設定
設定への直接リンク

コンストラクターオプション
コンストラクターオプションへの直接リンク

speechModel?:

OpenAIConfig
= { name: 'tts-1' }
Text-to-Speech 合成の設定。
OpenAIConfig

name?:

'tts-1' | 'tts-1-hd' | 'whisper-1'
モデル名。より高品質な音声には 'tts-1-hd' を使用します。

apiKey?:

string
OpenAI API キー。未指定の場合は OPENAI_API_KEY 環境変数を使用します。

listeningModel?:

OpenAIConfig
= { name: 'whisper-1' }
Speech-to-Text 認識の設定。
OpenAIConfig

name?:

'tts-1' | 'tts-1-hd' | 'whisper-1'
モデル名。より高品質な音声には 'tts-1-hd' を使用します。

apiKey?:

string
OpenAI API キー。未指定の場合は OPENAI_API_KEY 環境変数を使用します。

speaker?:

OpenAIVoiceId
= 'alloy'
音声合成に使用するデフォルトの Voice ID。

メソッド
メソッドへの直接リンク

speak()
speakへの直接リンク

OpenAI の Text-to-Speech モデルを使用してテキストを音声に変換します。

input:

string | NodeJS.ReadableStream
音声に変換するテキストまたはテキストストリーム。

options?:

Options
設定オプション。
Options

speaker?:

OpenAIVoiceId
音声合成に使用する Voice ID。

speed?:

number
発話速度の倍率。

戻り値:Promise<NodeJS.ReadableStream>

listen()
listenへの直接リンク

OpenAI の Whisper モデルを使用して音声を文字起こしします。

audioStream:

NodeJS.ReadableStream
文字起こしする音声ストリーム。

options?:

Options
設定オプション。
Options

filetype?:

string
入力ストリームの音声形式。

戻り値:Promise<string>

getSpeakers()
getspeakersへの直接リンク

使用可能な Voice オプションの配列を返します。各要素には次の値が含まれます。

voiceId:

string
Voice の一意な識別子

注意事項
注意事項への直接リンク

  • API キーは、コンストラクターオプションまたは OPENAI_API_KEY 環境変数で指定できます
  • tts-1-hd モデルはより高品質な音声を提供しますが、処理に時間がかかる場合があります
  • 音声認識は mp3、wav、webm など複数の音声形式をサポートします