将s16le的音频流转换为float类型

这是一个将s16le格式音频文件转换为float类型并写入新文件的示例代码。


以下是代码的讲解:

  1. 定义WavHeader结构体,用于存储WAV文件头中的信息。
  2. 从命令行参数中获取输入和输出文件名(第一个参数代表程序自身,因此输入文件名为第二个参数,输出文件名为第三个参数)。
  3. 打开输入文件和输出文件,如果打开失败则返回错误码。
  4. 读取WAV文件头并检查其格式是否正确,如果不正确则返回错误码。
  5. 计算音频数据中的采样点数和每个采样点占用的字节数。
  6. 分配内存空间来存储音频数据,如果分配失败则返回错误码。
  7. 读取输入文件中的音频数据,并将每个采样点的值转换为float类型。
  8. 输出一些关于音频数据的基本信息。
  9. 将转换后的音频数据写入输出文件。
  10. 释放内存空间,关闭输入和输出文件,程序结束。
  11. 需要注意的是,在写入输出文件时,我们使用了fwrite函数,将整个音频数据数组写入文件。

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

typedef struct {
    char chunkId[4];
    int chunkSize;
    char format[4];
    char subchunk1Id[4];
    int subchunk1Size;
    short audioFormat;
    short numChannels;
    int sampleRate;
    int byteRate;
    short blockAlign;
    short bitsPerSample;
    char subchunk2Id[4];
    int subchunk2Size;
} WavHeader;

int main(int argc, char**argv) {
    const char* infile = argv[1];
    FILE* infp = fopen(infile, "rb");
    if (!infp) {
        printf("Failed to open input file %s.\n", infile);
        return 1;
    }

    const char* outfile = argv[2];
    FILE* outfp = fopen(outfile, "wb");
    if (!outfp) {
        printf("Failed to open input file %s.\n", infile);
        return 1;
    }

    // Read WAV file header
    WavHeader wavHeader;
    fread(&wavHeader, sizeof(WavHeader), 1, infp);
    if (strncmp(wavHeader.chunkId, "RIFF", 4) != 0 ||
        strncmp(wavHeader.format, "WAVE", 4) != 0 ||
        strncmp(wavHeader.subchunk1Id, "fmt ", 4) != 0 ||
        wavHeader.audioFormat != 1) {
        printf("Invalid WAV file.\n");
        fclose(infp);
        return 1;
    }

    // Calculate number of samples and bytes per sample
    int numSamples = wavHeader.subchunk2Size / (wavHeader.numChannels * (wavHeader.bitsPerSample / 8));
    int bytesPerSample = wavHeader.bitsPerSample / 8;

    // Allocate memory for audio data
    float* buffer = (float*) malloc(numSamples * wavHeader.numChannels * sizeof(float));
    if (!buffer) {
        printf("Failed to allocate memory.\n");
        fclose(infp);
        return 1;
    }

    // Read audio data and convert to float
    int i, j;
    short sampleValue;
    for (i = 0; i < numSamples; i++) {
        for (j = 0; j < wavHeader.numChannels; j++) {
            fread(&sampleValue, bytesPerSample, 1, infp);
            buffer[i * wavHeader.numChannels + j] = (float) sampleValue / 32768.0f;
        }
    }

    // Print some information about the audio data
    printf("Input file: %s\n", infile);
    printf("Format: %d-channel s16le, %d Hz\n", wavHeader.numChannels, wavHeader.sampleRate);
    printf("Duration: %.3f seconds\n", (float) numSamples / wavHeader.sampleRate);

    // write to output file.
    fwrite(buffer, numSamples * wavHeader.numChannels * sizeof(float), 1, outfp);

    // Clean up
    free(buffer);
    fclose(infp);
    fclose(outfp);

    return 0;
}


编译后测试


./s16letofloat chendu-96k.wav chendu-96kflt.pcm

ffmpeg 播放

ffmpeg -ar 96000 -ac 2 -f f32le -i chendu-96kflt.pcm -f wav pipe:1 | ffplay -
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值