Этот код рабочий, перепиши его, используя метод фурье. Код тут: import os import…
- User
Этот код рабочий, перепиши его, используя метод фурье. Код тут: 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)
codefor 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)
code# 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=(14, 4)) times = np.linspace(0, len(audio_data_resampled) / 8000, len(audio_data_resampled))*2000 # Calculate time values for x-axis ax.plot(times, audio_data_resampled, label='Oridgin 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='Voice_detected') plt.title(filename) plt.legend() plt.show() - Assistant