|
本帖最后由 haa14618 于 2012-2-15 00:04 编辑
- //錄音
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/ioctl.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <linux/soundcard.h>
- #include <termios.h>
- #define LENGTH 30
- #define RATE 44100
- #define SIZE 16
- #define CHANNELS 2
- #define RSIZE 100
- #define AUDIO_DEV_FILE "/dev/dsp"
- unsigned char buf[RSIZE];
- struct fhead{
- unsigned char a[4];
- long int b;
- unsigned char c[4];
- unsigned char d[4];
- long int e;
- short int f;
- short int g;
- long int h;
- long int i;
- short int j;
- short int k;
- unsigned char p[4];
- long int q;
- } wavehead;
- int set_wav_arg(void)
- {
- wavehead.a[0] = 'R';
- wavehead.a[1] = 'I';
- wavehead.a[2] = 'F';
- wavehead.a[3] = 'F';
- // wavehead.b = LENGTH * RATE * CHANNELS * SIZE / 8 - 8;
- wavehead.c[0] = 'W';
- wavehead.c[1] = 'A';
- wavehead.c[2] = 'V';
- wavehead.c[3] = 'E';
- wavehead.d[0] = 'f';
- wavehead.d[1] = 'm';
- wavehead.d[2] = 't';
- wavehead.d[3] = ' ';
- wavehead.e = 16;
- wavehead.f = 2;
- wavehead.g = CHANNELS;
- wavehead.h = RATE;
- wavehead.i = RATE * CHANNELS * SIZE / 8;
- wavehead.j = CHANNELS * SIZE / 8;
- wavehead.k = SIZE;
- wavehead.p[0] = 'd';
- wavehead.p[1] = 'a';
- wavehead.p[2] = 't';
- wavehead.p[3] = 'a';
- // wavehead.q = LENGTH * RATE * CHANNELS * SIZE / 8;
- }
- int recodred(void)
- {
- int status;
- int fd_f;
- int fd_dev_r;
- int arg;
- fd_dev_r = open(AUDIO_DEV_FILE, O_RDONLY, 0777);
- if (fd_dev_r < 0)
- {
- perror("Cannot open device");
- return 1;
- }
- /*
- arg = 16;
- ioctl(fd_dev_r,SOUND_PCM_WRITE_BITS, &arg);
- arg = 2;
- ioctl(fd_dev_r,SOUND_PCM_WRITE_CHANNELS, &arg);
- arg = 44100;
- ioctl(fd_dev_r,SOUND_PCM_WRITE_RATE, &arg);
- */
- /*
- arg = 16;
- ioctl(fd_dev_r,SNDCTL_DSP_SETFMT, &arg);
- arg = 2;
- ioctl(fd_dev_r,SNDCTL_DSP_CHANNELS, &arg);
- arg = 44100;
- ioctl(fd_dev_r,SNDCTL_DSP_SPEED, &arg);
- */
- system("rm -f *.wav");
- if((fd_f = open("./sound.wav", O_CREAT | O_RDWR, 0777)) == -1)
- {
- perror("cannot creat the sound file");
- }
- if((status = write(fd_f, &wavehead, sizeof(wavehead))) == -1)
- {
- perror("write to sound'head wrong!!");
- }
- while(1)
- {
- status = read(fd_dev_r, buf, sizeof(buf));
- if (status != sizeof(buf))
- {
- perror("read wrong number of bytes");
- }
-
- if (write(fd_f, buf, status) == -1)
- {
- perror("write to sound wrong!!");
- }
- }
- close(fd_dev_r);
- close(fd_f);
- }
-
- int main(void)
- {
- int fd_dev_r;
- set_wav_arg();
- recodred();
- }
复制代码
- //播放
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/ioctl.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <linux/soundcard.h>
- #include <termios.h>
-
- #define RSIZE 100
- #define AUDIO_DEV_FILE "/dev/dsp"
- unsigned char buf[RSIZE];
- int read_wav(void)
- {
- int status;
- int fd_f;
- int fd_dev_w;
- fd_dev_w = open(AUDIO_DEV_FILE, O_WRONLY, 0777);
- if (fd_dev_w < 0)
- {
- perror("Cannot open device");
- return 1;
- }
-
- printf("Play...:\n");
-
- if ((fd_f = open("./sound.wav", O_RDONLY, 0777)) == -1)
- {
- perror("cannot creat the sound file");
- }
-
- lseek(fd_f, 44, SEEK_SET);
-
- while(1)
- {
- status = read(fd_f, buf, sizeof(buf));
- if(status == 0)
- break;
- if(status != sizeof(buf))
- {
- perror("write wrong number of bytes");
- }
- status = write(fd_dev_w, buf, sizeof(buf));
- if (status != sizeof(buf))
- {
- perror("wrote2 wrong number of bytes");
- }
- }
- close(fd_f);
- close(fd_dev_w);
- return 0;
- }
- int main(void)
- {
- read_wav();
- return 0;
- }
复制代码 [/code]
問題1:
試過了幾種方法,設定採樣、量化及聲道發現
SOUND_PCM_WRITE_及SNDCTL_DSP_兩種方式
錄出來的檔案,都無法播放
是不是我那設定錯誤???
問題2:
使用fhead所儲存的檔案是可以播放的
但是問題又來了,
播放的時候會有延遲,要等一段時間才有聲音
後來我用lseek();函數讀取後面的字節
是可以正常的播放
但是換一個錄音檔就會有問題,
不知道是錄音時還是播放的關係???
問題3:
我把在板子上可以播放的檔案
抓到WINDOWS 7 底下播放
會出現格式錯誤的訊息
是還有哪裡設定不對嗎???
求助啊!!!:'(
|
|