当前位置:   article > 正文

c语言实现录音功能_c语言 录音

c语言 录音

写了个录音功能的小程序,给大家分享下,测试环境为ubuntu(Linux version 2.6.32-24-generic (buildd@rothera) (gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) ) #39-Ubuntu SMP Wed Jul 28 06:07:29 UTC 2010)

<main.h>
#ifndef  _MAIN_H_
#define  _MAIN_H_

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

#define AUDIO_SAVE_PATH       ("/home/audio.txt")
#define AUDIO_PATH            ("/dev/dsp")


#define LENGTH            (3)        //录音时间
#define RATE              (8000)     //采样频率
#define SIZE              (16)       //量化位数
#define CHANNELS          (2)        //声道数目
#define MAX_BUFF_LEN      (LENGTH*RATE*SIZE*CHANNELS/8)         //保存录音的音频数据

enum
{
    ZJF_AUDIO_NULL = 0,
    ZJF_AUDIO_RECORD,
    ZJF_AUDIO_STOP,
    ZJF_AUDIO_PLAY
};

typedef struct Audio
{
    int Status;
}AudioStatus_t;

extern int ZJF_Audio_SetStatus_API(int status);
extern void ZJF_Audio_RecordPro_API( );
extern void ZJF_Audio_Ctrl_API( );
#endif
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
<ZJF_Audio_Record.c>

#include "main.h"

AudioStatus_t audio;


int ZJF_Audio_SetStatus_API(int status)
{
    printf("设置状态: %d \n",status);
    audio.Status = status;
    return 0;
}

void ZJF_Audio_RecordPro_API( )
{
    int wfd    = 0;   
    int a_fd   = 0;
    int temp   = 0;
    int status = 0;
    int ret    = 0;
    int sret   = 0;
    char buf[MAX_BUFF_LEN] = { 0 };

    int len = MAX_BUFF_LEN;

    a_fd = open(AUDIO_PATH,O_RDWR);
    if(a_fd < 0)
    {
        printf("open file %s failed !!! \n",AUDIO_PATH);
        goto ERR;
    }
    wfd = open(AUDIO_SAVE_PATH,O_CREAT|O_RDWR);
    if(wfd < 0)
    {
        printf("open file %s failed !!! \n",AUDIO_SAVE_PATH);
        goto ERR;
    }

    //设置采样时的量化位数
    temp = SIZE;
    status = ioctl(a_fd,SOUND_PCM_WRITE_BITS,&temp);
    if(status==-1)
    {
      perror("Cannot set SOUND_PCM_WRITE_BITS ");
      goto ERR;
   }

   //设置采样声道数目
   temp = CHANNELS;
   status=ioctl(a_fd,SOUND_PCM_WRITE_CHANNELS,&temp);
   if(status==-1){
      perror("Cannot set SOUND_PCM_WRITE_CHANNELS");
      goto ERR;
   }

   //设置采样频率
   temp = RATE;  
   status=ioctl(a_fd,SOUND_PCM_WRITE_RATE,&temp);
   if(status==-1)
   {
     perror("Cannot set SOUND_PCM_WRITE_RATE");
     goto ERR;
   }

   memset(&audio,0,sizeof(AudioStatus_t));
   while(1)
   {
       if(audio.Status == ZJF_AUDIO_NULL)
       {
           sleep(1);
           continue;
       }
       else if(audio.Status == ZJF_AUDIO_RECORD)
       {   
           //printf("录音 \n");
           ret = read(a_fd,buf,len);
           if(ret < 0)
           {
               perror("read err:");
               goto ERR;
           }

           if(ret > 0)
           {                           
               if(write(wfd,buf,len) < 0)
               {
                   perror("write err:");
                   goto ERR;
               }                           
           }           
       }
       else if(audio.Status == ZJF_AUDIO_STOP)
       {
           printf("audio stop !!! \n");
           goto ERR;
       }
       else if(audio.Status == ZJF_AUDIO_PLAY)
       {
           ret = read(wfd,buf,len);
           if(ret < 0)
           {
               perror("ZJF_AUDIO_PLAY read err:");
               goto ERR;
           }
           if(ret > 0)
           {
               if(write(a_fd,buf,len) < 0)
               {
                   perror("ZJF_AUDIO_PLAY write err:");
                   goto ERR;
               }
               //ioctl(a_fd,SOUND_PCM_SYNC,0);
           }        
       }
   }

ERR:    
    if(a_fd > 0)
    {
        close(a_fd);
        a_fd = -1;
    }
    if(wfd > 0)
    {
        close(wfd);
        wfd = -1;
    }
    return ;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
<ZJF_Audio_Ctrl.c>
#include "main.h"


void ZJF_Audio_Ctrl_API( )
{
    int type = 0;

    while(1)
    {
        printf("1: 录音 \n2: 停止 \n3: 播放\n当前状态是:%d\n",type);

        scanf("%d",&type);

        ZJF_Audio_SetStatus_API(type);
        if(ZJF_AUDIO_STOP == type)
        {
            break;
        }
    }

    return ;
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
<main.c>
#include "main.h"


int main()
{
    pthread_t ntid;

    if(pthread_create(&ntid,NULL,(void *)ZJF_Audio_RecordPro_API,NULL))  
    {  
        printf("can't create thread !!! \n");  
        return 1;  
    }

    if(pthread_create(&ntid,NULL,(void *)ZJF_Audio_Ctrl_API,NULL))  
    {  
        printf("can't create thread !!! \n");  
        return 1;  
    }

    while(1)
    {
        sleep(10);
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/742522
推荐阅读
相关标签
  

闽ICP备14008679号