~ubuntu-branches/ubuntu/trusty/python-pyaudio/trusty-proposed

« back to all changes in this revision

Viewing changes to test/record.py

  • Committer: Bazaar Package Importer
  • Author(s): Hubert Pham
  • Date: 2010-11-02 23:16:00 UTC
  • Revision ID: james.westby@ubuntu.com-20101102231600-cewidaqmf9gmi3za
Tags: upstream-0.2.4
ImportĀ upstreamĀ versionĀ 0.2.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
PyAudio example:
 
3
Record a few seconds of audio and save to a WAVE file.
 
4
"""
 
5
 
 
6
import pyaudio
 
7
import wave
 
8
import sys
 
9
 
 
10
chunk = 1024
 
11
FORMAT = pyaudio.paInt16
 
12
CHANNELS = 2
 
13
RATE = 44100
 
14
RECORD_SECONDS = 5
 
15
WAVE_OUTPUT_FILENAME = "output.wav"
 
16
 
 
17
if sys.platform == 'darwin':
 
18
    CHANNELS = 1
 
19
 
 
20
p = pyaudio.PyAudio()
 
21
 
 
22
stream = p.open(format = FORMAT,
 
23
                channels = CHANNELS,
 
24
                rate = RATE,
 
25
                input = True,
 
26
                frames_per_buffer = chunk)
 
27
 
 
28
print "* recording"
 
29
all = []
 
30
 
 
31
for i in range(0, RATE / chunk * RECORD_SECONDS):
 
32
    data = stream.read(chunk)
 
33
    all.append(data)
 
34
    
 
35
print "* done recording"
 
36
 
 
37
stream.stop_stream()
 
38
stream.close()
 
39
p.terminate()
 
40
 
 
41
# write data to WAVE file
 
42
data = ''.join(all)
 
43
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
 
44
wf.setnchannels(CHANNELS)
 
45
wf.setsampwidth(p.get_sample_size(FORMAT))
 
46
wf.setframerate(RATE)
 
47
wf.writeframes(data)
 
48
wf.close()