6

I use Python 2.7.3, Mac OS 10.8.2 and Xcode 4.5.1

I am trying to record sound using PyAudio following the instructions in http://people.csail.mit.edu/hubert/pyaudio/

and using the program

"""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()

It works well with RATE = 44100. But I want to record with RATE = 16000 and CHANNELS = 1

Changing the values give me an error asenter image description here

How will I be able to record with RATE = 16000?

VeilEclipse
  • 2,766
  • 9
  • 35
  • 53
  • Does upping the chunk size (e.g. to 4096) solve the problem? – nneonneo Oct 21 '12 at 05:08
  • @nneonneo: No it does not. Only works perfectly with 44100 – VeilEclipse Oct 21 '12 at 05:26
  • Hm. I've run into this issue before, but never solved it. It might be a bug in PortAudio, or an unsupported recording configuration. You might be able to use a sample rate conversion library if you want 16000 Hz output. – nneonneo Oct 21 '12 at 05:42
  • 1
    You can use e.g. [pylibsamplerate](http://code.google.com/p/pyzic/wiki/PyLibSampleRate) (requires Numpy), which has a very simple API to do conversion (simply `src_simple(a, 16000/44100.)` once you have your audio converted to a Numpy array, as `np.frombuffer(data, np.int16)`). But, this is still a bit more complicated than I suspect you want, so I hope someone can provide a good answer to this question soon. – nneonneo Oct 21 '12 at 06:20
  • Maybe this will help: [How do I get a list of my device's audio sample rates using PyAudio or PortAudio?](http://stackoverflow.com/questions/4623572/how-do-i-get-a-list-of-my-devices-audio-sample-rates-using-pyaudio-or-portaudio). Your device probably doesn't support 16000 as a sampling rate and as already suggested you'll have to downsample it from 44100. The `audiospeex` codec in pyaudio seems to support resampling. – Pedro Romano Oct 21 '12 at 10:36
  • I am having the same error, but at 44100. Mac OSX Python 2.6. Same code (from PyAudio examples). I clicked on the link above ('How do I get a list of ...') and it is okay with 44100. What am I missing? Thanks. – user426364 Feb 01 '13 at 15:57

1 Answers1

1

I experienced the same problem. The issue was fixed in portaudio, but brew installed an old version for me. I upgraded with

brew install portaudio --HEAD

and then was able to run your code with CHANNELS=1 and RATE=16000

Joseph Sheedy
  • 6,296
  • 4
  • 30
  • 31