DEV Community

Cover image for 🎤🎶 Elevate Your Sound Game: Recording and Processing Audio with Python! 🎧🔊
Baivab Sarkar
Baivab Sarkar

Posted on • Updated on

🎤🎶 Elevate Your Sound Game: Recording and Processing Audio with Python! 🎧🔊

🎵Explore functionalities like recording voice, applying filters, visualizing waveforms, and more!
Check out my GitHub repo for a collection of Python scripts for audio recording, playback, and processing. 🚀 #Python #AudioProcessing

GitHub: Python/Audio Recorder
Image description
Source Code: Python/Audio Recorder/record audio.ipynb

import os
import librosa
import librosa.display
import matplotlib.pyplot as plt
import warnings
import numpy as np
warnings.filterwarnings("ignore", category=RuntimeWarning)

def load_audio(file_path, duration=None, offset=0):
    audio_data, sample_rate = librosa.load(file_path, duration=duration, offset=offset)
    return audio_data, sample_rate

def plot_waveform(audio_data, sample_rate, output_path):
    plt.figure(figsize=(10, 4))
    librosa.display.waveshow(audio_data, sr=sample_rate)
    plt.title("Waveform")
    plt.xlabel("Time (seconds)")
    plt.ylabel("Amplitude")
    plt.savefig(os.path.join(output_path, "waveform.png"))
    plt.show()
    plt.close()

def plot_spectrogram(audio_data, sample_rate, output_path):
    plt.figure(figsize=(10, 4))
    spectrogram = librosa.feature.melspectrogram(y=audio_data, sr=sample_rate)
    librosa.display.specshow(librosa.power_to_db(spectrogram, ref=np.max), y_axis='mel', x_axis='time')
    plt.colorbar(format='%+2.0f dB')
    plt.title("Spectrogram")
    plt.savefig(os.path.join(output_path, "spectrogram.png"))
    plt.show()
    plt.close()

if __name__ == "__main__":
    input_file = "recorded_voice_sounddevice.wav"
    output_path = "."  # You can change this to the desired output directory

    audio_data, sample_rate = load_audio(input_file)

    # Print some information about the audio file
    print(f"Audio data shape: {audio_data.shape}")
    print(f"Sample rate: {sample_rate} Hz")

    # Plot the audio waveform and spectrogram
    plot_waveform(audio_data, sample_rate, output_path)
    plot_spectrogram(audio_data, sample_rate, output_path)

    print("Plots saved as 'waveform.png' and 'spectrogram.png' in the current directory.")
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
hardikchopra profile image
Hardik Chopra

Welcome aboard! Your first article sounds exciting and informative. Looking forward to exploring the world of audio with Python through your expertise! 😊🎶

Collapse
 
thisisdeveloper profile image
Baivab Sarkar

Thank you❤️
Absolutely, let's dive in! 🎧🐍 Get ready to explore the wonderful realm of audio with Python. 🎶🔊 Feel free to ask anything along the way. Let's make coding and sound collide! 🎵🤖