示例#1
0
def draw_waveform(signals, sr=16000, titles=None):
    """draw waveform of signal"""
    if not isinstance(signals, list):
        signals = [signals]

    if titles is not None:
        if not isinstance(titles, list):
            titles = [titles]

        if len(signals) != len(titles):
            titles = None

    fig = plt.figure(figsize=(18, 9))
    fig.subplots_adjust(left=0.2, right=0.95, hspace=0.3)
    n = len(signals)
    for i, sig in enumerate(signals):
        ax = fig.add_subplot(n, 1, i + 1)
        display.waveplot(sig, sr=sr, ax=ax)
        if titles is not None:
            ax.set_title(titles[i], x=-0.07, y=0.4, va='center', ha='right')

    buf = io.BytesIO()
    plt.savefig(buf, format='png')
    buf.seek(0)
    plt.close(fig)
    arr = buf2ndarray(buf)
    return arr
def draw_waveform_animated_faster(animation_path, signals, sr=16000, titles=None, fps=30):
    """draw waveform of signal"""
    if not isinstance(signals, list):
        signals = [signals]

    if titles is not None:
        if not isinstance(titles, list):
            titles = [titles]

        if len(signals) != len(titles):
            titles = None

    fig = plt.figure(figsize=(18, 9))
    canvas_width, canvas_height = fig.canvas.get_width_height()
    fig.subplots_adjust(left=0.2, right=0.95, hspace=0.3)
    n = len(signals)
    lines = []
    for i, sig in enumerate(signals):
        ax = fig.add_subplot(n, 1, i + 1)
        display.waveplot(sig, sr=sr, ax=ax)
        if titles is not None:
            ax.set_title(titles[i], x=-0.07, y=0.4, va='center', ha='right')

        # draw animated line
        X_VALS = np.linspace(0, len(sig)/sr, num=int(len(sig)/sr*fps))
        padding = 0
        min_line = min(sig) - padding
        max_line = max(sig) + padding
        l, v = plt.plot(X_VALS[0], min_line, X_VALS[-1], max_line, linewidth=2, color='#ff0000')
        lines.append(l)

    def animate(num):
        i = X_VALS[num]
        for line in lines:
            line.set_data([i, i], [-1, 1])

    # Open an ffmpeg process
    cmdstring = ('ffmpeg', 
                 '-y', '-r', '%d' % fps, # overwrite, 1fps
                 '-s', '%dx%d' % (canvas_width, canvas_height), # size of image string
                 '-pix_fmt', 'argb', # format
                 '-f', 'rawvideo',  '-i', '-', # tell ffmpeg to expect raw video from the pipe
                #  '-q:v', '5', # video quality when -vcodec=mpeg4
                 '-vcodec', 'h264', animation_path) # output encoding
    p = subprocess.Popen(cmdstring, stdin=subprocess.PIPE)

    # Draw frames and write to the pipe
    for frame in range(len(X_VALS)):
        # draw the frame
        animate(frame)
        fig.canvas.draw()

        # extract the image as an ARGB string
        string = fig.canvas.tostring_argb()

        # write to pipe
        p.stdin.write(string)

    # Finish up
    p.communicate()
示例#3
0
def isolate_events(loadpath, min_silence_len=500, silence_thresh=-35):
    '''
    Function to isolate singular audio events from an input file.
   
    args:
    loadpath : path to audio file
    min_silence_len : minimum time in ms to consider as silence
    silence_thresh : minimum level in dB for detecting a sound
    
    returns:
    events : list of arrays representing each audio event
    '''

    # RAW AUDIO SHOULD BE MONO WAV AT 22050 Hz !!!!!!!!!!!!!!!!
    raw_audio = AudioSegment.from_wav(loadpath)

    # Present raw audio:
    audio_array, sample_rate = lb.load(loadpath, sr=None)
    plt.figure(figsize=(12, 4))
    lbd.waveplot(audio_array, sr=sample_rate)
    plt.title('Recorded Audio')
    plt.show()
    # Isolate events:
    events = split_on_silence(
        raw_audio,
        # must be silent for at least half a second
        min_silence_len=min_silence_len,
        # consider it silent if quieter than
        silence_thresh=silence_thresh)

    return events
