wav_encode.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // This file is part of parrocchetto.
  2. // parrocchetto is free software: you can redistribute it and/or modify
  3. // it under the terms of the GNU General Public License as published by
  4. // the Free Software Foundation, either version 3 of the License, or
  5. // (at your option) any later version.
  6. // parrocchetto is distributed in the hope that it will be useful,
  7. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. // GNU General Public License for more details.
  10. // You should have received a copy of the GNU General Public License
  11. // along with parrocchetto. If not, see <http://www.gnu.org/licenses/>.
  12. #include <unistd.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. #include <sndfile.h>
  18. //#include <soxr.h>
  19. #include "g726.h"
  20. #include "g711.h"
  21. static const char *in_path = 0;
  22. static char out_name[1024];
  23. //static const char *out_path = 0;
  24. void parseargs(int argc, char** argv);
  25. void exit_with_usage(int code);
  26. //#define TARGET_SAMPLERATE 8042 //
  27. #define TARGET_G726_RATE 4 // (8 * TARGET_G726_RATE) Kbps
  28. int main(int argc, char *argv[])
  29. {
  30. SNDFILE *insnd;
  31. SF_INFO insnd_info;
  32. short *outpcm, *out_encoded, *out_compressed;
  33. size_t i, outpcm_len, out_encoded_len;
  34. //struct soxr_io_spec resampling_spec;
  35. G726_state encoder_state;
  36. //FILE *outfp;
  37. parseargs(argc, argv);
  38. insnd = sf_open(in_path, SFM_READ, &insnd_info);
  39. if(!insnd) {
  40. fprintf(stderr, "Unable to open input file: %s\n", sf_strerror(insnd));
  41. exit(254);
  42. }
  43. if(insnd_info.channels != 1) {
  44. fprintf(stderr, "Error: input file must be single channel.\n");
  45. sf_close(insnd);
  46. exit(253);
  47. }
  48. if((insnd_info.format & 0xffff) != SF_FORMAT_PCM_16) {
  49. fprintf(stderr, "Error: input file format must be plain signed 16-bit PCM.\n");
  50. sf_close(insnd);
  51. exit(252);
  52. }
  53. if(insnd_info.samplerate != 8000) {
  54. fprintf(stderr, "Error: input sample rate must be 8000Hz.\n");
  55. sf_close(insnd);
  56. exit(251);
  57. }
  58. // outfp = fopen(out_path, "w");
  59. // if(!outfp) {
  60. // perror("Error opening output file");
  61. // sf_close(insnd);
  62. // exit(250);
  63. // }
  64. // inpcm_len = insnd_info.frames;
  65. // outpcm_len = (size_t)((double)inpcm_len * TARGET_SAMPLERATE / insnd_info.samplerate + .5);
  66. // inpcm = (short*)malloc(sizeof(short) * inpcm_len);
  67. // outpcm = (short*)malloc(sizeof(short) * outpcm_len);
  68. //
  69. // // Read PCM data
  70. // sf_readf_short(insnd, inpcm, inpcm_len);
  71. //
  72. // // Resample
  73. // resampling_spec.itype = SOXR_INT16_I;
  74. // resampling_spec.otype = SOXR_INT16_I;
  75. // resampling_spec.scale = 1;
  76. // resampling_spec.e = 0;
  77. // resampling_spec.flags = 0;
  78. // soxr_oneshot(insnd_info.samplerate, TARGET_SAMPLERATE, 1, inpcm, inpcm_len, 0, outpcm, outpcm_len, 0, &resampling_spec, 0, 0);
  79. // free(inpcm);
  80. //
  81. // // Convert 16 to 14 bit
  82. // for(i = 0; i < outpcm_len; ++i)
  83. // outpcm[i] >>= 2;
  84. outpcm_len = insnd_info.frames;
  85. outpcm = (short*)malloc(sizeof(short) * outpcm_len);
  86. sf_readf_short(insnd, outpcm, outpcm_len);
  87. // Compress with padding
  88. out_encoded_len = outpcm_len & ~0x7f;
  89. if(outpcm_len & 0x7f)
  90. out_encoded_len += 0x80;
  91. out_compressed = (short*)calloc(out_encoded_len, sizeof(short));
  92. alaw_compress(outpcm_len, outpcm, out_compressed);
  93. free(outpcm);
  94. // Encode
  95. out_encoded = (short*)calloc(out_encoded_len, sizeof(short));
  96. G726_encode(out_compressed, out_encoded, out_encoded_len, "1", TARGET_G726_RATE, 1, &encoder_state);
  97. free(out_compressed);
  98. // Pack and save (only 32Kbps right now)
  99. printf("const unsigned %s[] = {", out_name);
  100. if(out_encoded_len > 3) {
  101. printf(" 0x%04x",
  102. out_encoded[0] |
  103. (out_encoded[1] << 4) |
  104. (out_encoded[2] << 8) |
  105. (out_encoded[3] << 12));
  106. for(i = 4; i < out_encoded_len; i += 4) {
  107. printf(", 0x%04x",
  108. out_encoded[i + 0] |
  109. (out_encoded[i + 1] << 4) |
  110. (out_encoded[i + 2] << 8) |
  111. (out_encoded[i + 3] << 12));
  112. }
  113. }
  114. printf(" };\n");
  115. //fclose(outfp);
  116. free(out_encoded);
  117. return 0;
  118. }
  119. void exit_with_usage(int code)
  120. {
  121. fprintf((code ? stderr : stdout), "Usage: wav_encode [-h] -o <output.h> <input.wav>\n");
  122. exit(code);
  123. }
  124. void parseargs(int argc, char** argv)
  125. {
  126. int i, opt;
  127. out_name[0] = 0;
  128. while((opt = getopt(argc, argv, "hn:")) != -1) {
  129. switch(opt) {
  130. case 'n':
  131. strcpy(optarg, out_name);
  132. break;
  133. // case 'o':
  134. // out_path = optarg;
  135. // break;
  136. case 'h':
  137. default: // '?'
  138. exit_with_usage(opt == 'h' ? 0 : 255);
  139. }
  140. }
  141. // if(!out_path) {
  142. // fprintf(stderr, "Error: missing output parameter.\n");
  143. // exit_with_usage(1);
  144. // }
  145. if(optind >= argc) {
  146. fprintf(stderr, "Error: missing input parameter.\n");
  147. exit_with_usage(2);
  148. }
  149. in_path = argv[optind++];
  150. if(!out_name[0]) {
  151. strncpy(out_name, in_path, sizeof(out_name) - 1);
  152. if(!isalpha(out_name[0]))
  153. out_name[0] = '_';
  154. for(i = 0; out_name[i]; ++i) {
  155. if(!isalnum(out_name[i]))
  156. out_name[i] = '_';
  157. }
  158. }
  159. if(optind != argc) {
  160. fprintf(stderr, "Warning: ignoring trailing garbage.\n");
  161. }
  162. }