LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-27-2003, 01:43 PM   #1
vasudevadas
Member
 
Registered: Jul 2003
Location: Bedford, UK
Distribution: Slackware 11.0, LFS 6.1
Posts: 519

Rep: Reputation: 30
OSS API - opening /dev/dsp when device busy


Hi all,

I am writing an audio application, using the OSS API (I will make it work with ALSA too later). My code does the following:

#include <sys/soundcard.h>
#include <sys/ioctl.h>
#include <sys/unistd.h>
#include <sys/fcntl.h>

....

int *p_audio_fd;

if ((*p_audio_fd = open(DEVICE_NAME, O_WRONLY, 0)) == -1)
{
perror(DEVICE_NAME);
retcode = cannot_open_audio_device;
}

However, what actually happens, if the audio device is busy, is that my code simply waits patiently until the audio device becomes available. It then gets on with its processing. This is very nice, but it's not what I want it to do. I want it instead to raise an error like "/dev/dsp cannot be opened - device busy" like I have seen other audio applications do.

Since open() is clearly not returning -1 when the device is busy, as I expected, how do I code this behaviour?

Thanks for your time!
 
Old 11-29-2003, 08:30 AM   #2
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
I think I might be able to help.
I am familiar with the OSS API and that code should act as you want it to.
Or at least on my machine it does. I have a couple questions though, what device are
you trying to open? Is it /dev/dsp or something else? Also, could you post some of your actual code? I write an app called SOAP, a video game song player written in OSS. You can download it at http://www.sourceforge.net/projects/soap if you want to take a look at my code, its pretty straightforward.

Last edited by jinksys; 11-29-2003 at 08:32 AM.
 
Old 11-30-2003, 08:40 AM   #3
mhearn
LQ Guru
 
Registered: Nov 2002
Location: Durham, England
Distribution: Fedora Core 4
Posts: 1,565

Rep: Reputation: 57
Are you sure you're not actually using ALSA with OSS emulation?

Native OSS will return an error if the device is busy. ALSA will block. There is no way around this behaviour that I know of.
 
Old 12-01-2003, 03:36 AM   #4
vasudevadas
Member
 
Registered: Jul 2003
Location: Bedford, UK
Distribution: Slackware 11.0, LFS 6.1
Posts: 519

Original Poster
Rep: Reputation: 30
You're right, I am using the OSS emulation of ALSA. I will give it native ALSA support at a later time, but I also want to maintain OSS-compatibility, as not everyone manages to get ALSA to work.

I will post some of my actual code when I get home. Thanks for your time!

Last edited by vasudevadas; 12-01-2003 at 03:39 AM.
 
Old 12-01-2003, 09:48 AM   #5
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
My machine users alsa OSS emulation and the code works as expected on it.
 
Old 12-01-2003, 12:40 PM   #6
vasudevadas
Member
 
Registered: Jul 2003
Location: Bedford, UK
Distribution: Slackware 11.0, LFS 6.1
Posts: 519

Original Poster
Rep: Reputation: 30
Very interesting. Now, when I come to try it, if I run my program first and then press play in Xmms, Xmms waits until I kill my application before it can play music. It does not raise an error. It definitely used to, but I believe I would have been using OSS/Free then. Since I was asked, my code to initialise the audio device is:

#include <stdio.h>
#include <sys/soundcard.h>
#include <sys/ioctl.h>
#include <sys/unistd.h>
#include <sys/fcntl.h>
#include "arctracker.h"

#define DEVICE_NAME "/dev/dsp"

