nice-advantage-11558
08/07/2025, 3:54 PMtall-airline-16756
08/07/2025, 3:54 PMtall-airline-16756
08/07/2025, 3:55 PMnice-advantage-11558
08/07/2025, 3:55 PMtall-airline-16756
08/07/2025, 3:56 PMaudio_data = np.frombuffer(frame.data, dtype=np.int16)
audio_data = audio_data * 1.5 # Feel free to change the multiplier as needed
adjusted_frame = rtc.AudioFrame(
data=audio_data.astype(np.int16).tobytes(),
sample_rate=frame.sample_rate,
num_channels=frame.num_channels,
samples_per_channel=len(audio_data) // frame.num_channels,
)
2. Client-Side Volume Adjustment:
• You can also use Web Audio APIs to amp up the audio before it reaches the STT system:
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
const gainNode = audioContext.createGain();
// Connect the audio stream to the gain node
const source = audioContext.createMediaStreamSource(stream);
source.connect(gainNode);
gainNode.connect(audioContext.destination);
// Set the gain (volume)
gainNode.gain.setValueAtTime(1.5, 0); // 1.5x volume gain
Just a heads-up: make sure the volume boost doesn’t cause any clipping or distortion. It’s a good idea to test and find that sweet spot for the best STT accuracy. If you need any help tweaking it, I’m here for you!nice-advantage-11558
08/07/2025, 3:56 PMtall-airline-16756
08/07/2025, 3:57 PMclass MyAgent(Agent):
def __init__(self, *, gain_db: float = 0.0) -> None:
super().__init__()
self.gain_db = gain_db
async def realtime_audio_output_node(
self, audio: AsyncIterable[rtc.AudioFrame], model_settings: ModelSettings
) -> AsyncIterable[rtc.AudioFrame]:
return self._apply_gain(
Agent.default.realtime_audio_output_node(self, audio, model_settings)
)
async def _apply_gain(
self, audio: AsyncIterable[rtc.AudioFrame]
) -> AsyncIterable[rtc.AudioFrame]:
async for frame in audio:
yield self._process_audio(frame)
def _process_audio(self, frame: rtc.AudioFrame) -> rtc.AudioFrame:
audio_data = np.frombuffer(frame.data, dtype=np.int16).astype(np.float32)
audio_data *= 10 ** (self.gain_db / 20) # Convert dB to amplitude ratio
audio_data = np.clip(audio_data, -32768, 32767).astype(np.int16)
return rtc.AudioFrame(
data=audio_data.tobytes(),
sample_rate=frame.sample_rate,
num_channels=frame.num_channels,
samples_per_channel=frame.samples_per_channel,
)
Here’s how it works:
• First, set the gain you want in decibels (for example, gain_db=3.0
to boost by +3dB).
• The code converts that dB value into an amplitude ratio and multiplies it with the audio data to apply the gain.
• It also takes care of clipping the audio so it stays within the valid 16-bit integer range, avoiding distortion.
If you’d like, you can even add a dynamic control to adjust the gain on the fly while it’s running. Just let me know if you want help with that or anything else!nice-advantage-11558
08/07/2025, 3:58 PMtall-airline-16756
08/07/2025, 3:58 PMrealtime_audio_output_node
method. Here’s a simple example to get you started:
class MyAgent(Agent):
def __init__(self, *, gain_factor: float = 2.0) -> None:
super().__init__()
self.gain_factor = gain_factor
async def realtime_audio_output_node(
self, audio: AsyncIterable[rtc.AudioFrame], model_settings: ModelSettings
) -> AsyncIterable[rtc.AudioFrame]:
async for frame in audio:
yield self._apply_gain(frame)
def _apply_gain(self, frame: rtc.AudioFrame) -> rtc.AudioFrame:
audio_data = np.frombuffer(frame.data, dtype=np.int16)
audio_data = (audio_data * self.gain_factor).astype(np.int16) # Apply gain
return rtc.AudioFrame(
data=audio_data.tobytes(),
sample_rate=frame.sample_rate,
num_channels=frame.num_channels,
samples_per_channel=frame.samples_per_channel,
)
Here’s how it works:
• Pick the gain_factor
you want (for example, gain_factor=2.0
will double the volume).
• The gain is applied by multiplying the audio data by this factor in the _apply_gain
method.
• Just a heads up: be careful with the gain factor to avoid any audio clipping or distortion.
If you want me to help tweak this or if you have any questions about your specific setup, just let me know—I’m here to help!nice-advantage-11558
08/07/2025, 9:42 PMtall-airline-16756
08/07/2025, 9:42 PMnice-advantage-11558
08/07/2025, 9:43 PMtall-airline-16756
08/07/2025, 9:44 PMnice-advantage-11558
08/07/2025, 9:44 PMtall-airline-16756
08/07/2025, 9:45 PM