Giter Club home page Giter Club logo

audio-spectrum-analyzer-in-python's Introduction

Audio-Spectrum-Analyzer-in-Python

A live audio spectrum analyzer using pyAudio, matplotlib and scipy.

pip install numpy
pip install matplotlib
pip install scipy

# Installing pyAudio
pip install pipwin
pipwin install pyaudio

# Running the spectrum analyzer
python spec_anim.py

Made from a YouTube tutorial series found here.

A series of Jupyter notebooks and python files which stream audio from a microphone using pyaudio.

Part 1 is a notebook which streams audio and displays the waveform with matplotlib.

Part 2 adds a spectrum viewer using scipy.fftpack to compute the FFT.

audio-spectrum-analyzer-in-python's People

Contributors

markjay4k avatar omarisai avatar perlinwarp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

audio-spectrum-analyzer-in-python's Issues

AttributeError

I have experimented with the 'Notebook' code and created a Python File for it. I tried running it in both Python and Jyupter and it came up with this -

Traceback (most recent call last):
File "spectrum_analyzer.py", line 30, in <module>
    line.set_ydata(data_int)
AttributeError: 'list' object has no attribute 'set_ydata'

A little help would be great!

It is not showing my live speech wave form

whenever I ran the code it is showing continuous wave forms without having any input.After connecting michrophone also it is giving the same waveforms always(oscillations). How can I make it take my live speech and show it as wave form.
Thanking you

Unable to load numpy_formathandler accelerator from OpenGL_accelerate

Hi Mark,

I came across your python audio visualizer on youtube. I decided to re-create it for my youtube channel. I'm having some trouble with loading the "numpy_formathandler accelerator from OpenGL_accelerate". When I run the code it throws back this error and I am not able to maneuver the grid like you are doing in the video. It kind of just sits there until I close out of the python shell.

screen shot 2018-01-12 at 10 26 03 pm

screen shot 2018-01-12 at 10 26 13 pm

I did some research on why this could be happening.. something about the PyOpenGL install not downloading properly. I'm still troubleshooting and researching this error but was wondering if you have any ideas as to what may be happening. I have also attached my version of the code so that you can see if I can mistyping or missing anything.

https://github.com/thedigitalempress/screenDance

Thanks!

Odd error in attempts to run audio_spectrum.py

I'm a bit new to Python and some of the modules used here. Odd errors, that I'm just unable to troubleshoot.

 thismanager.window.setGeometry( 5, 120, 1910, 1070)
  File "/usr/lib/python3.5/tkinter/__init__.py", line 1952, in __getattr__
    return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'setGeometry'

I'm using Python3 on a Xubuntu (Ubuntu with XFCE) in a virtualbox. I've tested pyAudio samples so I know microphone (and speakers) are fully functional. Running in a Python virtual environment.

Note: I'm able to run $ python3 audio_spectrumQT.py successfully in the same virtualbox. Works just fine.

Any clue as to what's going on here?

Many thanks to Mark Jay for the youtube videos and this github repo, enabling us to better learn how Fast Fourier Transforms (FFT) work.

reference:

Let's Build an Audio Spectrum Analyzer in Python! (pt. 1) the waveform viewer.
https://www.youtube.com/watch?v=AShHJdSIxkY
Mark Jay        Published on Sep 9, 2017

Let's Build an Audio Spectrum Analyzer in Python! (pt. 2) the spectrum viewer
https://www.youtube.com/watch?v=aQKX3mrDFoY
Mark Jay        Published on Sep 10, 2017

Let's Build an Audio Spectrum Analyzer in Python! (pt. 3) Switching to PyQtGraph
https://www.youtube.com/watch?v=RHmTgapLu4s
Mark Jay        Published on Oct 8, 2017

RuntimeWarning: divide by zero encountered in log10 x = np.log10(x)

I copied the code exactly and my spectrum plot isn't showing anything. I am getting "...\site-packages\pyqtgraph\graphicsItems\PlotDataItem.py:531: RuntimeWarning: divide by zero encountered in log10 x = np.log10(x)" in red but the program still runs. Please help its urgent thank you.

Converting chunk to numpy array

I use the code below and it seems fine.

data = stream.read(CHUNK)
data_np = np.fromstring(data, np.int16) <=== matching "FORMAT = pyaudio.paInt16"

Conversion of audio data to numpy array seems to be buggy...

