Someone said this, learn it : The primary issue is that MP3 codecs used by ffmpeg add…
- User
Someone said this, learn it : The primary issue is that MP3 codecs used by ffmpeg add silence to the end of the encoded audio (and your approach is producing multiple individual audio files).
If possible, use a lossless format like wave and then reduce the file size with gzip or similar. You may also be able to use lossless audio compression (for example, flac) but it probably depends on how the encoder works.
I don’t have a conclusive explanation for the audible artifacts you’re hearing, but it could be that you’re splitting the audio at a point where the signal is non-zero. If a sound begins with a sample with a value of 100 (for example), that would sound like a digital popping sound. The MP3 compression may also alter the sound though, especially at lower bit rates. If this is the issue, a 1ms fade in will eliminate the pop without a noticeable audible “fade” (though potentially introduce other artifacts) - a longer fade in (like 20 or 50 ms would avoid strange frequency domain artifacts but would introduce noticeable a “fade in”.
If you’re willing to do a little more (coding) work, you can search for a “zero crossing” (basically, a place where the signal is at a zero point naturally) and split the audio there.
Probably the best approach if it’s possible:
Encode the entire signal as a single, compressed file, and send the bytes (of that one file) down to the client in chunks for playback as a single stream. If you use constant bitrate mp3 encoding (CBR) you can send almost perfectly 1 second long chunks just by counting bytes. e.g., with 256kbps CBR, just send 256 KB at a time.
My code with cracking audio problem : import asyncio import edge_tts import pyaudio from io import BytesIO from pydub import AudioSegment
TEXT = “aHello World!” VOICE = “en-GB-SoniaNeural”
p = pyaudio.PyAudio()
async def stream_tts(text: str, voice: str) -> None:
We’re assuming a certain format, channels, and rate
This will need to be dynamic based on the actual audio data from TTS
stream = p.open( format=pyaudio.paInt16, channels=1, rate=26000, output=True )
communicate = edge_tts.Communicate(text, voice)
Process and play audio chunks as they arrive
async for chunk in communicate.stream(): if chunk[“type”] == “audio”: try: audio_segment = AudioSegment.from_file(BytesIO(chunk[“data”]), format=“mp3”)
Write data to the stream directly without extra buffering
stream.write(audio_segment.raw_data)
If this is the last chunk, break after playing
if chunk.get(‘end’, False): break except Exception as e: print(“Error processing audio chunk:”, e)
Cleanup
stream.stop_stream() stream.close() p.terminate()
if name == “main”:
Run the asyncio event loop
asyncio.run(stream_tts(TEXT, VOICE))
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.