红联Linux门户
Linux帮助

linux音频编程打开dev/dsp忙问题,请帮忙。

发布时间:2011-08-31 11:30:30来源:红联作者:plf1943
运行提示错误为 open of dev/dsp failed:Device or resource busy
源程序代码如下 请各位大侠帮忙 万分感谢

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define LENGTH 3
#define RATE 8000
#define SIZE 8
#define CHANNELS 1

unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8];

void *read_sound(void *arg1)
{
int fd;
int arg;
int status;
int count;
FILE * outfile;
outfile = fopen("tt.wav","wb");

fd = open("/dev/dsp", O_RDONLY);
if(fd < 0)
{
perror("open of /dev/dsp failed");
exit(1);

}




arg = SIZE;
status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);
if(status == -1)
perror("SOUND_PCM_WRITE_BITS ioctl failed");
if(arg != SIZE)
perror("unable to set sample size");

arg = CHANNELS;
status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg);
if(status == -1)
perror("SOUND_PCM_WRITE_CHANNELSI ioctl failed");
if(arg != CHANNELS)
perror("unable to set number of channels");


arg = RATE;
status = ioctl(fd,SOUND_PCM_WRITE_RATE, &arg );
if (status == -1)
perror("SOUND_PCM_WRITE_WRITE ioctl failed");

while(count < 20)
{
status = read(fd, buf, sizeof(buf));

if(status != sizeof(buf))
perror("read wrong number of bytes");
printf("ok\n");
fwrite( buf, sizeof(unsigned char),status,outfile);
count++;

}

close(fd);
return NULL;


}




void *write_sound(void *arg2)
{
int fd;
int arg;
int status;
int count = 0;



fd = open("/dev/dsp", O_WRONLY);
if(fd < 0)
{
perror("open of /dev/dsp failed");
exit(1);

}




arg = SIZE;
status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);
if(status == -1)
perror("SOUND_PCM_WRITE_BITS ioctl failed");
if(arg != SIZE)
perror("unable to set sample size");

arg = CHANNELS;
status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg);
if(status == -1)
perror("SOUND_PCM_WRITE_CHANNELSI ioctl failed");
if(arg != CHANNELS)
perror("unable to set number of channels");


arg = RATE;
status = ioctl(fd,SOUND_PCM_WRITE_RATE, &arg );
if (status == -1)
perror("SOUND_PCM_WRITE_WRITE ioctl failed");

while(count < 25)
{
status = write(fd, buf, sizeof(buf));

if(status != sizeof(buf))
perror("wrote wrong number of bytes");

count++;

}

close(fd);
return NULL;


}


int main()
{
pthread_t tidp1, tidp2;
int error;
error = pthread_create(&tidp1,NULL,read_sound,NULL);
if(error != 0)
{
printf("pthread_create is not created...");
return -1;
}

error = pthread_create(&tidp2,NULL,read_sound,NULL);
if(error != 0)
{
printf("pthread_create is not created...");
return -1;
}

pthread_join(tidp1,NULL);
pthread_join(tidp1,NULL);


return 0;




}
文章评论

共有 1 条评论

  1. 于 2012-12-17 19:15:29发表:

    你这个问题解决了吗?