Spoken Language Processing in Python
Daniel Bourke
Machine Learning Engineer/YouTube Creator
from pydub import AudioSegment # Import audio file wav_file = AudioSegment.from_file("wav_file.wav")
# Increase by 10 decibels louder_wav_file = wav_file + 10
# Export louder audio file louder_wav_file.export(out_f="louder_wav_file.wav", format="wav")
<_io.BufferedRandom name='louder_wav_file.wav'>
def make_wav(wrong_folder_path, right_folder_path):
# Loop through wrongly formatted files for file in os.scandir(wrong_folder_path):
# Only work with files with audio extensions we're fixing if file.path.endswith(".mp3") or file.path.endswith(".flac"):
# Create the new .wav filename out_file = right_folder_path + os.path.splitext(os.path.basename(file.path))[0] + ".wav"
# Read in the audio file and export it in wav format AudioSegment.from_file(file.path).export(out_file, format="wav")
print(f"Creating {out_file}")
# Call our new function
make_wav("data/wrong_formats/", "data/right_format/")
Creating data/right_types/wav_file.wav
Creating data/right_types/flac_file.wav
Creating data/right_types/mp3_file.wav
def make_no_static_louder(static_quiet, louder_no_static): # Loop through files with static and quiet (already in wav format) for file in os.scandir(static_quiet_folder_path):
# Create new file path out_file = louder_no_static + os.path.splitext(os.path.basename(file.path))[0] + ".wav"
# Read the audio file audio_file = AudioSegment.from_file(file.path)
# Remove first three seconds and add 10 decibels and export audio_file = (audio_file[3100:] + 10).export(out_file, format="wav") print(f"Creating {out_file}")
# Remove static and make louder
make_no_static_louder("data/static_quiet/", "data/louder_no_static/")
Creating data/louder_no_static/speech-recognition-services.wav
Creating data/louder_no_static/order-issue.wav
Creating data/louder_no_static/help-with-acount.wav
Spoken Language Processing in Python