| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="mixer-device-objects.html" /> |
| 13 | <link rel="prev" href="module-ossaudiodev.html" /> |
| 14 | <link rel="parent" href="module-ossaudiodev.html" /> |
| 15 | <link rel="next" href="mixer-device-objects.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>14.11.1 Audio Device Objects </title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="14.11 ossaudiodev " |
| 25 | href="module-ossaudiodev.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="14.11 ossaudiodev " |
| 28 | href="module-ossaudiodev.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="14.11.2 Mixer Device Objects" |
| 31 | href="mixer-device-objects.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="module-ossaudiodev.html">14.11 ossaudiodev </A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="module-ossaudiodev.html">14.11 ossaudiodev </A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="mixer-device-objects.html">14.11.2 Mixer Device Objects</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION00161110000000000000000"></A><A NAME="ossaudio-device-objects"></A> |
| 56 | <BR> |
| 57 | 14.11.1 Audio Device Objects |
| 58 | </H2> |
| 59 | |
| 60 | <P> |
| 61 | Before you can write to or read from an audio device, you must call |
| 62 | three methods in the correct order: |
| 63 | |
| 64 | <OL> |
| 65 | <LI><tt class="method">setfmt()</tt> to set the output format |
| 66 | </LI> |
| 67 | <LI><tt class="method">channels()</tt> to set the number of channels |
| 68 | </LI> |
| 69 | <LI><tt class="method">speed()</tt> to set the sample rate |
| 70 | </LI> |
| 71 | </OL> |
| 72 | Alternately, you can use the <tt class="method">setparameters()</tt> method to set all |
| 73 | three audio parameters at once. This is more convenient, but may not be |
| 74 | as flexible in all cases. |
| 75 | |
| 76 | <P> |
| 77 | The audio device objects returned by <tt class="function">open()</tt> define the |
| 78 | following methods: |
| 79 | |
| 80 | <P> |
| 81 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 82 | <td><nobr><b><tt id='l2h-4775' xml:id='l2h-4775' class="method">close</tt></b>(</nobr></td> |
| 83 | <td><var></var>)</td></tr></table></dt> |
| 84 | <dd> |
| 85 | Explicitly close the audio device. When you are done writing to or |
| 86 | reading from an audio device, you should explicitly close it. A closed |
| 87 | device cannot be used again. |
| 88 | </dl> |
| 89 | |
| 90 | <P> |
| 91 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 92 | <td><nobr><b><tt id='l2h-4776' xml:id='l2h-4776' class="method">fileno</tt></b>(</nobr></td> |
| 93 | <td><var></var>)</td></tr></table></dt> |
| 94 | <dd> |
| 95 | Return the file descriptor associated with the device. |
| 96 | </dl> |
| 97 | |
| 98 | <P> |
| 99 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 100 | <td><nobr><b><tt id='l2h-4777' xml:id='l2h-4777' class="method">read</tt></b>(</nobr></td> |
| 101 | <td><var>size</var>)</td></tr></table></dt> |
| 102 | <dd> |
| 103 | Read <var>size</var> bytes from the audio input and return them as a Python |
| 104 | string. Unlike most <span class="Unix">Unix</span> device drivers, OSS audio devices in |
| 105 | blocking mode (the default) will block <tt class="function">read()</tt> until the |
| 106 | entire requested amount of data is available. |
| 107 | </dl> |
| 108 | |
| 109 | <P> |
| 110 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 111 | <td><nobr><b><tt id='l2h-4778' xml:id='l2h-4778' class="method">write</tt></b>(</nobr></td> |
| 112 | <td><var>data</var>)</td></tr></table></dt> |
| 113 | <dd> |
| 114 | Write the Python string <var>data</var> to the audio device and return the |
| 115 | number of bytes written. If the audio device is in blocking mode (the |
| 116 | default), the entire string is always written (again, this is different |
| 117 | from usual <span class="Unix">Unix</span> device semantics). If the device is in non-blocking |
| 118 | mode, some data may not be written--see <tt class="method">writeall()</tt>. |
| 119 | </dl> |
| 120 | |
| 121 | <P> |
| 122 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 123 | <td><nobr><b><tt id='l2h-4779' xml:id='l2h-4779' class="method">writeall</tt></b>(</nobr></td> |
| 124 | <td><var>data</var>)</td></tr></table></dt> |
| 125 | <dd> |
| 126 | Write the entire Python string <var>data</var> to the audio device: waits |
| 127 | until the audio device is able to accept data, writes as much data as it |
| 128 | will accept, and repeats until <var>data</var> has been completely written. |
| 129 | If the device is in blocking mode (the default), this has the same |
| 130 | effect as <tt class="method">write()</tt>; <tt class="method">writeall()</tt> is only useful in |
| 131 | non-blocking mode. Has no return value, since the amount of data |
| 132 | written is always equal to the amount of data supplied. |
| 133 | </dl> |
| 134 | |
| 135 | <P> |
| 136 | The following methods each map to exactly one |
| 137 | <tt class="function">ioctl()</tt> system call. The correspondence is obvious: for |
| 138 | example, <tt class="method">setfmt()</tt> corresponds to the <code>SNDCTL_DSP_SETFMT</code> |
| 139 | ioctl, and <tt class="method">sync()</tt> to <code>SNDCTL_DSP_SYNC</code> (this can be useful |
| 140 | when consulting the OSS documentation). If the underlying |
| 141 | <tt class="function">ioctl()</tt> fails, they all raise <tt class="exception">IOError</tt>. |
| 142 | |
| 143 | <P> |
| 144 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 145 | <td><nobr><b><tt id='l2h-4780' xml:id='l2h-4780' class="method">nonblock</tt></b>(</nobr></td> |
| 146 | <td><var></var>)</td></tr></table></dt> |
| 147 | <dd> |
| 148 | Put the device into non-blocking mode. Once in non-blocking mode, there |
| 149 | is no way to return it to blocking mode. |
| 150 | </dl> |
| 151 | |
| 152 | <P> |
| 153 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 154 | <td><nobr><b><tt id='l2h-4781' xml:id='l2h-4781' class="method">getfmts</tt></b>(</nobr></td> |
| 155 | <td><var></var>)</td></tr></table></dt> |
| 156 | <dd> |
| 157 | Return a bitmask of the audio output formats supported by the |
| 158 | soundcard. Some of the formats supported by OSS are: |
| 159 | |
| 160 | <P> |
| 161 | <div class="center"><table class="realtable"> |
| 162 | <thead> |
| 163 | <tr> |
| 164 | <th class="left" >Format</th> |
| 165 | <th class="left" >Description</th> |
| 166 | </tr> |
| 167 | </thead> |
| 168 | <tbody> |
| 169 | <tr><td class="left" valign="baseline">AFMT_MU_LAW</td> |
| 170 | <td class="left" >a logarithmic encoding (used by Sun <code>.au</code> files and |
| 171 | <span class="file">/dev/audio</span>)</td></tr> |
| 172 | <tr><td class="left" valign="baseline">AFMT_A_LAW</td> |
| 173 | <td class="left" >a logarithmic encoding</td></tr> |
| 174 | <tr><td class="left" valign="baseline">AFMT_IMA_ADPCM</td> |
| 175 | <td class="left" >a 4:1 compressed format defined by the Interactive Multimedia |
| 176 | Association</td></tr> |
| 177 | <tr><td class="left" valign="baseline">AFMT_U8</td> |
| 178 | <td class="left" >Unsigned, 8-bit audio</td></tr> |
| 179 | <tr><td class="left" valign="baseline">AFMT_S16_LE</td> |
| 180 | <td class="left" >Signed, 16-bit audio, little-endian byte order (as used by |
| 181 | Intel processors)</td></tr> |
| 182 | <tr><td class="left" valign="baseline">AFMT_S16_BE</td> |
| 183 | <td class="left" >Signed, 16-bit audio, big-endian byte order (as used by 68k, |
| 184 | PowerPC, Sparc)</td></tr> |
| 185 | <tr><td class="left" valign="baseline">AFMT_S8</td> |
| 186 | <td class="left" >Signed, 8 bit audio</td></tr> |
| 187 | <tr><td class="left" valign="baseline">AFMT_U16_LE</td> |
| 188 | <td class="left" >Unsigned, 16-bit little-endian audio</td></tr> |
| 189 | <tr><td class="left" valign="baseline">AFMT_U16_BE</td> |
| 190 | <td class="left" >Unsigned, 16-bit big-endian audio</td></tr></tbody> |
| 191 | </table></div> |
| 192 | Consult the OSS documentation for a full list of audio formats, and note |
| 193 | that most devices support only a subset of these formats. Some older |
| 194 | devices only support <tt class="constant">AFMT_U8</tt>; the most common format used |
| 195 | today is <tt class="constant">AFMT_S16_LE</tt>. |
| 196 | </dl> |
| 197 | |
| 198 | <P> |
| 199 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 200 | <td><nobr><b><tt id='l2h-4782' xml:id='l2h-4782' class="method">setfmt</tt></b>(</nobr></td> |
| 201 | <td><var>format</var>)</td></tr></table></dt> |
| 202 | <dd> |
| 203 | Try to set the current audio format to <var>format</var>--see |
| 204 | <tt class="method">getfmts()</tt> for a list. Returns the audio format that the device |
| 205 | was set to, which may not be the requested format. May also be used to |
| 206 | return the current audio format--do this by passing an ``audio format'' |
| 207 | of |
| 208 | <tt class="constant">AFMT_QUERY</tt>. |
| 209 | </dl> |
| 210 | |
| 211 | <P> |
| 212 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 213 | <td><nobr><b><tt id='l2h-4783' xml:id='l2h-4783' class="method">channels</tt></b>(</nobr></td> |
| 214 | <td><var>nchannels</var>)</td></tr></table></dt> |
| 215 | <dd> |
| 216 | Set the number of output channels to <var>nchannels</var>. A value of 1 |
| 217 | indicates monophonic sound, 2 stereophonic. Some devices may have more |
| 218 | than 2 channels, and some high-end devices may not support mono. |
| 219 | Returns the number of channels the device was set to. |
| 220 | </dl> |
| 221 | |
| 222 | <P> |
| 223 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 224 | <td><nobr><b><tt id='l2h-4784' xml:id='l2h-4784' class="method">speed</tt></b>(</nobr></td> |
| 225 | <td><var>samplerate</var>)</td></tr></table></dt> |
| 226 | <dd> |
| 227 | Try to set the audio sampling rate to <var>samplerate</var> samples per |
| 228 | second. Returns the rate actually set. Most sound devices don't |
| 229 | support arbitrary sampling rates. Common rates are: |
| 230 | <div class="center"><table class="realtable"> |
| 231 | <thead> |
| 232 | <tr> |
| 233 | <th class="left" >Rate</th> |
| 234 | <th class="left" >Description</th> |
| 235 | </tr> |
| 236 | </thead> |
| 237 | <tbody> |
| 238 | <tr><td class="left" valign="baseline">8000</td> |
| 239 | <td class="left" >default rate for <span class="file">/dev/audio</span></td></tr> |
| 240 | <tr><td class="left" valign="baseline">11025</td> |
| 241 | <td class="left" >speech recording</td></tr> |
| 242 | <tr><td class="left" valign="baseline">22050</td> |
| 243 | <td class="left" ></td></tr> |
| 244 | <tr><td class="left" valign="baseline">44100</td> |
| 245 | <td class="left" >CD quality audio (at 16 bits/sample and 2 channels)</td></tr> |
| 246 | <tr><td class="left" valign="baseline">96000</td> |
| 247 | <td class="left" >DVD quality audio (at 24 bits/sample)</td></tr></tbody> |
| 248 | </table></div> |
| 249 | </dl> |
| 250 | |
| 251 | <P> |
| 252 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 253 | <td><nobr><b><tt id='l2h-4785' xml:id='l2h-4785' class="method">sync</tt></b>(</nobr></td> |
| 254 | <td><var></var>)</td></tr></table></dt> |
| 255 | <dd> |
| 256 | Wait until the sound device has played every byte in its buffer. (This |
| 257 | happens implicitly when the device is closed.) The OSS documentation |
| 258 | recommends closing and re-opening the device rather than using |
| 259 | <tt class="method">sync()</tt>. |
| 260 | </dl> |
| 261 | |
| 262 | <P> |
| 263 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 264 | <td><nobr><b><tt id='l2h-4786' xml:id='l2h-4786' class="method">reset</tt></b>(</nobr></td> |
| 265 | <td><var></var>)</td></tr></table></dt> |
| 266 | <dd> |
| 267 | Immediately stop playing or recording and return the device to a |
| 268 | state where it can accept commands. The OSS documentation recommends |
| 269 | closing and re-opening the device after calling <tt class="method">reset()</tt>. |
| 270 | </dl> |
| 271 | |
| 272 | <P> |
| 273 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 274 | <td><nobr><b><tt id='l2h-4787' xml:id='l2h-4787' class="method">post</tt></b>(</nobr></td> |
| 275 | <td><var></var>)</td></tr></table></dt> |
| 276 | <dd> |
| 277 | Tell the driver that there is likely to be a pause in the output, making |
| 278 | it possible for the device to handle the pause more intelligently. You |
| 279 | might use this after playing a spot sound effect, before waiting for |
| 280 | user input, or before doing disk I/O. |
| 281 | </dl> |
| 282 | |
| 283 | <P> |
| 284 | The following convenience methods combine several ioctls, or one ioctl |
| 285 | and some simple calculations. |
| 286 | |
| 287 | <P> |
| 288 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 289 | <td><nobr><b><tt id='l2h-4788' xml:id='l2h-4788' class="method">setparameters</tt></b>(</nobr></td> |
| 290 | <td><var>format, nchannels, samplerate </var><big>[</big><var>, strict=False</var><big>]</big><var></var>)</td></tr></table></dt> |
| 291 | <dd> |
| 292 | |
| 293 | <P> |
| 294 | Set the key audio sampling parameters--sample format, number of |
| 295 | channels, and sampling rate--in one method call. <var>format</var>, |
| 296 | <var>nchannels</var>, and <var>samplerate</var> should be as specified in the |
| 297 | <tt class="method">setfmt()</tt>, <tt class="method">channels()</tt>, and <tt class="method">speed()</tt> |
| 298 | methods. If <var>strict</var> is true, <tt class="method">setparameters()</tt> checks to |
| 299 | see if each parameter was actually set to the requested value, and |
| 300 | raises <tt class="exception">OSSAudioError</tt> if not. Returns a tuple (<var>format</var>, |
| 301 | <var>nchannels</var>, <var>samplerate</var>) indicating the parameter values that |
| 302 | were actually set by the device driver (i.e., the same as the return |
| 303 | values of <tt class="method">setfmt()</tt>, <tt class="method">channels()</tt>, and <tt class="method">speed()</tt>). |
| 304 | |
| 305 | <P> |
| 306 | For example, |
| 307 | <div class="verbatim"><pre> |
| 308 | (fmt, channels, rate) = dsp.setparameters(fmt, channels, rate) |
| 309 | </pre></div> |
| 310 | is equivalent to |
| 311 | <div class="verbatim"><pre> |
| 312 | fmt = dsp.setfmt(fmt) |
| 313 | channels = dsp.channels(channels) |
| 314 | rate = dsp.rate(channels) |
| 315 | </pre></div> |
| 316 | </dl> |
| 317 | |
| 318 | <P> |
| 319 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 320 | <td><nobr><b><tt id='l2h-4789' xml:id='l2h-4789' class="method">bufsize</tt></b>(</nobr></td> |
| 321 | <td><var></var>)</td></tr></table></dt> |
| 322 | <dd> |
| 323 | Returns the size of the hardware buffer, in samples. |
| 324 | </dl> |
| 325 | |
| 326 | <P> |
| 327 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 328 | <td><nobr><b><tt id='l2h-4790' xml:id='l2h-4790' class="method">obufcount</tt></b>(</nobr></td> |
| 329 | <td><var></var>)</td></tr></table></dt> |
| 330 | <dd> |
| 331 | Returns the number of samples that are in the hardware buffer yet to be |
| 332 | played. |
| 333 | </dl> |
| 334 | |
| 335 | <P> |
| 336 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 337 | <td><nobr><b><tt id='l2h-4791' xml:id='l2h-4791' class="method">obuffree</tt></b>(</nobr></td> |
| 338 | <td><var></var>)</td></tr></table></dt> |
| 339 | <dd> |
| 340 | Returns the number of samples that could be queued into the hardware |
| 341 | buffer to be played without blocking. |
| 342 | </dl> |
| 343 | |
| 344 | <P> |
| 345 | |
| 346 | <DIV CLASS="navigation"> |
| 347 | <div class='online-navigation'> |
| 348 | <p></p><hr /> |
| 349 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 350 | <tr> |
| 351 | <td class='online-navigation'><a rel="prev" title="14.11 ossaudiodev " |
| 352 | href="module-ossaudiodev.html"><img src='../icons/previous.png' |
| 353 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 354 | <td class='online-navigation'><a rel="parent" title="14.11 ossaudiodev " |
| 355 | href="module-ossaudiodev.html"><img src='../icons/up.png' |
| 356 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 357 | <td class='online-navigation'><a rel="next" title="14.11.2 Mixer Device Objects" |
| 358 | href="mixer-device-objects.html"><img src='../icons/next.png' |
| 359 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 360 | <td align="center" width="100%">Python Library Reference</td> |
| 361 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 362 | href="contents.html"><img src='../icons/contents.png' |
| 363 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 364 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 365 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 366 | <td class='online-navigation'><a rel="index" title="Index" |
| 367 | href="genindex.html"><img src='../icons/index.png' |
| 368 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 369 | </tr></table> |
| 370 | <div class='online-navigation'> |
| 371 | <b class="navlabel">Previous:</b> |
| 372 | <a class="sectref" rel="prev" href="module-ossaudiodev.html">14.11 ossaudiodev </A> |
| 373 | <b class="navlabel">Up:</b> |
| 374 | <a class="sectref" rel="parent" href="module-ossaudiodev.html">14.11 ossaudiodev </A> |
| 375 | <b class="navlabel">Next:</b> |
| 376 | <a class="sectref" rel="next" href="mixer-device-objects.html">14.11.2 Mixer Device Objects</A> |
| 377 | </div> |
| 378 | </div> |
| 379 | <hr /> |
| 380 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 381 | </DIV> |
| 382 | <!--End of Navigation Panel--> |
| 383 | <ADDRESS> |
| 384 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 385 | </ADDRESS> |
| 386 | </BODY> |
| 387 | </HTML> |