python-利用pyaudio進行聲音錄製及簡單例項程式碼分享

NO IMAGE

一、關於pyaudio的安裝

現在pyaudio的版本為:PyAudio v0.2.9,若想安裝其他版本,請參考以下連結:http://people.csail.mit.edu/hubert/pyaudio/packages/
1、Windows 使用者:
通過pip安裝:
python -m pip install pyaudio
2、Mac OS X 使用者:
利用Homebrew安裝portaudio,然後通過pip安裝pyaudio:
brew install portaudio
pip install pyaudio

沒有安裝過Homebrew的使用者可通過在終端中輸入:ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)” 進行安裝,若有相關疑問可參考以下連結:http://brew.sh(Homebrew和Pyaudio的安裝需要事先安裝過Xcode的ComandLineTools)
相關連結:http://people.csail.mit.edu/hubert/pyaudio/#downloads

二、關於pyaudio的使用

為方便大家,列舉簡單的播放錄製的使用方法,這些程式碼在以上的連結裡均可找到,同時還有其他的一些功能的程式碼例項,在這裡我不一一列舉。

Play:

“””PyAudio Example: Play a WAVE file.”””
import pyaudio
import wave
import sys
CHUNK = 1024
if len(sys.argv) < 2:
    print(“Plays a wave file.\n\nUsage: %s filename.wav” % sys.argv[0])
    sys.exit(-1)
wf = wave.open(sys.argv[1], ‘rb’)
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),channels=wf.getnchannels(),rate=wf.getframerate(),output=True)
data = wf.readframes(CHUNK)
while data != ”:
    stream.write(data)
    data = wf.readframes(CHUNK)
stream.stop_stream()
stream.close()
p.terminate()

Record:

“””PyAudio example: Record a few seconds of audio and save 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()

三、關於聲音錄製的應用

之前有在python上做聊天室以及通訊功能的例項,其中利用到pyaudio進行聲音的錄製,對程式碼進行了簡單修改。為了便於儲存及識別,採用了將檔名命名為時間的形式,同時利用了全域性變數JUDGE進行錄製結束的判斷。程式碼中的NOW與FILENAME用於儲存錄製時間及錄製檔名,便於引用。本例項的最長錄製時間為30s,使用時可根據個人需求更改TIME的值以達到修改最長錄製時間的目的。使用時,先呼叫recorder中的函式record_wave,然後在錄製結束時將recorder中的全域性變數JUDGE置為False,即可完成錄製。
以下為recorder.py的原始碼:
   
#!usr/bin/env python
#coding=utf-8

import numpy as np
from pyaudio import PyAudio,paInt16
from datetime import datetime
import wave
from Tkinter import *

#define of params
NUM_SAMPLES = 2000
framerate = 8000
channels = 1
sampwidth = 2
#the longest record time
TIME = 30

FILENAME = ”
NOW = ”
SAVE = ”
JUDGE = True

def save_wave_file(filename, data):
  ”’save the date to the wav file”’
  wf = wave.open(filename, ‘wb’)
  wf.setnchannels(channels)
  wf.setsampwidth(sampwidth)
  wf.setframerate(framerate)
  wf.writeframes(“”.join(data))
  wf.close()

def record_wave():
  #open the input of wave
  pa = PyAudio()
  stream = pa.open(format = paInt16, channels = 1,rate = framerate, input = True,frames_per_buffer = NUM_SAMPLES)
  save_buffer = []
  count = 0
  global JUDGE
  while JUDGE and count<TIME*4:
    #read NUM_SAMPLES sampling data
    string_audio_data = stream.read(NUM_SAMPLES)
    save_buffer.append(string_audio_data)
    count = 1
    print “.”

  now = datetime.now().strftime(“%Y-%m-%d_%H_%M_%S”)
  filename = now “.wav”
  SAVE = save_wave_file(filename, save_buffer)
  save_buffer = []
  print filename, “saved”
  global FILENAME, NOW, JUDGE
  FILENAME = filename
  NOW = now

本文為博主用做學習記錄之需,即興而寫,供大家學習參考,若有程式碼排版等錯誤,敬請諒解。
pyaudio相關API文件地址 :  http://people.csail.mit.edu/hubert/pyaudio/docs/#class-pyaudio