sin函数生成PCM文件

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Hsu_smile/article/details/53004545
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
/*
 *output sin pcm file
 *freq from 0 to 21000
 *step is 100
 *16bits
 *2 channel
 *44100 sample rate
 *each file  has 5 second data
 */
double pi = 3.1415926;

int main(int argc, char ** argv)
{
    double x = 0;
    short y = 0;
    int f = 0;
    int fs = 44100 * 2;//sample rate is 44100 2 channel
    char name[64] = {0};
    int fd = 0;
    for (f = 0; f < 8000; f += 1000) {
        double w = 2 * pi * f;
        double step  = (double)1.0 / (double)fs;
        memset(name, 0, 64);
        sprintf(name, "%dHZ-2ch-44100.pcm", f);
        fd = open(name, O_CREAT | O_RDWR | O_TRUNC, 0777);
        for(x = 0; x <= 5; x += step) {
           y = sin(w * x) * 16384;
           write(fd, &y, 2);
       }
    close (fd);
   }
   return 0;
}
利用正弦函数采样生成音频RAW数据存入PCM文件.用Audacity打开可以看到波形:


展开阅读全文

没有更多推荐了,返回首页