Using ioctl

K

klappnase

Hello,

I am writing an app that records from the soundcard using ossaudiodev.
In the OSS programmer's guide they recommend when reading data
fragments from the soundcard
to use the fragment size as it is requested by the driver. According to
the programmer's guide
the ioctl call to determine the requested fragment size is:

int frag_size;
if ioctl(audio_fd, SNDCTL_DSP_GETBLKSIZE, &frag_size) == -1)
error();

Unfortunately this procedure is not implemented in the ossaudiodev
module, so I
tried to write it myself.
From reading the fcntl module's docs, I came to the following solution:

try:
f = array.array('h', [0])
fcntl.ioctl(audio_fd, ossaudiodev.SNDCTL_DSP_GETBLKSIZE, f, 1)
frag_size = f.tolist()[0]
except:
frag_size = -1
if frag_size <= 0:
frag_size = 4096


This *seems* to work, I tried several soundcards and got frag_size
values like 4096, 8192 or 16384 .
However I am not really sure about what I am doing there, so I would
feel more confident
if anyone could explain how ioctl is supposed to be used ( I also felt
that I should use
the try...except block for the call in case it fails, but I don't have
an idea "except *what*").

Any hints are much appreciated.

thanks in advance

Michael
 
S

Scott David Daniels

From reading the fcntl module's docs, I came to the following solution:

try:
f = array.array('h', [0])
fcntl.ioctl(audio_fd, ossaudiodev.SNDCTL_DSP_GETBLKSIZE, f, 1)
frag_size = f.tolist()[0]
except:
frag_size = -1
if frag_size <= 0:
frag_size = 4096

I would feel more confident if anyone could explain how ioctl is supposed
> to be used ( I also felt that I should use the try...except block for the
> call in case it fails, but I don't have an idea "except *what*").
Fairly normal practice is to wait for a failure (or try to instigate
one) _without_ the try: ... except: ..., and then use the one you get.
Or, you could go for IOError, which sounds right to me. remember your
current "except:" is catching any attempt at KeyBoardEscape (Control-C,
Control-Break, and such like).

--Scott David Daniels
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top