How to read realtime microphone audio volume in python and ffmpeg or similar

前端 未结 2 508
天命终不由人
天命终不由人 2020-12-24 14:25

I\'m trying to read, in near-realtime, the volume coming from the audio of a USB microphone in Python.

I have the pieces, but can\'t figure out how to put

相关标签:
2条回答
  • 2020-12-24 15:18

    Python 3 user here
    I had few problems to make that work so I used: https://python-sounddevice.readthedocs.io/en/0.3.3/examples.html#plot-microphone-signal-s-in-real-time
    And I need to install sudo apt-get install python3-tk for python 3.6 look Tkinter module not found on Ubuntu
    Then I modified script:

    #!/usr/bin/env python3
    import numpy as np
    import sounddevice as sd
    
    duration = 10 #in seconds
    
    def audio_callback(indata, frames, time, status):
       volume_norm = np.linalg.norm(indata) * 10
       print("|" * int(volume_norm))
    
    
    stream = sd.InputStream(callback=audio_callback)
    with stream:
       sd.sleep(duration * 1000)
    

    And yes it working :)

    0 讨论(0)
  • 2020-12-24 15:21

    Thanks to @Matthias for the suggestion to use the sounddevice module. It's exactly what I need.

    For posterity, here is a working example that prints real-time audio levels to the shell:

    # Print out realtime audio volume as ascii bars
    
    import sounddevice as sd
    import numpy as np
    
    def print_sound(indata, outdata, frames, time, status):
        volume_norm = np.linalg.norm(indata)*10
        print ("|" * int(volume_norm))
    
    with sd.Stream(callback=print_sound):
        sd.sleep(10000)
    

    0 讨论(0)
提交回复
热议问题