gpt4 book ai didi

c - 声音播放器无法读取我的程序生成的 WAV 文件,提示缺少 header

转载 作者:行者123 更新时间:2023-11-30 16:12:20 24 4
gpt4 key购买 nike

我正在编写一个程序,该程序呈现波形类型(正弦波、方波、波齿波等)并将其写入文件。问题是声音播放器说标题丢失,即使我写了它。数据类型应该以小尾数法编写。

我尝试用不同的方式多次重写I/O函数,但没有成功。我尝试用要用测试数据写入的数据填充数组,但即使这样也不起作用。文件写入时出现问题。

主要功能:

int main (int argc, char** argv){
if (argc != 6){
printf("Error: expecting more arguments\n");
return -1;
}

//First argument: voice
int voice = atof(argv[1]);


//Second argument: frequency
float frequency = atof(argv[2]);


//Third argument: amplitude
float amplitude = atof(argv[3]);


//Fourth argument: numsample
int numsamples = atof(argv[4]);

//Fifth argument: output file name
char* filename = argv[5];

int16_t buffer[numsamples*2];
memset(buffer, 0.1, numsamples*2);

if (check_args(voice, frequency, amplitude) != 0) {


//Sine wave
if (voice == 0) {
//render_sine_wave_stero(buffer, numsamples, frequency, amplitude);
}

//If square wave
else if (voice == 1){
//render_square_wave_stereo(buffer, numsamples, frequency, amplitude);
}

//Sawtooth
else if (voice == 2) {
//render_sawtooth_wave_stero(buffer, numsamples, frequency, amplitude);
}





FILE *outputfile = fopen(filename, "wb+");
if (outputfile == NULL){
printf("Error: file failed to open");
return 1;
}

write_wave_header(outputfile, numsamples);

write_s16_buf(outputfile, buffer, numsamples*2);

fclose(outputfile);

printf("Finished\n");

header 写入功能:

#define PI                 3.14159265358979323846
#define SAMPLES_PER_SECOND 44100u
#define NUM_CHANNELS 2u
#define BITS_PER_SAMPLE 16u

void write_wave_header(FILE *out, unsigned num_samples) {
/*
* See: http://soundfile.sapp.org/doc/WaveFormat/
*/
uint32_t ChunkSize, Subchunk1Size, Subchunk2Size;
uint16_t NumChannels = NUM_CHANNELS;
uint32_t ByteRate = SAMPLES_PER_SECOND * NumChannels * (BITS_PER_SAMPLE/8u);
uint16_t BlockAlign = NumChannels * (BITS_PER_SAMPLE/8u);

/* Subchunk2Size is the total amount of sample data */
Subchunk2Size = num_samples * NumChannels * (BITS_PER_SAMPLE/8u);
Subchunk1Size = 16u;
ChunkSize = 4u + (8u + Subchunk1Size) + (8u + Subchunk2Size);

/* Write the RIFF chunk descriptor */
write_bytes(out, "RIFF", 4u);
write_u32(out, ChunkSize);
write_bytes(out, "WAVE", 4u);

/* Write the "fmt " sub-chunk */
write_bytes(out, "fmt ", 4u); /* Subchunk1ID */
write_u32(out, Subchunk1Size);
write_u16(out, 1u); /* PCM format */
write_u16(out, NumChannels);
write_u32(out, SAMPLES_PER_SECOND); /* SampleRate */
write_u32(out, ByteRate);
write_u16(out, BlockAlign);
write_u16(out, BITS_PER_SAMPLE);

/* Write the beginning of the "data" sub-chunk, but not the actual data */
write_bytes(out, "data", 4); /* Subchunk2ID */
write_u32(out, Subchunk2Size);
}

IO功能:

void write_byte(FILE* out, char val){

fwrite(&val, sizeof(char), 1, out);
}

void write_bytes(FILE* out, const char data[], unsigned n){

for (unsigned i=0; i<n; i++){
write_byte(out, data[n]);
}
}

void write_u16(FILE* out, uint16_t value){

char bytes[] = {0, 0};
//Lowest byte
bytes[0] = value & 0xFF;
//Biggest byte
bytes[1] = value>>8;
write_bytes(out, bytes, 2u);


//fwrite(&value, sizeof(uint16_t), 1, out);
}

void write_u32(FILE* out, uint32_t value){

uint16_t first = value & 0x0000FFFF;
uint16_t second = value >> 16;;

write_u16(out, first);
write_u16(out, second);

//fwrite(&value, sizeof(uint32_t), 1, out);
}

void write_s16(FILE* out, int16_t value){

uint16_t newV = (uint16_t)value;

write_u16(out, newV);

//fwrite(&value, sizeof(int16_t), 1, out);
}

void write_s16_buf(FILE* out, const int16_t buf[], unsigned n){
for (unsigned i=0; i<n; i++){
write_s16(out, buf[n]);
}
}

我希望声音播放器可以读取通过调用 ./render_tone 1 100 0.1 44100 out.wav 生成的文件,但 out.wav 会导致错误,指出读取时缺少 header 。

最佳答案

您的 write_bytes() 函数中有一个拼写错误(我假设):

void write_bytes(FILE* out, const char data[], unsigned n){

for (unsigned i=0; i<n; i++){
write_byte(out, data[n]);
}
}

您应该使用data[i]而不是data[n]。现在如果您调用:

write_bytes(out, "RIFF", 4u);

它将写入 \0 字符(NUL 终止 c 字符串)4 次。

关于c - 声音播放器无法读取我的程序生成的 WAV 文件,提示缺少 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58357678/

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