-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp.py
More file actions
35 lines (26 loc) · 1.2 KB
/
Copy pathtemp.py
File metadata and controls
35 lines (26 loc) · 1.2 KB
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
import numpy as np
sample_rate = 16000
bytes_per_sample = 2
file_full = "test_full.wav"
file_trunc = "test_from30.wav"
start_time = 30 # seconds
def to_array(audio_data):
"""Convert audio data to numpy array"""
return np.frombuffer(audio_data, dtype=np.int16).astype(np.float32) / np.iinfo(np.int16).max
#read 10 frames starting from the 30 seconds mark of file_full using f.read
with open(file_full, 'rb') as f:
#read the first 44 bytes (WAV header)
header = f.read(44)
if not header.startswith(b'RIFF'):
f.seek(0) # Reset to start if not a valid WAV file
f.seek(start_time * sample_rate * bytes_per_sample)
chunk = f.read(10 * bytes_per_sample) # Read 10 seconds of audio
print(np.array2string(to_array(chunk), precision=6, suppress_small=True, separator=', '))
with open(file_trunc, 'rb') as f:
#read the first 44 bytes (WAV header)
header = f.read(44)
if not header.startswith(b'RIFF'):
f.seek(0) # Reset to start if not a valid WAV file
chunk = f.read(10 * bytes_per_sample) # Read 10 seconds of audio
#do not use scientific notation
print(np.array2string(to_array(chunk), precision=6, suppress_small=True, separator=', '))