【发布时间】:2011-03-30 08:25:00
【问题描述】:
出于病态的好奇心,我一直在尝试编写一个程序,该程序将在 C 中生成 4 秒 440 A 音符。但是,在 VLC 中播放输出的文件不会产生任何音乐。
使用 Wikipedia 作为此页面上 .au 标头的指南:http://en.wikipedia.org/wiki/Au_file_format 我想出了这个:
#include <stdio.h>
#include <math.h>
#define MAGIC_NUM 0x2e736e64
#define DEFAULT_OFFSET 24
#define UNKNOWN_SIZE 0xffffffff
#define BIT_32_PCM 5
#define STEREO 2
#define SAMPLE_RATE 8000
#define DURATION 4
#define MIDDLE_A 440
int main(int argc, char** argv) {
FILE* sound;
sound = fopen("output.au", "w");
//write header
fputc(MAGIC_NUM, sound);
fputc(DEFAULT_OFFSET, sound);
fputc(UNKNOWN_SIZE, sound);
fputc(BIT_32_PCM, sound);
fputc(SAMPLE_RATE, sound);
fputc(STEREO, sound);
//write a duration of a constant note
int i;
for(i = 0; i <= DURATION * SAMPLE_RATE; i++) {
fputc((int)floor(MIDDLE_A * sin(i)), sound);
}
fclose(sound);
return 0;
}
有谁知道我做错了什么?
【问题讨论】: