This Python script, implemented as a class-based audio processor, provides functionality for recording audio, playing it back, and visualizing the waveform using sounddevice, pygame, and matplotlib.
- Python 3.x
- ๐ต sounddevice
- ๐งฎ numpy
- ๐น๏ธ pygame
- ๐ matplotlib
Install the dependencies using:
pip install -r requirements.txt
Make sure to create a virtual environment and activate it before installing the dependencies.
- Clone the repository:
git clone https://github.com/ansarialireza/SpeechSignalVisualization.git
cd SpeechSignalVisualization
- Run the script:
python audio_processor.py
Adjust the duration
variable in the script to change the recording length.
import sounddevice as sd
import numpy as np
import pygame
import matplotlib.pyplot as plt
import random
class AudioProcessor:
def __init__(self, sample_rate=44100):
# ...
def record_audio(self, duration):
# ...
def play_audio(self, audio_data):
# ...
def plot_audio_waveform(self, audio_data, save_plot=True, plot_filename='audio_waveform.png'):
# ...
Records audio for the specified duration.
Plays back the recorded audio using pygame.
Plots and optionally saves the audio waveform using matplotlib.
if __name__ == "__main__":
audio_processor = AudioProcessor()
duration = 5
recorded_audio = audio_processor.record_audio(duration)
audio_processor.play_audio(recorded_audio)
audio_processor.plot_audio_waveform(recorded_audio)
This script records audio for 5 seconds, plays it back, and plots the waveform.
The waveform image (audio_waveform.png
) is saved in the same directory as the script and displayed below:
You can find the visual representation of the recorded audio waveform in this image.
This project is licensed under the MIT License - see the LICENSE file for details.