• 4025阅读
  • 1回复

linux 下进行语音编码 [复制链接]

上一主题 下一主题
离线lishuzheng
 

只看楼主 倒序阅读 楼主  发表于: 2007-12-28
linux 下进行语音编码
— 本帖被 XChinux 执行加亮操作(2008-07-17) —
想在linux 下进行语音编码!但是linux录音的函数是read(fd,buf,size)
其中buf是存储录音数据的,但是它的类型是char*的,我想把它变成short*类型的,
然后存储为short类型的数组中,请问应该怎么编程?????
录音程序如下:
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/soundcard.h>

#define LENGTH 3
/* 存储秒数 */
#define RATE 8000
/* 采样频率 */
#define SIZE 8
/* 量化位数 */
#define CHANNELS 1
/* 声道数目 */

/* 用于保存数字音频数据的内存缓冲区 */
unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8];

int main()
{

int fd;
/* 声音设备的文件描述符 */

int arg;
/* 用于ioctl调用的参数 */

int status;
/* 系统调用的返回值 */


/* 打开声音设备 */

fd = open("/dev/dsp", O_RDWR);

if (fd < 0)
{

perror("open of /dev/dsp failed");

exit(1);

}


/* 设置采样时的量化位数 */

arg = SIZE;

status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);

if (status == -1)

perror("SOUND_PCM_WRITE_BITS ioctl failed");

if (arg != SIZE)

perror("unable to set sample size");


/* 设置采样时的声道数目 */

arg = CHANNELS;

status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg);

if (status == -1)



perror("SOUND_PCM_WRITE_CHANNELS ioctl failed");

if (arg != CHANNELS)

perror("unable to set number of channels");


/* 设置采样时的采样频率 */

arg = RATE;

status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);

if (status == -1)

perror("SOUND_PCM_WRITE_WRITE ioctl failed");


/* 循环,直到按下Control-C */

while (1)
{

printf("Say something:\n");

status = read(fd, buf, sizeof(buf)); /* 录音 */

if (status != sizeof(buf))

perror("read wrong number of bytes");

printf("You said:\n");

status = write(fd, buf, sizeof(buf)); /* 回放 */

if (status != sizeof(buf))

perror("wrote wrong number of bytes");


/* 在继续录音前等待回放结束 */

status = ioctl(fd, SOUND_PCM_SYNC, 0);

if (status == -1)

perror("SOUND_PCM_SYNC ioctl failed");

}
}
离线lishuzheng

只看该作者 1楼 发表于: 2007-12-28
没有人关注啊 

希望大家能给些意见。

下面是我修改后的程序,虽然程序最后能输出数字了,但是当我静音不说话的时候,它的输出是15420,而且它的输出值都是大于等于零的。按说应该在静音的时候输出为0,且输出有正有负,请问大家有什么好的提议!!!
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/soundcard.h>
#include <fcntl.h>
#include <sys/stat.h>
#define RATE 8000            /* 采样频率 */
#define SIZE 8              /* 量化位数 */
#define CHANNELS 1          /* 声道数目 */
#define LENGTH  3  /* 存储秒数 */
int fd;    /* 声音设备的文件描述符 */
int arg;    /* 用于ioctl调用的参数 */
int status;  /* 系统调用的返回值 */
short  frame[LENGTH*RATE*SIZE*CHANNELS/8];
// short  voice[LENGTH*RATE*SIZE*CHANNELS/8];
#define NUMBER (LENGTH*RATE*SIZE*CHANNELS/8)
int main()    //开始录音
{
    int i;
//  char *lpData;
    /* 打开声音设备 */
  fd = open("/dev/dsp", O_RDWR);
  if (fd < 0)
{
    perror("open of /dev/dsp failed");
    return 0;
  }

  /* 设置采样时的量化位数 */
  arg = SIZE;
  status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);
  if (status == -1)
    perror("SOUND_PCM_WRITE_BITS ioctl failed");
  if (arg != SIZE)
    perror("unable to set sample size");
  /* 设置采样时的声道数目 */
  arg = CHANNELS; 
  status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg);
  if (status == -1)
perror("SOUND_PCM_WRITE_CHANNELS ioctl failed");
  if (arg != CHANNELS)
    perror("unable to set number of channels");
  /* 设置采样时的采样频率 */
  arg = RATE;
  status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);
  if (status == -1)
    perror("SOUND_PCM_WRITE_WRITE ioctl failed");
  // lpData=(char *)&frame;
    printf("Say something:\n");
    status = read(fd, frame,NUMBER ); /* 录音 */
    if (status != NUMBER)
      perror("read wrong number of bytes");
   
    printf("Yousaide:\n");
   
    status=write(fd, frame,NUMBER );
    if(status!=NUMBER)
    perror("wrote wrong number of bytes");
//  short *buff;

//  buff=(short *)lpData;
   
    for(i=0;i<12000;i++)
    {
//    voice[i++]=*buff++;
   
     printf("  %d",frame);
    }
    close(fd);
    return 0;
  }
快速回复
限100 字节
 
上一个 下一个