Python Forum
How to do real-time audio signal processing using python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to do real-time audio signal processing using python
#1
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.
Reply
#2
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
Reply
#3
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/visua...uild-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.
Reply
#4
I did not need Visual C++. I used Anaconda and tested in Jupyter Notebook
Reply
#5
(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
Reply
#6
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.
Reply
#7
Does anyone have any suggestions?
Reply
#8
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error python audio codiac 3 4,115 Mar-30-2023, 03:12 PM
Last Post: deanhystad
  Non-blocking real-time plotting slow_rider 5 3,595 Jan-07-2023, 09:47 PM
Last Post: woooee
  How to change UTC time to local time in Python DataFrame? SamKnight 2 1,592 Jul-28-2022, 08:23 AM
Last Post: Pedroski55
  python audio analysis kiyoshi7 3 1,763 Feb-22-2022, 06:09 PM
Last Post: Axel_Erfurt
  Real time database satyanarayana 3 1,660 Feb-16-2022, 01:37 PM
Last Post: buran
  Real time data satyanarayana 3 23,079 Feb-16-2022, 07:46 AM
Last Post: satyanarayana
  Real time Detection and Display Gilush 0 1,782 Feb-05-2022, 08:28 PM
Last Post: Gilush
  mysql.connector.errors.ProgrammingError: Failed processing format-parameters; Python ilknurg 3 5,580 Jan-18-2022, 06:25 PM
Last Post: ilknurg
  Real-Time output of server script on a client script. throwaway34 2 2,045 Oct-03-2021, 09:37 AM
Last Post: ibreeden
  Why recursive function consumes more of processing time than loops? M83Linux 9 4,218 May-20-2021, 01:52 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020