You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

346 lines
10.0KB

  1. // Convert a set of WAV audio files to C data arrays for the Teensy3 Audio Library
  2. // Copyright 2014, Paul Stoffregen (paul@pjrc.com)
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. // compile with: gcc -O2 -Wall -o wav2sketch wav2sketch.c
  22. // i686-w64-mingw32-gcc -s -O2 -Wall wav2sketch.c -o wav2sketch.exe
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <stdint.h>
  26. #include <stdarg.h>
  27. #include <string.h>
  28. #include <ctype.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <dirent.h>
  32. uint8_t ulaw_encode(int16_t audio);
  33. void print_byte(FILE *out, uint8_t b);
  34. void filename2samplename(void);
  35. uint32_t padding(uint32_t length, uint32_t block);
  36. uint8_t read_uint8(FILE *in);
  37. int16_t read_int16(FILE *in);
  38. uint32_t read_uint32(FILE *in);
  39. void die(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
  40. // WAV file format:
  41. // https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
  42. const char *filename="";
  43. char samplename[64];
  44. unsigned int bcount, wcount;
  45. unsigned int total_length=0;
  46. int pcm_mode=0;
  47. void wav2c(FILE *in, FILE *out, FILE *outh)
  48. {
  49. uint32_t header[5];
  50. int16_t format, channels, bits;
  51. uint32_t rate;
  52. uint32_t i, length, padlength=0, arraylen;
  53. int32_t audio=0;
  54. // read the WAV file's header
  55. for (i=0; i<5; i++) {
  56. header[i] = read_uint32(in);
  57. }
  58. if (header[0] != 0x46464952 || header[2] != 0x45564157
  59. || header[3] != 0x20746D66 || header[4] != 0x00000010) {
  60. die("error in format of file %s", filename);
  61. }
  62. // read the audio format parameters
  63. format = read_int16(in);
  64. channels = read_int16(in);
  65. rate = read_uint32(in);
  66. read_uint32(in); // ignore byterate
  67. read_int16(in); // ignore blockalign
  68. bits = read_int16(in);
  69. //printf("format: %d, channels: %d, rate: %d, bits %d\n", format, channels, rate, bits);
  70. if (format != 1)
  71. die("file %s is compressed, only uncompressed supported", filename);
  72. if (rate != 44100 && rate != 22050 && rate != 11025 /*&& rate != 8000*/ )
  73. die("sample rate %d in %s is unsupported\n"
  74. "Only 44100, 22050, 11025 work", rate, filename);
  75. if (channels != 1 && channels != 2)
  76. die("file %s has %d channels, but only 1 & 2 are supported", filename, channels);
  77. if (bits != 16)
  78. die("file %s has %d bit format, but only 16 is supported", filename, bits);
  79. // read the data header, skip non-audio data
  80. while (1) {
  81. header[0] = read_uint32(in);
  82. length = read_uint32(in);
  83. if (header[0] == 0x61746164) break; // beginning of actual audio data
  84. // skip over non-audio data
  85. for (i=0; i < length; i++) {
  86. read_uint8(in);
  87. }
  88. }
  89. // the length must be a multiple of the data size
  90. if (channels == 2) {
  91. if (length % 4) die("file %s data length is not a multiple of 4", filename);
  92. length = length / 4;
  93. }
  94. if (channels == 1) {
  95. if (length % 1) die("file %s data length is not a multiple of 2", filename);
  96. length = length / 2;
  97. }
  98. if (length > 0xFFFFFF) die("file %s data length is too long", filename);
  99. bcount = 0;
  100. // AudioPlayMemory requires padding to 2.9 ms boundary (128 samples @ 44100)
  101. if (rate == 44100) {
  102. padlength = padding(length, 128);
  103. format = 1;
  104. } else if (rate == 22050) {
  105. padlength = padding(length, 64);
  106. format = 2;
  107. } else if (rate == 11025) {
  108. padlength = padding(length, 32);
  109. format = 3;
  110. }
  111. if (pcm_mode) {
  112. arraylen = ((length + padlength) * 2 + 3) / 4 + 1;
  113. format |= 0x80;
  114. } else {
  115. arraylen = (length + padlength + 3) / 4 + 1;
  116. }
  117. total_length += arraylen;
  118. // output a minimal header, just the length, #bits and sample rate
  119. fprintf(outh, "extern const unsigned int AudioSample%s[%d];\n", samplename, arraylen);
  120. fprintf(out, "// Converted from %s, using %d Hz, %s encoding\n", filename, rate,
  121. (pcm_mode ? "16 bit PCM" : "u-law"));
  122. fprintf(out, "const unsigned int AudioSample%s[%d] = {\n", samplename, arraylen);
  123. fprintf(out, "0x%08X,", length | (format << 24));
  124. wcount = 1;
  125. // finally, read the audio data
  126. while (length > 0) {
  127. if (channels == 1) {
  128. audio = read_int16(in);
  129. } else {
  130. audio = read_int16(in);
  131. audio += read_int16(in);
  132. audio /= 2;
  133. }
  134. if (pcm_mode) {
  135. print_byte(out, audio);
  136. print_byte(out, audio >> 8);
  137. } else {
  138. print_byte(out, ulaw_encode(audio));
  139. }
  140. length--;
  141. }
  142. while (padlength > 0) {
  143. print_byte(out, 0);
  144. padlength--;
  145. }
  146. while (bcount > 0) {
  147. print_byte(out, 0);
  148. }
  149. if (wcount > 0) fprintf(out, "\n");
  150. fprintf(out, "};\n");
  151. }
  152. uint8_t ulaw_encode(int16_t audio)
  153. {
  154. uint32_t mag, neg;
  155. // http://en.wikipedia.org/wiki/G.711
  156. if (audio >= 0) {
  157. mag = audio;
  158. neg = 0;
  159. } else {
  160. mag = audio * -1;
  161. neg = 0x80;
  162. }
  163. mag += 128;
  164. if (mag > 0x7FFF) mag = 0x7FFF;
  165. if (mag >= 0x4000) return neg | 0x70 | ((mag >> 10) & 0x0F); // 01wx yz00 0000 0000
  166. if (mag >= 0x2000) return neg | 0x60 | ((mag >> 9) & 0x0F); // 001w xyz0 0000 0000
  167. if (mag >= 0x1000) return neg | 0x50 | ((mag >> 8) & 0x0F); // 0001 wxyz 0000 0000
  168. if (mag >= 0x0800) return neg | 0x40 | ((mag >> 7) & 0x0F); // 0000 1wxy z000 0000
  169. if (mag >= 0x0400) return neg | 0x30 | ((mag >> 6) & 0x0F); // 0000 01wx yz00 0000
  170. if (mag >= 0x0200) return neg | 0x20 | ((mag >> 5) & 0x0F); // 0000 001w xyz0 0000
  171. if (mag >= 0x0100) return neg | 0x10 | ((mag >> 4) & 0x0F); // 0000 0001 wxyz 0000
  172. return neg | 0x00 | ((mag >> 3) & 0x0F); // 0000 0000 1wxy z000
  173. }
  174. // compute the extra padding needed
  175. uint32_t padding(uint32_t length, uint32_t block)
  176. {
  177. uint32_t extra;
  178. extra = length % block;
  179. if (extra == 0) return 0;
  180. return block - extra;
  181. }
  182. // pack the output bytes into 32 bit words, lsb first, and
  183. // format the data nicely with commas and newlines
  184. void print_byte(FILE *out, uint8_t b)
  185. {
  186. static uint32_t buf32=0;
  187. buf32 |= (b << (8 * bcount++));
  188. if (bcount >= 4) {
  189. fprintf(out, "0x%08X,", buf32);
  190. buf32 = 0;
  191. bcount = 0;
  192. if (++wcount >= 8) {
  193. fprintf(out, "\n");
  194. wcount = 0;
  195. }
  196. }
  197. }
  198. // convert the WAV filename into a C-compatible name
  199. void filename2samplename(void)
  200. {
  201. int len, i, n;
  202. char c;
  203. len = strlen(filename) - 4;
  204. if (len >= sizeof(samplename)-1) len = sizeof(samplename)-1;
  205. for (i=0, n=0; n < len; i++) {
  206. c = filename[i];
  207. if (isalpha(c) || c == '_' || (isdigit(c) && n > 0)) {
  208. samplename[n] = (n == 0) ? toupper(c) : tolower(c);
  209. n++;
  210. }
  211. }
  212. samplename[n] = 0;
  213. }
  214. const char *title = "// Audio data converted from WAV file by wav2sketch\n\n";
  215. int main(int argc, char **argv)
  216. {
  217. DIR *dir;
  218. struct dirent *f;
  219. struct stat s;
  220. FILE *fp, *outc=NULL, *outh=NULL;
  221. char buf[128];
  222. int i, len;
  223. // By default, audio is u-law encoded to reduce the memory requirement
  224. // in half. However, u-law does add distortion. If "-16" is specified
  225. // on the command line, the original 16 bit PCM samples are used.
  226. for (i=1; i < argc; i++) {
  227. if (strcmp(argv[i], "-16") == 0) pcm_mode = 1;
  228. }
  229. dir = opendir(".");
  230. if (!dir) die("unable to open directory");
  231. while (1) {
  232. f = readdir(dir);
  233. if (!f) break;
  234. //if ((f->d_type & DT_DIR)) continue; // skip directories
  235. //if (!(f->d_type & DT_REG)) continue; // skip special files
  236. if (stat(f->d_name, &s) < 0) continue; // skip if unable to stat
  237. if (S_ISDIR(s.st_mode)) continue; // skip directories
  238. if (!S_ISREG(s.st_mode)) continue; // skip special files
  239. filename = f->d_name;
  240. len = strlen(filename);
  241. if (len < 5) continue;
  242. if (strcasecmp(filename + len - 4, ".wav") != 0) continue;
  243. fp = fopen(filename, "rb");
  244. if (!fp) die("unable to read file %s", filename);
  245. filename2samplename();
  246. printf("converting: %s --> AudioSample%s\n", filename, samplename);
  247. snprintf(buf, sizeof(buf), "AudioSample%s.cpp", samplename);
  248. outc = fopen(buf, "w");
  249. if (outc == NULL) die("unable to write %s", buf);
  250. snprintf(buf, sizeof(buf), "AudioSample%s.h", samplename);
  251. outh = fopen(buf, "w");
  252. if (outh == NULL) die("unable to write %s\n", buf);
  253. fprintf(outh, "%s", title);
  254. fprintf(outc, "%s", title);
  255. fprintf(outc, "#include \"%s\"\n\n", buf);
  256. wav2c(fp, outc, outh);
  257. //wav2c(fp, stdout, stdout);
  258. fclose(outc);
  259. fclose(outh);
  260. fclose(fp);
  261. }
  262. printf("Total data size %d bytes\n", total_length * 4);
  263. return 0;
  264. }
  265. uint8_t read_uint8(FILE *in)
  266. {
  267. int c1;
  268. c1 = fgetc(in);
  269. if (c1 == EOF) die("error, end of data while reading from %s\n", filename);
  270. c1 &= 255;
  271. return c1;
  272. }
  273. int16_t read_int16(FILE *in)
  274. {
  275. int c1, c2;
  276. c1 = fgetc(in);
  277. if (c1 == EOF) die("error, end of data while reading from %s\n", filename);
  278. c2 = fgetc(in);
  279. if (c2 == EOF) die("error, end of data while reading from %s\n", filename);
  280. c1 &= 255;
  281. c2 &= 255;
  282. return (c2 << 8) | c1;
  283. }
  284. uint32_t read_uint32(FILE *in)
  285. {
  286. int c1, c2, c3, c4;
  287. c1 = fgetc(in);
  288. if (c1 == EOF) die("error, end of data while reading from %s\n", filename);
  289. c2 = fgetc(in);
  290. if (c2 == EOF) die("error, end of data while reading from %s\n", filename);
  291. c3 = fgetc(in);
  292. if (c3 == EOF) die("error, end of data while reading from %s\n", filename);
  293. c4 = fgetc(in);
  294. if (c4 == EOF) die("error, end of data while reading from %s\n", filename);
  295. c1 &= 255;
  296. c2 &= 255;
  297. c3 &= 255;
  298. c4 &= 255;
  299. return (c4 << 24) | (c3 << 16) | (c2 << 8) | c1;
  300. }
  301. void die(const char *format, ...)
  302. {
  303. va_list args;
  304. va_start(args, format);
  305. fprintf(stderr, "wav2sketch: ");
  306. vfprintf(stderr, format, args);
  307. fprintf(stderr, "\n");
  308. exit(1);
  309. }