def plot_wave(filename, sr=16000, axis=False):
    data, sr = librosa.load(filename, sr)
    plt.figure()
    axis = "on" if axis else "off"
    plt.axis(axis)
    display.waveplot(data, sr)
    plt.show()
示例#5
0
def plot_fig(y,save='',x_axis='time', max_points=50000.0, offset=0.0, color='#333333', alpha=1.0, show_filename=True, plot=True):
    plt.figure()
    waveplot(y=y,sr=44100,x_axis=x_axis,max_points=max_points,
             offset=offset,color=color,alpha=alpha)
    plt.show()
    if save != '':
        plt.savefig(save)
def plot_band_pass_filter(data_path, bpf_kernel_size=33):
    plt.rcParams["font.size"] = axis_font_size
    y, sr = librosa.load(data_path, sr=16000)
    plt.figure(figsize=(24, 6))
    plt.subplot(2, 5, 1)
    display.waveplot(y, sr=sr, x_axis='none')
    plt.title("raw waveform")
    # raw spectrogram
    plt.subplot(2, 5, 6)
    display.specshow(librosa.amplitude_to_db(np.abs(librosa.stft(y)),
                                             ref=np.max),
                     sr=sr,
                     y_axis='linear')
    plt.colorbar(format="%+2.0f dB")
    plt.title("Linear power spectrogram")
    for low_hz, plot_idx in zip([0, 1000, 2000, 3000, 4000, 5000, 6000, 7000],
                                [2, 3, 4, 5, 7, 8, 9, 10]):
        high_hz = low_hz + 1000
        y_input = torch.tensor(y).unsqueeze(0).unsqueeze(0)
        bpf = BandPassFilter(kernel_size=bpf_kernel_size,
                             stride=1,
                             padding=bpf_kernel_size // 2,
                             low_hz=low_hz,
                             high_hz=high_hz)
        y_pass = bpf.forward(y_input).squeeze().numpy()
        plt.subplot(2, 5, plot_idx)

        display.specshow(librosa.amplitude_to_db(np.abs(librosa.stft(y_pass)),
                                                 ref=np.max),
                         sr=sr)
        plt.colorbar(format="%+2.0f dB")
        plt.title("frequency band:[{}, {}]".format(low_hz, high_hz))
    plt.tight_layout()
    plt.show()
    def animate(t):

        # Access our initialized variables from outside the function
        global last_t
        global tempgraph

        # Round time to nearest 0.5 second. This limits the number of times we
        # update the graph. Each graph update costs 10-20 seconds to draw, so by
        # limiting this, we can render the graph an order of magnitude faster
        temp_t = np.round(t * 2) / 2.0

        # Only update the graph if we are at the next 0.5 seconds
        if t > 0 and temp_t != last_t:

            # Update our timekeeping variable
            last_t = temp_t

            # Delete the previous graph, otherwise we will keep plotting over
            # the same graphs again and again
            for coll in (ax.collections):
                ax.collections.remove(coll)

            # Load only the played portion of the mp3 and plot them
            y2, sr2 = librosa.load('extracted_audio.mp3',
                                   mono=False,
                                   duration=t)
            waveplot(y2, sr=sr2, color='b', alpha=0.8)
            waveplot(y, sr=sr, color='b', alpha=0.25)

            # Update the output graph
            tempgraph = mplfig_to_npimage(fig)

        return tempgraph
