这是什么原因造成的呢?

软件和网站开发以及相关技术探讨
回复
vitamin
帖子: 247
注册时间: 2008-09-23 21:13

这是什么原因造成的呢?

#1

帖子 vitamin » 2008-12-14 15:08

music.1.cxx: In function ‘int play_sound(char*)’:
music.1.cxx:37: 错误: 从类型‘void*’到类型‘unsigned char*’的转换无效
music.1.cxx:57: 错误: ‘ioctl’在此作用域中尚未声明
music.1.cxx: At global scope:
music.1.cxx:82: 错误: ‘::main’必须返回‘int’
music.1.cxx: In function ‘int main()’:
music.1.cxx:84: 警告: 不建议使用从字符串常量到‘char*’的转换



//扬声器发声程序

# include <unistd.h>
# include <fcntl.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <unistd.h>
# include <stdlib.h>
# include <stdio.h>
# include <linux/soundcard.h>

# define Audio_Device "/dev/dsp"
//不同的声音有着不同播放参数,这些参数可以使用file命令获得
# define Sample_Size 8 // ther're two kinds of bits,8 bits and 16 bits
# define Sample_Rate 8000 /* sampling rate */

int play_sound(char *filename){
struct stat stat_buf;
unsigned char *buf = NULL;
int handler,fd;
int result;
int arg,status;
//打开声音文件,将文件读入内存
fd = open(filename,O_RDONLY);
if (fd<0)
return -1;

if (fstat(fd, &stat_buf)){
close(fd);
return -1;
}
if (!stat_buf.st_size){
close(fd);
return -1;
}

buf = malloc(stat_buf.st_size);
if (!buf){
close(fd);
return -1;
}

if (read(fd, buf, stat_buf.st_size) < 0){
free(buf);
close(fd);
return -1;
}

// 打开声卡设备,并设置声卡播放参数,这些参数必须与声音文件参数一致
handler = open(Audio_Device, O_WRONLY);
if (handler == -1){
perror("open Audio_Device fail");
return -1;
}

arg=Sample_Rate;
status=ioctl(handler,SOUND_PCM_WRITE_RATE, &arg);
if(status==-1){
perror("error from SOUND_PCM_WRITE_RATE ioctl");
return -1;
}

arg=Sample_Size;
status=ioctl(handler,SOUND_PCM_WRITE_BITS, &arg);
if(status==-1){
perror("error from SOUND_PCM_WRITE_BITS ioctl");
return -1;
}

result=write(handler,buf,stat_buf.st_size);
if(result==-1){
perror("Fail to play the sound!");
return -1;
}

free(buf);
close(fd);
close(handler);
return result;
}

void main(void)
{
play_sound("/c++/aurora2.wav");
}
念几句佛经就以为自己在悟道,引用几句圣经就以为自己是上帝的使者。
躲在阴暗的角落就以为看不见你那肮脏的灵魂?
游走在灰色领域就以为安然无恙?
举头三尺有神明,不是不报是时候未到。
回复