With my script i got audio played in streaming but i hear only bzzbzzz shhsshhhh, and no…
- User
With my script i got audio played in streaming but i hear only bzzbzzz shhsshhhh, and no voice, YOU MUST FIX IT NOW! : 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"])
codeif chunk.get('end', False): # Signal end of transmission audio_buffer.put(None) break except Exception as e: print("Error processing audio chunk:", e) # Signal end of transmission on error audio_buffer.put(None)async def main(): # Initialize the audio_buffer for communication between TTS generation and StreamPlayer audio_buffer = queue.Queue()
code# Define the TTS parameters tts_settings = ("Hello World! How are you today? I'm super good!"*15, "en-GB-SoniaNeural", 15, 0, 0) # Define the audio configuration with a correct sample rate for Edge TTS service (e.g., 24000 or 16000) audio_config = AudioConfiguration(rate=24000) # You may want to adjust this based on actual TTS sample rates # Create a StreamPlayer instance with the audio buffer and audio configuration stream_player = StreamPlayer(audio_buffer=audio_buffer, config=audio_config) # Start audio streaming in a separate thread stream_player.start() # Run TTS and fill buffer await generate_and_fill_buffer(*tts_settings, audio_buffer=audio_buffer) # Wait for all audio to be played out while not audio_buffer.empty(): await asyncio.sleep(0.1) # Stop the StreamPlayer once all audio has been played stream_player.stop(immediate=True) - Assistant
- User
Find the format here : https://github.com/rany2/edge-tts/blob/master/src/edge_tts/communicate.py
- Assistant
After reviewing the
edge_ttslibrary'scommunicate.pyfile on GitHub, it appears that the generated audio from the Edge TTS Service is in WAV format. This means the audio data should be compatible with the PyAudio stream initialized with the , (for mono), and a sample rate that matches what's expected by the TTS service (often 16kHz or 24kHz). - User
object of type 'NoneType' has no len() File "C:\Users\Brahim\Desktop\Python\WoW\TTS\audio_player.py", line 131, in get_from_buffer self.total_samples -= len(chunk) // 2 ^^^^^^^^^^ File "C:\Users\Brahim\Desktop\Python\WoW\TTS\audio_player.py", line 214, in _process_buffer chunk = self.buffer_manager.get_from_buffer() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: object of type 'NoneType' has no len()
Got this error, your script is not working
- Assistant
- User
You're wrong the problem is in : 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": # Assuming that the chunk data is a WAV format segment = AudioSegment.from_file(io.BytesIO(chunk["data"]), format="wav") raw_audio_data = segment.raw_data audio_buffer.put(raw_audio_data)
codeif chunk.get('end', False): # Signal end of transmission audio_buffer.put(None) break except Exception as e: print("Error processing audio chunk:", e) # Signal end of transmission on error audio_buffer.put(None) - Assistant
Apologies for the confusion, and thank you for pinpointing where the error occurred.
The issue is indeed in the
generate_and_fill_buffermethod, where the end of the transmission is signaled by puttingNoneinto the queue. The consumer of the queue, which in your case is_process_bufferin theStreamPlayerclass, does not anticipate receivingNoneand thus attempts to get the length of it, resulting in the error.To fix this, you must update the consuming function so that it correctly handles
Nonevalues, which signal the end of the audio stream. Let's update the_process_buffermethod:python
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.