メインコンテンツへ移動

Speech-to-Text(STT)

Mastra の Speech-to-Text(STT)は、複数のサービス Provider で音声入力をテキストに変換するための標準化されたインターフェースを提供します。 STT により、音声対応アプリケーションは人の発話に応答できます。ハンズフリー操作に対応し、障害のあるユーザーのアクセシビリティを高め、より自然なインターフェースを提供します。

設定
設定への直接リンク

Mastra で STT を使用するには、Voice Provider の初期化時に listeningModel を指定します。これには次のようなパラメーターが含まれます。

  • name: 使用する STT モデル。
  • apiKey: 認証に使用する API キー。
  • Provider 固有のオプション: Voice Provider ごとに必要またはサポートされる追加オプション。

動作: これらのパラメーターはすべて任意です。使用する Voice Provider に応じて、Provider が用意するデフォルト設定を使用できます。

const voice = new OpenAIVoice({
listeningModel: {
name: 'whisper-1',
apiKey: process.env.OPENAI_API_KEY,
},
})

// If using default settings the configuration can be simplified to:
const voice = new OpenAIVoice()

利用可能な Provider
利用可能な Providerへの直接リンク

Mastra は複数の Speech-to-Text Provider をサポートしており、それぞれに異なる機能と長所があります。

  • OpenAI: Whisper モデルによる高精度な文字起こし
  • Azure: エンタープライズレベルの信頼性を備えた Microsoft の音声認識
  • ElevenLabs: 複数言語に対応する高度な音声認識
  • Google: 幅広い言語に対応する Google の音声認識
  • Cloudflare: 低遅延アプリケーション向けにエッジ最適化された音声認識
  • Deepgram: さまざまなアクセントを高精度で認識する AI 音声認識
  • Sarvam: インド諸語とそのアクセントに特化

各 Provider は個別のパッケージとして実装されており、必要に応じてインストールできます。

pnpm add @mastra/voice-openai@latest # Example for OpenAI

listen メソッドを使用する
listen メソッドを使用するへの直接リンク

STT の中心となる listen() メソッドは、音声をテキストに変換します。使用方法を次に示します。

import { Agent } from '@mastra/core/agent'
import { OpenAIVoice } from '@mastra/voice-openai'
import { getMicrophoneStream } from '@mastra/node-audio'

const voice = new OpenAIVoice()

const agent = new Agent({
id: 'voice-agent',
name: 'Voice Agent',
instructions: 'You are a voice assistant that provides recommendations based on user input.',
model: 'openai/gpt-5.6-sol',
voice,
})

const audioStream = getMicrophoneStream() // Assume this function gets audio input

const transcript = await agent.voice.listen(audioStream, {
filetype: 'm4a', // Optional: specify the audio file type
})

console.log(`User said: ${transcript}`)

const { text } = await agent.generate(
`Based on what the user said, provide them a recommendation: ${transcript}`,
)

console.log(`Recommendation: ${text}`)

音声ファイルを文字起こしする
音声ファイルを文字起こしするへの直接リンク

listen() メソッドは、マイクまたはファイルからの音声データストリームを受け取ります。音声ファイルを文字起こしする場合は createReadStream() を使用します。

import { createReadStream } from 'fs'
import path from 'path'

const audioFilePath = path.join(process.cwd(), 'agent.m4a')
const audioStream = createReadStream(audioFilePath)

const transcription = await agent.voice.listen(audioStream, {
filetype: 'm4a',
})

console.log(`Transcription: ${transcription}`)

Agent で利用できる Voice Provider の概要については、Mastra の Voiceを参照してください。