示例#8
0
def compare_values(attribute, values, file):
    """
    attributes (str): the names of the attribute you like to test.
    values (list): list of values to compare.
    file (str): file name to perform the tests.
    """
    wavs = []
    for idx, val in enumerate(values):
        set_val_cmd = "AP.{}={}".format(attribute, val)
        exec(set_val_cmd)
        wav = AP.load_wav(file)
        spec = AP.spectrogram(wav)
        spec_norm = AP._denormalize(spec.T)
        plt.subplot(len(values), 2, 2*idx + 1)
        plt.imshow(spec_norm.T, aspect="auto", origin="lower")
        #         plt.colorbar()
        plt.tight_layout()
        wav_gen = AP.inv_spectrogram(spec)
        wavs.append(wav_gen)
        plt.subplot(len(values), 2, 2*idx + 2)
        display.waveplot(wav, alpha=0.5)
        display.waveplot(wav_gen, alpha=0.25)
        plt.title("{}={}".format(attribute, val))
        plt.tight_layout()
    
    wav = AP.load_wav(file)
    print(" > Ground-truth")
    IPython.display.display(IPython.display.Audio(wav, rate=AP.sample_rate))
    
    for idx, wav_gen in enumerate(wavs):
        val = values[idx]
        print(" > {} = {}".format(attribute, val))
        IPython.display.display(IPython.display.Audio(wav_gen, rate=AP.sample_rate))
示例#9
0
def plot_audio(filepath):
	x, fs = load(filepath, sr = None, mono = True)
	plt.figure(figsize=(16,4))
	waveplot(x,sr=fs)
	plt.title("Waveform for {}".format(filepath))
	plt.tight_layout()
	plt.show()
示例#10
0
def plot_waves(sound_names, raw_sounds):
    i = 1
    for n, f in zip(sound_names, raw_sounds):
        plt.subplot(6, 1, i)
        libdisplay.waveplot(np.array(f), sr=22050)
        plt.title(n.title() + ' - Waveplot')
        i += 1
示例#11
0
def waveplot(y: np.ndarray, sr: int, processed=None):
    plot_wave = st.checkbox("Waveplot")
    if plot_wave:
        st.sidebar.markdown("#### Waveplot settings")
        start_second = st.sidebar.number_input("start second",
                                               min_value=0,
                                               max_value=len(y) // sr,
                                               value=0,
                                               step=1,
                                               key="waveplot_start")
        end_second = st.sidebar.number_input("end second",
                                             min_value=0,
                                             max_value=len(y) // sr,
                                             value=len(y) // sr,
                                             step=1,
                                             key="waveplot_end")

        start_index = start_second * sr
        if end_second == len(y) // sr:
            end_index = len(y)
        else:
            end_index = end_second * sr
        fig = plt.figure(figsize=(12, 4))
        plt.grid(True)
        display.waveplot(y[start_index:end_index], sr=sr, alpha=0.5)
        if processed is not None:
            display.waveplot(processed[start_index:end_index],
                             sr=sr,
                             alpha=0.5,
                             color="red")

        st.pyplot(fig)
示例#12
0
文件: main.py 项目: quangliem/xltn
def main():
    # y_or, sr = librosa.load("khoosoothunhus.wav", duration=10)
    y_or, sr = librosa.load("A96.wav", duration=10)
    # print(y_or)
    fig1 = plt.figure()
    fig1.suptitle('mouse hover over figure or axes to trigger events')
    ax1 = fig1.add_subplot(411)
    ax1.set_title("do thi am")
    display.waveplot(y_or, sr=sr)
    ax2 = fig1.add_subplot(412)
    ax2.set_title('tu tuong quan')
    fig2 = plt.figure()
    ax3 = fig2.add_subplot(211)
    ax3.set_title("bien thien f0 tu_tuong_quan")
    ax4= fig1.add_subplot(413)
    ax5 =fig2.add_subplot(212)
    ax5.set_title("bien thien f0 amdf")
    print("sample rate: ", sr)
    ax_lpc_fft = fig1.add_subplot(414)
    # fig3 = plt.figure()
    # ax_lpc_fft = fig3.add_subplot(111)
    cursor = SnaptoCursor(ax1, ax2, ax3, ax4, ax5, ax_lpc_fft, y_or, sr)
    # plt.connect('motion_notify_event', cursor.mouse_move)
    fig1.canvas.mpl_connect('button_press_event', cursor.mouse_click)
    # plt.connect('button_press_event', cursor.mouse_click)


    plt.show()
示例#13
0
def show_audio_signal(ai, ctx, **kwargs):
    if(ai.nchannels > 1):
        _,axs = plt.subplots(ai.nchannels, 1, figsize=(6,4*ai.nchannels))
        for i,channel in enumerate(ai.sig):
            waveplot(channel.numpy(), ai.sr, ax=axs[i], **kwargs)
    else:
        axs = plt.subplots(ai.nchannels, 1)[1] if ctx is None else ctx
        waveplot(ai.sig.squeeze(0).numpy(), ai.sr, ax=axs, **kwargs)
