gpt4 book ai didi

c - 为什么我的 .wav 文件没有运行?

转载 作者:太空宇宙 更新时间:2023-11-04 03:37:35 24 4
gpt4 key购买 nike

在不使用结构的情况下,我尝试创建一个 wave 文件,该文件会以 900 Hz 的频率生成持续 5 秒的音调。然而,当我运行它时,它没有运行,甚至没有给我采样率或字节数。这是我的代码:

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

int main () {
FILE* fp;
fp = fopen("sine.wav", "wb");
if (fp == NULL) {
printf("File does not exist.\n");
return EXIT_FAILURE;
}
char ChunkID[4] = "RIFF", Format[4] = "WAVE", Subchunk1ID[4] = "fmt", Subchunk2ID[4] = "data";
unsigned int ChunkSize, Subchunk1Size, Subchunk2Size;
unsigned short int AudioFormat, NumChannels, BlockAlign, BitsPerSample;
int SampleRate, ByteRate;
Subchunk1Size = 16;
AudioFormat = 1;
NumChannels = 1;
SampleRate = 44100;
ByteRate = 2 * SampleRate;
BitsPerSample = 16;
BlockAlign = NumChannels * BitsPerSample / 8;
Subchunk2Size = 10 * ByteRate;
ChunkSize = Subchunk2Size + 41 - 8;
fwrite(ChunkID, 4, 1, fp);
fwrite(&ChunkSize, 4, 1, fp);
fwrite(Format, 4, 1, fp);
fwrite(Subchunk1ID, 4, 1, fp);
fwrite(&Subchunk1Size, 4, 1, fp);
fwrite(&AudioFormat, 2, 1, fp);
fwrite(&NumChannels, 2, 1, fp);
fwrite(&SampleRate, 4, 1, fp);
fwrite(&ByteRate, 4, 1, fp);
fwrite(&BlockAlign, 2, 1, fp);
fwrite(&BitsPerSample, 2, 1, fp);
fwrite(Subchunk2ID, 4, 1, fp);
fwrite(&Subchunk2Size, 4, 1, fp);
int i, amplitude = 37000;
short int audio;
float w, freq = 900 * 4;
w = 2.0 * M_PI * freq;
for(i = 0; i < 5 * SampleRate; i++) {
if (i < 5 * SampleRate / 4)
w = 900 * 4;
else
w = 0;
w *= 2.0 * M_PI;
audio = amplitude * sin(w * i / SampleRate);
fwrite(&audio, 2, 1, fp);
}
fclose(fp);
return EXIT_SUCCESS;
}

最佳答案

最明显的问题是 Subchunk1ID,它是 "fmt"

应该是"fmt ",注意末尾的空格。

关于c - 为什么我的 .wav 文件没有运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31227350/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com