return_status initialise_sound_device(int *p_audio_fd,
long *p_sample_rate,
mono_stereo *p_stereo_mode,
format *p_sample_format)
{
return_status retcode = success;
int sample_format = AFMT_S16_LE; /* signed 16-bit little-endian */
int channels = 2;

if ((*p_audio_fd = open(DEVICE_NAME, O_WRONLY, 0)) == -1)
{
perror(DEVICE_NAME);
retcode = cannot_open_audio_device;
}

if (retcode == success)
{
if (ioctl(*p_audio_fd, SNDCTL_DSP_SETFMT, &sample_format) == -1)
{
perror("SNDCTL_DSP_SETFMT");
retcode = cannot_set_sample_format;
}

switch(sample_format)
{
case AFMT_S16_LE:
*p_sample_format = bits_16_signed_little_endian;
break;
case AFMT_S16_BE:
*p_sample_format = bits_16_signed_big_endian;
break;
case AFMT_U8:
*p_sample_format = bits_8_unsigned;
break;
case AFMT_S8:
*p_sample_format = bits_8_signed;
break;
case AFMT_U16_LE:
*p_sample_format = bits_16_unsigned_little_endian;
break;
case AFMT_U16_BE:
*p_sample_format = bits_16_unsigned_big_endian;
break;
default:
fprintf(stderr,"Cannot set audio device to suitable sample format\n");
retcode = bad_sample_format;
}
}

if (retcode == success)
{
if (ioctl(*p_audio_fd, SNDCTL_DSP_CHANNELS, &channels) == -1)
{
perror("SNDCTL_DSP_CHANNELS");
retcode = cannot_set_channels;
}

switch (channels)
{
case 1:
*p_stereo_mode = mono;
break;
case 2:
*p_stereo_mode = stereo;
break;
default:
fprintf(stderr,"Audio output must be in either one or two channels, actual=%d\n", channels);
retcode = bad_channels;
}
}

if (retcode == success)
{
if (ioctl(*p_audio_fd, SNDCTL_DSP_SPEED, p_sample_rate) == -1)
{
perror("SNDCTL_DSP_SPEED");
retcode = cannot_set_sample_rate;
}
}

printf("Opened audio device %s for output, device parameters are:\n", DEVICE_NAME);
printf("Sample format=%d, number of channels=%d, sample rate=%dKHz\n",
sample_format, channels, *p_sample_rate);

return (retcode);
}

Last edited by vasudevadas; 12-01-2003 at 12:41 PM.
 
Old 12-01-2003, 12:49 PM   #7
vasudevadas
Member
 
Registered: Jul 2003
Location: Bedford, UK
Distribution: Slackware 11.0, LFS 6.1
Posts: 519

Original Poster
Rep: Reputation: 30
jinksys, does that mean that you get an error if a program attempts to open the audio device whilst it is already open? Have you any idea how to implement this behaviour? I think I am using ALSA 0.9.0, it is the version shipped as standard with Mandrake 9.1 at any rate.

(Now I see Xmms behaving in the same way as my own code, I am less inclined to view it as aberrant).
 
Old 12-01-2003, 01:36 PM   #8
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
When I run my program and push play on XMMS it gives me the usual error.
When I run my program after I open XMMS and play a song, it gives me an
error saying that I cannot open the device, its in use.
What output driver does your XMMS use?
 
Old 12-01-2003, 01:39 PM   #9
vasudevadas
Member
 
Registered: Jul 2003
Location: Bedford, UK
Distribution: Slackware 11.0, LFS 6.1
Posts: 519

Original Poster
Rep: Reputation: 30
It's using the ALSA output plugin libALSA.so.
 
Old 12-01-2003, 02:01 PM   #10
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
Oh, hmm, I use the OSS output driver. See if you have an OSS output driver available, and see if your app behaves any differently.
 
Old 12-02-2003, 12:11 PM   #11
vasudevadas
Member
 
Registered: Jul 2003
Location: Bedford, UK
Distribution: Slackware 11.0, LFS 6.1
Posts: 519

Original Poster
Rep: Reputation: 30
If I set Xmms to use the OSS output plugin, then:

(1) If I run my application first and while it is playing press "play" in Xmms, it will raise an error "Couldn't open audio."

(2) If I start playing a tune in Xmms first and while it is playing try to run my application, the call to open() will block until Xmms releases the audio device.

This is most perplexing!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
/dev/dsp device or resource busy Daem0hn Linux - General 4 12-11-2005 12:22 PM
Why is my /dev/dsp always busy ? mnguyen Linux - Software 1 03-22-2004 02:13 AM
"/dev/dsp: Device or resource busy" Cdzin Linux - Hardware 4 02-25-2004 06:39 AM
/dev/dsp + device busy/no audio + esd mghere Linux - Newbie 7 12-11-2003 11:51 PM
open /dev/[sound/]dsp: Device or resource busy tearinox Linux - Newbie 1 10-24-2003 06:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration