"Develop skills" Python audio processing · PyAudio started tutorial (entry and playing sound)
When required Python processing audio data, and play sounds using python reads necessary, a useful process described below PyAudio audio toolkit.
PyAudio Python is an open source toolkit, by the name implies, is to provide a voice operation kit. Provides a recorded processing and other functions, can be regarded as OpenCv voice field.
1 Introduction
PyAudio for cross-platform audio library I O / PortAudio provides Python bindings. Use PyAudio, you can easily use Python playback on a variety of platforms and record audio, such as GNU / Linux, Microsoft Windows and Apple Mac OS X / macOS.
PyAudio inspiration from:
- pyPortAudio / fastaudio : PortAudio V18 API Python bindings.
- tkSnack : Tcl / Tk and Python cross-platform sound kit.
2. Install
The current version is PyAudio v0.2.11 . PyAudio use pip install on most platforms. For the previous version v0.2.9, PyAudio distribution installation binary files that are archived here .
Microsoft Windows
Use pip installation:
python -m pip install pyaudio
notes:
- If pip has not been bundled with your Python installation, where acquisition.
- Obtain and install the pip PyAudio wheel (prepackaged binaries). Currently, there are wheels compatible Python 2.7,3.4,3.5 and 3.6 official release . For these versions, you can use the 32-bit and 64-bit wheel.
- These include the use of MinGW binaries built PortAudio v19 v190600_20161030. They only supports Windows MME API, does not include support for DirectX, ASIO and the like. If you need support not included in the API, you need to compile PortAudio and PyAudio.
Apple Mac OS X.
Use Homebrew install the necessary portaudio library, and then use pip install PyAudio:
brew install portaudio
pip install pyaudio
notes:
- If you have not installed, download Homebrew .
- pip PyAudio download the source code and build it into your Python version.
- Homebrew and build PyAudio also need to install Xcode command-line tool ( for more information ).
Debian / Ubuntu
Installed with the package manager PyAudio:
sudo apt-get install python-pyaudio python3-pyaudio
Without the latest version of PyAudio, please use pip install it:
pip install pyaudio
notes:
- pip download PyAudio source and build it into your system. Be sure to pre-installed portaudio library development kit (
portaudio19-dev
) and python development kit (python-all-dev
). - In order to isolate the system package, consider virtualenv the installation PyAudio.
PyAudio sources
The source code (the PyPI) the Package Index downloaded from the Python: pypi.python.org/pypi/PyAudio .
Or clone git repository:
git clone https://people.csail.mit.edu/hubert/git/pyaudio.git
Build from source PyAudio, you also need to build PortAudio V19 . Some explanation about the building PyAudio for a variety of platforms, see the compilation tips . To use Microsoft Visual Studio to build PyAudio, check out Sebastian Audet's description .
3. Example
1). The audio collection
The following piece of code demonstrates how to capture an audio from a computer microphone, capture audio long time 4s, save the file output.wav
Tqdm module used, the reading process can be easily displayed, as follows:
* recording
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 172/172 [00:03<00:00, 43.40it/s]
* done recording
import pyaudio
import wave
from tqdm import tqdm
def record_audio(wave_out_path,record_second):
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
wf = wave.open(wave_out_path, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
print("* recording")
for i in tqdm(range(0, int(RATE / CHUNK * record_second))):
data = stream.read(CHUNK)
wf.writeframes(data)
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
wf.close()
record_audio("output.wav",record_second=4)
To use PyAudio, first use pyaudio.PyAudio()
(1) of Example PyAudio, which is provided portaudio system.
To record or play audio, please use pyaudio.PyAudio.open()
(2) Open the desired audio stream parameters in the necessary equipment. This sets the pyaudio.Stream
play or record audio.
By using streaming pyaudio.Stream.write()
to play audio using audio data or streaming audio data pyaudio.Stream.read()
. (3)
Note that, in "blocking mode", each pyaudio.Stream.write()
, or pyaudio.Stream.read()
prevent a given frame until all / are requesting player / recorder. Or, to dynamically generate audio data or processing audio data recorded immediately, use the "Callback model" as outlined below.
Use pyaudio.Stream.stop_stream()
pause playback / recording, and pyaudio.Stream.close()
terminates the flow. (4)
Finally, pyaudio.PyAudio.terminate()
(5) the session is terminated portaudio
2) Play Audio
Use the following playback function to play saved 1) audio output.wav
By tqdm, display the playback progress bar, as follows:
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 172/172 [00:03<00:00, 43.40it/s]
"""PyAudio Example: Play a WAVE file."""
import pyaudio
import wave
from tqdm import tqdm
def play_audio(wave_path):
CHUNK = 1024
wf = wave.open(wave_path, 'rb')
# instantiate PyAudio (1)
p = pyaudio.PyAudio()
# open stream (2)
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
# read data
data = wf.readframes(CHUNK)
# play stream (3)
datas = []
while len(data) > 0:
data = wf.readframes(CHUNK)
datas.append(data)
for d in tqdm(datas):
stream.write(d)
# stop stream (4)
stream.stop_stream()
stream.close()
# close PyAudio (5)
p.terminate()
play_audio("output.wav")
2). Callbacks way to play audio
When the need to execute other programs during playing audio, playing manner can use the callback, the following sample code:
"""PyAudio Example: Play a WAVE file."""
import pyaudio
import wave
from tqdm import tqdm
import time
def play_audio_callback(wave_path):
CHUNK = 1024
wf = wave.open(wave_path, 'rb')
# instantiate PyAudio (1)
p = pyaudio.PyAudio()
def callback(in_data, frame_count, time_info, status):
data = wf.readframes(frame_count)
return (data, pyaudio.paContinue)
# open stream (2)
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True,
stream_callback=callback)
# read data
stream.start_stream()
while stream.is_active():
time.sleep(0.1)
# stop stream (4)
stream.stop_stream()
stream.close()
# close PyAudio (5)
p.terminate()
play_audio_callback("output.wav")
Reference:
1.http://people.csail.mit.edu/hubert/pyaudio/