示例#14
0
 def update_waveform(self, audio_data, audio_sample_rate):
     self.clear_axes()
     if audio_data is not None:
         libdisplay.waveplot(audio_data,
                             audio_sample_rate,
                             x_axis="time",
                             ax=self.axes)
     self.update_canvas()
示例#15
0
def audio_display():
    sr = 1000
    sound1, sound2, base = load_data(sr)
    plt.subplot(2, 1, 1)
    display.waveplot(sound1, sr=sr, alpha=0.25)
    plt.subplot(2, 1, 2)
    display.waveplot(base, sr=sr, alpha=0.25)
    plt.tight_layout()
    plt.show()
示例#16
0
def plot_wav(path, y_hat, y_target, key, global_step, sample_rate):
    plt.figure(figsize=(16, 9))
    plt.subplot(2, 1, 1)
    waveplot(y_target, sr=sample_rate)
    plt.subplot(2, 1, 2)
    waveplot(y_hat, sr=sample_rate)
    plt.tight_layout()
    plt.suptitle(f"record: {key}, global step: {global_step}")
    plt.savefig(path, format="png")
    plt.close()
示例#17
0
def waveform(file_path: str) -> None:
    """
    Draw audio waveform

    Args:
        file_path (str): audio file path
    """
    data, sampling_rate = librosa.load(file_path)
    plt.figure(figsize=(15, 5))
    display.waveplot(data, sr=sampling_rate)
    plt.show()
示例#18
0
 def plot_signal(self, title=None, out=None):
     import matplotlib.pyplot as plt
     if title is None:
         title = self.filename
     plt.title(title)
     waveplot(self.signal, sr=self.samplerate)
     if out is None:
         plt.show()
     else:
         plt.savefig(out, dpi=300)
     plt.close()
示例#19
0
def display_features(audio_filepath):
    x, sr, X, Xdb, mfccs = extract_features(audio_filepath)
    plt.figure()
    lbdisplay.waveplot(x, sr=sr)
    plt.show()
    plt.figure()
    spectrogram = lbdisplay.specshow(Xdb, sr=sr, x_axis="time", y_axis="hz")
    plt.colorbar()
    plt.figure()
    mfcc = lbdisplay.specshow(mfccs, sr=sr, x_axis="time")
    plt.show()
def plot_amplitude(original_samples, shifted_samples, sampling_rate):

	librosa_display.waveplot(original_samples, sr=sampling_rate, label='Original')
	librosa_display.waveplot(shifted_samples, sr=sampling_rate, label='Shifted')

	plt.title('Original vs Shifted samples')
	plt.legend()
	plt.ylabel('Amplitude')
	plt.show()

	return
def waves(raw_sounds):
    i = 1
    fig = plt.figure(figsize=(12, 15))
    for file, name in zip(raw_sounds, sound_names):
        plt.subplot(10, 1, i)
        lsd.waveplot(np.array(file), x_axis=None)
        plt.title(name.title())
        i += 1
    plt.suptitle("Waveplot", x=0.52, y=1.02, fontsize=15)
    fig.tight_layout()
    plt.savefig('waveplot.jpg')
