-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreviewAudio.py
More file actions
30 lines (28 loc) · 749 Bytes
/
Copy pathpreviewAudio.py
File metadata and controls
30 lines (28 loc) · 749 Bytes
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
'''
Plays an array of audio signal. Blocking.
'''
import pyaudio
import numpy as np
PAGE_LEN = 4096
def previewAudio(signal, sr):
signal = np.float32(signal)
signal /= np.max(np.abs(signal))
pa = pyaudio.PyAudio()
stream = pa.open(
format = pyaudio.paFloat32, channels = 1, rate = sr,
output = True, frames_per_buffer = PAGE_LEN,
)
i = 0
while i < signal.size - PAGE_LEN:
stream.write(signal[i:i+PAGE_LEN], PAGE_LEN)
i += PAGE_LEN
last = signal[i:i+PAGE_LEN]
stream.write(
np.concatenate([
last,
np.zeros((PAGE_LEN - last.size, ), dtype = np.float32)
]), PAGE_LEN,
)
stream.stop_stream()
stream.close()
pa.terminate()