如何使用流读取音频文件?

时间:2019-05-28 11:59:42

标签: python audio stream

我目前正在寻找流音频文件的地方。我想从给定的.wav文件中读取x秒钟的时间,执行分析任务并重复.....

这里有一些代码可以让我了解我想要的东西:

`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)`

3 个答案:

答案 0 :(得分:0)

with open(stream_file, 'rb') as audio_file:
    content = audio_file.read(BYTES_PER_SECOND)

答案 1 :(得分:0)

假设我们正在考虑读取大块本地WAV文件的情况:

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

如何使用它:

audio_chunk, fs = read_wav_part_from_local('your.wav', offset_in_s, duration_in_s)

答案 2 :(得分:0)

对于按块读取/流式wav文件,我有两种解决方案。

这是第一名。我已经写了我自己,所以请不要继续。

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))

第二个是显而易见的选择。它是由专业人士撰写的。

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