示例#22
0
def waveplot(y: np.ndarray,
             sr: int,
             processed=None,
             tp: pd.DataFrame = None,
             fp: pd.DataFrame = None):
    plot_wave = st.checkbox("Waveplot")
    if plot_wave:
        st.sidebar.markdown("#### Waveplot settings")
        start_second = st.sidebar.number_input("start second",
                                               min_value=0,
                                               max_value=len(y) // sr,
                                               value=0,
                                               step=1,
                                               key="waveplot_start")
        end_second = st.sidebar.number_input("end second",
                                             min_value=0,
                                             max_value=len(y) // sr,
                                             value=len(y) // sr,
                                             step=1,
                                             key="waveplot_end")

        start_index = start_second * sr
        if end_second == len(y) // sr:
            end_index = len(y)
        else:
            end_index = end_second * sr
        fig = plt.figure(figsize=(12, 4))
        plt.grid(True)
        display.waveplot(y[start_index:end_index], sr=sr, alpha=0.5)
        if processed is not None:
            display.waveplot(processed[start_index:end_index],
                             sr=sr,
                             alpha=0.5,
                             color="red")
        if tp is not None and len(tp) > 0:
            for _, row in tp.iterrows():
                plt.axvspan(row["t_min"],
                            row["t_max"],
                            color="g",
                            alpha=0.5,
                            label=str(row["species_id"]))

        if fp is not None and len(fp) > 0:
            for _, row in fp.iterrows():
                plt.axvspan(row["t_min"],
                            row["t_max"],
                            color="r",
                            alpha=0.5,
                            label=str(row["species_id"]))
        plt.legend()

        st.pyplot(fig)
示例#23
0
def show_audio_signal(ai, ctx, ax=None, title="", **kwargs):
    ax = ifnone(ax, ctx)
    if ax is None:
        _, ax = plt.subplots()
    ax.axis(False)
    for i, channel in enumerate(ai):
        # x_start, y_start, x_lenght, y_lenght, all in percent
        ia = ax.inset_axes((i / ai.nchannels, 0.2, 1 / ai.nchannels, 0.7))
        waveplot(channel.cpu().numpy(), ai.sr, ax=ia, **kwargs)
        ia.set_title(f"Channel {i}")
    ax.set_title(title)

    return ax
示例#24
0
def waveplot(sample, samplerate):
	""" Create a waveplot figure of the raw sample
	"""
	plt.figure(figsize=(12, 6))

	display.waveplot(y=sample, sr=samplerate)

	plt.title('Waveplot of sound sample')
	plt.xlabel('Time (sec)')
	plt.ylabel('Amplitude')

	plt.savefig(os.path.join(paths.path2Output, 'sample_waveplot.png'))
	plt.close()
示例#25
0
def show_wav(wav,
             sr=8000,
             figsize=(10, 4),
             specgram_lib='librosa',
             save_to=None,
             n_fft=2048,
             hop_length=None):
    if hop_length is None:
        hop_length = n_fft // 4

    if type(wav) == str:
        wav, sr = lr.load(wav)
    elif type(wav) == torch.Tensor:
        wav = wav.detach().numpy()

    fig, axes = plt.subplots(nrows=1, ncols=2, figsize=figsize)
    plt.sca(axes[0])
    lrd.waveplot(wav, sr=sr, ax=axes[0])
    plt.ylabel('Amplitude')

    tticks, tlabels = plt.xticks()

    plt.sca(axes[1])
    if specgram_lib == 'librosa':
        S_db = lr.amplitude_to_db(
            np.abs(lr.stft(wav, n_fft=n_fft, hop_length=hop_length)))
        img = lrd.specshow(S_db,
                           sr=sr,
                           ax=axes[1],
                           hop_length=hop_length,
                           x_axis='s',
                           y_axis='hz')
        fig.colorbar(img, ax=axes[1])
    elif specgram_lib == 'matplotlib':
        plt.specgram(wav, Fs=sr, mode='magnitude')
        plt.xlabel('Time')
        plt.ylabel('Frequency')
    else:
        raise ValueError(
            f'Invalid `specgram_lib={spegram_lib}`, should be one of (`librosa`, `matplotlib`)'
        )

    plt.xticks(tticks)

    fig.tight_layout()
    if save_to is not None:
        plt.savefig(save_to, bbox_inches='tight')

    plt.show()

    ipd.display(ipd.Audio(wav, rate=sr))
 def plot_waves(self, sound_names, raw_sounds):
     i = self.i
     fig = plt.figure(figsize=self.figsize, dpi=self.dpi)
     for n, f in zip(sound_names, raw_sounds):
         plt.subplot(10, 1, i)
         display.waveplot(np.array(f), sr=22050)
         plt.title(n.title())
         i += 1
     plt.suptitle("Figure 1: Waveplot",
                  x=self.x,
                  y=self.y,
                  fontsize=self.fontsize)
     plt.tight_layout()
     plt.show()
