|
前两天用OSS音频接口写的一个录音和播放小程序,播放的时候,声音比较小,但是如果大声一点录,放出来还能稍微大点,但还是比较小,什么原因?贴上代码:录音的:(按下板子上的key1开始录音,松开按键录音结束,录音存放在record.wav里面)
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <linux/soundcard.h>
- #define AUDIO_DEVICE "/dev/dsp"
- #define RATE 48000
- #define CHANNELS 2
- #define SIZE 16
- #define PAGE_SIZE 4096
- #define RECORD_DIR "/tmp/"
- #define KEY_DEVICE "/dev/IRQ-Test"
- #define KEY_N 4
- #define KEY1 0
- int main (int argc, char *argv[])
- {
- int keyfd, dspfd;
- char key_buf[KEY_N], record_buf[PAGE_SIZE];
- int arg;
- /*open dsp read only and init*/
- dspfd = open (AUDIO_DEVICE, O_RDONLY);
- if (-1 == dspfd) {
- perror ("##open dsp erro");
- exit (1);
- }
- /*set format*/
- arg = SIZE;
- if (-1 == ioctl (dspfd, SOUND_PCM_WRITE_BITS, &arg)) {
- perror ("##set simple format erro");
- goto out3;
- }
- /*set channels*/
- arg = CHANNELS;
- if (-1 == ioctl (dspfd, SOUND_PCM_WRITE_CHANNELS, &arg)) {
- perror ("##set channels erro");
- goto out3;
- }
- /*set speed*/
- arg = RATE;
- if (-1 == ioctl (dspfd, SOUND_PCM_WRITE_RATE, &arg)) {
- perror ("##set speed erro");
- goto out3;
- }
- /*create a file to store the record file*/
- FILE *filep = fopen (RECORD_DIR"record.wav", "w");
- if (NULL == filep) {
- printf ("##open file to record erro\n");
- goto out3;
- }
- /*open the key ,read only and nonblock*/
- keyfd = open (KEY_DEVICE, O_RDONLY | O_NONBLOCK);
- if (-1 == keyfd) {
- perror ("##open key erro");
- goto out2;
- }
- printf ("<press down the KEY1 to record> \n");
- int record_end_flag = 1;
- while (record_end_flag) {
- if ((-1 == read (keyfd, key_buf, KEY_N)) && (EAGAIN != errno)) {
- perror ("##read key vaule erro");
- goto out1;
- }
- /*KEY1 down*/
- while (key_buf[KEY1] != '0') {
- if ((-1 == read (keyfd, key_buf, KEY_N)) && (EAGAIN != errno)) {
- perror ("##read key vaule erro");
- goto out1;
- }
- /*key down*/
- int num = read (dspfd, record_buf, PAGE_SIZE);
- int write_n = fwrite (record_buf, sizeof (char), num, filep);
- if (write_n < num) {
- printf ("##write to the record file erro\n");
- goto out1;
- }
- /*key up, record end*/
- if (key_buf[KEY1] == '0') {
- record_end_flag = 0;
- printf ("<record end>\n");
- }
- }
- }
- out1:
- close (keyfd);
- out2:
- fclose (filep);
- out3:
- close (dspfd);
- return 0;
- }
复制代码
播放的:(播放录音record.wav)
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <linux/soundcard.h>
- #define AUDIO_DEVICE "/dev/dsp"
- #define RATE 48000
- #define CHANNELS 2
- #define SIZE 16
- #define PAGE_SIZE 4096
- #define VOLUME 100 //max
- int main (int argc, char *argv[])
- {
- int dspfd;
- char record_buf[PAGE_SIZE];
- int arg;
- if (argc < 2){
- printf ("Usage: ./playrec filename\n");
- exit (1);
- }
- /*open dsp write only and init*/
- dspfd = open (AUDIO_DEVICE, O_WRONLY);
- if (-1 == dspfd) {
- perror ("##open dsp erro");
- exit (1);
- }
- /*set format*/
- arg = SIZE;
- if (-1 == ioctl (dspfd, SOUND_PCM_WRITE_BITS, &arg)) {
- perror ("##set simple format erro");
- goto out1;
- }
- /*set channels*/
- arg = CHANNELS;
- if (-1 == ioctl (dspfd, SOUND_PCM_WRITE_CHANNELS, &arg)) {
- perror ("##set channels erro");
- goto out1;
- }
- /*set speed*/
- arg = RATE;
- if (-1 == ioctl (dspfd, SOUND_PCM_WRITE_RATE, &arg)) {
- perror ("##set speed erro");
- goto out1;
- }
- /*set the volume*/
- arg = (VOLUME << 8) | VOLUME;
- if (-1 == ioctl (dspfd, SOUND_MIXER_WRITE_VOLUME, &arg)) {
- perror ("##set volume erro");
- goto out1;
- }
- /*open the record file*/
- FILE *filep = fopen (argv[1], "r");
- if (NULL == filep) {
- printf ("##open the record file erro\n");
- goto out1;
- }
- /*play the record*/
- int num;
- while ((num = fread (record_buf, sizeof (char), PAGE_SIZE, filep)) > 0) {
- if (-1 == write (dspfd, record_buf, num)) {
- perror ("##write dsp erro");
- goto out2;
- }
-
- }
- out2:
- fclose (filep);
- out1:
- close (dspfd);
- return 0;
- }
复制代码
|
|