This exercise plays and writes an audio file, then draws a sine wave
You can find various audio files within the following link:
Free Audio-Files Sound Effects Download - Pixabay
Step 1: Import the required libraries
import matplotlib.pyplot as plt
import librosa # Install this with pip install librosa
import librosa.display
from pydub import AudioSegment # Install this with pip install pydub
from pydub.playback import play
import os
Step 2: Read an audio file and then convert it from mp3 to wav
Note: We must download ffmpeg
and ffprobe
for pydub
to work. Simply write: brew install ffmpeg
and brew install ffprobe
.
# Convert mp3 to wav
file_path = 'birdsinging.mp3'
audio = AudioSegment.from_mp3(file_path)
export_path = "birdsinging.wav"
audio.export(export_path, format="wav")
print("Converted mp3 to wav!!")
# Play the audio file
print("Playing the audio file...", file_path)
play(audio)
Output:
Converted mp3 to wav!!
Playing the audio file... birdsinging.mp3
Input #0, wav, from '/var/folders/gc/49grx5f90nv3f12yh4jhhf740000gn/T/tmpz2ap8nyd.wav':
Duration: 00:00:19.64, bitrate: 1411 kb/s
Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, 2 channels, s16, 1411 kb/s
19.57 M-A: 0.000 fd= 0 aq= 0KB vq= 0KB sq= 0B
Write the audio file with librosa, then return: