@@ -32,7 +32,7 @@ def remove_wav_bytes_header(wav_bytes: bytes):
32
32
return wav_file .get_body_data ()
33
33
34
34
35
- def read_np_to_wav (audio_data : np .ndarray ) -> bytes :
35
+ def covert_to_s16le (audio_data : np .ndarray ) -> bytes :
36
36
audio_data : np .ndarray = audio_data / np .max (np .abs (audio_data ))
37
37
audio_data = (audio_data * 32767 ).astype (np .int16 )
38
38
return audio_data .tobytes ()
@@ -104,8 +104,10 @@ async def enqueue_to_stream(self) -> AsyncGenerator[bytes, None]:
104
104
105
105
chunk_data = bytes ()
106
106
async for sample_rate , audio_data in self .enqueue_stream ():
107
- encoder .set_header (sample_rate = sample_rate )
108
- audio_bytes = read_np_to_wav (audio_data = audio_data )
107
+ encoder .set_header (
108
+ sample_rate = sample_rate , sample_width = audio_data .dtype .itemsize
109
+ )
110
+ audio_bytes = covert_to_s16le (audio_data = audio_data )
109
111
110
112
logger .debug (f"write audio_bytes len: { len (audio_bytes )} " )
111
113
encoder .write (audio_bytes )
@@ -151,8 +153,10 @@ async def enqueue_to_stream_join(self) -> AsyncGenerator[bytes, None]:
151
153
encoder = self .get_encoder ()
152
154
chunk_data = bytes ()
153
155
async for sample_rate , audio_data in self .enqueue_stream ():
154
- encoder .set_header (sample_rate = sample_rate )
155
- audio_bytes = read_np_to_wav (audio_data = audio_data )
156
+ encoder .set_header (
157
+ sample_rate = sample_rate , sample_width = audio_data .dtype .itemsize
158
+ )
159
+ audio_bytes = covert_to_s16le (audio_data = audio_data )
156
160
encoder .write (audio_bytes )
157
161
158
162
encoder .close ()
@@ -172,8 +176,10 @@ async def enqueue_to_bytes(self) -> bytes:
172
176
async with cancel_on_disconnect (self .current_request ):
173
177
try :
174
178
sample_rate , audio_data = await self .enqueue ()
175
- audio_bytes = read_np_to_wav (audio_data = audio_data )
176
- encoder .set_header (sample_rate = sample_rate )
179
+ audio_bytes = covert_to_s16le (audio_data = audio_data )
180
+ encoder .set_header (
181
+ sample_rate = sample_rate , sample_width = audio_data .dtype .itemsize
182
+ )
177
183
encoder .write (audio_bytes )
178
184
encoder .close ()
179
185
buffer = encoder .read_all ()
0 commit comments