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, чтобы сохранить полный путь к файлу
codereturn 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
codevoiced_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]) - 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.
-
- 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)
- Assistant
- User
Данные по оси x должны колебаться от 0-14000 а у нас на графике они колеблются от 0 до 7
- Assistant
- User
Код опять выводит интервалы в секундах, а не милисекундах
- Assistant