⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sound.c

📁 linux声卡编程 可以播放.wav格式的音乐。
💻 C
字号:
/*************************************************************************** *   Copyright (C) 2008 by root   * *   root@localhost.localdomain   * *                                                                         * *   This program is free software; you can redistribute it and/or modify  * *   it under the terms of the GNU General Public License as published by  * *   the Free Software Foundation; either version 2 of the License, or     * *   (at your option) any later version.                                   * *                                                                         * *   This program is distributed in the hope that it will be useful,       * *   but WITHOUT ANY WARRANTY; without even the implied warranty of        * *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         * *   GNU General Public License for more details.                          * *                                                                         * *   You should have received a copy of the GNU General Public License     * *   along with this program; if not, write to the                         * *   Free Software Foundation, Inc.,                                       * *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             * ***************************************************************************/#ifdef HAVE_CONFIG_H#include <config.h>#endif#include <unistd.h>#include <fcntl.h>#include <sys/types.h>#include <sys/ioctl.h>#include <sys/stat.h>#include <stdio.h>#include <stdlib.h>#include <linux/soundcard.h>#define LENGTH 3#define RATE 44100#define SIZE 16#define CHANNELS 2//unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8];int main(int argc, char *argv[]){ 	int fd1,fd2;	int arg;	int status;	unsigned char* buf=NULL;	struct stat stat_buf;	//打开声音设备	fd1=open("/dev/dsp",O_RDWR);		if(fd1<0){		perror("cannot open /dev/dsp device");		return 1;	}	/*==一下设置声卡参数 ==*/		//设置采样的量化位数	arg=SIZE;	status=ioctl(fd1,SOUND_PCM_WRITE_BITS,&arg);		if(status==-1){		perror("error");		return 1;	}		//设置采样声道数目	arg=CHANNELS;	status=ioctl(fd1,SOUND_PCM_WRITE_CHANNELS,&arg);	if(status==-1){		perror("error");		return 1;	}	//设置采样频率	arg=RATE;	status=ioctl(fd1,SOUND_PCM_WRITE_RATE,&arg);	if(status==-1){		perror("error");		return 1;	}	//打开声音文件	fd2=open("/root/bgm_013.wav",O_RDONLY);	if(fd2<0)		printf("error fd2");	//把声音文件的信息保存到stat结够体中	if(fstat(fd2,&stat_buf)){		close(fd2);		printf("error");		return 1;	}		buf=malloc(stat_buf.st_size);	if(!buf){		close(fd2);		printf("error");		return -1;	}				if(read(fd2,buf,stat_buf.st_size)<0){		free(buf);		printf("error");		close(fd2);  		return -1;	}			status =write(fd1,buf,stat_buf.st_size);		if(status !=sizeof(buf)){			perror("wrote wrong number of bytes");		}			free(buf);	close(fd1);	close(fd2);	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -