This code stream play audio that are shushing and buzzing, with no voice, fix it : def…
- User
This code stream play audio that are shushing and buzzing, with no voice, fix it : def _play_chunk(self, chunk): """ Plays a chunk of audio data.
codeArgs: chunk: Chunk of audio data to be played. """ # handle mpeg if self.audio_stream.config.format == pyaudio.paCustomFormat: # convert to pcm using pydub segment = AudioSegment.from_file(io.BytesIO(chunk), format="mp3") chunk = segment.raw_data sub_chunk_size = 1024 for i in range(0, len(chunk), sub_chunk_size): sub_chunk = chunk[i:i + sub_chunk_size] if not self.muted: self.audio_stream.stream.write(sub_chunk) if self.on_audio_chunk: self.on_audio_chunk(sub_chunk) if not self.first_chunk_played and self.on_playback_start: self.on_playback_start() self.first_chunk_played = True # Pause playback if the event is set while self.pause_event.is_set(): time.sleep(0.01) if self.immediate_stop.is_set(): breakHere's my other code for context :
import asyncio import pyaudio from audio_player import AudioConfiguration, StreamPlayer import edge_tts import logging
from io import BytesIO from pydub import AudioSegment import queue import threading
Other utility imports (e.g., time, io) may be required depending on your environment
def format_pitch(pitch): return f"{pitch:+.0f}Hz"
def format_percent(percent): return f"{percent:+.0f}%"
async def generate_and_fill_buffer(text, voice, speed=0, pitch=0, volume=0, audio_buffer=None): communicate = edge_tts.Communicate(text, voice, rate=format_percent(speed), pitch=format_pitch(pitch), volume=format_percent(volume)) try: async for chunk in communicate.stream(): if chunk["type"] == "audio": audio_buffer.put(chunk["data"])
code - Assistant
The provided code samples include a playback routine for streaming audio chunks and a main routine for using Edge TTS to generate speech and feed it into an audio buffer. The method is responsible for playing the said chunks. If the audio stream is only producing shushing and buzzing noises with no voice, it could suggest an issue with the audio format or conversion.
- User
DO THE JOB!
- Assistant
- User
Modify the following script to fix it : “”“Plays a chunk of audio data.Args:chunk: Chunk of audio data to be played.“””# handle mpegif self.audio_stream.config.format == pyaudio.paCustomFormat:# convert to pcm using pydubsegment = AudioSegment.from_file(io.BytesIO(chunk), format=“mp3”)chunk = segment.raw_datasub_chunk_size = 1024for i in range(0, len(chunk), sub_chunk_size):sub_chunk = chunk[i:i + sub_chunk_size]if not self.muted:self.audio_stream.stream.write(sub_chunk)if self.on_audio_chunk:self.on_audio_chunk(sub_chunk)if not self.first_chunk_played and self.on_playback_start:self.on_playback_start()self.first_chunk_played = True# Pause playback if the event is setwhile self.pause_event.is_set():time.sleep(0.01)if self.immediate_stop.is_set():break
- Assistant