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