Linux下的OSS音訊介面程式設計(轉)
Linux下的OSS音訊介面程式設計(轉)[@more@]
CODE:
/*
Name: SndKit.c Copyright: GPLv2 Author: rockins(ybc2084@163.com) Date: 15-10-06 18:22 Description: implent raw sound record/play run: ./SndKit [-h] [-d device] [-c channel] [-b bits] [-f hz] [-l len] e.g.: ./SndKit -h show help information ./SndKit -r record.wav record audio from microphone(/dev/dsp) ./SndKit -d /dev/dsp -p record.wav playback record.wav via /dev/dsp ./SndKit -b 8 -f 22 -r reacord.wav record audio in 8 bit & 22k hz ./SndKit -d /dev/dsp -c 2 -r record.wav record audio in /dev/dsp and stereo ./SndKit -r -l 40 record.wav record 40k audio data */ #include #include #include #include #include #include #include #define TRUE 1 #define FALSE 0 #define FMT8BITS AFMT_U8 /*unsigned 8 bits(for almost PC) */ #define FMT16BITS AFMT_S16_LE /*signed 16 bits,little endian */ #define FMT8K 8000 /*default sampling rate */ #define FMT11K 11025 /*11,22,44,48 are three pop rate */ #define FMT22K 22050 #define FMT44K 44100 #define FMT48K 48000 #define MONO 1 #define STEREO 2 #define ACT_RECORD 0 #define ACT_PLAY 1 #define DFT_SND_DEV "/dev/dsp" #define DFT_SND_FMT FMT8BITS #define DFT_SND_SPD FMT8K #define DFT_SND_CHN MONO #define DFT_LEN 1024 /*default record length:40k */ #define BUFF_SIZE 512 /*buffer size:512 Bytes */ /************** function prototype ********************/ void Usage(void); int OpenDevice(const char *, unsigned int); int OpenFile(const char *, unsigned int); int CloseDevice(unsigned int); int CloseFile(unsigned int); /************** function implementation **************/ int main(int argc, char *argv[]) { unsigned int snd_fmt = DFT_SND_FMT; /*sound format,bits */ unsigned int snd_chn = DFT_SND_CHN; /*sound channel number */ unsigned int snd_spd = DFT_SND_SPD; /*sound speed,frequency */ unsigned char *s_file = NULL; /*file hold sound data */ unsigned char *snd_device = DFT_SND_DEV; /*sound device */ unsigned int recd_or_play = ACT_PLAY; /*flag(default play) */ unsigned char buff[BUFF_SIZE]; /*sound buffer */ unsigned long len = DFT_LEN * 1024; /*record or play length(k) */ int snd_fd; /*sound device descriptor */ int s_fd; /*sound data file descrit */ ssize_t n; /*bytes already in or out */ ssize_t nRD; /*bytes readin */ ssize_t nWR; /*bytes write out */ unsigned int opt_chr; /*hold cli option */ unsigned int opt_err = FALSE; /*indicate whether parse option error */ /*parse cli option via getopt routine */ optind = 0; /*set optindex to 0 */ while ((opt_chr = getopt(argc, argv, "hd:c:b:f:l:r:p:")) != -1) { switch (opt_chr) { case 'h': Usage(); return (0); /*return, 0 denote success */ break; case 'd': snd_device = optarg; /*sound device name */ break; case 'c': if (atoi(optarg) == 1) /*select channel number */ snd_chn = MONO; else if (atoi(optarg) == 2) snd_chn = STEREO; else { opt_err = 1; /*error occur */ fprintf(stderr, "wrong channel setting," "should be 1 or 2(default 1,MONO) "); } break; case 'b': if (atoi(optarg) == 8) /*select data bit */ snd_fmt = FMT8BITS; else if (atoi(optarg) == 16) snd_fmt = FMT16BITS; else { opt_err = 1; /*error occur */ fprintf(stderr, "wrong bits setting," "should be 8 or 16(default 8) "); } break; case 'f': if (atoi(optarg) == 8) snd_spd = FMT8K; /*sampling rate:8kbps */ else if (atoi(optarg) == 11) snd_spd = FMT11K; /*smapling rate:11.025k */ else if (atoi(optarg) == 22) snd_spd = FMT22K; /*sampling rate:22.050k */ else if (atoi(optarg) == 44) snd_spd = FMT44K; /*sampling rate:44.100k */ else if (atoi(optarg) == 48) snd_spd = FMT48K; /*sampling rate:48k */ else { opt_err = 1; /*error occur */ fprintf(stderr, "wrong sampling rate," "should be 8,11,22,or 44(default 8kbps) "); } break; case 'l': len = atoi(optarg) * 1024; /*record length(k) */ break; case 'r': recd_or_play = ACT_RECORD; s_file = optarg; /*file to record sound */ break; case 'p': recd_or_play = ACT_PLAY; s_file = optarg; /*file to play sound */ break; case '?': opt_err = 1; fprintf(stderr, "unknown option:%c ", optopt); break; default: opt_err = 1; fprintf(stderr, "getopt error:%d ", opterr); break; } } /*check if cli option parsed right*/ if (opt_err || argc < 2) { fprintf(stderr, "parse command option failed!!! " "run ./SndKit -h for help "); return (-1); } /*open device */ if ((snd_fd = OpenDevice(snd_device, recd_or_play)) < 0) { fprintf(stderr, "cannot open device %s:%s ", snd_device, strerror(errno)); return (-1); } /*open sound data file */ if ((s_fd = OpenFile(s_file, recd_or_play)) < 0) { fprintf(stderr, "cannot open sound file %s:%s ", s_file, strerror(errno)); return (-1); } /*set sound format */ if (SetFormat(snd_fd, snd_fmt, snd_chn, snd_spd) < 0) { fprintf(stderr, "cannot set %s in bit %d, channel %d, speed %d ", snd_device, snd_fmt, snd_chn, snd_spd); return (-1); } /*do real action:record or playback */ if (recd_or_play == ACT_RECORD) { /*record sound into data file*/ n = 0; while (n < len) { nRD = 0; /*amount read from sound device */ if ((nRD = read(snd_fd, buff, BUFF_SIZE)) < 0) { perror("read sound device failed"); return (-1); } if (n + nRD <= len) /*the len is not full */ nWR = nRD; /*write amount to sound data file */ else nWR = len - n; /*len will be overflow */ unsigned long old_nWR = nWR; /*s hold nWR's old value */ unsigned long t = 0L; /*temp counter */ while (nWR > 0) { if ((t = write(s_fd, buff, nWR)) < 0) { perror("write sound data file failed"); return (-1); } nWR -= t; } n += old_nWR; } } else if (recd_or_play == ACT_PLAY) { /*write sound data to device*/ while (TRUE) { nRD = 0L; /*read amount from sound device */ if ((nRD = read(s_fd, buff, BUFF_SIZE)) < 0) { perror("read sound data file failed"); return (-1); } else if (nRD == 0) { /*nRD == 0 means all sound stream output */ printf("sound data play complete! "); exit(0); } nWR = nRD; while (nWR > 0) { /*write into device*/ if ((n = write(snd_fd, buff, nWR)) < 0) { perror("write sound device file failed"); return (-1); } nWR -= n; } } } /*close sound device and sound data file*/ CloseDevice(snd_fd); CloseFile(s_fd); return (0); } /* * OpenDevice():open sound device * params: * dev_name -- device name,such as /dev/dsp * flag -- flag(ACT_RECORD or ACT_PLAY) * returns: * file descriptor of sound device if sucess * -1 if failed */ int OpenDevice(const char *dev_name, unsigned int flag) { int dev_fd; /*open sound device */ if (flag == ACT_RECORD) { if ((dev_fd = open(dev_name, O_RDONLY)) < 0) { return (-1); } } else if (flag == ACT_PLAY) { if ((dev_fd = open(dev_name, O_WRONLY)) < 0) { return (-1); } } return (dev_fd); } /* * CloseDevice():close the sound device * params: * dev_fd -- the sound device's file descriptor * returns: * 0 if success * -1 if error occured */ int CloseDevice(unsigned int dev_fd) { return (close(dev_fd)); } /* * OpenFile():open sound data file * params: * file_name -- file name,e.g,record.wav * flag -- flag(ACT_RECORD or ACT_PLAY) * returns: * file descriptor of sound data file if sucess * -1 if failed */ int OpenFile(const char *file_name, unsigned int flag) { int file_fd; /*open sound data file */ if (flag == ACT_RECORD) { if ((file_fd = open(file_name, O_WRONLY)) < 0) { return (-1); } } else if (flag == ACT_PLAY) { if ((file_fd = open(file_name, O_RDONLY)) < 0) { return (-1); } } return (file_fd); } /* * CloseFile():close the sound data file * params: * file_fd -- the sound data file's descriptor * returns: * 0 if success * -1 if error occured */ int CloseFile(unsigned int file_fd) { return (close(file_fd)); } /* * SetFormat():Set Record and Playback format * params; * fd -- device file descriptor * chn -- channel(MONO or STEREO) * bits -- FMT8BITS(8bits), FMT16BITS(16bits) * hz -- FMT8K(8000HZ), FMT16K(16000HZ), FMT22K(22000HZ), * FMT44K(44000HZ),FMT48K(48000HZ) * returns: * return 0 if success, else return -1 * notes: * parameter setting order should be like as: * 1.sample format(number of bits) * 2.number of channels(mono or stereo) * 3.sampling rate(speed) */ int SetFormat(unsigned int fd, unsigned int bits, unsigned int chn, unsigned int hz) { int ioctl_val; /* set bit format */ ioctl_val = bits; if (ioctl(fd, SNDCTL_DSP_SETFMT, &ioctl_val) == -1) { fprintf(stderr, "Set fmt to bit %d failed:%s ", bits, strerror(errno)); return (-1); } if (ioctl_val != bits) { fprintf(stderr, "do not support bit %d, supported %d ", bits, ioctl_val); return (-1); } /*set channel */ ioctl_val = chn; if ((ioctl(fd, SNDCTL_DSP_CHANNELS, &ioctl_val)) == -1) { fprintf(stderr, "Set Audio Channels %d failed:%s ", chn, strerror(errno)); return (-1); } if (ioctl_val != chn) { fprintf(stderr, "do not support channel %d,supported %d ", chn, ioctl_val); return (-1); } /*set speed */ ioctl_val = hz; if (ioctl(fd, SNDCTL_DSP_SPEED, &ioctl_val) == -1) { fprintf(stderr, "Set speed to %d failed:%s ", hz, strerror(errno)); return (-1); } if (ioctl_val != hz) { fprintf(stderr, "do not support speed %d,supported is %d ", hz, ioctl_val); return (-1); } return (0); } /* * Usage():print help information * params:(none) * returns:(none) */ void Usage(void) { printf ("run: ./SndKit [-h] [-d device] [-c channel] [-b bits] [-f hz] [-l len] " "Description: implent raw sound record/play " "option: " " -h: print help informations " " -d device: assign sound device to record or playback(default /dev/dsp) " " -c channel: indicate in MONO or STEREO channel(default MONO) " " -b bits: assign sampling bits depth(default 8bits unsigned) " " -f hz: indicate sampling rate,i.e,frequence(default 8kbps) " " -l len: indicate recording sound's length(default 1024k) " " -r|-p file: indicate record in or playback(no default,must give out explicitly) " "e.g.: " " ./SndKit -h show help information " " ./SndKit -r record.wav record audio from microphone(/dev/dsp) " " ./SndKit -d /dev/dsp -p record.wav playback record.wav via /dev/dsp " " ./SndKit -b 8 -f 22 -r reacord.wav record audio in 8 bit & 22k hz " " ./SndKit -d /dev/dsp -c 2 -r record.wav record audio in /dev/dsp and stereo " " ./SndKit -r -l 40 record.wav record 40k audio data in record.wav "); } 來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10617542/viewspace-962222/,如需轉載,請註明出處,否則將追究法律責任。
上一篇:
Linux下的後門和日誌工具(轉)
下一篇:
DOS檔案轉換成UNIX檔案格式(轉)
請登入後發表評論
登入
全部評論
|
相關文章
- Linux下的GTK圖形介面程式設計(轉)Linux程式設計
- linux音訊程式設計指南Linux音訊程式設計
- Linux下的OpenGL程式設計(轉)Linux程式設計
- Linux下的音訊採集與回放(轉)Linux音訊
- LinuxCOSS音訊程式設計Linux音訊程式設計
- Linux下的shell程式設計入門(轉)Linux程式設計
- WPF下的視訊錄製介面設計
- Linux下C語言程式設計(轉)LinuxC語言程式設計
- Linux下的多執行緒程式設計(轉)Linux執行緒程式設計
- linux下c程式設計中的DOS攻擊程式(轉)LinuxC程式程式設計
- 程式設計之路-介面美化 (轉)程式設計
- (轉)Linux下 C++呼叫C 實現socket網路通訊程式設計LinuxC++程式設計
- linux下的SHELL程式設計Linux程式設計
- 使用Microsoft Agent的COM介面程式設計(轉)ROS程式設計
- (整合)Linux下的多程式程式設計Linux程式設計
- Linux 程式設計之Shell程式設計(轉)Linux程式設計
- iOS音訊程式設計之實時語音通訊(對講機功能)iOS音訊程式設計
- linux下音效卡程式設計試探(轉)Linux程式設計
- Linux下C語言程式設計簡介(轉)LinuxC語言程式設計
- 如何播放通過微信下載介面下載的音訊檔案音訊
- 【秒懂音視訊開發】09_音訊錄製02_程式設計音訊程式設計
- Linux的shell程式設計(一)(轉)Linux程式設計
- Linux的shell程式設計(二)(轉)Linux程式設計
- Linux的shell程式設計(三)(轉)Linux程式設計
- Linux的shell程式設計(四)(轉)Linux程式設計
- Linux下多執行緒程式設計與訊號處理易疏忽的一個例子(轉)Linux執行緒程式設計
- 高效的音訊製作與槍和車的音訊設計方案音訊
- linux下bluetooth程式設計(三)HCI層程式設計Linux程式設計
- linux下bluetooth程式設計(八)SDP層程式設計Linux程式設計
- 訊息抽象層設計和實現-OSS.DataFlow抽象
- 音訊設計經驗分享:聲音功能的設計與創意表現音訊
- Linux系統程式設計之訊號中斷處理(下)Linux程式設計
- Linux核心程式設計(阻塞程式)(轉)Linux程式設計
- Linux環境下的Socket程式設計Linux程式設計
- “Linux程式設計”小結(程式間通訊)Linux程式設計
- Linux系統下的多執行緒程式設計入門(轉)Linux執行緒程式設計
- 轉賬介面設計
- Linux核心模組的程式設計方法(轉)Linux程式設計