Python Forum
How to do real-time audio signal processing using python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to do real-time audio signal processing using python (/thread-21674.html)



How to do real-time audio signal processing using python - Zenolen - Oct-09-2019

Hi all, hope I am posting in the right place.

I am quite new to Python, and maybe I am bighting off more than I can chew but I am trying to make an audio filer that works in real time (low latency). i.e. sound continuously flows into the mic, is processed by my code and will flow continuously out to the speaker. (In Windows 10)

Now what I was hoping is that someone could point me to a free library (is that the right word?) that can take the sound from the mic and give me the data. I would then process it and hand it back to be sent to the speaker.

I have found libraries that would do this but unfortunately they were difficult for me to understand. I don’t need anything snazzy, just something that will give me the audio data from the mic and I will hand it back. I’ll do the rest.

I would appreciate any help.

Thanks.


RE: How to do real-time audio signal processing using python - jefsummers - Oct-09-2019

PyAudio is probably the library you will want to use.
For starters, the linked video gets you through the parts you are asking about.
PyAudio streaming audio


RE: How to do real-time audio signal processing using python - Zenolen - Oct-18-2019

Hi jefsummers,

Thanks for your help and sorry for the late reply.

Pyaudio seems like just what I’m looking for and thanks for the link to the tutorial too. The only problem is I’m having trouble installing it. I used:

pip install pyaudio

But I got the error:

error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools

The link does not work but I read on the net that any version later then 14 should also be fine.

So I installed C++ build tools from Visual Studio Build Tools 2019. Unfortunately I still get the same error when trying to install pyaudio. Maybe it's not installed to the command line but I was having difficulty working out how to do that.

Any further help would be appreciated.


RE: How to do real-time audio signal processing using python - jefsummers - Oct-19-2019

I did not need Visual C++. I used Anaconda and tested in Jupyter Notebook


RE: How to do real-time audio signal processing using python - snippsat - Oct-19-2019

(Oct-18-2019, 08:26 PM)Zenolen Wrote: So I installed C++ build tools from Visual Studio Build Tools 2019. Unfortunately I still get the same error when trying to install pyaudio. Maybe it's not installed to the command line but I was having difficulty working out how to do that.

Any further help would be appreciated.
Download wheel here.
Wheel is pre-complied with all stuff needed.
Example Python 37 and 32-bit would be:
pip install PyAudio-0.2.11-cp37-cp37m-win32.whl
64-bit would be(also this is Python version 32-bit or 64-bit not OS).
pip install PyAudio‑0.2.11‑cp37‑cp37m‑win_amd64.whl



RE: How to do real-time audio signal processing using python - Zenolen - Oct-26-2019

Hi jefsummers and snippsat,

Thanks very much for both your suggestions, I appreciate it. In the end I chose to get Anaconda as it also looks good for deep learning, which I also want to get into.

I have now got pyaudio installed.

But I’m still having problems. I have been trying hard to get it to work and I have got the blocking mode to play the microphone input to the output. But I have been searching the net for some time and I can't find a clear explanation of exactly how the “callback” mode is supposed to be used. I found examples of how to stream sounds to the output device (and I have got that to work) but not how to get data from the mic. I also couldn’t find the format pyaudio uses to record data, which I will need to know if I’m to write something to modify it.

I don’t know if I’m missing something obvious but I’m stuck just when I feel so close to getting it working.

Any suggestions would be great.


RE: How to do real-time audio signal processing using python - Zenolen - Nov-03-2019

Does anyone have any suggestions?


RE: How to do real-time audio signal processing using python - jefsummers - Nov-04-2019

The following works. I got it from GitHub https://gist.github.com/mabdrabo/8678538
and modified it to work with Python 3. It will record several seconds from the microphone then saves if off as a .wav file
import pyaudio
import wave
 
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "file.wav"
 
audio = pyaudio.PyAudio()
 
# start Recording
stream = audio.open(format=FORMAT, channels=CHANNELS,
                rate=RATE, input=True,
                frames_per_buffer=CHUNK)
print ("recording...")
frames = []
 
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data)
print ("finished recording")
 
 
# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()
 
waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()