본문으로 건너뛰기

음성.연결()

그만큼connect()메서드는 실시간 음성 간 통신을 위해 WebSocket 또는 WebRTC 연결을 설정합니다. 이 메소드는 다음과 같은 다른 실시간 기능을 사용하기 전에 호출되어야 합니다.send()또는answer().

사용예
사용예에 대한 직접 링크

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<string, unknown>
Provider별 연결 옵션

반환 값
반환 값에 대한 직접 링크

연결이 성공적으로 설정되면 완료되는 Promise<void>를 반환합니다.

공급자별 옵션
공급자별 옵션에 대한 직접 링크

각 실시간 음성 Provider는 다음과 같은 다양한 옵션을 지원할 수 있습니다.connect() method:

OpenAI 실시간
OpenAI 실시간에 대한 직접 링크

options?:

Options
구성 옵션입니다.
Options

timeout?:

number
밀리초 단위의 연결 제한 시간

reconnect?:

boolean
연결이 끊어졌을 때 자동으로 다시 연결할지 여부

함께 사용CompositeVoice
using-with-compositevoice에 대한 직접 링크

CompositeVoice를 사용하면 connect() 메서드가 구성된 실시간 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()

메모
메모에 대한 직접 링크

  • 이 메서드는 음성 간 통신 기능을 지원하는 실시간 음성 Provider에서만 구현됩니다.
  • 이 기능을 지원하지 않는 음성 Provider에서 호출하면 경고를 기록하고 즉시 완료됩니다.
  • send() 또는 answer()와 같은 다른 실시간 메서드를 사용하기 전에 연결을 설정해야 합니다.
  • 음성 인스턴스 사용을 마치면 리소스를 올바르게 정리하기 위해 close()를 호출하세요.
  • 일부 Provider는 구현에 따라 연결이 끊어졌을 때 자동으로 다시 연결할 수 있습니다.
  • 연결 오류는 일반적으로 포착하여 처리해야 하는 예외로 발생합니다.
  • 음성.전송(): 음성 제공자에게 오디오 데이터를 보냅니다.
  • 음성.답변(): 음성 제공자가 응답하도록 트리거합니다.
  • 음성.닫기(): 실시간 서비스 연결을 끊습니다.
  • 음성.온(): 음성 이벤트에 대한 이벤트 리스너를 등록합니다.