~ubuntu-branches/ubuntu/trusty/pyalsaaudio/trusty

« back to all changes in this revision

Viewing changes to recordtest.py

  • Committer: Bazaar Package Importer
  • Author(s): Artur Rona
  • Date: 2010-01-29 19:52:21 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20100129195221-l6y4xmauznm77dl3
Tags: 0.5+svn36-1ubuntu1
* Merge from debian testing (LP: #514453, #331171), remaining changes:
  - Call setup.py install with --root= --install-layout=deb.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
1
3
## recordtest.py
2
4
##
3
5
## This is an example of a simple sound capture script.
7
9
## writing the data to standard out.
8
10
##
9
11
## To test it out do the following:
10
 
## python recordtest.py > out.raw # talk to the microphone
 
12
## python recordtest.py out.raw # talk to the microphone
11
13
## aplay -r 8000 -f S16_LE -c 1 out.raw
12
14
 
13
 
import alsaaudio
 
15
 
 
16
# Footnote: I'd normally use print instead of sys.std(out|err).write,
 
17
# but we're in the middle of the conversion between python 2 and 3
 
18
# and this code runs on both versions without conversion
 
19
 
14
20
import sys
15
21
import time
16
 
 
17
 
# Open the device in nonblocking capture mode. The last argument could
18
 
# just as well have been zero for blocking mode. Then we could have
19
 
# left out the sleep call in the bottom of the loop
20
 
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE,alsaaudio.PCM_NONBLOCK)
21
 
 
22
 
# Set attributes: Mono, 8000 Hz, 16 bit little endian samples
23
 
inp.setchannels(1)
24
 
inp.setrate(8000)
25
 
inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
26
 
 
27
 
# The period size controls the internal number of frames per period.
28
 
# The significance of this parameter is documented in the ALSA api.
29
 
# For our purposes, it is suficcient to know that reads from the device
30
 
# will return this many frames. Each frame being 2 bytes long.
31
 
# This means that the reads below will return either 320 bytes of data
32
 
# or 0 bytes of data. The latter is possible because we are in nonblocking
33
 
# mode.
34
 
inp.setperiodsize(160)
35
 
 
36
 
loops = 1000000
37
 
while loops > 0:
38
 
  loops -= 1
39
 
  # Read data from device
40
 
  l,data = inp.read()
41
 
  
42
 
  if l:
43
 
    # actual data read. Write it to stdout
44
 
    sys.stdout.write(data)
45
 
  time.sleep(.001)
 
22
import getopt
 
23
import alsaaudio
 
24
 
 
25
def usage():
 
26
    sys.stderr.write('usage: recordtest.py [-c <card>] <file>\n')
 
27
    sys.exit(2)
 
28
 
 
29
if __name__ == '__main__':
 
30
 
 
31
    card = 'default'
 
32
 
 
33
    opts, args = getopt.getopt(sys.argv[1:], 'c:')
 
34
    for o, a in opts:
 
35
        if o == '-c':
 
36
            card = a
 
37
 
 
38
    if not args:
 
39
        usage()
 
40
 
 
41
    f = open(args[0], 'wb')
 
42
 
 
43
    # Open the device in nonblocking capture mode. The last argument could
 
44
    # just as well have been zero for blocking mode. Then we could have
 
45
    # left out the sleep call in the bottom of the loop
 
46
    inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NONBLOCK, card)
 
47
 
 
48
    # Set attributes: Mono, 44100 Hz, 16 bit little endian samples
 
49
    inp.setchannels(1)
 
50
    inp.setrate(44100)
 
51
    inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
 
52
 
 
53
    # The period size controls the internal number of frames per period.
 
54
    # The significance of this parameter is documented in the ALSA api.
 
55
    # For our purposes, it is suficcient to know that reads from the device
 
56
    # will return this many frames. Each frame being 2 bytes long.
 
57
    # This means that the reads below will return either 320 bytes of data
 
58
    # or 0 bytes of data. The latter is possible because we are in nonblocking
 
59
    # mode.
 
60
    inp.setperiodsize(160)
 
61
    
 
62
    loops = 1000000
 
63
    while loops > 0:
 
64
        loops -= 1
 
65
        # Read data from device
 
66
        l, data = inp.read()
 
67
      
 
68
        if l:
 
69
            f.write(data)
 
70
            time.sleep(.001)