> Discover all available pages from the documentation index: https://mastra.zisheng.pro/ja/llms.txt # voice.connect() `connect()` メソッドは、リアルタイムの音声間通信のために WebSocket または WebRTC 接続を確立します。`send()` や `answer()` など、ほかのリアルタイム機能を使用する前に呼び出す必要があります。 ## 使用例 ```typescript import { OpenAIRealtimeVoice } from '@mastra/voice-openai-realtime' import Speaker from '@mastra/node-speaker' const speaker = new Speaker({ sampleRate: 24100, // Audio sample rate in Hz - standard for high-quality audio on MacBook Pro channels: 1, // Mono audio output (as opposed to stereo which would be 2) bitDepth: 16, // Bit depth for audio quality - CD quality standard (16-bit resolution) }) // Initialize a real-time voice provider const voice = new OpenAIRealtimeVoice({ realtimeConfig: { model: 'gpt-5.1-realtime', apiKey: process.env.OPENAI_API_KEY, options: { sessionConfig: { turn_detection: { type: 'server_vad', threshold: 0.6, silence_duration_ms: 1200, }, }, }, }, speaker: 'alloy', // Default voice }) // Connect to the real-time service await voice.connect() // Now you can use real-time features voice.on('speaker', stream => { stream.pipe(speaker) }) // With connection options await voice.connect({ timeout: 10000, // 10 seconds timeout reconnect: true, }) ``` ## パラメーター **options** (`Record`): Provider 固有の接続オプション ## 戻り値 接続が正常に確立されると解決する `Promise` を返します。 ## Provider 固有のオプション 各リアルタイム Voice Provider が `connect()` メソッドでサポートするオプションは異なる場合があります。 ### OpenAI Realtime **options** (`Options`): 設定オプション。 **options.timeout** (`number`): 接続タイムアウト(ミリ秒) **options.reconnect** (`boolean`): 接続が失われたときに自動的に再接続するかどうか ## `CompositeVoice` での使用 `CompositeVoice` を使用する場合、`connect()` メソッドは設定されたリアルタイム Provider に処理を委譲します。 ```typescript import { CompositeVoice } from '@mastra/core/voice' import { OpenAIRealtimeVoice } from '@mastra/voice-openai-realtime' const realtimeVoice = new OpenAIRealtimeVoice() const voice = new CompositeVoice({ realtime: realtimeVoice, }) // This will use the OpenAIRealtimeVoice provider await voice.connect() ``` ## 注意事項 - このメソッドは、音声間通信をサポートするリアルタイム Voice Provider だけが実装します - この機能をサポートしない Voice Provider で呼び出すと、警告を記録して直ちに解決します - `send()` や `answer()` など、ほかのリアルタイムメソッドを使用する前に接続を確立する必要があります - Voice インスタンスの使用を終えたら、`close()` を呼び出してリソースを適切に解放してください - Provider の実装によっては、接続が失われたときに自動的に再接続する場合があります - 通常、接続エラーは例外としてスローされるため、キャッチして処理する必要があります ## 関連メソッド - [voice.send()](https://mastra.zisheng.pro/ja/reference/voice/voice.send):Voice Provider に音声データを送信します - [voice.answer()](https://mastra.zisheng.pro/ja/reference/voice/voice.answer):Voice Provider に応答を指示します - [voice.close()](https://mastra.zisheng.pro/ja/reference/voice/voice.close):リアルタイムサービスから切断します - [voice.on()](https://mastra.zisheng.pro/ja/reference/voice/voice.on):Voice イベントのイベントリスナーを登録します