blob: bafd7c5311b5eb6a2859950a3d48bffe7b5162a0 [file] [log] [blame]
Fred Drake83c1a391998-02-19 18:59:48 +00001\section{Built-in Module \sectcode{sunaudiodev}}
2\label{module-sunaudiodev}
3\bimodindex{sunaudiodev}
4
5This module allows you to access the sun audio interface. The sun
6audio hardware is capable of recording and playing back audio data
7in U-LAW format with a sample rate of 8K per second. A full
8description can be gotten with \samp{man audio}.
9
10The module defines the following variables and functions:
11
12\setindexsubitem{(in module sunaudiodev)}
13\begin{excdesc}{error}
14This exception is raised on all errors. The argument is a string
15describing what went wrong.
16\end{excdesc}
17
18\begin{funcdesc}{open}{mode}
19This function opens the audio device and returns a sun audio device
20object. This object can then be used to do I/O on. The \var{mode} parameter
21is one of \code{'r'} for record-only access, \code{'w'} for play-only
22access, \code{'rw'} for both and \code{'control'} for access to the
23control device. Since only one process is allowed to have the recorder
24or player open at the same time it is a good idea to open the device
25only for the activity needed. See the audio manpage for details.
26\end{funcdesc}
27
28\subsection{Audio Device Objects}
29
30The audio device objects are returned by \code{open} define the
31following methods (except \code{control} objects which only provide
32getinfo, setinfo and drain):
33
34\setindexsubitem{(audio device method)}
35
36\begin{funcdesc}{close}{}
37This method explicitly closes the device. It is useful in situations
38where deleting the object does not immediately close it since there
39are other references to it. A closed device should not be used again.
40\end{funcdesc}
41
42\begin{funcdesc}{drain}{}
43This method waits until all pending output is processed and then returns.
44Calling this method is often not necessary: destroying the object will
45automatically close the audio device and this will do an implicit drain.
46\end{funcdesc}
47
48\begin{funcdesc}{flush}{}
49This method discards all pending output. It can be used avoid the
50slow response to a user's stop request (due to buffering of up to one
51second of sound).
52\end{funcdesc}
53
54\begin{funcdesc}{getinfo}{}
55This method retrieves status information like input and output volume,
56etc. and returns it in the form of
57an audio status object. This object has no methods but it contains a
58number of attributes describing the current device status. The names
59and meanings of the attributes are described in
60\file{/usr/include/sun/audioio.h} and in the audio man page. Member names
61are slightly different from their C counterparts: a status object is
62only a single structure. Members of the \code{play} substructure have
63\samp{o_} prepended to their name and members of the \code{record}
64structure have \samp{i_}. So, the C member \code{play.sample_rate} is
65accessed as \code{o_sample_rate}, \code{record.gain} as \code{i_gain}
66and \code{monitor_gain} plainly as \code{monitor_gain}.
67\end{funcdesc}
68
69\begin{funcdesc}{ibufcount}{}
70This method returns the number of samples that are buffered on the
71recording side, i.e.
72the program will not block on a \function{read()} call of so many samples.
73\end{funcdesc}
74
75\begin{funcdesc}{obufcount}{}
76This method returns the number of samples buffered on the playback
77side. Unfortunately, this number cannot be used to determine a number
78of samples that can be written without blocking since the kernel
79output queue length seems to be variable.
80\end{funcdesc}
81
82\begin{funcdesc}{read}{size}
83This method reads \var{size} samples from the audio input and returns
Fred Drake21237741998-04-03 07:06:01 +000084them as a Python string. The function blocks until enough data is available.
Fred Drake83c1a391998-02-19 18:59:48 +000085\end{funcdesc}
86
87\begin{funcdesc}{setinfo}{status}
88This method sets the audio device status parameters. The \var{status}
89parameter is an device status object as returned by \function{getinfo()} and
90possibly modified by the program.
91\end{funcdesc}
92
93\begin{funcdesc}{write}{samples}
Fred Drake21237741998-04-03 07:06:01 +000094Write is passed a Python string containing audio samples to be played.
Fred Drake83c1a391998-02-19 18:59:48 +000095If there is enough buffer space free it will immediately return,
96otherwise it will block.
97\end{funcdesc}
98
99There is a companion module, \module{SUNAUDIODEV}, which defines useful
100symbolic constants like \constant{MIN_GAIN}, \constant{MAX_GAIN},
101\constant{SPEAKER}, etc. The names of
102the constants are the same names as used in the \C{} include file
103\code{<sun/audioio.h>}, with the leading string \samp{AUDIO_}
104stripped.
105\refstmodindex{SUNAUDIODEV}
106
107Useability of the control device is limited at the moment, since there
108is no way to use the ``wait for something to happen'' feature the
109device provides.