织梦CMS - 轻松建站从此开始!

罗索

Linux 下录制和播放 声音, record & play audio in linux

jackyhwei 发布于 2011-07-01 09:21 点击:次 
Linux 下利用OSS架构来实现录制和播放声音的代码。
TAG:

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

#define LENGTH 3    /* how many seconds of speech to store */
#define RATE 44100   /* the sampling rate */
#define SIZE 16      /* sample size: 8 or 16 bits */
#define CHANNELS 1  /* 1 = mono 2 = stereo */

/* this buffer holds the digitized audio */
//unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8];

int main()
{
    int fdplay;
    int fdrec;    
    int arg;
    int status;
    FILE *fp = 0;
    int i=0;
    int bufferSize = 1024;
    short *buf = 0;
    /* open sound device */
    fdplay = open("/dev/dsp", O_WRONLY);
    if (fdplay < 0) {
    printf("open of /dev/dsp failed\n");
    return 0;
    }
    fdrec = open("/dev/dsp1", O_RDONLY);
    if (fdrec < 0) {
    printf("open of /dev/dsp2 failed\n");
    exit(1);
    }
    /* set sampling parameters */
    arg = SIZE;       /* sample size */
    status = ioctl(fdplay, SOUND_PCM_WRITE_BITS, &arg);
    if (status == -1)
    {
        printf("SOUND_PCM_WRITE_BITS ioctl failed\n");
        goto exit;
    }
    if (arg != SIZE)
    {
        printf("unable to set play sample size\n");
        goto exit;
    }
    
    arg = SIZE;       /* sample size */
    status = ioctl(fdrec, SOUND_PCM_READ_BITS, &arg);
    if (status == -1)
    {
        printf("SOUND_PCM_RAED_BITS ioctl failed\n");
        goto exit;
    }
    if (arg != SIZE)
    {
        printf("unable to set rec sample size = %d\n", arg);
        goto exit;
    }
    arg = CHANNELS;  /* mono or stereo */
    status = ioctl(fdplay, SOUND_PCM_WRITE_CHANNELS, &arg);
    if (status == -1)
    {
        printf("SOUND_PCM_WRITE_CHANNELS ioctl failed\n");
        goto exit;

    }
    if (arg != CHANNELS)
    {
        printf("unable to set play number of channels\n");
        goto exit;
    }
    arg = CHANNELS;  /* mono or stereo */
    status = ioctl(fdrec, SOUND_PCM_READ_CHANNELS, &arg);
    if (status == -1)
    {
        printf("SOUND_PCM_RAED_CHANNELS ioctl failed\n");
        goto exit;
    }
    if (arg != CHANNELS)
    {
        printf("unable to set rec number of channels\n");
        goto exit;
    }    

    arg = RATE;       /* sampling rate */
    status = ioctl(fdplay, SOUND_PCM_WRITE_RATE, &arg);
    if (status == -1)
    {
        printf("SOUND_PCM_WRITE_WRITE ioctl failed");
        goto exit;
    }
    arg = RATE;       /* sampling rate */
    status = ioctl(fdrec, SOUND_PCM_READ_RATE, &arg);
    if (status == -1)
    {
        printf("SOUND_PCM_READ_WRITE ioctl failed");    
        goto exit;
    }
    //fp = fopen("1.wav", "r");
    buf = (short*)malloc(bufferSize*4);    
    while (1)
    {
        if(read(fdrec, buf, bufferSize*4) != bufferSize*4)
        {
            printf("read file failed\n");
            break;
        }
        printf("Play one sample %d\n", i++);

        if ((write(fdplay, buf, bufferSize*4)) != bufferSize*4)
        {
            printf("PLay one buffer %d\n", i++);
            break;
        }        
        
        if(i >100)
            break;
        //status = ioctl(fd, SOUND_PCM_SYNC, 0);
          /*if (status == -1)
        {
            printf("SOUND_PCM_SYNC ioctl failed");
            break;
        }*/
    }
    free(buf);
    ioctl(fdplay, SOUND_PCM_SYNC, 0);
exit:
    close(fdplay);
    close(fdrec);

}

(carlsonlee_freec)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201107/14641.html]
本文出处:hi.baidu.com/carlsonlee_freec 作者:carlsonlee_freec
顶一下
(1)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容