示例#27
0
def plot_sound():
    amplitudes, frequencies = librosa.load("microphone-results.wav")
    plt.subplot(211)
    display.waveplot(y=amplitudes, sr=frequencies)
    # calcul de la transformee de Fourier et des frequences
    fourier = fft(amplitudes)
    # affichage de la transformee de Fourier
    plt.subplot(212)
    plt.plot(fourier)
    plt.grid()
    plt.xlabel("frequency domaine")
    plt.ylabel("Magnitude")
    plt.legend()
    plt.show()
def draw_waveform_animated_better_quality(animation_path, signals, sr=16000, titles=None, fps=30):
    """draw waveform of signal"""
    if not isinstance(signals, list):
        signals = [signals]

    if titles is not None:
        if not isinstance(titles, list):
            titles = [titles]

        if len(signals) != len(titles):
            titles = None

    fig = plt.figure(figsize=(18, 9))
    fig.subplots_adjust(left=0.2, right=0.95, hspace=0.3)
    n = len(signals)
    lines = []
    for i, sig in enumerate(signals):
        ax = fig.add_subplot(n, 1, i + 1)
        display.waveplot(sig, sr=sr, ax=ax)
        if titles is not None:
            ax.set_title(titles[i], x=-0.07, y=0.4, va='center', ha='right')

        # draw animated line
        X_VALS = np.linspace(0, len(sig)/sr, num=int(len(sig)/sr*fps))
        padding = 0
        min_line = min(sig) - padding
        max_line = max(sig) + padding
        l, v = plt.plot(X_VALS[0], min_line, X_VALS[-1], max_line, linewidth=2, color='#ff0000')
        lines.append(l)

    def init():
        for line in lines:
            line.set_data([], [])
        return lines

    def animate(num):
        i = X_VALS[num]
        for line in lines:
            line.set_data([i, i], [-1, 1])
        return lines

    line_anim = animation.FuncAnimation(fig, animate, frames=len(X_VALS), init_func=init, blit=True)

    # save animated plot
    # writer = FFMpegWriter(fps=fps)
    writer = animation.writers['ffmpeg'](fps=fps)
    print('Saving animated plot to', animation_path)
    line_anim.save(animation_path, writer=writer)
    print('Done')
示例#29
0
    def plot_audio(self, outside_series=None, outside_sr=None):
        """
        Plot audio

        :param outside_series:
        :param outside_sr:
        :return:
        """

        y = self.select_series(outside_series)
        sr = self.select_sr(outside_sr)

        plt.figure(figsize=(14, 5))
        waveplot(y, sr=sr)
        plt.show()
示例#30
0
def plot_waveform(temp, wav, channels):
    y, sr = librosa.load(wav, mono=False)
    if channels == 1:
        y = y.reshape(-1, len(y))
    DPI = 72
    plt.figure(1, figsize=(16, 9), dpi=DPI) 
    plt.subplots_adjust(wspace=0, hspace=0) 
    for n in range(channels): 
        plt.subplot(2, 1, n + 1, facecolor='200')
        display.waveplot(y[n], sr)
        plt.grid(True, color='w') 
    waveform = str(Path(temp) / 'waveform.png')
    plt.savefig(waveform, dpi=DPI)
    waveform = str(base64.b64encode(open(waveform, 'rb').read()))[2: -1]
    return waveform
示例#31
0
def waveplot(path, y_hat, y_target, hparams):
	sr = hparams.sample_rate

	plt.figure(figsize=(12, 4))
	if y_target is not None:
		ax = plt.subplot(2, 1, 1)
		dsp.waveplot(y_target, sr=sr)
		ax.set_title('Target waveform')
		ax = plt.subplot(2, 1, 2)
		dsp.waveplot(y_hat, sr=sr)
		ax.set_title('Prediction waveform')
	else:
		ax = plt.subplot(1, 1, 1)
		dsp.waveplot(y_hat, sr=sr)
		ax.set_title('Generated waveform')

	plt.tight_layout()
	plt.savefig(path, format="png")
	plt.close()