for i in range(10): # Just showing the results for the first 10 files
print(f"File: {files[i]}".ljust(25), end='')
vad_segments = energy_vad(X[i], sr_list[i])
print(f"Speech Segments: {vad_segments}")
Assistant
Unfortunately, I don't have access to the dataset you're using, but I can provide general steps to implement VAD and analyze the speech segments from an audio file. Here's the step by step process:
To use the directory you provided, simply replace the directory variable in the code with your directory path.
Here's the updated code with your directory path:
python
User
C:\Users\lpoti\AppData\Local\Temp\ipykernel_1544\2768444591.py:38: UserWarning: PySoundFile failed. Trying audioread instead.
audio, sr = librosa.load(file_path, sr=None)
c:\Users\lpoti\AppData\Local\Programs\Python\Python311\Lib\site-packages\librosa\core\audio.py:184: FutureWarning: librosa.core.audio.__audioread_load
Deprecated as of librosa version 0.10.0.
It will be removed in librosa version 1.0.
y, sr_native = __audioread_load(path, offset, duration, dtype)
LibsndfileError Traceback (most recent call last)
File c:\Users\lpoti\AppData\Local\Programs\Python\Python311\Lib\site-packages\librosa\core\audio.py:176, in load(path, sr, mono, offset, duration, dtype, res_type)
175 try:
--> 176 y, sr_native = __soundfile_load(path, offset, duration, dtype)
178 except sf.SoundFileRuntimeError as exc:
179 # If soundfile failed, try audioread instead
Assistant
It seems like there is an issue with the audio file format, and both PySoundFile and audioread backends of librosa failed to read the file. To solve this problem, you can try doing the following:
Make sure your audio files are in a format supported by PySoundFile or audioread, such as WAV, MP3, or FLAC. If your files are in a different format, you may need to convert them first.
Check if the files in the dataset are actually audio files. Sometimes, there might be some non-audio files inside the folder.
Make sure you have the required dependencies installed for PySoundFile and audioread. For example, you may need to install the package and the library if they are not already installed.
Define a function called energy_vad that takes an audio signal, sample rate, and some other parameters as input and returns the speech segments.
Read an audio file using the librosa.load() method.
Apply the energy_vad function on the audio signal to find the speech segments.
Here is a sample code with comments explaining each step:
python
import librosa
import numpy as np
import os
def energy_vad(audio, sr, frame_size=25, hop_size=10, energy_threshold=0.02, zcr_threshold=0.02):
frame_length = int(sr * frame_size / 1000)
hop_length = int(sr * hop_size / 1000)
energy = np.square(librosa.feature.rms(audio, frame_length=frame_length, hop_length=hop_length))
zcr = librosa.feature.zero_crossing_rate(audio, frame_length=frame_length, hop_length=hop_length)
speech_energy = np.squeeze(energy) > energy_threshold
speech_zcr = np.squeeze(zcr) > zcr_threshold
speech_segments = []
speech = False
start = 0
for i in range(len(speech_energy)):
if speech_energy[i] and speech_zcr[i]:
if not speech:
speech = True
start = i * hop_length
else:
if speech:
speech = False
end = i * hop_length
speech_segments.append((start, end))
return speech_segments
# Replace with your dataset directory
directory = "/path/to/your/dataset"
# Iterate over files in the directory
for file_name in os.listdir(directory):
# Load the audio file
file_path = os.path.join(directory, file_name)
audio, sr = librosa.load(file_path, sr=None)
# Apply VAD to find speech segments
vad_segments = energy_vad(audio, sr)
print(f"File: {file_name}".ljust(25), end='')
print(f"Speech Segments: {vad_segments}")
This script will print the speech segments found in each audio file in the provided dataset directory. Replace the dataset directory with the path to your dataset, and you should have a working example to analyze speech segments from audio files.
import librosa
import numpy as np
import os
def energy_vad(audio, sr, frame_size=25, hop_size=10, energy_threshold=0.02, zcr_threshold=0.02):
frame_length = int(sr * frame_size / 1000)
hop_length = int(sr * hop_size / 1000)
energy = np.square(librosa.feature.rms(audio, frame_length=frame_length, hop_length=hop_length))
zcr = librosa.feature.zero_crossing_rate(audio, frame_length=frame_length, hop_length=hop_length)
speech_energy = np.squeeze(energy) > energy_threshold
speech_zcr = np.squeeze(zcr) > zcr_threshold
speech_segments = []
speech = False
start = 0
for i in range(len(speech_energy)):
if speech_energy[i] and speech_zcr[i]:
if not speech:
speech = True
start = i * hop_length
else:
if speech:
speech = False
end = i * hop_length
speech_segments.append((start, end))
return speech_segments
# Use the directory you provided
directory = "C:\\Users\\lpoti\\Documents\\DS_21\\audio_yes_no\\waves_yesno 2"
# Iterate over files in the directory
for file_name in os.listdir(directory):
# Load the audio file
file_path = os.path.join(directory, file_name)
audio, sr = librosa.load(file_path, sr=None)
# Apply VAD to find speech segments
vad_segments = energy_vad(audio, sr)
print(f"File: {file_name}".ljust(25), end='')
print(f"Speech Segments: {vad_segments}")
Copy and paste this code into your Python script, and it should analyze the speech segments from the audio files in the provided directory. If you have any issues with the path due to different operating system or environment, try using the raw string format for the directory path: