Reading input sound signal using Python
Asked Answered
R

4

55

I need to get a sound signal from a jack-connected microphone and use the data for immediate processing in Python.

The processing and subsequent steps are clear. I am lost only in getting the signal from the program. The number of channels is irrelevant, one is enough. I am not going to play the sound back so there should be no need for ASIO on soundcard.

My question is: how can I capture Jack audio from Python? (It would be great if there were a package, well documented and niches examples :-).

Recor answered 11/2, 2016 at 16:22 Comment(0)
T
83

Have you tried pyaudio? To install:

python -m pip install pyaudio

Recording example, from the official website:

PyAudio example: Record a few seconds of audio and save it to a WAVE file.

import pyaudio
import wave

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"

p = pyaudio.PyAudio()

stream = p.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("* done recording")

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()

This example works on my laptop with Python 2.7.11 (and 3.5.1) in Windows 8.1, pyaudio 0.2.9.

Tanya answered 14/2, 2016 at 11:1 Comment(2)
Is it possible to record from microphone and play to a speaker at the same time? In programming's perspective, after read CHUNK size data, play it through speaker's stream. The delay is too short to be detected by human ears.Minicam
Note that on macOS you must brew install portaudio before installing PyAudio.Brodeur
O
25

Are you planning to get audio from microphone in pieces or streaming? Either case , sounddevice may be employed.

You can install the python module using
pip install sounddevice --user

Please refer to official site for API details.

sounddevice will record audio from your laptop microphone (standard audio input) and play on speaker or headphones (standard audio output). You can use the sound object for further processing.

import sounddevice as sd
import numpy as np
import scipy.io.wavfile as wav

fs=44100
duration = 5  # seconds
myrecording = sd.rec(duration * fs, samplerate=fs, channels=2,dtype='float64')
print "Recording Audio"
sd.wait()
print "Audio recording complete , Play Audio"
sd.play(myrecording, fs)
sd.wait()
print "Play Audio Complete"

Here is the Output : Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. ================================ RESTART ===================

Recording Audio
Audio recording complete , Play Audio
Play Audio Complete

Oxa answered 19/2, 2016 at 20:0 Comment(0)
C
3

I would consider using pysox, the python bindings for libsox.

You can get pysox package from PyPI.

Cog answered 14/2, 2016 at 10:35 Comment(0)
U
3

If the requirement is Jack, then you may want to use PyJack, which is the Python binding for Jack.

Furthermore, the source code has an example for what you want to do, that is, to capture audio. See the file capture.py

You must consider that to avoid missing a block, you must call jack.process every 500 *(buffer_size/sample_rate) milliseconds. jack.process throw exceptions when you miss audio blocks (jack.InputSyncError and jack.OutputSyncError).

Ulric answered 19/2, 2016 at 1:55 Comment(1)
I think that with jack the OP meant the actual audio jack (that is no USB or whatever) but I might be wrong.Conservation

© 2022 - 2024 — McMap. All rights reserved.