blob: a0064c3b25b2048b8710dc6e0c3fb87c495d6ed5 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`sunaudiodev` --- Access to Sun audio hardware
3===================================================
4
5.. module:: sunaudiodev
6 :platform: SunOS
7 :synopsis: Access to Sun audio hardware.
8
9
10.. index:: single: u-LAW
11
12This module allows you to access the Sun audio interface. The Sun audio hardware
13is capable of recording and playing back audio data in u-LAW format with a
14sample rate of 8K per second. A full description can be found in the
15:manpage:`audio(7I)` manual page.
16
17.. index:: module: SUNAUDIODEV
18
19The module :mod:`SUNAUDIODEV` defines constants which may be used with this
20module.
21
22This module defines the following variables and functions:
23
24
25.. exception:: error
26
27 This exception is raised on all errors. The argument is a string describing what
28 went wrong.
29
30
31.. function:: open(mode)
32
33 This function opens the audio device and returns a Sun audio device object. This
34 object can then be used to do I/O on. The *mode* parameter is one of ``'r'`` for
35 record-only access, ``'w'`` for play-only access, ``'rw'`` for both and
36 ``'control'`` for access to the control device. Since only one process is
37 allowed to have the recorder or player open at the same time it is a good idea
38 to open the device only for the activity needed. See :manpage:`audio(7I)` for
39 details.
40
41 As per the manpage, this module first looks in the environment variable
42 ``AUDIODEV`` for the base audio device filename. If not found, it falls back to
43 :file:`/dev/audio`. The control device is calculated by appending "ctl" to the
44 base audio device.
45
46
47.. _audio-device-objects:
48
49Audio Device Objects
50--------------------
51
52The audio device objects are returned by :func:`open` define the following
53methods (except ``control`` objects which only provide :meth:`getinfo`,
54:meth:`setinfo`, :meth:`fileno`, and :meth:`drain`):
55
56
57.. method:: audio device.close()
58
59 This method explicitly closes the device. It is useful in situations where
60 deleting the object does not immediately close it since there are other
61 references to it. A closed device should not be used again.
62
63
64.. method:: audio device.fileno()
65
66 Returns the file descriptor associated with the device. This can be used to set
67 up ``SIGPOLL`` notification, as described below.
68
69
70.. method:: audio device.drain()
71
72 This method waits until all pending output is processed and then returns.
73 Calling this method is often not necessary: destroying the object will
74 automatically close the audio device and this will do an implicit drain.
75
76
77.. method:: audio device.flush()
78
79 This method discards all pending output. It can be used avoid the slow response
80 to a user's stop request (due to buffering of up to one second of sound).
81
82
83.. method:: audio device.getinfo()
84
85 This method retrieves status information like input and output volume, etc. and
86 returns it in the form of an audio status object. This object has no methods but
87 it contains a number of attributes describing the current device status. The
88 names and meanings of the attributes are described in ``<sun/audioio.h>`` and in
89 the :manpage:`audio(7I)` manual page. Member names are slightly different from
90 their C counterparts: a status object is only a single structure. Members of the
91 :cdata:`play` substructure have ``o_`` prepended to their name and members of
92 the :cdata:`record` structure have ``i_``. So, the C member
93 :cdata:`play.sample_rate` is accessed as :attr:`o_sample_rate`,
94 :cdata:`record.gain` as :attr:`i_gain` and :cdata:`monitor_gain` plainly as
95 :attr:`monitor_gain`.
96
97
98.. method:: audio device.ibufcount()
99
100 This method returns the number of samples that are buffered on the recording
101 side, i.e. the program will not block on a :func:`read` call of so many samples.
102
103
104.. method:: audio device.obufcount()
105
106 This method returns the number of samples buffered on the playback side.
107 Unfortunately, this number cannot be used to determine a number of samples that
108 can be written without blocking since the kernel output queue length seems to be
109 variable.
110
111
112.. method:: audio device.read(size)
113
114 This method reads *size* samples from the audio input and returns them as a
115 Python string. The function blocks until enough data is available.
116
117
118.. method:: audio device.setinfo(status)
119
120 This method sets the audio device status parameters. The *status* parameter is
121 an device status object as returned by :func:`getinfo` and possibly modified by
122 the program.
123
124
125.. method:: audio device.write(samples)
126
127 Write is passed a Python string containing audio samples to be played. If there
128 is enough buffer space free it will immediately return, otherwise it will block.
129
130The audio device supports asynchronous notification of various events, through
131the SIGPOLL signal. Here's an example of how you might enable this in Python::
132
133 def handle_sigpoll(signum, frame):
134 print 'I got a SIGPOLL update'
135
136 import fcntl, signal, STROPTS
137
138 signal.signal(signal.SIGPOLL, handle_sigpoll)
139 fcntl.ioctl(audio_obj.fileno(), STROPTS.I_SETSIG, STROPTS.S_MSG)
140
141
142:mod:`SUNAUDIODEV` --- Constants used with :mod:`sunaudiodev`
143=============================================================
144
145.. module:: SUNAUDIODEV
146 :platform: SunOS
147 :synopsis: Constants for use with sunaudiodev.
148
149
150.. index:: module: sunaudiodev
151
152This is a companion module to :mod:`sunaudiodev` which defines useful symbolic
153constants like :const:`MIN_GAIN`, :const:`MAX_GAIN`, :const:`SPEAKER`, etc. The
154names of the constants are the same names as used in the C include file
155``<sun/audioio.h>``, with the leading string ``AUDIO_`` stripped.
156