Skip to main content

voice.connect()

connect() 方法用于建立 WebSocket 或 WebRTC 连接,以实现实时语音对语音通信。在使用其他实时功能(如 send()answer())之前,必须调用此方法。

🌐 The connect() method establishes a WebSocket or WebRTC connection for real-time speech-to-speech communication. This method must be called before using other real-time features like send() or answer().

使用示例
Direct link to 使用示例

🌐 Usage Example

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,
});

参数
Direct link to 参数

🌐 Parameters

options?:

Record<string, unknown>
Provider-specific connection options

返回值
Direct link to 返回值

🌐 Return Value

返回一个 Promise<void>,当连接成功建立时会被解析。

🌐 Returns a Promise<void> that resolves when the connection is successfully established.

特定于提供商的选项
Direct link to 特定于提供商的选项

🌐 Provider-Specific Options

每个实时语音提供商可能对 connect() 方法支持不同的选项:

🌐 Each real-time voice provider may support different options for the connect() method:

OpenAI 实时
Direct link to OpenAI 实时

🌐 OpenAI Realtime

options.timeout?:

number
= 30000
Connection timeout in milliseconds

options.reconnect?:

boolean
= false
Whether to automatically reconnect on connection loss

与复合语音一起使用
Direct link to 与复合语音一起使用

🌐 Using with CompositeVoice

在使用 CompositeVoice 时,connect() 方法会委托给配置的实时提供程序:

🌐 When using CompositeVoice, the connect() method delegates to the configured real-time provider:

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();

注意
Direct link to 注意

🌐 Notes

  • 该方法仅由支持语音到语音功能的实时语音提供商实现
  • 如果调用不支持此功能的语音提供商,它将记录一条警告并立即完成。
  • 在使用其他实时方法如 send()answer() 之前,必须先建立连接
  • 完成语音实例后,调用 close() 来正确清理资源
  • 一些提供商可能会在连接丢失时自动重新连接,这取决于它们的实现方式
  • 连接错误通常会作为异常抛出,应该被捕获和处理

🌐 Related Methods