The two steps conversion of audio data to numpy array seems buggy to me.
It should take into account (and afaik it does not) that the most significant bit of every second byte has negative weight (i.e. the numeric representation is in two's complement).
Perhaps a good starting point is to use this one step conversion
data_np = np.frombuffer(data_inp, dtype='<i2')
which lands in the range [-32768..+32767] and add an offset if desired

Error

I ran the code and I'm getting this error:
self.app = QtApplications.QApplication(sys.argv) NameError: name 'QtApplications' is not defined
Can anyone help me?
Thanks

syntax error and module error

Hello,

I am trying to use this code on a raspberry pito make a live oscilloscope (as a prototype) for my modular synthesizer. I have 2 main errors.

one says:
File "/home/pi/Desktop/Waveformviewer1.py", line 13
%matplotlib tk
^
SyntaxError: invalid syntax

I tried bypassing it by commenting it with a #. but it brought up:

                        File "/home/pi/Desktop/Waveformviewer1.py", line 5, in <module>
                           import pyaduio
                        ImportError: No module named 'pyaudio'

To resolve this I have tried running install commands in the terminal updates upgrades all that sort of back-end stuff but it's still not working.

please help

Pop-up window of Audio-spectrum analyzer is appearing but it seems that there is no signal input as there is a non moving flat line on the 2 displays

Hello Mark

First of all all thank you very much for the very nice video on youtube with great ideas and the script that you provide on your website. Those are very interesting projects!

I'm trying in macOS 10.14 and run then code of Spectrum analyzer that you provide with the following code.

"""
Notebook for streaming data from a microphone in realtime

audio is captured using pyaudio
then converted from binary data to ints using struct
then displayed using matplotlib

scipy.fftpack computes the FFT

if you don't have pyaudio, then run

>>> pip install pyaudio

note: with 2048 samples per chunk, I'm getting 20FPS
when also running the spectrum, its about 15FPS
"""

import pyaudio
import os
import struct
import numpy as np
import matplotlib.pyplot as plt
from scipy.fftpack import fft
import time
from tkinter import TclError

# to display in separate Tk window
%matplotlib tk

# constants
CHUNK = 1024 * 2             # samples per frame
FORMAT = pyaudio.paInt16     # audio format (bytes per sample?)
CHANNELS = 1                 # single channel for microphone
RATE = 44100                 # samples per second

# create matplotlib figure and axes
fig, (ax1, ax2) = plt.subplots(2, figsize=(15, 7))

# pyaudio class instance
p = pyaudio.PyAudio()

# stream object to get data from microphone
stream = p.open(
    format=FORMAT,
    channels=CHANNELS,
    rate=RATE,
    input=True,
    output=True,
    frames_per_buffer=CHUNK
)

# variable for plotting
x = np.arange(0, 2 * CHUNK, 2)       # samples (waveform)
xf = np.linspace(0, RATE, CHUNK)     # frequencies (spectrum)

# create a line object with random data
line, = ax1.plot(x, np.random.rand(CHUNK), '-', lw=2)

# create semilogx line for spectrum
line_fft, = ax2.semilogx(xf, np.random.rand(CHUNK), '-', lw=2)

# format waveform axes
ax1.set_title('AUDIO WAVEFORM')
ax1.set_xlabel('samples')
ax1.set_ylabel('volume')
ax1.set_ylim(0, 255)
ax1.set_xlim(0, 2 * CHUNK)
plt.setp(ax1, xticks=[0, CHUNK, 2 * CHUNK], yticks=[0, 128, 255])

# format spectrum axes
ax2.set_xlim(20, RATE / 2)

print('stream started')

# for measuring frame rate
frame_count = 0
start_time = time.time()

while True:

    # binary data
    data = stream.read(CHUNK)

    # convert data to integers, make np array, then offset it by 127
    data_int = struct.unpack(str(2 * CHUNK) + 'B', data)

    # create np array and offset by 128
    data_np = np.array(data_int, dtype='b')[::2] + 128

    line.set_ydata(data_np)

    # compute FFT and update line
    yf = fft(data_int)
    line_fft.set_ydata(np.abs(yf[0:CHUNK])  / (128 * CHUNK))

    # update figure canvas
    try:
        fig.canvas.draw()
        fig.canvas.flush_events()
        frame_count += 1

    except TclError:

        # calculate average frame rate
        frame_rate = frame_count / (time.time() - start_time)

        print('stream stopped')
        print('average frame rate = {:.0f} FPS'.format(frame_rate))
        break

I made sure to allow Atom (my text editor) and nteract to the microphone.

There is a pop-up window for both spectrum and wave appearance. However the lines of both those lines stays flat as follow as if there was no signal input:

screenshot 2018-11-05 at 15 13 29

At the same time I got the following error message in the text editor:

stream started
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-4-7818c8173441> in <module>()
     80 
     81     # binary data
---> 82     data = stream.read(CHUNK)
     83 
     84     # convert data to integers, make np array, then offset it by 127

/anaconda3/lib/python3.6/site-packages/pyaudio.py in read(self, num_frames, exception_on_overflow)
    606                           paCanNotReadFromAnOutputOnlyStream)
    607 
--> 608         return pa.read_stream(self._stream, num_frames, exception_on_overflow)
    609 
    610     def get_read_available(self):

OSError: [Errno -9981] Input overflowed

Do you have any idea of where the problem could be

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.