import librosa import numpy as np def energyvad(audio, sr, framesize=25, hopsize=10,…
- User
import librosa import numpy as np
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_segmentsLoad the dataset
directory = "/content/drive/MyDrive/School21/day09/datasets/audio_yes_no/waves_yesno 2" X, labels, sr_list, files = load_dataset(directory)
Apply VAD to find speech segments
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:
- Import required libraries.
- Define a function called
energy_vadthat takes an audio signal, sample rate, and some other parameters as input and returns the speech segments.
- User
"C:\Users\lpoti\Documents\DS_21\audio_yes_no\waves_yesno 2"
- Assistant
To use the directory you provided, simply replace the
directoryvariable in the code with your directory path.Here's the updated code with your directory path:
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}") - 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
PySoundFileandaudioreadbackends oflibrosafailed 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
PySoundFileoraudioread, 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
PySoundFileandaudioread. For example, you may need to install theSoundFilepackage and theffmpeglibrary if they are not already installed.
- Make sure your audio files are in a format supported by
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.