Let's Build an Audio Spectrum Analyzer in Python! (pt. 1) the waveform viewer.

Поділитися
Вставка
  • Опубліковано 30 вер 2024

КОМЕНТАРІ • 327

  • @jkrigelman
    @jkrigelman 5 років тому +67

    The data format over the audio channel you requested is a signed short value (Int16). But when you sed struct you tried to convert it to unsigned chars (UInt8). You should be able to get the data with struct if you change it to unpack your sample size as signed short data.
    data_int = struct.unpack( str( CHUNK ) + 'h', data ) # Signed Short - 16 bits
    This will give you your sample set about the origin as you would typically see in a time plot with no extra manipulation.

    • @ssbian9520
      @ssbian9520 4 роки тому

      how can we modify the plot ?

  • @betr8
    @betr8 3 роки тому +19

    TIME TO MAKE MY FLYING ARROW CONTROLLED BY WHISTLE

  • @luisdanielosa
    @luisdanielosa 6 років тому +18

    This is great. Thanks a lot for posting it. Setting data_np = np.frombuffer(data, dtype = np.int16) works a lot better (no need to subsample, pywave datatype es int16). You'd need to readjust ax.set_ylim(-2**15, 2**15)

    • @MarkJay
      @MarkJay  6 років тому +2

      thanks! yes definitely that's a better option. makes the code cleaner and faster

  • @priyankasrivastava6757
    @priyankasrivastava6757 5 років тому +9

    I am using MacOS and i am getting 0s (zeros) value in my tuples that mean it didn't take any data from my audio? pls help me out.

    • @lazzapie
      @lazzapie 5 років тому +6

      Hi, I had this problem, try changing 'output = true' to 'output=False'. That worked for me. (running on macOS).

    • @yuvigerstein
      @yuvigerstein 5 років тому

      @@lazzapie Thanks, this worked for me, on MacOS :)

    • @jpaldama9963
      @jpaldama9963 4 роки тому

      output=false because that returns 2000+ samples at zero.

    • @rwharrington87
      @rwharrington87 4 роки тому

      Ah, makes sense.

  • @dosaimre
    @dosaimre 4 роки тому +5

    Hi Mark, This video series is really great ! I use Python 3.6.4 and the '%matplotlib tk' line gives an error; 'End of statement expected.'
    Thus the program does not run neither in IntelliJ nor in Jupyter notebook. I could change over to Pqt5 GUI, but want to get to the root of this issue. Can you give me any idea?

    • @acusticajax3618
      @acusticajax3618 3 роки тому +1

      I had the same problem in Spyder IDE ! I would like to know what may be causing this too, these compatibility issues are actually pretty common in python codes, unfortunately :/

  • @gasperstucin7860
    @gasperstucin7860 6 років тому +10

    if you are still getting weird display of your audio stream after adding 127, try adding 128 instead

    •  6 років тому

      thx bro it worked for me^^

  • @williamstanton6599
    @williamstanton6599 6 років тому +10

    Bytes are 8 bits and ints are 16 bits. Converting from bytes to ints adds a zero byte to the front, doubling the size of the data. Also with signed bytes everything from 128 -255 is considered negative.

    • @MarkJay
      @MarkJay  6 років тому +3

      yeah I definitely went about it wrong using struct. using numpy.frombuffer() really simplifies process going from bytes to ints

    • @gaoxuewu5411
      @gaoxuewu5411 5 років тому

      @@MarkJay I am so sorry.This cannot run in my pc. I use Spyder to running the program. my code is the same as yours.
      when I run the program, it cannot draw a figure and it will throw return pa.read_stream(self._stream, num_frames, exception_on_overflow). I cannot understand this. can you teach me ?

    • @edgecase928
      @edgecase928 5 років тому

      @@gaoxuewu5411 Try reducing the size of the chunk

  • @Marcin648
    @Marcin648 5 років тому +6

    Great series!
    We open the stream in Int16 format, so we can simply unpack it this way:
    data_int = struct.unpack(str(CHUNK) + 'h', data)

  • @darkknight3975
    @darkknight3975 6 років тому +3

    Why do you have 2 * CHUNK data? You showed that it is there but didn't explain why.. also, later you take every alternate data points(10:07) so maybe those extra data points are junk?

  • @ramkrishan2124
    @ramkrishan2124 5 років тому +4

    Try
    %matplotlib qt
    if tk gives you error

    • @MarkJay
      @MarkJay  5 років тому +1

      that or %matplotlib qt5

  • @mohammadbahabadi4086
    @mohammadbahabadi4086 5 років тому +12

    I keep getting syntax error on %matplotlib tk. Any idea where that could be from?

    • @nfarabee91
      @nfarabee91 5 років тому +1

      stackoverflow.com/questions/44225002/why-doesnt-matplotlib-inline-work-in-python-script

  • @rahulranjan7004
    @rahulranjan7004 3 роки тому +1

    Very thanks for uploading it .. but could u pls just tell how to make it react on computers audio also.. with microphone ??

  • @ealgoo
    @ealgoo 5 років тому +2

    Your video is really helpful in helping getting started with pyaudio. Thank you so much.
    Here is tip you can reduce complexity in changing 2 bytes into int... just one line has to be changed.
    Instead of
    >> data_int = np.array(struct.unpack(str(2*CHUNK)+'B', data), dtype='b')[::2] + 127
    use
    >> data_int = struct.unpack(str(CHUNK)+'h', data)
    :)

    • @edgecase928
      @edgecase928 5 років тому

      Why don't you multiply by 2? Because I got an error that says "unpack requires a buffer of 2048 bytes"

  • @rwharrington87
    @rwharrington87 4 роки тому +2

    If you're doing this in a normal python script via matplotlib.use('TkAgg'), you may have to enable interactive mode: plot.ion()

    • @mimanshumaheshwari
      @mimanshumaheshwari 3 роки тому

      could you explain how that works ....?

    • @rwharrington87
      @rwharrington87 3 роки тому

      @@mimanshumaheshwari Not really sure how it works behind the scenes. I just remember googling for solutions and this line fixed the issue 👍

  • @yanoaziz8632
    @yanoaziz8632 6 років тому +3

    so beautiful
    what dose it mean %matplotlib tk and how can fix my invalid syntax on python
    thanks

    • @MarkJay
      @MarkJay  6 років тому +3

      this is only needed if you're using jupyter. it creates a separate window for the plot. if you're using a normal python file, you can remove it

    • @yanoaziz8632
      @yanoaziz8632 6 років тому

      thanks

  • @pisoiorfan
    @pisoiorfan 5 років тому +2

    The chart is choppy at 8:00 because you unpack two byte integers into array of single bytes.

  • @arashroshani6693
    @arashroshani6693 6 років тому +3

    Great work there! Thank you for your video. I try what you are explaining and when I print the data all the entries are 0 as if there is no input. I am using my laptop's built-in microphone and I am confused about what is going wrong.

    • @zxx2623
      @zxx2623 5 років тому

      I have the same problem with you, did you already fix it?

  • @UrThysis
    @UrThysis 6 років тому +3

    i'd really wanna see some DSP and more audio tutorials with python because i've tried to learn JUCE for C++ but it's way more syntax heavy than python so I started with Py
    Great vid btw

  • @candyreid59
    @candyreid59 2 роки тому

    My script is hanging- I followed step by step and checked my output data binary then int and watched the plot as it changed with all the changes. Then when I did the automated update- thefig.canvas.draw()... the program hangs. I tried reducing CHUNK to just 2* instead of 4. Still hangs. im searching through the comments but i dont see anyone with this issue. I can only believe im doing something specifically odd. The only thing i see thats odd is that the args of np.arange are not highlighted as Mark's is. everything else is highlighted just as his is. help ?

  • @apeted1708
    @apeted1708 4 роки тому +1

    I am not getting the figure in separate figure window even on using % matplotlib tk, what to do ?

  • @paszczap
    @paszczap 3 роки тому +1

    Hi! :)
    Than you for the video :)
    I can't understand one thing. Can't we do it in much simplier way using:
    data_int = struct.unpack(str(2 * CHUNK) + 'b', data)
    (instead 'B') without any further conversion?

  • @negocioesquisito
    @negocioesquisito 6 років тому +5

    for me the window is not showing, i print the data_int in the console and the numbers are showing in console but the window not appears

    • @MarkJay
      @MarkJay  6 років тому

      do you have matplotlib installed?

    • @negocioesquisito
      @negocioesquisito 6 років тому +5

      i resolve putting this line 'plt.show(block=False)' before the while, thanks, do you know one way to get the frequency number of the audio ?

    • @MarkJay
      @MarkJay  6 років тому +1

      Negocio Esquisito awesome! Check out pt2 in the series. We add a spectrum analyzer

    • @Arik1989
      @Arik1989 5 років тому

      @@negocioesquisito Thanks , I didn't know about the 'block = False' thing, that's good to know in general

    • @MauriceBierhuizen
      @MauriceBierhuizen 5 років тому

      @@negocioesquisito thanks!

  • @intheblade
    @intheblade 5 років тому +1

    As you use pyaudio.paInt16 as format (16bit wave), why not use "struct.unpack(str(CHUNK) + 'h', data)" decode stream in short(int16)?

  • @russochypriota
    @russochypriota 4 роки тому +3

    Great tutorial, thanks for uploading! Do you know how it would be possible to read the output of the audio card, not the microphone?

    • @divyanshushambharka
      @divyanshushambharka 4 роки тому +1

      Actually, I was thinking the same.

    • @EvanColesHarris
      @EvanColesHarris 4 роки тому

      Ditto!

    • @saikatdas4614
      @saikatdas4614 3 роки тому

      what I am thinking is to use audio card output channels can be input through line in in MB and so the channel will be Line In. Pls correct me. I am gonna use this from my bluetooth ic output

    • @AdiMehaindroo
      @AdiMehaindroo 2 роки тому

      How can we do it seriously from the audio card so we dont have to use a mic

  • @TheRadiant78
    @TheRadiant78 2 роки тому +1

    HI! Thanks for video!! I have one Question, can this code work without struct , if i take data from librosa ??

    • @MarkJay
      @MarkJay  2 роки тому +1

      Thanks. Yes, I would recommend using numpy.frombuffer instead. Its much simpler to use and I think its faster

  • @Mecca0497
    @Mecca0497 6 років тому +2

    after I run the code, the display not really as good as you. It's only show 3 numbers; 0 1 and 255. The matplotlib window giving me a long vertical lines, even when my voice detected, it does moving but with vertical lines from the center then spread to left and right (no horizontal line)
    after I closed the window it gave me an error message:
    TclError Traceback (most recent call last)
    C:\Anaconda3\lib\site-packages\matplotlib\backends\tkagg.py in blit(photoimage, aggimage, bbox, colormode)
    21 "PyAggImagePhoto", photoimage,
    ---> 22 id(data), colormode, id(bbox_array))
    23 except Tk.TclError:
    TclError: this isn't a Tk application
    During handling of the above exception, another exception occurred:
    TclError Traceback (most recent call last)
    in ()
    20 data_int = np.array(struct.unpack(str(2 * CHUNK) + 'B', data), dtype='b')[::2] + 127
    21 line.set_ydata(data_int)
    ---> 22 fig.canvas.draw()
    23 fig.canvas.flush_events()
    C:\Anaconda3\lib\site-packages\matplotlib\backends\backend_tkagg.py in draw(self)
    350 def draw(self):
    351 FigureCanvasAgg.draw(self)
    --> 352 tkagg.blit(self._tkphoto, self.renderer._renderer, colormode=2)
    353 self._master.update_idletasks()
    354
    C:\Anaconda3\lib\site-packages\matplotlib\backends\tkagg.py in blit(photoimage, aggimage, bbox, colormode)
    28 _tkagg.tkinit(id(tk), 0)
    29 tk.call("PyAggImagePhoto", photoimage,
    ---> 30 id(data), colormode, id(bbox_array))
    31 except (ImportError, AttributeError, Tk.TclError):
    32 raise
    TclError: this isn't a Tk application
    what happened....???

    • @MarkJay
      @MarkJay  6 років тому

      Try using np.frombuffer. it's a much better way of converting the audio data from bytes to ints

    • @visionandimage5893
      @visionandimage5893 3 роки тому

      Getting same issue brother, have you solved it now?

  • @zachroussel1594
    @zachroussel1594 6 років тому +1

    is it possible to make this exact same thing, but use Pyserial instead of Pyaudio. Im trying to collect data from an arduino serial port and display its signal/Frequency spectrum in real time.

    • @apostolosanastasiadis475
      @apostolosanastasiadis475 5 років тому

      hello zach. seems like we are working on a similar project.I have the same problem here. Did you find a way to do it?

  • @hixidom2274
    @hixidom2274 3 місяці тому

    Really great. Gonna test this out and throw a FFT on it if it works

  • @foadsf
    @foadsf 5 років тому

    for those poor people like me who are stuck with Windows and there are no pyadio installation through pip as discussed here stackoverflow.com/questions/52283840/i-cant-install-pyaudio-on-my-python-how-to-do-it , you may download it from this page: www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio and then pip install the .whl file

  • @kaushekkumartr7641
    @kaushekkumartr7641 5 років тому +2

    Hey there dude... Good tutorial
    I am getting invalid output device as a error... Dont know how to rectify it. Appreciate if you can help :)

  • @jayakrishnanharikumaran676
    @jayakrishnanharikumaran676 2 роки тому

    Why do we need the comma after line as in " line, = ax.plot(..)"

  • @richardyang4204
    @richardyang4204 6 років тому +3

    hello, how can i install pyaudio and portaudio(i am using window)?

    • @MarkJay
      @MarkJay  6 років тому

      richard yang you should be able to just do
      pip install pyaudio

    • @richardyang4204
      @richardyang4204 6 років тому

      this is what i got when i type in "pip install pyaudio" in cmd
      Installing collected packages: pyaudio
      Running setup.py install for pyaudio ... error
      Complete output from command c:\users\sry\appdata\local\programs\python\python37\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\sry\\AppData\\Local\\Temp\\pip-install-u93v30ro\\pyaudio\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('
      ', '
      ');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\sry\AppData\Local\Temp\pip-record-sndx4fd0\install-record.txt --single-version-externally-managed --compile:
      running install
      running build
      running build_py
      creating build
      creating build\lib.win-amd64-3.7
      copying src\pyaudio.py -> build\lib.win-amd64-3.7
      running build_ext
      building '_portaudio' extension
      creating build\temp.win-amd64-3.7
      creating build\temp.win-amd64-3.7\Release
      creating build\temp.win-amd64-3.7\Release\src
      C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -DMS_WIN64=1 -Ic:\users\sry\appdata\local\programs\python\python37\include -Ic:\users\sry\appdata\local\programs\python\python37\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\include\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\cppwinrt" /Tcsrc/_portaudiomodule.c /Fobuild\temp.win-amd64-3.7\Release\src/_portaudiomodule.obj
      _portaudiomodule.c
      c:\users\sry\appdata\local\programs\python\python37\include\pyconfig.h(117): warning C4005: 'MS_WIN64': 巨集重複定義
      c:\users\sry\appdata\local\programs\python\python37\include\pyconfig.h(117): note: 命令列引數: 請參閱 'MS_WIN64' 先前的定義
      src/_portaudiomodule.c(29): fatal error C1083: 無法開啟包含檔案: 'portaudio.h': No such file or directory
      error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Tools\\MSVC\\14.14.26428\\bin\\HostX86\\x64\\cl.exe' failed with exit status 2
      ----------------------------------------
      Command "c:\users\sry\appdata\local\programs\python\python37\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\sry\\AppData\\Local\\Temp\\pip-install-u93v30ro\\pyaudio\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('
      ', '
      ');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\sry\AppData\Local\Temp\pip-record-sndx4fd0\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:\Users\sry\AppData\Local\Temp\pip-install-u93v30ro\pyaudio\

    • @foxtubelp6359
      @foxtubelp6359 5 років тому

      @@richardyang4204 you require the microsoft visual studio

  • @susovan97
    @susovan97 5 років тому +2

    Thanks for the video! How do I extract features from .wav files using this library or other libraries? Thanks in advance!

    • @bkhd
      @bkhd 4 роки тому

      use librosa library librosa.github.io/librosa/

  • @reemashrestha9718
    @reemashrestha9718 6 років тому +1

    After seeing the other comments,I tried to replace your original code by
    data_int=np.frombuffer(data,dtype=np.int16)+127
    and I could see the change in waveform but when there is no voice,unlike yours,I don't see clear background.I still see noise waveform.

    • @blobanater
      @blobanater 5 років тому

      I get exactly the same with 2 microphones

    • @N0ha92
      @N0ha92 4 роки тому +1

      Hi, have you been able to fix it?

  • @michaelschmidlin4274
    @michaelschmidlin4274 6 років тому +1

    I am having a bit of trouble getting a pop up window like you do. I have matplot lib and I am working in Jupyter notebooks. I also looked through the comments and didn't find anything that worked. You mentioned that matplot lib uses tkinter? Do I need to have tkinter installed before it will work? I didn't see you do it but maybe I need to import it?
    Great video though! Any suggestions are welcome!

    • @MarkJay
      @MarkJay  6 років тому

      Tkinter is part of python standard library so it should work. If you check part3 of the series, I made a python file version. Maybe that will work better

  • @surendrathakur3479
    @surendrathakur3479 3 роки тому

    Can it appear in gui for desktop voice assistant? Please help me

  • @MatthewBreithaupt
    @MatthewBreithaupt 4 роки тому +1

    It looks like you should be interpreting the bytes as _Unsigned_ int16, then you shouldn't have to subtract 127 (or 128)

  • @JuanJoseMartinezGuerrero
    @JuanJoseMartinezGuerrero 5 років тому +1

    Can I further improve on your code in order to AI interpret data?

  • @adityanandgaokar6796
    @adityanandgaokar6796 4 роки тому +1

    Hello ,
    I am getting this error while unpacking "struct.error: unpack requires a buffer of 8192 bytes" Help me to sort this thing

    • @martin94sj
      @martin94sj 4 роки тому

      me too

    • @martin94sj
      @martin94sj 4 роки тому

      i think i found the answer
      use this: data_int = struct.unpack(str(len(data)) + 'b', data)

  • @ahyoharyanto4291
    @ahyoharyanto4291 2 роки тому

    hi can i use sound lm393 module for microphone?

  • @mischamin7188
    @mischamin7188 3 роки тому +1

    can you have a tutorial on how to visualize the pitch graph of sounds?

    • @MarkJay
      @MarkJay  3 роки тому

      will consider this

  • @DaveCampbellKY
    @DaveCampbellKY 5 років тому +2

    good stuff, wasn't aware that this could be done so directly. sadly, i also get all 0s as my audio samples - so running this on a mac may require a few more setup steps. i have not been able to uncover the proper way to do this, despite a bit of research. would LOVE it if someone could provide details on what it takes to get the audio input from the default mic into pyaudio on a mac.

    • @priyankasrivastava6757
      @priyankasrivastava6757 5 років тому

      Have you got your answer what are the few more steps??? Coz I am using MacOS and i am getting 0s value in my tuples that mean it didn't take any data from my audio? pls help me out

    • @DaveCampbellKY
      @DaveCampbellKY 5 років тому +1

      so, i changed "Output=False", and some data started to show up. and then i got some overflow errors, but then got data again. hopefully that will move you forward.
      Please be aware there is a lot of mis-information in this tutorial. i don't think the author fully understands what the pyaudio.paInt16 data format is - (a 2-byte signed integer), as everything after that is a muddy mess of bad data structure manipulation. ask yourself why len(data) = 8192 when the CHUNK is set to 4096. (because each piece of CHUNK is 2 bytes! that's what paInt16 IS).
      when using the proper data format, the data values will range from -32767 to +32767.
      you might consider loading the numpy library, and then converting 'data' variable to a properly structured variable with:
      amplitude = np.fromstring(data, dtype='int16')
      and then try to use that variable (amplitude) to plot the values, ignoring all that struct.unpack business.
      by the way, if you do look at those bytes individually, you will discover they are stored as twos-complement - but across 2 bytes, of course. but doing so is just bad tech - use a Int16 data type since the data was created with an Int16 data type.

    • @priyankasrivastava6757
      @priyankasrivastava6757 5 років тому

      @@DaveCampbellKY thankyou for this important information it's really helpful.
      And can you tell me(or send me the link) from where you got these information and do this coding part as well (github link) if you have. Thankyou so much

    • @DaveCampbellKY
      @DaveCampbellKY 5 років тому +1

      @@priyankasrivastava6757 - it might be time to 'roll up your sleeves' and dig into some source material - this is actually a fairly straightforward exercise, to take the inspiration from the original video and correct it using proper data structures. i haven't done that myself - but may, and if i do i will post it here. in the meantime, these are useful references:
      pyaudio home - people.csail.mit.edu/hubert/pyaudio/
      pyaudio documentation - people.csail.mit.edu/hubert/pyaudio/docs/
      numpy.dtype docs - docs.scipy.org/doc/numpy/reference/generated/numpy.dtype.html
      twos-complement details - en.wikipedia.org/wiki/Two%27s_complement

    • @priyankasrivastava6757
      @priyankasrivastava6757 5 років тому

      @@DaveCampbellKY Thankyou

  • @shreytripathi5683
    @shreytripathi5683 3 роки тому

    are you that guy from this is your daily dose of internet?

  • @unfrostedpoptart
    @unfrostedpoptart 6 років тому +2

    Hi. This is helpful for me since I’m new to Python. The problem you’re having with the data looking split - around 7:05 in the video - is pretty obviously because the data is coming back as 2’s-complement in the hex values and your conversion to integer doesn’t do that correctly. Please look up 2’s-complement to see how to do the conversion.

    • @MarkJay
      @MarkJay  6 років тому

      David Rogoff thanks, I will check it out. I notice this everytime I use struct.unpack

    • @MoXyiD
      @MoXyiD 5 років тому

      To convert from 2s cm is just a not +1 right?

  • @Enigma-rv4jw
    @Enigma-rv4jw 4 роки тому +1

    Why did he add 'B' in data_int?

  • @mikekilmer3769
    @mikekilmer3769 3 роки тому

    Thank you much. On OSX, may need to install `brew install portaudio` for `pip install pyaudio` to succeed.

  • @sunilkumarvengalil2305
    @sunilkumarvengalil2305 3 місяці тому

    Great post. Well presented. I was looking for a way to process a live audio stream. This video will help a lot. Thank Mark for this video.

  • @hoteny
    @hoteny 9 місяців тому

    So if you have discontinuity, then you apply a window function like hann, right? I dont know a lot but moving it instead felt weird. (7:50)

  • @lynerscruxifyl3049
    @lynerscruxifyl3049 6 років тому +2

    matplotlib tk has a syntax error, what should I do i am using IDLE python to code

    • @MarkJay
      @MarkJay  6 років тому

      Are you using jupyter?

    • @lynerscruxifyl3049
      @lynerscruxifyl3049 6 років тому

      no i am not

    • @MarkJay
      @MarkJay  6 років тому

      @@lynerscruxifyl3049 I see. That line is a magic function that only works with jupyter

    • @lynerscruxifyl3049
      @lynerscruxifyl3049 6 років тому

      so what should i replace it with? im quite new to coding haha

    • @MarkJay
      @MarkJay  6 років тому

      @@lynerscruxifyl3049 try jupyter.
      Just do
      pip install jupyter
      Then type
      jupyter notebook
      In the cmd window

  • @MilitaireFrancais
    @MilitaireFrancais 6 років тому

    Hey I am getting an overflow error: File "/Users/nameofcomputer/PycharmProjects/untitled/venv/lib/python2.7/site-packages/pyaudio.py", line 608, in read
    return pa.read_stream(self._stream, num_frames, exception_on_overflow)
    IOError: [Errno -9981] Input overflowed
    Is there a fix for this?

    • @MarkJay
      @MarkJay  6 років тому

      are you using macos or linux? I'm not sure how to get pyaudo working with those operating systems

  • @queksiangyee5880
    @queksiangyee5880 4 роки тому

    Hi, I fully followed this vid code and code it in jupyter notebook, it doesn't occur any error but when it runs, it didn't come out anything. Anyone knows what is the problem? It didn't come out as what the video shows the final output.

  • @rec-trick
    @rec-trick 2 роки тому

    how to add autotune with realtime ?

  • @jayshaligram4474
    @jayshaligram4474 4 роки тому +1

    Hi Mark! Thanks for this awesome video. Wanted to ask you.. is there any way we can analyze mean frequency/fundamental frequency and save that data into a text file? time vs fundamental frequency data. ?

  • @perlinwarp
    @perlinwarp 3 роки тому

    If the jupyter notebook cell running p = pyaudio.PyAudio() never terminates, try switching the backend from tk to qt: %matplotlib qt

  • @hoangtrong7351
    @hoangtrong7351 Рік тому

    how to run project help me pls

  • @aravinda513
    @aravinda513 4 роки тому

    I know the doubt is pretty silly. ...How does it show the spectrum when I haven't even given any audio as input? Only if I input an audio data it can plot the spectrum, right?

  • @TheBontenbal
    @TheBontenbal 2 роки тому

    Great script. I had to enter %matplotlib notebook to the cell to get it working properly in Jupyter.

  • @Matheusbirosk
    @Matheusbirosk 6 років тому +1

    Hello, how can I plot the graph in a external box as you've done?

    • @MarkJay
      @MarkJay  6 років тому

      Matheus Silva yes, check out part 3 of the series. I made a normal python file version

  • @SaadTahirVelocity7
    @SaadTahirVelocity7 7 років тому +2

    Hey Mark Jay! Your tutorial is amazing and exactly what I was looking for. But there is a small problem i'm facing. I can't see the graph being plot. I am running Anaconda Shell and copied you code completely. The line which you added at first i.e %matplotlib tk is giving me an error so I removed out. Please help. Thank you

    • @MarkJay
      @MarkJay  7 років тому

      Saad Tahir thanks! Glad you like them. That line is an ipython magic function. Try opening a shell by typing "ipython" (instead of python). You can also start a jupyter notebook and work in there. Just type "jupyter notebook" in a cmd window

    • @MarkJay
      @MarkJay  7 років тому

      I also created a python file for the viewer (instead of a jupyter notebook). you can try running this scrip.
      github.com/markjay4k/Audio-Spectrum-Analyzer-in-Python/blob/master/audio_spectrum.py

    • @lynngu5933
      @lynngu5933 7 років тому

      Hi Mark! Thanks a lot for the video and code you uploaded. I just have a question when I run the python file you wrote for the viewer( I use sublime). After I run the code, the picture only shows up for a second, and the program just kind of crashes with "OSError: [Errno -9981] Input overflowed" error. And I a wondering what goes wrong.. Can you please help. Thanks so much!

    • @MarkJay
      @MarkJay  7 років тому

      Lynn Gu glad you like them! Are you using the notebook version? In the github repo theres a python file version that will work in a text editor like sublime

    • @lynngu5933
      @lynngu5933 7 років тому

      Thanks for the response!!! Yeah I used the github version, however I keep getting the input overflow error and I don't quite know why..

  • @furkan2724
    @furkan2724 4 роки тому

    I have just 0 ,1 and 255 byte. is this a problem?

  • @tvnkchitmuoy2656
    @tvnkchitmuoy2656 3 роки тому

    Hello, why do i run data_int then all my outputs are \x00?

    • @MarkJay
      @MarkJay  3 роки тому

      Sorry I'm not sure.

  • @danielli2078
    @danielli2078 6 років тому +1

    Hey Mark great video! I'm trying to average the volume level in each chunk, save it as an int, and amplify sound based on the level of the int. So for example, if the average of the int from our mic increases, I amplify the sound from a file playing. If it decreases, I lower the sound. Basically changing the output of a sound file based on the volume level from our mic. Any thoughts on how I can go about this?

    • @MarkJay
      @MarkJay  6 років тому

      Daniel Li that's basically the functionality of a compressor. You can get the average value of a chunk using numpy.average. I'm not sure about how to use that to affect another audio source

  • @yurimota677
    @yurimota677 6 років тому +1

    Hello. My graphic is not that clean. It seems there is a huge interference, how can I filtrate it?

    • @MarkJay
      @MarkJay  6 років тому

      Yuri Mota do you mean the microphone? The best way is to get the cleanest signal into the computer. The normal headphone jack can be noisy. If you can get a digital audio interface, you will get much cleaner input

  • @SarangSaraf
    @SarangSaraf 5 років тому

    Why '%' is not recognized by pycharm? I am not able to see any graph after running this code.

    • @PaulBlxck
      @PaulBlxck 5 років тому

      Because it's an Anaconda Magic Function. You don't need it in PyCharm.

  • @MrBahura
    @MrBahura 6 років тому

    Hi it is running , plot is displayed - however I get random noise on the graph not related either to mic or other I/O sound devices .
    Is there e simple way to select which device is acting as Input. I want to analyze the music coming out default output device instead from mic. Using last Anaconda distribution with Jupyter notebook on WIN10. Thanks

  • @kelvinchandra9216
    @kelvinchandra9216 5 років тому

    my spectrum window can't show up. I'm using python 3.7

  • @brachboz
    @brachboz 5 років тому

    its lot easier to save the recorded file as eg output.wav , so than u can read it with those two lines of code and get a numpy_array
    "from scipy.io import wavfile
    fs, data = wavfile.read('output.wav')"
    for more details see scipy.io documentation

  • @sunnyRajkarnikar
    @sunnyRajkarnikar 4 роки тому

    at 7:53 could the reason that the plot comes up like that be that the binary of negative number is taken as 1's or 2's complement?

  • @kvcshekar1
    @kvcshekar1 6 років тому +1

    Hi Mark, I like the project. I wanted to know would you be available as a tutor as I have detailed questions and also I like to build few things on top of this.

    • @MarkJay
      @MarkJay  6 років тому

      glad you like the project. Sorry but I dont have the time for tutoring. feel free to ask questions if you get stuck on a particular thing though

  • @farrukhshahzad1035
    @farrukhshahzad1035 2 роки тому

    hello @Mark Jay: can we do it in reverse as well? means from signals to audio?

  • @martinkopecky8334
    @martinkopecky8334 5 років тому

    I found the solution to the ploting problem, it's indeed 16-bit integer, SIGNED -> data_int = struct.unpack(str(2*lenght_of_data_chunk) + 'h', data)

  • @YouKnowWhoIAm118
    @YouKnowWhoIAm118 4 роки тому

    Hm, I have a really stange problem: If I leave out the + 127 my code works, otherwise the numpy array has some kind of modulo properties and the numbers stay inside the range from -127 to 127, so for example a 8 what should become a 132 is a -119 then. Any idea why this can happen?

    • @YouKnowWhoIAm118
      @YouKnowWhoIAm118 4 роки тому

      By "my code works" I mean I get a curve that lies between -127 and 127, but looks right

  • @sameersiddiqui7139
    @sameersiddiqui7139 4 роки тому

    Which API in this code did you use to read audio from.the microphone!?

  • @el07694
    @el07694 3 роки тому

    Can you please make a video like the above which will have real time datetimes as x ticks instead of just number of samples?

  • @jackgregorski8688
    @jackgregorski8688 2 роки тому

    Hi, I'm very new to python, but am trying to use it for a school project. Is there any way that something similar to this could be used, but instead of using microphone input you analyzed the output coming from the speakers?

    • @MarkJay
      @MarkJay  2 роки тому

      If its coming out a speaker, you likely already have the digital audio on the computer and can process. Or input that audio to a computer

  • @IBEtrading
    @IBEtrading 5 років тому

    % i have python 3.6 and give invalid syntax ???

  • @bobinwobbin4094
    @bobinwobbin4094 3 роки тому

    I dont know if im missing something, but where do we even do stuff like asigning mic and stuff like this....Does pyaudio just do all of that by itsself?

    • @Batselot
      @Batselot 3 роки тому

      That's the exact same question I had in mind but I tried it and it still works

  • @computervisiondev
    @computervisiondev 2 роки тому

    Many thanks!!

  • @jeetshah8513
    @jeetshah8513 4 роки тому

    Hey, I have a doubt
    If I have a music sample which has two components of say guitar and piano.
    SO is there any way we can split that sample into ins components?

  • @seeking9145
    @seeking9145 4 роки тому

    This plot 7:14 stucks in a white screen ... just the window appears and that's it.
    Do somebody know why it doesn't make anything?

  • @qzorn4440
    @qzorn4440 5 років тому

    sweet video, I would like to do this on a Rock64-4gb sbc, any ideas...os, etc? thanks.

  • @vrankyrule
    @vrankyrule 5 років тому

    i am so confused with different types of python ide and this one which is defining every in[ ] separately can someone help me with this basic step of why and what

  • @Esore996
    @Esore996 6 років тому +5

    You can do
    data_int = np.fromstring(data, dtype=np.int16)

    • @MarkJay
      @MarkJay  6 років тому +2

      First Last thanks, I think that's a better method for converting the bytes than struct

    • @gabrielversion16
      @gabrielversion16 6 років тому

      i just want to thank you both mark jay and adam rose, the code adam rose submitted fixes a problem when audio goes beyond 255 or under 0 (what i can guess is saturation) that the base code has, it yielded better results when i tried this method and it even worked in the part 3. please pin this comment so that everybody can know

  • @clearly_blurry3416
    @clearly_blurry3416 5 років тому

    Instead of using matplotlib, I'm outputting my data to a txt file. Except I get a weird ellipsis with 3 numbers either side?

  • @Shahid8233
    @Shahid8233 6 років тому +1

    what is IDE you used for development ?

    • @MarkJay
      @MarkJay  6 років тому

      I mostly use jupyter. otherwise I use atom.io

    • @pavlevod
      @pavlevod 6 років тому +1

      I suggest trying PyCharm Community, its free and open source. I have tried sublime, vs code, atom and that's all cool but for example they don't list all the methods or properties after dot and they are not as good at auto-completing stuff as PyCharm is. In addition to that, PyCharm will sometimes tell you errors ahead of time and will also fetch documentation for particular function. Atom, sublime and vscode are not really IDEs, they are more like text editors for writing quick scripts

  • @ctkqiang
    @ctkqiang 5 років тому

    Is this the same technic can be apply to EEG, ECG, EMG signal processing?

  • @sushilkulkarni2243
    @sushilkulkarni2243 4 роки тому

    I want to compare two audio files and want to find similarity score . How i have to do? I have one audio .wav file of 4 to 6 seconds and another audio .wav file of about 1 to 2 seconds.

  • @johndoe-di6vg
    @johndoe-di6vg 6 років тому

    you get twice the size in the data, because 2 bytes represent one 16 bit
    signed integer, which is what you want to read.
    you should unpack with the 'h' flag
    data_int = np.array(struct.unpack(str(CHUNK)+'h', data))
    and set limits from about -600 to 600
    ax.set_ylim(-600,600)

    • @johndoe-di6vg
      @johndoe-di6vg 6 років тому +1

      actually the limits should be -32768 to 32767,
      ax.set_ylim(-32768,32767)

  • @samiraziur09
    @samiraziur09 6 років тому

    Hey Mark.... really appreciate your work. I am using pycharm using python 3.6. Everything works fine like you said but I get an error : "_tkinter.TclError: this isn't a Tk application " after I manually close the streaming. Can you please hellp in this ?

    • @MarkJay
      @MarkJay  6 років тому

      yes, if you check the github code, I added a try statement to handle this error

  • @richardyo888
    @richardyo888 6 років тому

    hi, i want to try your code, but i had a problem, but it´s something strange, it says:
    AttributeError: 'Window' object has no attribute 'setGeometry'
    i don´t know if something its missing, or something else...
    im working on raspberry 3 with ubuntuMate...

    • @MarkJay
      @MarkJay  6 років тому

      richardyo888 you can remove that line of code. I just used that to center the figure on my screen, but its not necessary

  • @Parirash123
    @Parirash123 4 роки тому

    Awesome.... Thanks for making this video Mark.

  • @amirabenyahia1902
    @amirabenyahia1902 2 роки тому

    Hey Mark Jay , actually after running the : import pyaudio in jupyter notebook i've found this error : ModuleNotFoundError Traceback (most recent call last)
    in
    ----> 1 import pyaudio
    ModuleNotFoundError: No module named 'pyaudio'

    • @MarkJay
      @MarkJay  2 роки тому

      Ok. That means you need to install pyaudio. If you are on windows, you can just do pip install pyaudio and everything should work. If you are on linux, also need pulse audio installed and to start an instance of pulse audio before running pyaudio

    • @amirabenyahia1902
      @amirabenyahia1902 2 роки тому

      @@MarkJay I'm working on windows , I try it on prompt right ?

  • @Wei-ym6bs
    @Wei-ym6bs 4 роки тому

    Why does my graph look so weird

  • @minabernal563
    @minabernal563 5 років тому

    im having this error
    AttributeError: '_tkinter.tkapp' object has no attribute 'createfilehandler'
    how can i resolve this?

  • @hexene6679
    @hexene6679 7 років тому +1

    Hey ! thank you so much, this is really helping me understand the idea :) I'm getting this error while using jupyter notebook:
    NameError Traceback (most recent call last)
    in ()
    ----> 1 p = pyaudio.PyAudio()
    2
    3 stream = p.open(
    4 format = FORMAT,
    5 channels = CHANNELS,
    NameError: name 'pyaudio' is not defined
    I'm using Python 2.7 btw, maybe that's it?

    • @hexene6679
      @hexene6679 7 років тому

      The error appears when I try 5:23 btw

    • @MarkJay
      @MarkJay  7 років тому

      Luis Ech glad you liked it! Did you import pyaudio?

    • @hexene6679
      @hexene6679 7 років тому +1

      i feel really stupid, thanks! also, got any kind of book recommendations or free course about pyaudio/matplotlib/numpy? it would really make my day :)

    • @MarkJay
      @MarkJay  7 років тому

      Theres tons of material on matplotlib on their website. Its one of the most well documented libraries. Same with numpy. Pyaudio on the other hand has very little material. Id say check out their online docs, they usually have example code to woek with

    • @hexene6679
      @hexene6679 7 років тому +1

      I'll try that, thanks again, you helped me start my first university project :)

  • @ichtube
    @ichtube 5 років тому +4

    My fave part: chunk length long. :)

  • @christopherdeladia6652
    @christopherdeladia6652 6 років тому +1

    do i still have to download pyaudio?

    • @MarkJay
      @MarkJay  6 років тому

      christopher deladia yes you will need it

  • @xandutheil67
    @xandutheil67 4 роки тому

    Hey, nice vieo! IS this possible to do this on an mp3 or a wave file?

  • @vasishtakanthi4231
    @vasishtakanthi4231 6 років тому +1

    Hi Mark Jay!!!
    Thanks for your video!!! ... its pretty cool to see how one can do a ton of things with audio in python.
    Although, I stumbled with many issues... I'm not sure why it isn't working for me.... ill try to explain to you what I got so far..... and a hit of warning, I'm a superb nooob to python :-P..
    first off, your code has to work in normal python script, right? ... regardless whether one uses anaconda, or jupyter, right??? if not, then what shud I do extra in order to run the code properly in python script?
    if yes,
    i have written the code exactly how showed it.... when i plotted the tuple obtained after using the struct.unpack, i got a plot, but its not at all like yours in 7:20 .... it looked more like a series of pulses, and all of them of equal amplitude, from 0 to 250... from there, i know for sure that something's up.... i did a bit of snooping around in the internet, and i saw similar examples, where ppl used input_device_index ..... but it seems that it works perfectly for you anyways.... although, I'm not sure if the input mic has anything to do with the plot i got, or???
    then i created the np.array as u mentioned, and there were values going to zero as you mentioned.... then i used the [ : : 2], but nothing happened... it was the same....
    i tried using the np.array without the +127 to see how it looks, and the plot ranges from -1 to 1, and there are values, that pivot around zero...... i guess this is fine in general, but here also, i tried using the [ : : 2], but nothing happened anyways....
    is it possible for you to help me out on this??? i would really appreciated it!!!! Thanks for the video, it was really awesome!!

    • @MarkJay
      @MarkJay  6 років тому

      Vasishta Kanthi I'm glad you liked the video. If you check the github repo, I made a version of the code in a normal python file. I'll share the link. For me the issue is with struct.unpack. after calling unpack I had to
      1. Convert the tuple to a numpy array with dtype=8bit int.
      2. Then add 128 to bring the 0 point to the center range.
      3. Then slice out every other point in the array.
      At this point you could convert the array to a float32 dtype and resale however you like.

    • @MarkJay
      @MarkJay  6 років тому

      Vasishta Kanthi check out this file
      github.com/markjay4k/Audio-Spectrum-Analyzer-in-Python/blob/master/audio_spectrum.py

    • @vasishtakanthi4231
      @vasishtakanthi4231 6 років тому

      Hi Mark Jay!!
      Thank you very much for your reply, and thanks for the new code you shared. it is helpful, and more intuitive as a class object.... but again Ive fallen into some errors.... some of them I have fixed it, but in the end I'm ending with a division by zero error when calculating the self.fr=frame_count/(time.time()-start_time)
      first I realized I didn't have the pyqtgraph module, as well as the pyQt4 module, but I installed it successfully..
      then, for this line:
      thismanager.window.setGeometry(5, 120, 1910, 1070)
      apparently there is no attribute 'setGeometry' ... I'm not sure if I have to install a new module for this, ......... do I??
      but I realized this was just resizing the window for the time and frequency plot in real time... so I just commented it and the stream started, but then, I ended with the division by zero error....
      the plot I got, the time plot was zero, and the frequency plot was full of noise.....
      cud this be an issue with the input mic??.... if so , how should I fix it??
      once again, thanks for your valuable help!!

    • @MarkJay
      @MarkJay  6 років тому

      Vasishta Kanthi sounds like it could be the microphone. Can you raise the input volume on the mic to get more signal into it?

    • @vasishtakanthi4231
      @vasishtakanthi4231 6 років тому

      hi mark jay....
      I checked the mic volume, and it was already in full volume.... but then I realized that earlier, I didn't make any sound when the streaming started... so now I made a sound test before the stream started, and now it works!!!!! although, a bunch of errors have come up, I think I can try debugging them ....
      thank you very much !!!! wish you a happy and prosperous new year!!!!
      you have my like and subscription :-D

  • @martinbollig8270
    @martinbollig8270 4 роки тому

    Needed to add plt.ion() to get an image