def vad(audiodata, samplerate, frameduration=0.025, energythreshold=0.6): framesize =…
- User
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 - Assistant
The vad function is a simplistic voice activity detection (VAD) algorithm that identifies speech segments in an audio signal. It takes the following parameters:
- audio_data: A 1D NumPy array of the audio data samples.
- sample_rate: The sample rate of the audio data (e.g., 16000 for 16 kHz).
- frame_duration: The duration of each frame in seconds used for analysis (default is 0.025, i.e., 25 milliseconds).
- energy_threshold: The threshold value used to determine if a frame contains speech or not (default is 0.6).
The function works by dividing the input audio signal into frames with the specified frame_duration and calculates the energy (sum of squares of samples) for each frame. If the energy is greater than the given energy_threshold, it is considered speech, and if it's lower, it is considered silence.
It then creates a Boolean array with the same length as audio_data marking the frame's status, True for silence and False for speech. Using this array, it extracts the start and end indices of speech segments, ignoring the silence parts, and stores them as pairs in a list called segments.
- 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=550 warnings.warn(
IndexError Traceback (most recent call last) Cell In[12], line 18 15 vad_segments_test = [vad(audio, sample_rate) for audio in X_test] 17 # Создайте обучающий и тестовый датасеты с использованием energy_vad сегментов и функции make_dataset ---> 18 X_train_stft, Y_train_stft = make_dataset(X_train, labels_train, vad_segments_train) 19 X_test_stft, Y_test_stft = make_dataset(X_test, labels_test, vad_segments_test)
Cell In[11], line 79, in make_dataset(samples, labels, vad_segments) 76 stft_db = librosa.amplitude_to_db(abs(stft)) 78 X.append(stft_db) # Добавляем спектрограмму с речью ---> 79 y.append(labels[sample][segment]) # Добавляем метку для этой спектрограммы 81 return np.array(X), np.array(y)
IndexError: list index out of range
- Assistant
The IndexError you're encountering is likely because there's a mismatch between the length of the
labelslist and the number of segments identified by the VAD function. Make sure that thevad_segmentslist contains the same number of segments as the number of elements in the inputlabelslist, as this might be causing the issue.One possible solution to avoid this IndexError is to check the length of
vad_segmentsandlabelsbefore iterating through them in themake_datasetfunction, and only process those samples that have matching labels and VAD segments:def make_dataset(samples, labels, vad_segments): X = [] y = [] for sample in range(len(samples)): if len(vad_segments[sample]) != len(labels[sample]): continue audio_data = samples[sample] for segment in range(len(vad_segments[sample])): start, end = vad_segments[sample][segment] stft = librosa.stft(audio_data[start:end]) stft_db = librosa.amplitude_to_db(abs(stft)) X.append(stft_db) # Add speech spectrogram y.append(labels[sample][segment]) # Add label for this spectrogram return np.array(X), np.array(y) - User
:70: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray. return np.array(X), np.array(y)
ValueError Traceback (most recent call last) in <cell line: 18>() 16 17 # Создайте обучающий и тестовый датасеты с использованием energy_vad сегментов и функции make_dataset ---> 18 X_train_stft, Y_train_stft = make_dataset(X_train, labels_train, vad_segments_train) 19 X_test_stft, Y_test_stft = make_dataset(X_test, labels_test, vad_segments_test)
in make_dataset(samples, labels, vad_segments) 68 y.append(labels[sample][segment]) # Add label for this spectrogram 69 ---> 70 return np.array(X), np.array(y)
ValueError: could not broadcast input array from shape (1025,12) into shape (1025,)
- Assistant
The ValueError you are encountering is caused by trying to create a NumPy array from a list of arrays with different shapes. In this case, the shape of the STFT spectrograms (elements in the list X) might vary due to differently-sized segments produced by VAD.
To resolve this issue and create a dataset with fixed-shape arrays, you can use padding or truncating the segments to a specific length or choosing a fixed segment length for the vad function.
Here's a modification of the make_dataset function that pads or truncates the spectrograms to a fixed length before adding to the dataset. You can adjust the max_length parameter to control the length of the resulting spectrograms:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.