import os import webrtcvad import wave import collections import contextlib import sys…
- User
import os import webrtcvad import wave import collections import contextlib import sys from glob import glob import librosa import matplotlib.pyplot as plt
def load_dataset(directory: str): sr = None X, labels, files = [], [], [] for f in glob(directory + "/*.wav"): filename = os.path.basename(f) y = [int(label) for label in filename[:-4].split("_")] x, sr = librosa.load(f) X.append(x) labels.append(y) files.append(f) # Изменено на f, чтобы сохранить полный путь к файлу
return X, labels, sr, filesdef read_wave(path): x, sr = librosa.load(path, sr=None) if sr not in (16000, 32000, 48000): x = librosa.resample(x, orig_sr=sr, target_sr=16000) sr = 16000 pcm_data = x.astype('int16').tobytes() return pcm_data, sr
def frame_generator(frame_duration_ms, audio, sample_rate): n = int(sample_rate * (frame_duration_ms / 1000.0) * 2) offset = 0 timestamp = 0.0 duration = (1.0 / sample_rate) * n while offset + n < len(audio): yield audio[offset:offset + n], timestamp, duration timestamp += duration offset += n
def vad_collector(sample_rate, frame_duration_ms, padding_duration_ms, vad, frames): num_padding_frames = int(padding_duration_ms / frame_duration_ms) ring_buffer = collections.deque(maxlen=num_padding_frames) triggered = False
voiced_frames = [] for frame, timestamp, duration in frames: is_speech = vad.is_speech(frame, sample_rate) if not triggered: ring_buffer.append((frame, is_speech)) num_voiced = len([f for f, speech in ring_buffer if speech]) if num_voiced > 0.9 * ring_buffer.maxlen: triggered = True for f, s in ring_buffer: voiced_frames.append(f) ring_buffer.clear() else: voiced_frames.append(frame) ring_buffer.append((frame, is_speech)) num_unvoiced = len([f for f, speech in ring_buffer if not speech]) if num_unvoiced > 0.9 * ring_buffer.maxlen: triggered = False yield b''.join([f.bytes for f in voiced_frames]) ring_buffer.clear() voiced_frames = [] if voiced_frames: yield b''.join([fbytes for f in voiced_frames])def run_vad(audio_path): audio, sample_rate = read_wave(audio_path) vad = webrtcvad.Vad(3) frames = frame_generator(30, audio, sample_rate) frames = list(frames) segments = vad_collector(sample_rate, 30, 300, vad, frames) speech_segments = [] for i, segment in enumerate(segments): path = 'chunk-%002d.wav' % (i,) with open(path, 'wb') as f: f.write(segment) speech_segments.append(path) return speech_segments
def plot_vad(X, sr, speech_segments): plt.figure(figsize=(12, 4)) plt.plot(X, label='Signal') timestamps = []
for segment in speech_segments: segment_data, _ = librosa.load(segment, sr=sr) timestamp = librosa.samples_to_time(list(X).index(segment_data[0]), sr=sr) timestamps.append(timestamp) speech_starts = timestamps speech_ends = timestamps[1:] + [librosa.samples_to_time(len(X), sr=sr)] for segment_start, segment_end in zip(speech_starts, speech_ends): plt.axvspan(segment_start, segment_end, color='red', alpha=0.5) plt.xlabel('Time (s)') plt.ylabel('Amplitude') plt.legend(loc='best') plt.title('Voice Activity Detection') plt.show()Замените 'path/to/your/directory' на локальный путь к вашей директории с аудиофайлами
directory = '/content/drive/MyDrive/School21/day09/datasets/audio_yes_no/waves_yesno 2' X, labels, sr, files = load_dataset(directory)
for i, audio_path in enumerate(files): speech_segments = run_vad(audio_path) plot_vad(X[i], sr, speech_segments)
- Assistant
In this code, some functions are implemented to perform Voice Activity Detection (VAD) on a given dataset of audio files. The implementation uses the WebRTC VAD library, which is a lightweight and efficient VAD module from Google.
Here's a brief explanation of what each function does:
-
load_dataset(): This function loads a dataset of WAV audio files from a specified directory, returning the audio data, labels, the sample rate, and the file paths. -
read_wave(): This function reads a single WAV file and returns its data as PCM (pulse-code modulation) data and its sample rate. It also makes sure the sample rate is one of the supported rates (16000, 32000, 48000) by resampling if necessary. -
frame_generator(): This function generates framed audio data from the input audio data, with a specified duration in milliseconds. It's a Python generator function that yields audio frames, their starting timestamp, and their duration. -
vad_collector(): This function is the core of the VAD process. It takes the sample rate, frame_duration, padding_duration, a VAD instance, and the input audio frames, and returns the voiced audio segments as a generator of byte strings. -
run_vad(): This function runs the whole VAD process on a given audio file, by calling the previously mentioned functions in sequence. It returns a list of speech segment filepaths, which are temporary chunks of the original audio containing only the voiced portions. -
plot_vad(): This function creates a plot of the original audio signal, with the detected voiced segments overlaid in red.
In the main part of the code, the
load_dataset()function is called to load the audio dataset from a specified directory. Then, for each audio file in this dataset, the VAD process is run using therun_vad()function, and the resulting voiced segments are plotted using theplot_vad()function.Make sure to replace
'/content/drive/MyDrive/School21/day09/datasets/audio_yes_no/waves_yesno 2'with the path to the folder containing your dataset of WAV audio files. -
- User
Данный код выводит верные данные по оси x и выводит легенду, помоги мне дополнить вот этот код import os import torchaudio import matplotlib.pyplot as plt import torch import librosa as lr import numpy as np
Define the vad function
def vad(audio_data, sample_rate, frame_duration=0.025, energy_threshold=0.6): frame_size = int(sample_rate * frame_duration) num_frames = len(audio_data) // frame_size silence = np.ones(len(audio_data), dtype=bool)
for idx in range(num_frames): start = idx * frame_size end = (idx + 1) * frame_size frame_energy = np.sum(np.square(audio_data[start:end])) if frame_energy > energy_threshold: silence[start:end] = False segments = [] start = None for idx, value in enumerate(silence): if not value and start is None: # Start of speech segment start = idx elif value and start is not None: # End of speech segment segments.append([start, idx - 1]) start = None return segments, silencedirectory = '/content/drive/MyDrive/School21/day09/datasets/audio_yes_no/waves_yesno 2'
Loop through all files in the directory
for filename in os.listdir(directory): if filename.endswith('.wav'): # Load the audio file and find the speech boundaries audio_data, sample_rate = torchaudio.load(os.path.join(directory, filename)) audio_data_resampled = lr.resample(audio_data.numpy()[0], orig_sr=sample_rate, target_sr=8000) segments, silence = vad(audio_data_resampled, 8000)
# Resample the silence mask silence_resampled = np.interp(np.linspace(0, len(silence), len(audio_data_resampled)), np.arange(len(silence)), silence.astype(float)) # Plot the audio signal and speech boundaries fig, ax = plt.subplots(figsize=(10, 3)) frame_indices = np.arange(len(audio_data_resampled)) ax.plot(frame_indices, audio_data_resampled) # Create a threshold line based on the silence mask threshold_line = np.where(silence_resampled, 0, np.max(np.abs(audio_data_resampled))) ax.plot(frame_indices, threshold_line, color='red', linestyle='-') ax.set_xlabel('Frame Index') ax.set_ylabel('Amplitude') plt.title(filename) plt.show() так, чтобы по оси x были значения как в предыдущем коде и выводилась легенда - Assistant
You can update your code to include the X-axis in seconds and plot the legend as follows:
import os import torchaudio import matplotlib.pyplot as plt import torch import librosa as lr import numpy as np # Define the vad function def vad(audio_data, sample_rate, frame_duration=0.025, energy_threshold=0.6): frame_size = int(sample_rate * frame_duration) num_frames = len(audio_data) // frame_size silence = np.ones(len(audio_data), dtype=bool) for idx in range(num_frames): start = idx * frame_size end = (idx + 1) * frame_size frame_energy = np.sum(np.square(audio_data[start:end])) if frame_energy > energy_threshold: silence[start:end] = False segments = [] start = None for idx, value in enumerate(silence): if not value and start is None: # Start of speech segment start = idx elif value and start is not None: # End of speech segment segments.append([start, idx - 1]) start = None return segments, silence directory = '/content/drive/MyDrive/School21/day09/datasets/audio_yes_no/waves_yesno 2' # Loop through all files in the directory for filename in os.listdir(directory): if filename.endswith('.wav'): # Load the audio file and find the speech boundaries audio_data, sample_rate = torchaudio.load(os.path.join(directory, filename)) audio_data_resampled = lr.resample(audio_data.numpy()[0], orig_sr=sample_rate, target_sr=8000) segments, silence = vad(audio_data_resampled, 8000) # Resample the silence mask silence_resampled = np.interp(np.linspace(0, len(silence), len(audio_data_resampled)), np.arange(len(silence)), silence.astype(float)) # Plot the audio signal and speech boundaries fig, ax = plt.subplots(figsize=(10, 3)) times = np.linspace(0, len(audio_data_resampled) / 8000, len(audio_data_resampled)) # Calculate time values for x-axis ax.plot(times, audio_data_resampled, label='Signal') # Create a threshold line based on the silence mask threshold_line = np.where(silence_resampled, 0, np.max(np.abs(audio_data_resampled))) ax.plot(times, threshold_line, color='red', linestyle='-', label='Threshold') ax.set_xlabel('Time (s)') ax.set_ylabel('Amplitude') plt.title(filename) plt.legend() plt.show()This adds
timesvariable, which calculates time values for the X-axis based on the length of the audio_data_resampled array and the resampled sample rate (8000 in this case). Then, this variable is used for the X-axis in bothax.plot()calls. Additionally, I've addedlabelarguments for both plots to later display a legend. Finally, callingplt.legend()just beforeplt.show()adds the legend to the plot. - User
Данные по оси x должны колебаться от 0-14000 а у нас на графике они колеблются от 0 до 7
- Assistant
My apologies for the confusion. It seems like there's a mistake in calculating the time values. We should be calculating the time values based on the original sample rate, not the resampled one. Here's the corrected version:
import os import torchaudio import matplotlib.pyplot as plt import torch import librosa as lr import numpy as np # Define the vad function def vad(audio_data, sample_rate, frame_duration=0.025, energy_threshold=0.6): frame_size = int(sample_rate * frame_duration) num_frames = len(audio_data) // frame_size silence = np.ones(len(audio_data), dtype=bool) for idx in range(num_frames): start = idx * frame_size end = (idx + 1) * frame_size frame_energy = np.sum(np.square(audio_data[start:end])) if frame_energy > energy_threshold: silence[start:end] = False segments = [] start = None for idx, value in enumerate(silence): if not value and start is None: # Start of speech segment start = idx elif value and start is not None: # End of speech segment segments.append([start, idx - 1]) start = None return segments, silence directory = '/content/drive/MyDrive/School21/day09/datasets/audio_yes_no/waves_yesno 2' # Loop through all files in the directory for filename in os.listdir(directory): if filename.endswith('.wav'): # Load the audio file and find the speech boundaries audio_data, sample_rate = torchaudio.load(os.path.join(directory, filename)) audio_data_resampled = lr.resample(audio_data.numpy()[0], orig_sr=sample_rate, target_sr=8000) segments, silence = vad(audio_data_resampled, 8000) # Resample the silence mask silence_resampled = np.interp(np.linspace(0, len(silence), len(audio_data_resampled)), np.arange(len(silence)), silence.astype(float)) # Plot the audio signal and speech boundaries fig, ax = plt.subplots(figsize=(10, 3)) times = np.linspace(0, len(audio_data.numpy()[0]) / sample_rate, len(audio_data_resampled)) # Calculate time values for x-axis based on original sample rate ax.plot(times, audio_data_resampled, label='Signal') # Create a threshold line based on the silence mask threshold_line = np.where(silence_resampled, 0, np.max(np.abs(audio_data_resampled))) ax.plot(times, threshold_line, color='red', linestyle='-', label='Threshold') ax.set_xlabel('Time (s)') ax.set_ylabel('Amplitude') plt.title(filename) plt.legend() plt.show()This change calculates the time values based on the original sample rate (instead of the resampled one) and should now show the correct X-axis values ranging from 0 to 14,000 milliseconds (14 seconds).
- User
Код опять выводит интервалы в секундах, а не милисекундах
- Assistant
My apologies once again. You can simply multiply the time values by 1000 to get the X-axis values in milliseconds. Here's the corrected code:
import os import torchaudio import matplotlib.pyplot as plt import torch import librosa as lr import numpy as np # Define the vad function def vad(audio_data, sample_rate, frame_duration=0.025, energy_threshold=0.6): frame_size = int(sample_rate * frame_duration) num_frames = len(audio_data) // frame_size silence = np.ones(len(audio_data), dtype=bool) for idx in range(num_frames): start = idx * frame_size end = (idx + 1) * frame_size frame_energy = np.sum(np.square(audio_data[start:end])) if frame_energy > energy_threshold: silence[start:end] = False segments = [] start = None for idx, value in enumerate(silence): if not value and start is None: # Start of speech segment start = idx elif value and start is not None: # End of speech segment segments.append([start, idx - 1]) start = None return segments, silence directory = '/content/drive/MyDrive/School21/day09/datasets/audio_yes_no/waves_yesno 2' # Loop through all files in the directory for filename in os.listdir(directory): if filename.endswith('.wav'): # Load the audio file and find the speech boundaries audio_data, sample_rate = torchaudio.load(os.path.join(directory, filename)) audio_data_resampled = lr.resample(audio_data.numpy()[0], orig_sr=sample_rate, target_sr=8000) segments, silence = vad(audio_data_resampled, 8000) # Resample the silence mask silence_resampled = np.interp(np.linspace(0, len(silence), len(audio_data_resampled)), np.arange(len(silence)), silence.astype(float)) # Plot the audio signal and speech boundaries fig, ax = plt.subplots(figsize=(10, 3)) times = np.linspace(0, len(audio_data.numpy()[0]) / sample_rate, len(audio_data_resampled)) * 1000 # Calculate time values for x-axis in milliseconds ax.plot(times, audio_data_resampled, label='Signal') # Create a threshold line based on the silence mask threshold_line = np.where(silence_resampled, 0, np.max(np.abs(audio_data_resampled))) ax.plot(times, threshold_line, color='red', linestyle='-', label='Threshold') ax.set_xlabel('Time (ms)') ax.set_ylabel('Amplitude') plt.title(filename) plt.legend() plt.show()Now the X-axis values should be in milliseconds, ranging from 0 to 14,000.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.