我正在编写一个程序,以渲染波形类型(正弦波,方波,波齿等)并将其写入文件。问题是声音播放器说即使我写了标题,标题也丢失了。数据类型应该以小尾数形式编写。
我尝试以不同方式多次重写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");
标题编写功能:
#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 1100 0.1 44100 out.wav生成的文件可以被声音播放器读取,但是out.wav会导致错误,提示读取标头丢失。
您在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]
。