-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from AdityaSeth777/dev
Added features.
- Loading branch information
Showing
6 changed files
with
75 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |