绘图中的音频文件长度不正确,以及python中audioplot上的注释段不正确覆盖

问题描述 投票:1回答:1

我正在按照本教程(https://github.com/amsehili/audio-segmentation-by-classification-tutorial/blob/master/multiclass_audio_segmentation.ipynb)尝试使用我自己的训练数据和样本重新创建可视化输出。

我的音频文件长31秒 :https://www.dropbox.com/s/qae2u5dnnp678my/test_hold.wav?dl=0 注释文件在这里: https://www.dropbox.com/s/gm9uu1rjettm3qr/hold.lst?dl=0 https://www.dropbox.com/s/b6z1gt8i63c8ted/tring.lst?dl=0 我试图在python中绘制音频文件波形,然后从该波形顶部的注释文件中突出显示该音频中的“hold”和“tring”部分。

大胆的波形如下:enter image description here

代码如下:

import wave
import pickle
import numpy as np
from sklearn.mixture import GMM
import librosa

import warnings
warnings.filterwarnings('ignore')
SAMPLING_RATE =16000
wfp = wave.open("/home/vivek/Music/test_hold.wav")
audio_data = wfp.readframes(-1)
width = wfp.getsampwidth()
wfp.close()

# data as numpy array will be used to plot signal
fmt = {1: np.int8 , 2: np.int16, 4: np.int32}
signal = np.array(np.frombuffer(audio_data, dtype=fmt[width]), dtype=np.float64)


%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.pylab as pylab
pylab.rcParams['figure.figsize'] = 24, 18

def plot_signal_and_segmentation(signal, sampling_rate, segments=[]):
    _time = np.arange(0., np.ceil(float(len(signal))) / sampling_rate, 1./sampling_rate )
    if len(_time) > len(signal):
        _time = _time[: len(signal) - len(_time)]

    pylab.subplot(211)

    for seg in segments:

        fc = seg.get("fc", "g")
        ec = seg.get("ec", "b")
        lw = seg.get("lw", 2)
        alpha = seg.get("alpha", 0.4)

        ts = seg["timestamps"]

        # plot first segmentation outside loop to show one single legend for this class
        p = pylab.axvspan(ts[0][0], ts[0][1], fc=fc, ec=ec, lw=lw, alpha=alpha, label = seg.get("title", ""))

        for start, end in ts[1:]:
            p = pylab.axvspan(start, end, fc=fc, ec=ec, lw=lw, alpha=alpha)


    pylab.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
            borderaxespad=0., fontsize=22, ncol=2)

    pylab.plot(_time, signal)

    pylab.xlabel("Time (s)", fontsize=22)
    pylab.ylabel("Signal Amplitude", fontsize=22)
    pylab.show()

annotations = {}


ts = [line.rstrip("\r\n\t ").split(" ") for line in  open("/home/vivek/Music/hold.lst").readlines()]
ts = [(float(t[0]), float(t[1])) for t in ts]
annotations["hold"] = {"fc" : "y", "ec" : "y", "lw" : 0, "alpha" : 0.4, "title" : "Hold", "timestamps" : ts}

ts = [line.rstrip("\r\n\t ").split(" ") for line in  open("/home/vivek/Music/tring.lst").readlines()]
ts = [(float(t[0]), float(t[1])) for t in ts]
annotations["tring"] = {"fc" : "r", "ec" : "r", "lw" : 0, "alpha" : 0.9, "title" : "Tring", "timestamps" : ts}


def plot_annot():
    plot_signal_and_segmentation(signal, SAMPLING_RATE,
                             [annotations["tring"],
                              annotations["hold"]])

plot_annot()  

上面代码生成的图是:enter image description here

正如你所看到的那样,情节似乎认为文件长达90秒,实际上只有31秒长。此外,注释段被错误地覆盖/突出显示。

我做错了什么,我该如何解决?

PS:在波形中,矩形块是“tring”,其余四个“梯形”波形是保持音乐的区域。

python audio plot audio-processing
1个回答
2
投票

这里只是猜测。大胆屏幕截图显示44100的采样率。您的代码片段的SAMPLE_RATE变量初始化为16000.如果您将原始的31秒乘以两个速率之间的比率,则为31 * 44100/16000 = 85.44秒。

© www.soinside.com 2019 - 2024. All rights reserved.