123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- // This file is part of parrocchetto.
- // parrocchetto is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- // parrocchetto is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- // You should have received a copy of the GNU General Public License
- // along with parrocchetto. If not, see <http://www.gnu.org/licenses/>.
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <sndfile.h>
- //#include <soxr.h>
- #include "g726.h"
- #include "g711.h"
- static const char *in_path = 0;
- static char out_name[1024];
- //static const char *out_path = 0;
- void parseargs(int argc, char** argv);
- void exit_with_usage(int code);
- //#define TARGET_SAMPLERATE 8042 //
- #define TARGET_G726_RATE 4 // (8 * TARGET_G726_RATE) Kbps
- int main(int argc, char *argv[])
- {
- SNDFILE *insnd;
- SF_INFO insnd_info;
- short *outpcm, *out_encoded, *out_compressed;
- size_t i, outpcm_len, out_encoded_len;
- //struct soxr_io_spec resampling_spec;
- G726_state encoder_state;
- //FILE *outfp;
- parseargs(argc, argv);
- insnd = sf_open(in_path, SFM_READ, &insnd_info);
- if(!insnd) {
- fprintf(stderr, "Unable to open input file: %s\n", sf_strerror(insnd));
- exit(254);
- }
- if(insnd_info.channels != 1) {
- fprintf(stderr, "Error: input file must be single channel.\n");
- sf_close(insnd);
- exit(253);
- }
- if((insnd_info.format & 0xffff) != SF_FORMAT_PCM_16) {
- fprintf(stderr, "Error: input file format must be plain signed 16-bit PCM.\n");
- sf_close(insnd);
- exit(252);
- }
- if(insnd_info.samplerate != 8000) {
- fprintf(stderr, "Error: input sample rate must be 8000Hz.\n");
- sf_close(insnd);
- exit(251);
- }
- // outfp = fopen(out_path, "w");
- // if(!outfp) {
- // perror("Error opening output file");
- // sf_close(insnd);
- // exit(250);
- // }
- // inpcm_len = insnd_info.frames;
- // outpcm_len = (size_t)((double)inpcm_len * TARGET_SAMPLERATE / insnd_info.samplerate + .5);
- // inpcm = (short*)malloc(sizeof(short) * inpcm_len);
- // outpcm = (short*)malloc(sizeof(short) * outpcm_len);
- //
- // // Read PCM data
- // sf_readf_short(insnd, inpcm, inpcm_len);
- //
- // // Resample
- // resampling_spec.itype = SOXR_INT16_I;
- // resampling_spec.otype = SOXR_INT16_I;
- // resampling_spec.scale = 1;
- // resampling_spec.e = 0;
- // resampling_spec.flags = 0;
- // soxr_oneshot(insnd_info.samplerate, TARGET_SAMPLERATE, 1, inpcm, inpcm_len, 0, outpcm, outpcm_len, 0, &resampling_spec, 0, 0);
- // free(inpcm);
- //
- // // Convert 16 to 14 bit
- // for(i = 0; i < outpcm_len; ++i)
- // outpcm[i] >>= 2;
- outpcm_len = insnd_info.frames;
- outpcm = (short*)malloc(sizeof(short) * outpcm_len);
- sf_readf_short(insnd, outpcm, outpcm_len);
- // Compress with padding
- out_encoded_len = outpcm_len & ~0x7f;
- if(outpcm_len & 0x7f)
- out_encoded_len += 0x80;
- out_compressed = (short*)calloc(out_encoded_len, sizeof(short));
- alaw_compress(outpcm_len, outpcm, out_compressed);
- free(outpcm);
- // Encode
- out_encoded = (short*)calloc(out_encoded_len, sizeof(short));
- G726_encode(out_compressed, out_encoded, out_encoded_len, "1", TARGET_G726_RATE, 1, &encoder_state);
- free(out_compressed);
- // Pack and save (only 32Kbps right now)
- printf("const unsigned %s[] = {", out_name);
- if(out_encoded_len > 3) {
- printf(" 0x%04x",
- out_encoded[0] |
- (out_encoded[1] << 4) |
- (out_encoded[2] << 8) |
- (out_encoded[3] << 12));
- for(i = 4; i < out_encoded_len; i += 4) {
- printf(", 0x%04x",
- out_encoded[i + 0] |
- (out_encoded[i + 1] << 4) |
- (out_encoded[i + 2] << 8) |
- (out_encoded[i + 3] << 12));
- }
- }
- printf(" };\n");
- //fclose(outfp);
- free(out_encoded);
- return 0;
- }
- void exit_with_usage(int code)
- {
- fprintf((code ? stderr : stdout), "Usage: wav_encode [-h] -o <output.h> <input.wav>\n");
- exit(code);
- }
- void parseargs(int argc, char** argv)
- {
- int i, opt;
- out_name[0] = 0;
- while((opt = getopt(argc, argv, "hn:")) != -1) {
- switch(opt) {
- case 'n':
- strcpy(optarg, out_name);
- break;
- // case 'o':
- // out_path = optarg;
- // break;
- case 'h':
- default: // '?'
- exit_with_usage(opt == 'h' ? 0 : 255);
- }
- }
- // if(!out_path) {
- // fprintf(stderr, "Error: missing output parameter.\n");
- // exit_with_usage(1);
- // }
- if(optind >= argc) {
- fprintf(stderr, "Error: missing input parameter.\n");
- exit_with_usage(2);
- }
- in_path = argv[optind++];
- if(!out_name[0]) {
- strncpy(out_name, in_path, sizeof(out_name) - 1);
- if(!isalpha(out_name[0]))
- out_name[0] = '_';
- for(i = 0; out_name[i]; ++i) {
- if(!isalnum(out_name[i]))
- out_name[i] = '_';
- }
- }
- if(optind != argc) {
- fprintf(stderr, "Warning: ignoring trailing garbage.\n");
- }
- }
|