Last active
November 20, 2017 20:41
-
-
Save algon-320/36cfdd3567a3a7ce98023f9f4c7b9724 to your computer and use it in GitHub Desktop.
wavファイルを書き出す
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
#include <string.h> | |
#include <stdint.h> | |
struct wave_header { | |
unsigned char riff[4]; // "RIFF" | |
unsigned int fsize; // ファイルサイズ - 8 | |
unsigned char type[4]; // "WAVE" | |
unsigned char fmt[4]; // "fmt " | |
unsigned int fmtbytes; // 16 | |
unsigned short pcm; // 1 | |
unsigned short channel; // 2 ステレオ | |
unsigned int sampling; // 44100 | |
unsigned int bytes_per_sec; // 44100 * 2 * 16/8 | |
unsigned short block_size; // 4 | |
unsigned short bit_per_sample; // 16 | |
unsigned char data[4]; // "data" | |
unsigned int wave_bytes; // 波形データのバイト数 | |
}; | |
struct wave_header gen_header() { | |
struct wave_header wh; | |
memcpy(wh.riff, "RIFF", 4); | |
wh.fsize = 00000; | |
memcpy(wh.type, "WAVE", 4); | |
memcpy(wh.fmt, "fmt ", 4); | |
wh.fmtbytes = 16; | |
wh.pcm = 1; | |
wh.channel = 2; | |
wh.sampling = 44100; | |
wh.bytes_per_sec = 176400; | |
wh.block_size = 4; | |
wh.bit_per_sample = 16; | |
memcpy(wh.data, "data", 4); | |
wh.wave_bytes = 00000; | |
return wh; | |
} | |
#define DATA_SIZE (44100 * 5 * 2) | |
#define DATA_BYTES (DATA_SIZE * 2) // signed short = 2 bytes | |
signed short *gen_sine_wave_lpcm() { | |
signed short *data = (signed short *)malloc(DATA_BYTES); | |
if(data == NULL) { | |
printf("memory allocation error.\n"); | |
exit(-1); | |
} | |
double unit_c = 261.626 / 44100.0 * 2 * acos(-1); // ド | |
double unit_e = 329.628 / 44100.0 * 2 * acos(-1); // ミ | |
double unit_g = 391.995 / 44100.0 * 2 * acos(-1); // ソ | |
for(int i=0; i<44100*5; i++) { | |
data[i*2+0] = (signed short)((sin(unit_c*i) + sin(unit_e*i)) * 10000.0); | |
data[i*2+1] = (signed short)((sin(unit_c*i) + sin(unit_g*i)) * 10000.0); | |
} | |
return data; | |
} | |
void build_wav() { | |
unsigned char *file_data = (unsigned char *)malloc(44 + DATA_BYTES); | |
if(file_data == NULL) { | |
printf("memory allocation error\n"); | |
exit(-1); | |
} | |
signed short *wave_data = gen_sine_wave_lpcm(); | |
struct wave_header wh = gen_header(); | |
wh.wave_bytes = (DATA_BYTES); | |
wh.fsize = (44 + wh.wave_bytes - 8); | |
memcpy(file_data+0, wh.riff, 4); | |
memcpy(file_data+4, &wh.fsize, 4); | |
memcpy(file_data+8, wh.type, 4); | |
memcpy(file_data+12, wh.fmt, 4); | |
memcpy(file_data+16, &wh.fmtbytes, 4); | |
memcpy(file_data+20, &wh.pcm, 2); | |
memcpy(file_data+22, &wh.channel, 2); | |
memcpy(file_data+24, &wh.sampling, 4); | |
memcpy(file_data+28, &wh.bytes_per_sec, 4); | |
memcpy(file_data+32, &wh.block_size, 2); | |
memcpy(file_data+34, &wh.bit_per_sample, 2); | |
memcpy(file_data+36, wh.data, 4); | |
memcpy(file_data+40, &wh.wave_bytes, 4); | |
memcpy(file_data+44, wave_data, DATA_BYTES); | |
FILE *fptr = fopen("output.wav","wb"); | |
if(fptr == NULL) { | |
printf("file opening error\n"); | |
exit(-1); | |
} | |
fwrite(file_data, sizeof(unsigned char), 44 + wh.wave_bytes, fptr); | |
fclose(fptr); | |
} | |
int main(int argc, char const *argv[]) { | |
build_wav(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
#include <string.h> | |
#include <stdint.h> | |
struct wave_header { | |
unsigned char riff[4]; // "RIFF" | |
unsigned int fsize; // ファイルサイズ - 8 | |
unsigned char type[4]; // "WAVE" | |
unsigned char fmt[4]; // "fmt " | |
unsigned int fmtbytes; // 16 | |
unsigned short pcm; // 1 | |
unsigned short channel; // 2 ステレオ | |
unsigned int sampling; // 44100 | |
unsigned int bytes_per_sec; // 44100 * 2 * 16/8 | |
unsigned short block_size; // 4 | |
unsigned short bit_per_sample; // 16 | |
unsigned char data[4]; // "data" | |
unsigned int wave_bytes; // 波形データのバイト数 | |
}; | |
struct wave_header gen_header() { | |
struct wave_header wh; | |
memcpy(wh.riff, "RIFF", 4); | |
wh.fsize = 00000; | |
memcpy(wh.type, "WAVE", 4); | |
memcpy(wh.fmt, "fmt ", 4); | |
wh.fmtbytes = 16; | |
wh.pcm = 1; | |
wh.channel = 2; | |
wh.sampling = 44100; | |
wh.bytes_per_sec = 176400; | |
wh.block_size = 4; | |
wh.bit_per_sample = 16; | |
memcpy(wh.data, "data", 4); | |
wh.wave_bytes = 00000; | |
return wh; | |
} | |
#define DATA_SIZE (44100 * 5 * 2) | |
#define DATA_BYTES (DATA_SIZE * 2) // signed short = 2 bytes | |
signed short *gen_sine_wave_lpcm() { | |
signed short *data = (signed short *)malloc(DATA_BYTES); | |
if(data == NULL) { | |
printf("memory allocation error.\n"); | |
exit(-1); | |
} | |
double unit_c = 261.626 / 44100.0 * 2 * acos(-1); // ド | |
double unit_e = 329.628 / 44100.0 * 2 * acos(-1); // ミ | |
double unit_g = 391.995 / 44100.0 * 2 * acos(-1); // ソ | |
for(int i=0; i<44100*5; i++) { | |
data[i*2+0] = (signed short)((sin(unit_c*i) + sin(unit_e*i)) * 10000.0); | |
data[i*2+1] = (signed short)((sin(unit_c*i) + sin(unit_g*i)) * 10000.0); | |
} | |
return data; | |
} | |
void build_wav() { | |
unsigned char *file_data = (unsigned char *)malloc(44 + DATA_BYTES); | |
if(file_data == NULL) { | |
printf("memory allocation error\n"); | |
exit(-1); | |
} | |
signed short *wave_data = gen_sine_wave_lpcm(); | |
struct wave_header wh = gen_header(); | |
wh.wave_bytes = (DATA_BYTES); | |
wh.fsize = (44 + wh.wave_bytes - 8); | |
// memcpy(file_data+0, wh.riff, 4); | |
// memcpy(file_data+4, &wh.fsize, 4); | |
// memcpy(file_data+8, wh.type, 4); | |
// memcpy(file_data+12, wh.fmt, 4); | |
// memcpy(file_data+16, &wh.fmtbytes, 4); | |
// memcpy(file_data+20, &wh.pcm, 2); | |
// memcpy(file_data+22, &wh.channel, 2); | |
// memcpy(file_data+24, &wh.sampling, 4); | |
// memcpy(file_data+28, &wh.bytes_per_sec, 4); | |
// memcpy(file_data+32, &wh.block_size, 2); | |
// memcpy(file_data+34, &wh.bit_per_sample, 2); | |
// memcpy(file_data+36, wh.data, 4); | |
// memcpy(file_data+40, &wh.wave_bytes, 4); | |
memcpy(file_data, &wh, sizeof(wh)); | |
memcpy(file_data+44, wave_data, DATA_BYTES); | |
FILE *fptr = fopen("output_direct.wav","wb"); | |
if(fptr == NULL) { | |
printf("file opening error\n"); | |
exit(-1); | |
} | |
fwrite(file_data, sizeof(unsigned char), 44 + wh.wave_bytes, fptr); | |
fclose(fptr); | |
} | |
int main(int argc, char const *argv[]) { | |
build_wav(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
#include <string.h> | |
#include <stdint.h> | |
struct wave_header { | |
unsigned char riff[4]; // "RIFF" | |
unsigned int fsize; // ファイルサイズ - 8 | |
unsigned char type[4]; // "WAVE" | |
unsigned char fmt[4]; // "fmt " | |
unsigned int fmtbytes; // 16 | |
unsigned short pcm; // 1 | |
unsigned short channel; // 2 ステレオ | |
unsigned int sampling; // 44100 | |
unsigned int bytes_per_sec; // 44100 * 2 * 16/8 | |
unsigned short block_size; // 4 | |
unsigned short bit_per_sample; // 16 | |
unsigned char data[4]; // "data" | |
unsigned int wave_bytes; // 波形データのバイト数 | |
}; | |
struct wave_header gen_header() { | |
struct wave_header wh; | |
memcpy(wh.riff, "RIFF", 4); | |
wh.fsize = 00000; | |
memcpy(wh.type, "WAVE", 4); | |
memcpy(wh.fmt, "fmt ", 4); | |
wh.fmtbytes = 16; | |
wh.pcm = 1; | |
wh.channel = 2; | |
wh.sampling = 44100; | |
wh.bytes_per_sec = 176400; | |
wh.block_size = 4; | |
wh.bit_per_sample = 16; | |
memcpy(wh.data, "data", 4); | |
wh.wave_bytes = 00000; | |
return wh; | |
} | |
#define DATA_SIZE (44100 * 5 * 2) | |
#define DATA_BYTES (DATA_SIZE * 2) // signed short = 2 bytes | |
signed short *gen_sine_wave_lpcm() { | |
signed short *data = (signed short *)malloc(DATA_BYTES); | |
if(data == NULL) { | |
printf("memory allocation error.\n"); | |
exit(-1); | |
} | |
double unit_c = 261.626 / 44100.0 * 2 * acos(-1); // ド | |
double unit_e = 329.628 / 44100.0 * 2 * acos(-1); // ミ | |
double unit_g = 391.995 / 44100.0 * 2 * acos(-1); // ソ | |
for(int i=0; i<44100*5; i++) { | |
data[i*2+0] = (signed short)((sin(unit_c*i) + sin(unit_e*i)) * 10000.0); | |
data[i*2+1] = (signed short)((sin(unit_c*i) + sin(unit_g*i)) * 10000.0); | |
} | |
return data; | |
} | |
void build_wav() { | |
// unsigned char *file_data = (unsigned char *)malloc(44 + DATA_BYTES); | |
// if(file_data == NULL) { | |
// printf("memory allocation error\n"); | |
// exit(-1); | |
// } | |
signed short *wave_data = gen_sine_wave_lpcm(); | |
struct wave_header wh = gen_header(); | |
wh.wave_bytes = (DATA_BYTES); | |
wh.fsize = (44 + wh.wave_bytes - 8); | |
// memcpy(file_data+0, wh.riff, 4); | |
// memcpy(file_data+4, &wh.fsize, 4); | |
// memcpy(file_data+8, wh.type, 4); | |
// memcpy(file_data+12, wh.fmt, 4); | |
// memcpy(file_data+16, &wh.fmtbytes, 4); | |
// memcpy(file_data+20, &wh.pcm, 2); | |
// memcpy(file_data+22, &wh.channel, 2); | |
// memcpy(file_data+24, &wh.sampling, 4); | |
// memcpy(file_data+28, &wh.bytes_per_sec, 4); | |
// memcpy(file_data+32, &wh.block_size, 2); | |
// memcpy(file_data+34, &wh.bit_per_sample, 2); | |
// memcpy(file_data+36, wh.data, 4); | |
// memcpy(file_data+40, &wh.wave_bytes, 4); | |
// memcpy(file_data, &wh, sizeof(wh)); | |
// memcpy(file_data+44, wave_data, DATA_BYTES); | |
FILE *fptr = fopen("output_direct___.wav","wb"); | |
if(fptr == NULL) { | |
printf("file opening error\n"); | |
exit(-1); | |
} | |
fwrite(&wh, sizeof(wh), 1, fptr); // 直接 | |
fwrite(wave_data, sizeof(unsigned char), wh.wave_bytes, fptr); | |
fclose(fptr); | |
} | |
int main(int argc, char const *argv[]) { | |
build_wav(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment