Reputation: 549
I am currently looking for away to stream audio-files. I'd like to read x amounts of seconds from a given .wav file, do my analysis task and repeat.....
Here is some code to get the idea of what i want:
`read_x_seconds = 30
file_length_in_min = 15
for x in range(file_length_in_min * (60 / read_x_seconds)):
y, fs = librosa.core.load(FILENAME, offset=x * read_x_seconds,
duration=read_x_seconds)
do_analysis(y, fs)`
Upvotes: 1
Views: 2099
Reputation: 549
I have got two solutions for blockwise reading / streaming wav files.
This is number one. I have written it my self, so don't relay on it.
def stream_gen(path: str):
WINDOW_s = 10
HEADER = 44
bytes_per_sampling: int
samplerate: int
CHUNk: int
first_block = True
run = True
with open(path, 'rb') as stream:
data = stream.read(HEADER)
samplerate = int.from_bytes(data[24:28], byteorder='little')
bits_per_sampling = int.from_bytes(data[34:36], byteorder='little')
if bits_per_sampling == 16:
dtype = 'int16'
elif bits_per_sampling == 32:
dtype = 'int32'
else:
raise IOError()
CHUNK = WINDOW_s * samplerate * (bits_per_sampling // 8)
while run:
data = stream.read(CHUNK)
if data == b'':
break
yield(np.frombuffer(data, dtype=dtype))
Number two is the obvious one to chose. It was written by professionals.
def soundfile_gen(path):
window_s = 10
samplerate = sf.info(path).samplerate
blocksize = samplerate * window_s
block_gen = sf.blocks(path, blocksize=blocksize)
return block_gen
Upvotes: 0
Reputation: 11397
Assuming we're considering a case of reading a chunk of local WAV file:
import wave
import numpy as np
def read_wav_part_from_local(path: str, start_s: float, duration_s: float):
with wave.open(path, mode='rb') as wavread:
fs = wavread.getframerate()
start = int(start_s * fs)
duration = int(duration_s * fs)
wavread.setpos(start)
wav_bytes = wavread.readframes(duration)
if wavread.getsampwidth() == 2:
dtype = 'int16'
elif wavread.getsampwidth() == 4:
dtype = 'int32'
else:
raise NotImplemented('I give up!')
wav_array = np.frombuffer(wav_bytes, dtype=dtype)
return wav_array, fs
How to use it:
audio_chunk, fs = read_wav_part_from_local('your.wav', offset_in_s, duration_in_s)
Upvotes: 1
Reputation: 940
with open(stream_file, 'rb') as audio_file:
content = audio_file.read(BYTES_PER_SECOND)
Upvotes: 0