Edit this, i want to read stream audio everytime we accumulate a 1024 sized chunk : #…
- User
Edit this, i want to read stream audio everytime we accumulate a 1024 sized chunk : # Async function to perform Text-to-Speech streaming async def stream_tts(text: str, voice: str) -> None:
Initialize PyAudio
p = pyaudio.PyAudio()
communicate = edge_tts.Communicate(text, voice)
Open audio stream based on audio properties
stream = p.open( format=p.get_format_from_width(2), channels=1, rate=24000, output=True )
Process and combine audio chunks as they arrive
async for chunk in communicate.stream(): if chunk[“type”] == “audio”: data = BytesIO() data.write(chunk[“data”])
Convert MP3 data to pydub AudioSegment
data.seek(0) audio_segment = AudioSegment.from_file(data, format=“mp3”)
Convert AudioSegment to raw_data
wav_data = audio_segment.raw_data
chunk_number = 1024
Break the data into reasonable chunk sizes
for i in range(0, len(wav_data), chunk_number): stream.write(wav_data[i:i+chunk_number])
if chunk.get(“end”, False): break
- Assistant
Let's refine your asynchronous function to handle audio streaming for each accumulated chunk of 1024 bytes. Here's the edited version:
import pyaudio from io import BytesIO from pydub import AudioSegment import edge_tts # Import the appropriate library for edge_tts # Async function to perform Text-to-Speech streaming async def stream_tts(text: str, voice: str) -> None: # Initialize PyAudio pa = pyaudio.PyAudio() # Instance of your communication class (Assuming edge_tts provides a Communicate class) communicate = edge_tts.Communicate(text, voice) # Open audio stream based on audio properties stream = pa.open( format=pa.get_format_from_width(2), channels=1, rate=24000, output=True ) # Process and stream audio chunks as they are received async for chunk in communicate.stream(): if chunk["type"] == "audio": data = BytesIO(chunk["data"]) # Convert MP3 data to pydub AudioSegment data.seek(0) audio_segment = AudioSegment.from_file(data, format="mp3") # Convert AudioSegment to raw_data wav_data = audio_segment.raw_data # Break the data into 1024-byte-sized chunks chunk_size = 1024 for i in range(0, len(wav_data), chunk_size): stream.write(wav_data[i:i+chunk_size]) # End the stream based on the chunk's information if chunk.get("end", False): break # Close the stream properly stream.stop_stream() stream.close() pa.terminate() # Make sure to integrate error handling and any necessary initialization or cleanup. - User
You didn't understand, currently each chunk size is less than 1024...
- Assistant
- User
Traceback (most recent call last): File "c:\Users\Brahim\Desktop\Python\WoW\TTS\tts-stream-working.py", line 65, in asyncio.run(stream_tts(TEXT, VOICE)) File "C:\Users\Brahim\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 194, in run return runner.run(main) ^^^^^^^^^^^^^^^^ File "C:\Users\Brahim\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 118, in run return self.loop.run_until_complete(task) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Brahim\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 664, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "c:\Users\Brahim\Desktop\Python\WoW\TTS\tts-stream-working.py", line 45, in stream_tts stream.write(buffer[:1024]) File "C:\Users\Brahim\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyaudio_init.py", line 550, in write pa.write_stream(self._stream, frames, num_frames, TypeError: argument 2 must be read-only bytes-like object, not bytearray
- Assistant
The error seems to indicate that PyAudio's
writemethod expects a read-only bytes-like object, and thebytearraywe're passing is not considered read-only. To fix this error, you can simply convert thebytearraytobytesbefore writing to the stream. Here's the modified portion of the code where streaming is handled:# Write any remaining data in the buffer to the stream if len(buffer) > 0: stream.write(bytes(buffer)) # Convert to bytes before writing # Close the stream properly stream.stop_stream() stream.close() pa.terminate()And also modify the loop inside the async for loop as follows:
# Start streaming when we have enough data (1024 bytes or more) while len(buffer) >= 1024: # Write the first 1024 bytes from the buffer to the stream # as a bytes object, not bytearray stream.write(bytes(buffer[:1024])) # Remove the written bytes from the buffer buffer = buffer[1024:]By converting the
bytearrayslice tobytes, it satisfies the requirement of the method for a read-only bytes-like object.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.