Having all kinds of problems I eventually built a python script by copying [url]http://stackoverflow.com/questions/8921 ... python[url] and then adding some code to write the file. The trouble was I kept getting IOError: [Errno Input overflowed] -9981. I replaced the orginal code as follows
Code: Select all
for i in range(0, RATE / chunk * RECORD_SECONDS):
data = stream.read(chunk)
frames.append(data)
# check for silence here by comparing the level with 0 (or some threshold) for
# the contents of data.
# then write data or not to a fileCode: Select all
for i in range(0, 1):
data = stream.read(chunk)
frames.append(data)Here is the final result.
Code: Select all
# main recording logic from http://stackoverflow.com/questions/892199/detect-record-audio-in-python
import pyaudio
import sys
import wave
chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 22050 #44100 might work also but not tested
RECORD_SECONDS = 10
WAVE_OUTPUT_FILENAME = "output.wav"
p = pyaudio.PyAudio()
frames = []
stream = p.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
frames_per_buffer = chunk)
print "* recording"
for i in range(0, 4):
data = stream.read(chunk)
frames.append(data)
print "* done"
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()I then tried the chunk variable firstly reducing it to 512 (didn't work) , and then doubling it to 2048. This seemed to work.
I then replaced the ammended code for the recording, as above, back to the original and tried recording times up to 30 seconds which seemed to work.
I hope that helps, no idea why this works.