Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / html / python / lib / ossaudio-device-objects.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="lib.html" title='Python Library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="mixer-device-objects.html" />
<link rel="prev" href="module-ossaudiodev.html" />
<link rel="parent" href="module-ossaudiodev.html" />
<link rel="next" href="mixer-device-objects.html" />
<meta name='aesop' content='information' />
<title>14.11.1 Audio Device Objects </title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="14.11 ossaudiodev "
href="module-ossaudiodev.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="14.11 ossaudiodev "
href="module-ossaudiodev.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="14.11.2 Mixer Device Objects"
href="mixer-device-objects.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-ossaudiodev.html">14.11 ossaudiodev </A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-ossaudiodev.html">14.11 ossaudiodev </A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="mixer-device-objects.html">14.11.2 Mixer Device Objects</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H2><A NAME="SECTION00161110000000000000000"></A><A NAME="ossaudio-device-objects"></A>
<BR>
14.11.1 Audio Device Objects
</H2>
<P>
Before you can write to or read from an audio device, you must call
three methods in the correct order:
<OL>
<LI><tt class="method">setfmt()</tt> to set the output format
</LI>
<LI><tt class="method">channels()</tt> to set the number of channels
</LI>
<LI><tt class="method">speed()</tt> to set the sample rate
</LI>
</OL>
Alternately, you can use the <tt class="method">setparameters()</tt> method to set all
three audio parameters at once. This is more convenient, but may not be
as flexible in all cases.
<P>
The audio device objects returned by <tt class="function">open()</tt> define the
following methods:
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4775' xml:id='l2h-4775' class="method">close</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Explicitly close the audio device. When you are done writing to or
reading from an audio device, you should explicitly close it. A closed
device cannot be used again.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4776' xml:id='l2h-4776' class="method">fileno</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Return the file descriptor associated with the device.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4777' xml:id='l2h-4777' class="method">read</tt></b>(</nobr></td>
<td><var>size</var>)</td></tr></table></dt>
<dd>
Read <var>size</var> bytes from the audio input and return them as a Python
string. Unlike most <span class="Unix">Unix</span> device drivers, OSS audio devices in
blocking mode (the default) will block <tt class="function">read()</tt> until the
entire requested amount of data is available.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4778' xml:id='l2h-4778' class="method">write</tt></b>(</nobr></td>
<td><var>data</var>)</td></tr></table></dt>
<dd>
Write the Python string <var>data</var> to the audio device and return the
number of bytes written. If the audio device is in blocking mode (the
default), the entire string is always written (again, this is different
from usual <span class="Unix">Unix</span> device semantics). If the device is in non-blocking
mode, some data may not be written--see <tt class="method">writeall()</tt>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4779' xml:id='l2h-4779' class="method">writeall</tt></b>(</nobr></td>
<td><var>data</var>)</td></tr></table></dt>
<dd>
Write the entire Python string <var>data</var> to the audio device: waits
until the audio device is able to accept data, writes as much data as it
will accept, and repeats until <var>data</var> has been completely written.
If the device is in blocking mode (the default), this has the same
effect as <tt class="method">write()</tt>; <tt class="method">writeall()</tt> is only useful in
non-blocking mode. Has no return value, since the amount of data
written is always equal to the amount of data supplied.
</dl>
<P>
The following methods each map to exactly one
<tt class="function">ioctl()</tt> system call. The correspondence is obvious: for
example, <tt class="method">setfmt()</tt> corresponds to the <code>SNDCTL_DSP_SETFMT</code>
ioctl, and <tt class="method">sync()</tt> to <code>SNDCTL_DSP_SYNC</code> (this can be useful
when consulting the OSS documentation). If the underlying
<tt class="function">ioctl()</tt> fails, they all raise <tt class="exception">IOError</tt>.
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4780' xml:id='l2h-4780' class="method">nonblock</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Put the device into non-blocking mode. Once in non-blocking mode, there
is no way to return it to blocking mode.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4781' xml:id='l2h-4781' class="method">getfmts</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Return a bitmask of the audio output formats supported by the
soundcard. Some of the formats supported by OSS are:
<P>
<div class="center"><table class="realtable">
<thead>
<tr>
<th class="left" >Format</th>
<th class="left" >Description</th>
</tr>
</thead>
<tbody>
<tr><td class="left" valign="baseline">AFMT_MU_LAW</td>
<td class="left" >a logarithmic encoding (used by Sun <code>.au</code> files and
<span class="file">/dev/audio</span>)</td></tr>
<tr><td class="left" valign="baseline">AFMT_A_LAW</td>
<td class="left" >a logarithmic encoding</td></tr>
<tr><td class="left" valign="baseline">AFMT_IMA_ADPCM</td>
<td class="left" >a 4:1 compressed format defined by the Interactive Multimedia
Association</td></tr>
<tr><td class="left" valign="baseline">AFMT_U8</td>
<td class="left" >Unsigned, 8-bit audio</td></tr>
<tr><td class="left" valign="baseline">AFMT_S16_LE</td>
<td class="left" >Signed, 16-bit audio, little-endian byte order (as used by
Intel processors)</td></tr>
<tr><td class="left" valign="baseline">AFMT_S16_BE</td>
<td class="left" >Signed, 16-bit audio, big-endian byte order (as used by 68k,
PowerPC, Sparc)</td></tr>
<tr><td class="left" valign="baseline">AFMT_S8</td>
<td class="left" >Signed, 8 bit audio</td></tr>
<tr><td class="left" valign="baseline">AFMT_U16_LE</td>
<td class="left" >Unsigned, 16-bit little-endian audio</td></tr>
<tr><td class="left" valign="baseline">AFMT_U16_BE</td>
<td class="left" >Unsigned, 16-bit big-endian audio</td></tr></tbody>
</table></div>
Consult the OSS documentation for a full list of audio formats, and note
that most devices support only a subset of these formats. Some older
devices only support <tt class="constant">AFMT_U8</tt>; the most common format used
today is <tt class="constant">AFMT_S16_LE</tt>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4782' xml:id='l2h-4782' class="method">setfmt</tt></b>(</nobr></td>
<td><var>format</var>)</td></tr></table></dt>
<dd>
Try to set the current audio format to <var>format</var>--see
<tt class="method">getfmts()</tt> for a list. Returns the audio format that the device
was set to, which may not be the requested format. May also be used to
return the current audio format--do this by passing an ``audio format''
of
<tt class="constant">AFMT_QUERY</tt>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4783' xml:id='l2h-4783' class="method">channels</tt></b>(</nobr></td>
<td><var>nchannels</var>)</td></tr></table></dt>
<dd>
Set the number of output channels to <var>nchannels</var>. A value of 1
indicates monophonic sound, 2 stereophonic. Some devices may have more
than 2 channels, and some high-end devices may not support mono.
Returns the number of channels the device was set to.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4784' xml:id='l2h-4784' class="method">speed</tt></b>(</nobr></td>
<td><var>samplerate</var>)</td></tr></table></dt>
<dd>
Try to set the audio sampling rate to <var>samplerate</var> samples per
second. Returns the rate actually set. Most sound devices don't
support arbitrary sampling rates. Common rates are:
<div class="center"><table class="realtable">
<thead>
<tr>
<th class="left" >Rate</th>
<th class="left" >Description</th>
</tr>
</thead>
<tbody>
<tr><td class="left" valign="baseline">8000</td>
<td class="left" >default rate for <span class="file">/dev/audio</span></td></tr>
<tr><td class="left" valign="baseline">11025</td>
<td class="left" >speech recording</td></tr>
<tr><td class="left" valign="baseline">22050</td>
<td class="left" ></td></tr>
<tr><td class="left" valign="baseline">44100</td>
<td class="left" >CD quality audio (at 16 bits/sample and 2 channels)</td></tr>
<tr><td class="left" valign="baseline">96000</td>
<td class="left" >DVD quality audio (at 24 bits/sample)</td></tr></tbody>
</table></div>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4785' xml:id='l2h-4785' class="method">sync</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Wait until the sound device has played every byte in its buffer. (This
happens implicitly when the device is closed.) The OSS documentation
recommends closing and re-opening the device rather than using
<tt class="method">sync()</tt>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4786' xml:id='l2h-4786' class="method">reset</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Immediately stop playing or recording and return the device to a
state where it can accept commands. The OSS documentation recommends
closing and re-opening the device after calling <tt class="method">reset()</tt>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4787' xml:id='l2h-4787' class="method">post</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Tell the driver that there is likely to be a pause in the output, making
it possible for the device to handle the pause more intelligently. You
might use this after playing a spot sound effect, before waiting for
user input, or before doing disk I/O.
</dl>
<P>
The following convenience methods combine several ioctls, or one ioctl
and some simple calculations.
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4788' xml:id='l2h-4788' class="method">setparameters</tt></b>(</nobr></td>
<td><var>format, nchannels, samplerate </var><big>[</big><var>, strict=False</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
<P>
Set the key audio sampling parameters--sample format, number of
channels, and sampling rate--in one method call. <var>format</var>,
<var>nchannels</var>, and <var>samplerate</var> should be as specified in the
<tt class="method">setfmt()</tt>, <tt class="method">channels()</tt>, and <tt class="method">speed()</tt>
methods. If <var>strict</var> is true, <tt class="method">setparameters()</tt> checks to
see if each parameter was actually set to the requested value, and
raises <tt class="exception">OSSAudioError</tt> if not. Returns a tuple (<var>format</var>,
<var>nchannels</var>, <var>samplerate</var>) indicating the parameter values that
were actually set by the device driver (i.e., the same as the return
values of <tt class="method">setfmt()</tt>, <tt class="method">channels()</tt>, and <tt class="method">speed()</tt>).
<P>
For example,
<div class="verbatim"><pre>
(fmt, channels, rate) = dsp.setparameters(fmt, channels, rate)
</pre></div>
is equivalent to
<div class="verbatim"><pre>
fmt = dsp.setfmt(fmt)
channels = dsp.channels(channels)
rate = dsp.rate(channels)
</pre></div>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4789' xml:id='l2h-4789' class="method">bufsize</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Returns the size of the hardware buffer, in samples.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4790' xml:id='l2h-4790' class="method">obufcount</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Returns the number of samples that are in the hardware buffer yet to be
played.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4791' xml:id='l2h-4791' class="method">obuffree</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Returns the number of samples that could be queued into the hardware
buffer to be played without blocking.
</dl>
<P>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="14.11 ossaudiodev "
href="module-ossaudiodev.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="14.11 ossaudiodev "
href="module-ossaudiodev.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="14.11.2 Mixer Device Objects"
href="mixer-device-objects.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-ossaudiodev.html">14.11 ossaudiodev </A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-ossaudiodev.html">14.11 ossaudiodev </A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="mixer-device-objects.html">14.11.2 Mixer Device Objects</A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>