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

« back to all changes in this revision

Viewing changes to test/module_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: Low Level C Module test.
 
3
 
 
4
Record a few seconds of audio and save to a WAVE file.
 
5
"""
 
6
 
 
7
import _portaudio
 
8
import wave
 
9
import sys
 
10
 
 
11
chunk = 1024
 
12
 
 
13
FORMAT = _portaudio.paInt16
 
14
CHANNELS = 2
 
15
RATE = 44100
 
16
RECORD_SECONDS = 5
 
17
WAVE_OUTPUT_FILENAME = "output.wav"
 
18
 
 
19
if sys.platform == 'darwin':
 
20
    CHANNELS = 1
 
21
 
 
22
print "* initializing"
 
23
_portaudio.initialize()
 
24
 
 
25
print "* opening"
 
26
stream = _portaudio.open(format = FORMAT,
 
27
                         channels = CHANNELS,
 
28
                         rate = RATE,
 
29
                         input = True,
 
30
                         frames_per_buffer = chunk)
 
31
 
 
32
print "* starting stream"
 
33
_portaudio.start_stream(stream)
 
34
 
 
35
print "* recording"
 
36
all = []
 
37
 
 
38
for i in range(0, 44100 / chunk * RECORD_SECONDS):
 
39
    data = _portaudio.read_stream(stream, chunk)
 
40
    all.append(data)
 
41
    
 
42
print "* stopping stream"
 
43
_portaudio.stop_stream(stream)
 
44
 
 
45
print "* closing stream"
 
46
_portaudio.close(stream)
 
47
 
 
48
# match all initialize() with terminate() calls
 
49
_portaudio.terminate()
 
50
 
 
51
# write data to WAVE file
 
52
data = ''.join(all)
 
53
 
 
54
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
 
55
wf.setnchannels(CHANNELS)
 
56
wf.setsampwidth(_portaudio.get_sample_size(FORMAT))
 
57
wf.setframerate(RATE)
 
58
wf.writeframes(data)
 
59
wf.close()
 
60
 
 
61