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