1 /* ------------------------------------------------------------------
2 * Copyright (C) 2009 Martin Storsjo
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
21 #include <interf_enc.h>
24 #include "wavreader.h"
26 void usage(const char* name) {
27 fprintf(stderr, "%s [-r bitrate] [-d] in.wav out.amr\n", name);
30 enum Mode findMode(const char* str) {
48 for (i = 0; i < sizeof(modes)/sizeof(modes[0]); i++) {
49 if (modes[i].rate == rate)
51 if (closest < 0 || closestdiff > abs(modes[i].rate - rate)) {
53 closestdiff = abs(modes[i].rate - rate);
56 fprintf(stderr, "Using bitrate %d\n", modes[closest].rate);
57 return modes[closest].mode;
60 int main(int argc, char *argv[]) {
61 enum Mode mode = MR122;
63 const char *infile, *outfile;
66 int format, sampleRate, channels, bitsPerSample;
69 while ((ch = getopt(argc, argv, "r:d")) != -1) {
72 mode = findMode(optarg);
83 if (argc - optind < 2) {
87 infile = argv[optind];
88 outfile = argv[optind + 1];
90 wav = wav_read_open(infile);
92 fprintf(stderr, "Unable to open wav file %s\n", infile);
95 if (!wav_get_header(wav, &format, &channels, &sampleRate, &bitsPerSample, NULL)) {
96 fprintf(stderr, "Bad wav file %s\n", infile);
100 fprintf(stderr, "Unsupported WAV format %d\n", format);
103 if (bitsPerSample != 16) {
104 fprintf(stderr, "Unsupported WAV sample depth %d\n", bitsPerSample);
108 fprintf(stderr, "Warning, only compressing one audio channel\n");
109 if (sampleRate != 8000)
110 fprintf(stderr, "Warning, AMR-NB uses 8000 Hz sample rate (WAV file has %d Hz)\n", sampleRate);
111 inputSize = channels*2*160;
112 inputBuf = (uint8_t*) malloc(inputSize);
114 amr = Encoder_Interface_init(dtx);
115 out = fopen(outfile, "wb");
121 fwrite("#!AMR\n", 1, 6, out);
126 read = wav_read_data(wav, inputBuf, inputSize);
131 for (i = 0; i < 160; i++) {
132 const uint8_t* in = &inputBuf[2*channels*i];
133 buf[i] = in[0] | (in[1] << 8);
135 n = Encoder_Interface_Encode(amr, mode, buf, outbuf, 0);
136 fwrite(outbuf, 1, n, out);
140 Encoder_Interface_exit(amr);