blob: 4529cefd3c05af409d53c626fd5f481ad30922ab [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{sunaudiodev} ---
2 Access to Sun audio hardware.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{builtin}{sunaudiodev}
4
Fred Drake295da241998-08-10 19:42:37 +00005\modulesynopsis{Access to Sun audio hardware.}
Fred Drakeb91e9341998-07-23 17:59:49 +00006
Fred Drake83c1a391998-02-19 18:59:48 +00007
Fred Drake295da241998-08-10 19:42:37 +00008This module allows you to access the Sun audio interface. The Sun
Fred Drake83c1a391998-02-19 18:59:48 +00009audio hardware is capable of recording and playing back audio data
Fred Drakefc576191998-04-04 07:15:02 +000010in u-LAW\index{u-LAW} format with a sample rate of 8K per second. A
11full description can be found in the \manpage{audio}{7I} manual page.
Fred Drake83c1a391998-02-19 18:59:48 +000012
13The module defines the following variables and functions:
14
Fred Drake83c1a391998-02-19 18:59:48 +000015\begin{excdesc}{error}
16This exception is raised on all errors. The argument is a string
17describing what went wrong.
18\end{excdesc}
19
20\begin{funcdesc}{open}{mode}
Barry Warsaw4a1cdd71998-10-31 23:19:00 +000021This function opens the audio device and returns a Sun audio device
Fred Drake83c1a391998-02-19 18:59:48 +000022object. This object can then be used to do I/O on. The \var{mode} parameter
23is one of \code{'r'} for record-only access, \code{'w'} for play-only
24access, \code{'rw'} for both and \code{'control'} for access to the
25control device. Since only one process is allowed to have the recorder
26or player open at the same time it is a good idea to open the device
Fred Drakefc576191998-04-04 07:15:02 +000027only for the activity needed. See \manpage{audio}{7I} for details.
Barry Warsaw4a1cdd71998-10-31 23:19:00 +000028
29As per the manpage, this module first looks in the environment
30variable \code{AUDIODEV} for the base audio device filename. If not
31found, it falls back to \file{/dev/audio}. The control device is
32calculated by appending ``ctl'' to the base audio device.
Fred Drake83c1a391998-02-19 18:59:48 +000033\end{funcdesc}
34
Fred Drakefc576191998-04-04 07:15:02 +000035
Fred Drake83c1a391998-02-19 18:59:48 +000036\subsection{Audio Device Objects}
Fred Drakefc576191998-04-04 07:15:02 +000037\label{audio-device-objects}
Fred Drake83c1a391998-02-19 18:59:48 +000038
Fred Drakefc576191998-04-04 07:15:02 +000039The audio device objects are returned by \function{open()} define the
Fred Drake83c1a391998-02-19 18:59:48 +000040following methods (except \code{control} objects which only provide
Barry Warsaw4a1cdd71998-10-31 23:19:00 +000041\method{getinfo()}, \method{setinfo()}, \method{fileno()}, and
42\method{drain()}):
Fred Drake83c1a391998-02-19 18:59:48 +000043
Fred Drakefc576191998-04-04 07:15:02 +000044\begin{methoddesc}[audio device]{close}{}
Fred Drake83c1a391998-02-19 18:59:48 +000045This method explicitly closes the device. It is useful in situations
46where deleting the object does not immediately close it since there
47are other references to it. A closed device should not be used again.
Fred Drakefc576191998-04-04 07:15:02 +000048\end{methoddesc}
Fred Drake83c1a391998-02-19 18:59:48 +000049
Barry Warsaw4a1cdd71998-10-31 23:19:00 +000050\begin{methoddesc}[audio device]{fileno}{}
51Returns the file descriptor associated with the device. This can be
52used to set up \code{SIGPOLL} notification, as described below.
53\end{methoddocs}
54
Fred Drakefc576191998-04-04 07:15:02 +000055\begin{methoddesc}[audio device]{drain}{}
Fred Drake83c1a391998-02-19 18:59:48 +000056This method waits until all pending output is processed and then returns.
57Calling this method is often not necessary: destroying the object will
58automatically close the audio device and this will do an implicit drain.
Fred Drakefc576191998-04-04 07:15:02 +000059\end{methoddesc}
Fred Drake83c1a391998-02-19 18:59:48 +000060
Fred Drakefc576191998-04-04 07:15:02 +000061\begin{methoddesc}[audio device]{flush}{}
Fred Drake83c1a391998-02-19 18:59:48 +000062This method discards all pending output. It can be used avoid the
63slow response to a user's stop request (due to buffering of up to one
64second of sound).
Fred Drakefc576191998-04-04 07:15:02 +000065\end{methoddesc}
Fred Drake83c1a391998-02-19 18:59:48 +000066
Fred Drakefc576191998-04-04 07:15:02 +000067\begin{methoddesc}[audio device]{getinfo}{}
Fred Drake83c1a391998-02-19 18:59:48 +000068This method retrieves status information like input and output volume,
69etc. and returns it in the form of
70an audio status object. This object has no methods but it contains a
71number of attributes describing the current device status. The names
72and meanings of the attributes are described in
Fred Drakefc576191998-04-04 07:15:02 +000073\file{/usr/include/sun/audioio.h} and in the \manpage{audio}{7I}
74manual page. Member names
75are slightly different from their \C{} counterparts: a status object is
76only a single structure. Members of the \cdata{play} substructure have
77\samp{o_} prepended to their name and members of the \cdata{record}
78structure have \samp{i_}. So, the \C{} member \cdata{play.sample_rate} is
79accessed as \member{o_sample_rate}, \cdata{record.gain} as \member{i_gain}
80and \cdata{monitor_gain} plainly as \member{monitor_gain}.
81\end{methoddesc}
Fred Drake83c1a391998-02-19 18:59:48 +000082
Fred Drakefc576191998-04-04 07:15:02 +000083\begin{methoddesc}[audio device]{ibufcount}{}
Fred Drake83c1a391998-02-19 18:59:48 +000084This method returns the number of samples that are buffered on the
Fred Drakefc576191998-04-04 07:15:02 +000085recording side, i.e.\ the program will not block on a
86\function{read()} call of so many samples.
87\end{methoddesc}
Fred Drake83c1a391998-02-19 18:59:48 +000088
Fred Drakefc576191998-04-04 07:15:02 +000089\begin{methoddesc}[audio device]{obufcount}{}
Fred Drake83c1a391998-02-19 18:59:48 +000090This method returns the number of samples buffered on the playback
91side. Unfortunately, this number cannot be used to determine a number
92of samples that can be written without blocking since the kernel
93output queue length seems to be variable.
Fred Drakefc576191998-04-04 07:15:02 +000094\end{methoddesc}
Fred Drake83c1a391998-02-19 18:59:48 +000095
Fred Drakefc576191998-04-04 07:15:02 +000096\begin{methoddesc}[audio device]{read}{size}
Fred Drake83c1a391998-02-19 18:59:48 +000097This method reads \var{size} samples from the audio input and returns
Fred Drake21237741998-04-03 07:06:01 +000098them as a Python string. The function blocks until enough data is available.
Fred Drakefc576191998-04-04 07:15:02 +000099\end{methoddesc}
Fred Drake83c1a391998-02-19 18:59:48 +0000100
Fred Drakefc576191998-04-04 07:15:02 +0000101\begin{methoddesc}[audio device]{setinfo}{status}
Fred Drake83c1a391998-02-19 18:59:48 +0000102This method sets the audio device status parameters. The \var{status}
103parameter is an device status object as returned by \function{getinfo()} and
104possibly modified by the program.
Fred Drakefc576191998-04-04 07:15:02 +0000105\end{methoddesc}
Fred Drake83c1a391998-02-19 18:59:48 +0000106
Fred Drakefc576191998-04-04 07:15:02 +0000107\begin{methoddesc}[audio device]{write}{samples}
Fred Drake21237741998-04-03 07:06:01 +0000108Write is passed a Python string containing audio samples to be played.
Fred Drake83c1a391998-02-19 18:59:48 +0000109If there is enough buffer space free it will immediately return,
110otherwise it will block.
Fred Drakefc576191998-04-04 07:15:02 +0000111\end{methoddesc}
Fred Drake83c1a391998-02-19 18:59:48 +0000112
Fred Drakefc576191998-04-04 07:15:02 +0000113There is a companion module,
114\module{SUNAUDIODEV}\refstmodindex{SUNAUDIODEV}, which defines useful
Fred Drake83c1a391998-02-19 18:59:48 +0000115symbolic constants like \constant{MIN_GAIN}, \constant{MAX_GAIN},
Fred Drakefc576191998-04-04 07:15:02 +0000116\constant{SPEAKER}, etc. The names of the constants are the same names
117as used in the \C{} include file \code{<sun/audioio.h>}, with the
118leading string \samp{AUDIO_} stripped.
Fred Drake83c1a391998-02-19 18:59:48 +0000119
Barry Warsaw4a1cdd71998-10-31 23:19:00 +0000120The audio device supports asynchronous notification of various events,
121through the SIGPOLL signal. Here's an example of how you might enable
122this in Python:
123
124\begin{verbatim}
125def handle_sigpoll(signum, frame):
126 print 'I got a SIGPOLL update'
127pp
128import fcntl, signal, STROPTS
129
130signal.signal(signal.SIGPOLL, handle_sigpoll)
131fcntl.ioctl(audio_obj.fileno(), STROPTS.I_SETSIG, STROPTS.S_MSG)
132\end{verbatim}