Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

Reading input sound signal using Python

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 :-).

question from:https://stackoverflow.com/questions/35344649/reading-input-sound-signal-using-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...