diff --git a/Audio_SpectraCLI/__init__.py b/Audio_SpectraCLI/__init__.py index 7ccf813..b28a9fd 100644 --- a/Audio_SpectraCLI/__init__.py +++ b/Audio_SpectraCLI/__init__.py @@ -1,3 +1,3 @@ # __init__.py -from .main import audio_visualizer +from .main import AudioSpectrumVisualizer diff --git a/Audio_SpectraCLI/main.py b/Audio_SpectraCLI/main.py index 5329f76..12149c3 100644 --- a/Audio_SpectraCLI/main.py +++ b/Audio_SpectraCLI/main.py @@ -1,40 +1,45 @@ -# main.py - -def audio_visualizer(): - import numpy as np - import matplotlib.pyplot as plt - import sounddevice as sd - import queue - import threading - import time - duration = 10 - fs = 44100 - block_size = 2048 - num_blocks = int(duration * fs / block_size) - - audio_queue = queue.Queue() - - def audio_callback(indata, frames, time, status): +# Aditya Seth +# Description: This file contains the main code for the Audio-SpectraCLI project. It is responsible for creating the AudioSpectrumVisualizer class which is used to visualize the audio spectrum in real-time. + +import numpy as np +import matplotlib.pyplot as plt +import sounddevice as sd +import queue +import threading +import time + + +class AudioSpectrumVisualizer: + def __init__(self, duration=10, fs=44100, block_size=2048, frequency_range=(20, 20000), color='blue'): + self.duration = duration + self.fs = fs + self.block_size = block_size + self.num_blocks = int(duration * fs / block_size) + self.frequency_range = frequency_range + self.color = color + self.audio_queue = queue.Queue() + + def audio_callback(self, indata, frames, time, status): if status: print(status) - audio_queue.put(indata.copy()) + self.audio_queue.put(indata.copy()) - def process_audio(): - freq_bins = np.fft.fftfreq(block_size, 1/fs) - spectrum = np.zeros(block_size) + def process_audio(self): + freq_bins = np.fft.fftfreq(self.block_size, 1/self.fs) + spectrum = np.zeros(self.block_size) plt.ion() fig, ax = plt.subplots() - x = np.arange(0, block_size) - line, = ax.plot(freq_bins, spectrum) - ax.set_xlim(0, 5000) + x = np.arange(0, self.block_size) + line, = ax.plot(freq_bins, spectrum, color=self.color) + ax.set_xlim(self.frequency_range) ax.set_ylim(0, 0.1) ax.set_xlabel('Frequency (Hz)') ax.set_ylabel('Magnitude') while True: - audio_block = audio_queue.get() - spectrum = np.abs(np.fft.fft(audio_block[:, 0], n=block_size)) + audio_block = self.audio_queue.get() + spectrum = np.abs(np.fft.fft(audio_block[:, 0], n=self.block_size)) max_magnitude = np.max(spectrum) if max_magnitude > ax.get_ylim()[1]: ax.set_ylim(0, max_magnitude * 1.1) @@ -42,11 +47,16 @@ def process_audio(): fig.canvas.draw() fig.canvas.flush_events() - audio_thread = threading.Thread(target=process_audio, daemon=True) - audio_thread.start() + def start_visualization(self): + audio_thread = threading.Thread(target=self.process_audio, daemon=True) + audio_thread.start() + + with sd.InputStream(callback=self.audio_callback, channels=1, samplerate=self.fs): + while True: + time.sleep(1) + + audio_thread.join() - with sd.InputStream(callback=audio_callback, channels=1, samplerate=fs): - while True: - time.sleep(1) - audio_thread.join() +# List of functions available for use: +__all__ = ['AudioSpectrumVisualizer'] diff --git a/Readme.md b/Readme.md index c8e607c..189a0c2 100644 --- a/Readme.md +++ b/Readme.md @@ -9,12 +9,13 @@ Audio Spectrum Visualization is a Python project that visualizes real-time audio input as a spectrum using Fast Fourier Transform (FFT). It provides an interactive CLI interface for users to start the visualization and exit the program. +## Current Features (with respect to 2.2) -## Features - -- Real-time audio spectrum visualization. -- Interactive CLI menu for easy navigation. -- Cross-platform compatibility (works on Windows, macOS, and Linux). +- Real-time visualization of Fast Fourier Transform (FFT) spectrum of audio input. +- Support for adjusting parameters such as duration, sampling rate, and block size. +- Seamless integration with SoundDevice for audio input capture. +- Customizable Frequency Range: Allow users to specify the frequency range to display in the spectrum. +- Color Customization: Provide options for users to customize the colors used in the spectrum visualization. ## Packaging @@ -51,17 +52,28 @@ pip install Audio-SpectraCLI 2. Import and use modules +- Create a Python file. +- You can use [Example.py](./tests/main.py) as a reference or use the following code : + ``` -from Audio_SpectraCLI import audio_visualizer +from Audio_SpectraCLI import AudioSpectrumVisualizer + +# Creating an instance of AudioSpectrumVisualizer with custom parameters. +audio_visualizer = AudioSpectrumVisualizer( + duration=5, frequency_range=(50, 5000), color='red') + +# Starting the audio spectrum visualization +audio_visualizer.start_visualization() ``` +- Once you have activated the audio_visualizer instance, feel free to use it wherever in the program. It consists of several parameters (which gives more control to the user), so make sure to configure and add those before using it in your code. + --- ## Upcoming Features -- CLI endpoints -- Customizable Frequency Range: Allow users to specify the frequency range to display in the spectrum. -- Color Customization: Provide options for users to customize the colors used in the spectrum visualization. +- CLI endpoints. +- Option to choose between CLI/GUI. - Save and Export: Implement functionality to save the generated spectrum as an image file or export data for further analysis. - Additional Audio Effects: Integrate additional audio effects or processing options to enhance the visualization. diff --git a/setup.cfg b/setup.cfg index f6b9551..7cea251 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,8 +1,8 @@ [metadata] name = Audio-SpectraCLI -version = 0.1 +version = 2.2 author = Aditya Seth -author_email = setha4195@gmail.com +author_email = contact@adityaseth.in description = AudioSpectraCLI is a command-line tool that provides real-time FFT visualization of audio spectra. It captures audio input from the microphone and displays the corresponding frequency spectrum directly in the terminal window, allowing users to monitor and analyze audio signals without the need for graphical interfaces. license = MIT home-page = https://github.com/AdityaSeth777/Audio-SpectraCLI \ No newline at end of file diff --git a/setup.py b/setup.py index 956f75c..358a0b2 100644 --- a/setup.py +++ b/setup.py @@ -2,9 +2,9 @@ setup( name='Audio_SpectraCLI', - version='2.1', + version='2.2', author="Aditya Seth", - long_description=open('Readme.md').read(), + long_description=open('Readme.md', encoding='utf-8').read(), long_description_content_type='text/markdown', packages=find_packages(), install_requires=[ diff --git a/tests/main.py b/tests/main.py index 3415668..2b2b6b5 100644 --- a/tests/main.py +++ b/tests/main.py @@ -1,3 +1,8 @@ -from Audio_SpectraCLI import audio_visualizer +from Audio_SpectraCLI import AudioSpectrumVisualizer -audio_visualizer() +# Create an instance of AudioSpectrumVisualizer with custom parameters +audio_visualizer = AudioSpectrumVisualizer( + duration=5, frequency_range=(50, 5000), color='red') + +# Start the audio spectrum visualization +audio_visualizer.start_visualization()