Skip to content

Commit

Permalink
Merge pull request #6 from AdityaSeth777/dev
Browse files Browse the repository at this point in the history
Added features.
  • Loading branch information
AdityaSeth777 authored Mar 28, 2024
2 parents e26e299 + 9e041a4 commit d20d294
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Audio_SpectraCLI/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# __init__.py

from .main import audio_visualizer
from .main import AudioSpectrumVisualizer
74 changes: 42 additions & 32 deletions Audio_SpectraCLI/main.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,62 @@
# 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)
line.set_ydata(spectrum)
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']
30 changes: 21 additions & 9 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.

Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[metadata]
name = Audio-SpectraCLI
version = 0.1
version = 2.2
author = Aditya Seth
author_email = [email protected]
author_email = [email protected]
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
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=[
Expand Down
9 changes: 7 additions & 2 deletions tests/main.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit d20d294

Please sign in to comment.