import os from glob import glob import librosa import numpy as np from…
- User
import os from glob import glob import librosa import numpy as np from sklearn.model_selection import train_test_split import random
New function to generate random vad_segments
def get_random_vad_segments(samples, num_segments=10): vad_segments = [] for sample in samples: sample_segments = [] sample_length = len(sample)
for _ in range(num_segments): start = random.randint(0, sample_length - 1) end = random.randint(start + 1, sample_length) segment = [start, end] sample_segments.append(segment) vad_segments.append(sample_segments) return vad_segmentsLoad the dataset and create training and testing sets
directory = "C:\Users\lpoti\Documents\DS_21\audio_yes_no\waves_yesno 2" X, labels, sr, files = load_dataset(directory)
Generate random vad_segments data
vad_segments = get_random_vad_segments(X)
X, y = make_dataset(X, labels, vad_segments) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
num_train_samples = len(X_train) num_test_samples = len(X_test) print(f"Number of train samples: {num_train_samples}") print(f"Number of test samples: {num_test_samples}")
- Assistant
It looks like you have not provided the
load_datasetandmake_datasetfunctions. Please provide these functions to understand and run the complete code. - User
importing the operating system from glob import glob
import librosa import numpy as np
def load_dataset(directory: str): """
:parameter directory: Path to the audio directory :refund: X - Spy Audio Signals tags - A list of tags (Example for downloading '0_0_0_1_0_1_1_0.wav': [0, 0, 0, 1, 0, 1, 1, 0])cp - sampling rates of Mikhailov 's audio recordings files - Name Mikhailov """ X, tags, sr, files = [], [], [], [] for f in the globe (directory + "/*.wav"): file name = os.path.basename(f) name = file name[:-4] y = [int(label) for the label in name.split("_")] x, sr = librosa.load(f) X.add(x) labels.add(y) files.add(file name)
returns X, labels, sr, filesdef make_dataset(samples, labels, vad_segments): """
:sample parameters: Audio signal - Assistant
- User
c:\Users\lpoti\AppData\Local\Programs\Python\Python311\Lib\site-packages\librosa\core\spectrum.py:256: UserWarning: n_fft=2048 is too large for input signal of length=1388 warnings.warn(
IndexError Traceback (most recent call last) Cell In[14], line 28 25 # Generate random vad_segments data 26 vad_segments = get_random_vad_segments(X) ---> 28 X, y = make_dataset(X, labels, vad_segments) 29 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) 31 num_train_samples = len(X_train)
Cell In[13], line 53, in make_dataset(samples, labels, vad_segments) 50 stft_db = librosa.amplitude_to_db(stft) 52 X.append(stft_db) # Adding a speech spectrogram ---> 53 y.append(labels[sample][segment]) # Add a label for this spectrogram 55 return np.array(X), np.array(y)
IndexError: list index out of range
- Assistant
The
IndexErroroccurs because thesegmentindex goes beyond the range of thelabels[sample]list. To fix this issue, you need to ensure that thesegmentindex does not exceed the length of thelabels[sample]list.Since
labelsconsists of 8 values for each audio file, you should also limit the number of segments you generate in theget_random_vad_segmentsfunction. I'll update this function below to ensure that you generate no more than 8 segments for each audio file:def get_random_vad_segments(samples, num_segments=8): vad_segments = [] for sample in samples: sample_segments = [] sample_length = len(sample) for _ in range(num_segments): start = random.randint(0, sample_length - 1) end = random.randint(start + 1, sample_length) segment = [start, end] sample_segments.append(segment) vad_segments.append(sample_segments) return vad_segmentsNow you can generate up to 8 random segments, matching the number of labels. If you still face any error, please let me know, and I'll help you debug the issue.
- User
- Assistant
The warnings you see are related to the fact that the input signal length is shorter than the
n_fftparameter used in thelibrosa.stftfunction. It happens because the segments you got fromget_random_vad_segmentshave varying lengths, and some of them are short.To handle this case, we can update the
make_datasetfunction to pad the input signal with zeros to at least length. Here's the updated function: - User
Our audio recordings contain both human speech and silence. For each record, we need to determine the segments of the record where the person is silent and where he utters words. This task is called Voice Activity Detection (VAD). Come up with or find a method by which you can recognize areas with speech on an audio recording.
For example: The record '0_0_0_1_0_1_1_0.wav' contains 137592 samples. Speech segments for this record (Marked in red): [[23996, 32539], [35410, 44925], [49493, 57410], [60458, 68635], [73308, 81278], [84001, 91942], [97381, 104166], [109018, 115573]] sftp
Output some examples of how your VAD algorithm works, by analogy with the example, for other audio recordings. Try to achieve the best quality of finding speech.
- Assistant
A simple Voice Activity Detection (VAD) algorithm can be implemented using an energy threshold and zero-crossing rate. By scanning the audio signal and comparing the energy and zero-crossing rate values against predetermined thresholds, it's possible to detect speech segments.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.