使用libsndfile写一个简单的wav转pcm工具

#include <stdlib.h>
#include <stdio.h>
#include <sndfile.h>
#include <memory.h>


#define RAW_BUFF_SIZE 1024

int main(int argv, const char *args[]) {
    if (argv!=2) {
        printf("please input wav file\n");
        exit(0);
    }

    SF_INFO info;
    SNDFILE *in = sf_open(args[1], SFM_READ, &info);
    if (!in) {
        fprintf(stderr, "cannot open file %s\n", args[1]);
        return 1;
    }

    char *p = strrchr(args[1], '.');
    int len = p-args[1];
    char *output_file = calloc(len+5, sizeof(char));
    strncpy(output_file, args[1], len);
    strcpy(output_file+len, ".pcm");

    SF_INFO onfo;
    onfo.channels = 1;
    onfo.samplerate = info.samplerate;
    onfo.format = SF_FORMAT_RAW | SF_FORMAT_PCM_16 ;
    onfo.sections = 0;
    onfo.seekable = 1;
    SNDFILE *out = sf_open(output_file, SFM_WRITE, &onfo);
    if (!out) {
        fprintf(stderr, "cannot open file %s\n", output_file);
        sf_close(in);
        return 2;
    }

    free(output_file);

    char buff[RAW_BUFF_SIZE];
    sf_count_t read = 0;
    do {
        read = sf_read_raw(in, buff, RAW_BUFF_SIZE);
        if (read >0) {
            sf_write_raw(out, buff, read);
        }
    } while (read >0);

    sf_close(in);
    sf_close(out);
}
电台调频方法在C语言中可以通过调用音频处理库来实现。以下是一个简单的例子,用于将一个音频文件调整到指定频率: ```c #include <stdio.h> #include <stdlib.h> #include <math.h> #include <sndfile.h> #define PI 3.14159265358979323846 int main(int argc, char *argv[]) { SNDFILE *infile, *outfile; SF_INFO sfinfo; float *buf, *outbuf; double ratio, phase = 0.0, freq = 1000.0; int nread, i, length, outlength, samplerate = 44100; if (argc < 3) { printf("Usage: %s inputfile outputfile\n", argv[0]); return 1; } infile = sf_open(argv[1], SFM_READ, &sfinfo); if (!infile) { printf("Error opening input file\n"); return 1; } outfile = sf_open(argv[2], SFM_WRITE, &sfinfo); if (!outfile) { printf("Error opening output file\n"); return 1; } buf = (float *) malloc(sfinfo.frames * sizeof(float)); outbuf = (float *) malloc(sfinfo.frames * sizeof(float)); nread = sf_read_float(infile, buf, sfinfo.frames); length = nread; outlength = nread; ratio = pow(2, (freq - 440) / 12.0) / pow(2, (440 - freq) / 12.0); for (i = 0; i < length; i++) { outbuf[i] = buf[i] * sin(phase); phase += 2 * PI * ratio / samplerate; if (phase >= 2 * PI) { phase -= 2 * PI; } } sf_write_float(outfile, outbuf, outlength); free(buf); free(outbuf); sf_close(infile); sf_close(outfile); return 0; } ``` 该程序通过调用libsndfile库来读取入音频文件,并使用sin函数来计算输出音频中每个样本的值。调整频率的方法是通过计算一些比率来实现的,其中440 Hz是标准音A的频率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值