fork download
  1. #ifndef wavehpp
  2. #define wavehpp
  3. #include <stdio.h>
  4. #include <vector>
  5. #include <math.h>
  6. using namespace std;
  7. class MONO_PCM{
  8. public:
  9. int fs; /* 標本化周波数 */
  10. int bits; /* 量子化精度 */
  11. std::vector<int> s; /* 音データ */
  12. //初期化
  13. MONO_PCM(int myfs=44100,int mybits=16)
  14. {
  15. fs = myfs;
  16. bits = mybits;
  17. }
  18. //要素追加
  19. void add(int data)
  20. {
  21. s.push_back(data);
  22. }
  23. //要素クリア
  24. void clear(){
  25. s.clear();
  26. }
  27. //書き出し
  28. int write(char* file_name)
  29. {
  30. FILE *fp;
  31. char riff_chunk_ID[4];
  32. long riff_chunk_size;
  33. char riff_form_type[4];
  34. char fmt_chunk_ID[4];
  35. long fmt_chunk_size;
  36. short fmt_wave_format_type;
  37. short fmt_channel;
  38. long fmt_samples_per_sec;
  39. long fmt_bytes_per_sec;
  40. short fmt_block_size;
  41. short fmt_bits_per_sample;
  42. char data_chunk_ID[4];
  43. long data_chunk_size;
  44. short data;
  45. int si;
  46. riff_chunk_ID[0] = 'R';
  47. riff_chunk_ID[1] = 'I';
  48. riff_chunk_ID[2] = 'F';
  49. riff_chunk_ID[3] = 'F';
  50. riff_chunk_size = 36 + s.size() * 2;
  51. riff_form_type[0] = 'W';
  52. riff_form_type[1] = 'A';
  53. riff_form_type[2] = 'V';
  54. riff_form_type[3] = 'E';
  55. fmt_chunk_ID[0] = 'f';
  56. fmt_chunk_ID[1] = 'm';
  57. fmt_chunk_ID[2] = 't';
  58. fmt_chunk_ID[3] = ' ';
  59. fmt_chunk_size = 16;
  60. fmt_wave_format_type = 1;
  61. fmt_channel = 1;
  62. fmt_samples_per_sec = fs; /* 標本化周波数 */
  63. fmt_bytes_per_sec = fs * bits / 8;
  64. fmt_block_size = bits / 8;
  65. fmt_bits_per_sample = bits; /* 量子化精度 */
  66. data_chunk_ID[0] = 'd';
  67. data_chunk_ID[1] = 'a';
  68. data_chunk_ID[2] = 't';
  69. data_chunk_ID[3] = 'a';
  70. data_chunk_size = s.size() * 2;
  71. fp = fopen(file_name, "wb");
  72. fwrite(riff_chunk_ID, 1, 4, fp);
  73. fwrite(&riff_chunk_size, 4, 1, fp);
  74. fwrite(riff_form_type, 1, 4, fp);
  75. fwrite(fmt_chunk_ID, 1, 4, fp);
  76. fwrite(&fmt_chunk_size, 4, 1, fp);
  77. fwrite(&fmt_wave_format_type, 2, 1, fp);
  78. fwrite(&fmt_channel, 2, 1, fp);
  79. fwrite(&fmt_samples_per_sec, 4, 1, fp);
  80. fwrite(&fmt_bytes_per_sec, 4, 1, fp);
  81. fwrite(&fmt_block_size, 2, 1, fp);
  82. fwrite(&fmt_bits_per_sample, 2, 1, fp);
  83. fwrite(data_chunk_ID, 1, 4, fp);
  84. fwrite(&data_chunk_size, 4, 1, fp);
  85. for (size_t n = 0; n < s.size(); n++){
  86. si = s[n];
  87. if (si > 0x7fff){
  88. si = 0x7fff; /* クリッピング */
  89. }else if (si < -0x7fff){
  90. si = -0x7fff;
  91. }
  92. data = (short)si; /* 四捨五入とオフセットの調節 */
  93. fwrite(&data, 2, 1, fp); /* 音データの書き出し */
  94. }
  95.  
  96. fclose(fp);
  97. return 0;
  98. }
  99. //読み込み
  100. int read(char* file_name)
  101. {
  102. FILE *fp;
  103. char riff_chunk_ID[4];
  104. long riff_chunk_size;
  105. char riff_form_type[4];
  106. char fmt_chunk_ID[4];
  107. long fmt_chunk_size;
  108. short fmt_wave_format_type;
  109. short fmt_channel;
  110. long fmt_samples_per_sec;
  111. long fmt_bytes_per_sec;
  112. short fmt_block_size;
  113. short fmt_bits_per_sample;
  114. char data_chunk_ID[4];
  115. long data_chunk_size;
  116. short data;
  117.  
  118. fp = fopen(file_name, "rb");
  119.  
  120. fread(riff_chunk_ID, 1, 4, fp);
  121. fread(&riff_chunk_size, 4, 1, fp);
  122. fread(riff_form_type, 1, 4, fp);
  123. fread(fmt_chunk_ID, 1, 4, fp);
  124. fread(&fmt_chunk_size, 4, 1, fp);
  125. fread(&fmt_wave_format_type, 2, 1, fp);
  126. fread(&fmt_channel, 2, 1, fp);
  127. fread(&fmt_samples_per_sec, 4, 1, fp);
  128. fread(&fmt_bytes_per_sec, 4, 1, fp);
  129. fread(&fmt_block_size, 2, 1, fp);
  130. fread(&fmt_bits_per_sample, 2, 1, fp);
  131. fread(data_chunk_ID, 1, 4, fp);
  132. fread(&data_chunk_size, 4, 1, fp);
  133.  
  134. fs = fmt_samples_per_sec; /* 標本化周波数 */
  135. bits = fmt_bits_per_sample; /* 量子化精度 */
  136. s.resize(data_chunk_size / 2); /* 音データの長さ */
  137. for (size_t n = 0; n < s.size() ; n++){
  138. fread(&data, 2, 1, fp); /* 音データの読み取り */
  139. s[n] = data;
  140. }
  141.  
  142. fclose(fp);
  143. return 0;
  144. }
  145. };
  146. #endif
  147.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/usr/lib/gcc/i586-linux-gnu/5/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty