forked from googlesamples/assistant-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_helpers.py
411 lines (341 loc) · 12.9 KB
/
audio_helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# Copyright (C) 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Helper functions for audio streams."""
import array
import logging
import math
import time
import threading
import wave
import click
import sounddevice as sd
DEFAULT_AUDIO_SAMPLE_RATE = 16000
DEFAULT_AUDIO_SAMPLE_WIDTH = 2
DEFAULT_AUDIO_ITER_SIZE = 3200
DEFAULT_AUDIO_DEVICE_BLOCK_SIZE = 6400
DEFAULT_AUDIO_DEVICE_FLUSH_SIZE = 25600
def normalize_audio_buffer(buf, volume_percentage, sample_width=2):
"""Adjusts the loudness of the audio data in the given buffer.
Volume normalization is done by scaling the amplitude of the audio
in the buffer by a scale factor of 2^(volume_percentage/100)-1.
For example, 50% volume scales the amplitude by a factor of 0.414,
and 75% volume scales the amplitude by a factor of 0.681.
For now we only sample_width 2.
Args:
buf: byte string containing audio data to normalize.
volume_percentage: volume setting as an integer percentage (1-100).
sample_width: size of a single sample in bytes.
"""
if sample_width != 2:
raise Exception('unsupported sample width:', sample_width)
scale = math.pow(2, 1.0*volume_percentage/100)-1
# Construct array from bytes based on sample_width, multiply by scale
# and convert it back to bytes
arr = array.array('h', buf)
for idx in range(0, len(arr)):
arr[idx] = int(arr[idx]*scale)
buf = arr.tostring()
return buf
def align_buf(buf, sample_width):
"""In case of buffer size not aligned to sample_width pad it with 0s"""
remainder = len(buf) % sample_width
if remainder != 0:
buf += b'\0' * (sample_width - remainder)
return buf
class WaveSource(object):
"""Audio source that reads audio data from a WAV file.
Reads are throttled to emulate the given sample rate and silence
is returned when the end of the file is reached.
Args:
fp: file-like stream object to read from.
sample_rate: sample rate in hertz.
sample_width: size of a single sample in bytes.
"""
def __init__(self, fp, sample_rate, sample_width):
self._fp = fp
try:
self._wavep = wave.open(self._fp, 'r')
except wave.Error as e:
logging.warning('error opening WAV file: %s, '
'falling back to RAW format', e)
self._fp.seek(0)
self._wavep = None
self._sample_rate = sample_rate
self._sample_width = sample_width
self._sleep_until = 0
def read(self, size):
"""Read bytes from the stream and block until sample rate is achieved.
Args:
size: number of bytes to read from the stream.
"""
now = time.time()
missing_dt = self._sleep_until - now
if missing_dt > 0:
time.sleep(missing_dt)
self._sleep_until = time.time() + self._sleep_time(size)
data = (self._wavep.readframes(size)
if self._wavep
else self._fp.read(size))
# When reach end of audio stream, pad remainder with silence (zeros).
if not data:
return b'\x00' * size
return data
def close(self):
"""Close the underlying stream."""
if self._wavep:
self._wavep.close()
self._fp.close()
def _sleep_time(self, size):
sample_count = size / float(self._sample_width)
sample_rate_dt = sample_count / float(self._sample_rate)
return sample_rate_dt
def start(self):
pass
def stop(self):
pass
@property
def sample_rate(self):
return self._sample_rate
class WaveSink(object):
"""Audio sink that writes audio data to a WAV file.
Args:
fp: file-like stream object to write data to.
sample_rate: sample rate in hertz.
sample_width: size of a single sample in bytes.
"""
def __init__(self, fp, sample_rate, sample_width):
self._fp = fp
self._wavep = wave.open(self._fp, 'wb')
self._wavep.setsampwidth(sample_width)
self._wavep.setnchannels(1)
self._wavep.setframerate(sample_rate)
def write(self, data):
"""Write bytes to the stream.
Args:
data: frame data to write.
"""
self._wavep.writeframes(data)
def close(self):
"""Close the underlying stream."""
self._wavep.close()
self._fp.close()
def start(self):
pass
def stop(self):
pass
def flush(self):
pass
class SoundDeviceStream(object):
"""Audio stream based on an underlying sound device.
It can be used as an audio source (read) and a audio sink (write).
Args:
sample_rate: sample rate in hertz.
sample_width: size of a single sample in bytes.
block_size: size in bytes of each read and write operation.
flush_size: size in bytes of silence data written during flush operation.
"""
def __init__(self, sample_rate, sample_width, block_size, flush_size):
if sample_width == 2:
audio_format = 'int16'
else:
raise Exception('unsupported sample width:', sample_width)
self._audio_stream = sd.RawStream(
samplerate=sample_rate, dtype=audio_format, channels=1,
blocksize=int(block_size/2), # blocksize is in number of frames.
)
self._block_size = block_size
self._flush_size = flush_size
self._sample_rate = sample_rate
def read(self, size):
"""Read bytes from the stream."""
buf, overflow = self._audio_stream.read(size)
if overflow:
logging.warning('SoundDeviceStream read overflow (%d, %d)',
size, len(buf))
return bytes(buf)
def write(self, buf):
"""Write bytes to the stream."""
underflow = self._audio_stream.write(buf)
if underflow:
logging.warning('SoundDeviceStream write underflow (size: %d)',
len(buf))
return len(buf)
def flush(self):
if self._audio_stream.active and self._flush_size > 0:
self._audio_stream.write(b'\x00' * self._flush_size)
def start(self):
"""Start the underlying stream."""
if not self._audio_stream.active:
self._audio_stream.start()
def stop(self):
"""Stop the underlying stream."""
if self._audio_stream.active:
self._audio_stream.stop()
def close(self):
"""Close the underlying stream and audio interface."""
if self._audio_stream:
self.stop()
self._audio_stream.close()
self._audio_stream = None
@property
def sample_rate(self):
return self._sample_rate
class ConversationStream(object):
"""Audio stream that supports half-duplex conversation.
A conversation is the alternance of:
- a recording operation
- a playback operation
Excepted usage:
For each conversation:
- start_recording()
- read() or iter()
- stop_recording()
- start_playback()
- write()
- stop_playback()
When conversations are finished:
- close()
Args:
source: file-like stream object to read input audio bytes from.
sink: file-like stream object to write output audio bytes to.
iter_size: read size in bytes for each iteration.
sample_width: size of a single sample in bytes.
"""
def __init__(self, source, sink, iter_size, sample_width):
self._source = source
self._sink = sink
self._iter_size = iter_size
self._sample_width = sample_width
self._volume_percentage = 50
self._stop_recording = threading.Event()
self._source_lock = threading.RLock()
self._recording = False
self._playing = False
def start_recording(self):
"""Start recording from the audio source."""
self._recording = True
self._stop_recording.clear()
self._source.start()
def stop_recording(self):
"""Stop recording from the audio source."""
self._stop_recording.set()
with self._source_lock:
self._source.stop()
self._recording = False
def start_playback(self):
"""Start playback to the audio sink."""
self._playing = True
self._sink.start()
def stop_playback(self):
"""Stop playback from the audio sink."""
self._sink.flush()
self._sink.stop()
self._playing = False
@property
def recording(self):
return self._recording
@property
def playing(self):
return self._playing
@property
def volume_percentage(self):
"""The current volume setting as an integer percentage (1-100)."""
return self._volume_percentage
@volume_percentage.setter
def volume_percentage(self, new_volume_percentage):
self._volume_percentage = new_volume_percentage
def read(self, size):
"""Read bytes from the source (if currently recording).
"""
with self._source_lock:
return self._source.read(size)
def write(self, buf):
"""Write bytes to the sink (if currently playing).
"""
buf = align_buf(buf, self._sample_width)
buf = normalize_audio_buffer(buf, self.volume_percentage)
return self._sink.write(buf)
def close(self):
"""Close source and sink."""
self._source.close()
self._sink.close()
def __iter__(self):
"""Returns a generator reading data from the stream."""
while True:
if self._stop_recording.is_set():
return
yield self.read(self._iter_size)
@property
def sample_rate(self):
return self._source._sample_rate
@click.command()
@click.option('--record-time', default=5,
metavar='<record time>', show_default=True,
help='Record time in secs')
@click.option('--audio-sample-rate',
default=DEFAULT_AUDIO_SAMPLE_RATE,
metavar='<audio sample rate>', show_default=True,
help='Audio sample rate in hertz.')
@click.option('--audio-sample-width',
default=DEFAULT_AUDIO_SAMPLE_WIDTH,
metavar='<audio sample width>', show_default=True,
help='Audio sample width in bytes.')
@click.option('--audio-iter-size',
default=DEFAULT_AUDIO_ITER_SIZE,
metavar='<audio iter size>', show_default=True,
help='Size of each read during audio stream iteration in bytes.')
@click.option('--audio-block-size',
default=DEFAULT_AUDIO_DEVICE_BLOCK_SIZE,
metavar='<audio block size>', show_default=True,
help=('Block size in bytes for each audio device '
'read and write operation..'))
@click.option('--audio-flush-size',
default=DEFAULT_AUDIO_DEVICE_FLUSH_SIZE,
metavar='<audio flush size>', show_default=True,
help=('Size of silence data in bytes written '
'during flush operation'))
def main(record_time, audio_sample_rate, audio_sample_width,
audio_iter_size, audio_block_size, audio_flush_size):
"""Helper command to test audio stream processing.
- Record 5 seconds of 16-bit samples at 16khz.
- Playback the recorded samples.
"""
end_time = time.time() + record_time
audio_device = SoundDeviceStream(sample_rate=audio_sample_rate,
sample_width=audio_sample_width,
block_size=audio_block_size,
flush_size=audio_flush_size)
stream = ConversationStream(source=audio_device,
sink=audio_device,
iter_size=audio_iter_size,
sample_width=audio_sample_width)
samples = []
logging.basicConfig(level=logging.INFO)
logging.info('Starting audio test.')
stream.start_recording()
logging.info('Recording samples.')
while time.time() < end_time:
samples.append(stream.read(audio_block_size))
logging.info('Finished recording.')
stream.stop_recording()
stream.start_playback()
logging.info('Playing back samples.')
while len(samples):
stream.write(samples.pop(0))
logging.info('Finished playback.')
stream.stop_playback()
logging.info('audio test completed.')
stream.close()
if __name__ == '__main__':
main()