cannam@154: /* Copyright (c) 2013 Jean-Marc Valin */ cannam@154: /* cannam@154: Redistribution and use in source and binary forms, with or without cannam@154: modification, are permitted provided that the following conditions cannam@154: are met: cannam@154: cannam@154: - Redistributions of source code must retain the above copyright cannam@154: notice, this list of conditions and the following disclaimer. cannam@154: cannam@154: - Redistributions in binary form must reproduce the above copyright cannam@154: notice, this list of conditions and the following disclaimer in the cannam@154: documentation and/or other materials provided with the distribution. cannam@154: cannam@154: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS cannam@154: ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT cannam@154: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR cannam@154: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER cannam@154: OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, cannam@154: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, cannam@154: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR cannam@154: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF cannam@154: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING cannam@154: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS cannam@154: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. cannam@154: */ cannam@154: cannam@154: /* This is meant to be a simple example of encoding and decoding audio cannam@154: using Opus. It should make it easy to understand how the Opus API cannam@154: works. For more information, see the full API documentation at: cannam@154: https://www.opus-codec.org/docs/ */ cannam@154: cannam@154: #include cannam@154: #include cannam@154: #include cannam@154: #include cannam@154: #include cannam@154: cannam@154: /*The frame size is hardcoded for this sample code but it doesn't have to be*/ cannam@154: #define FRAME_SIZE 960 cannam@154: #define SAMPLE_RATE 48000 cannam@154: #define CHANNELS 2 cannam@154: #define APPLICATION OPUS_APPLICATION_AUDIO cannam@154: #define BITRATE 64000 cannam@154: cannam@154: #define MAX_FRAME_SIZE 6*960 cannam@154: #define MAX_PACKET_SIZE (3*1276) cannam@154: cannam@154: int main(int argc, char **argv) cannam@154: { cannam@154: char *inFile; cannam@154: FILE *fin; cannam@154: char *outFile; cannam@154: FILE *fout; cannam@154: opus_int16 in[FRAME_SIZE*CHANNELS]; cannam@154: opus_int16 out[MAX_FRAME_SIZE*CHANNELS]; cannam@154: unsigned char cbits[MAX_PACKET_SIZE]; cannam@154: int nbBytes; cannam@154: /*Holds the state of the encoder and decoder */ cannam@154: OpusEncoder *encoder; cannam@154: OpusDecoder *decoder; cannam@154: int err; cannam@154: cannam@154: if (argc != 3) cannam@154: { cannam@154: fprintf(stderr, "usage: trivial_example input.pcm output.pcm\n"); cannam@154: fprintf(stderr, "input and output are 16-bit little-endian raw files\n"); cannam@154: return EXIT_FAILURE; cannam@154: } cannam@154: cannam@154: /*Create a new encoder state */ cannam@154: encoder = opus_encoder_create(SAMPLE_RATE, CHANNELS, APPLICATION, &err); cannam@154: if (err<0) cannam@154: { cannam@154: fprintf(stderr, "failed to create an encoder: %s\n", opus_strerror(err)); cannam@154: return EXIT_FAILURE; cannam@154: } cannam@154: /* Set the desired bit-rate. You can also set other parameters if needed. cannam@154: The Opus library is designed to have good defaults, so only set cannam@154: parameters you know you need. Doing otherwise is likely to result cannam@154: in worse quality, but better. */ cannam@154: err = opus_encoder_ctl(encoder, OPUS_SET_BITRATE(BITRATE)); cannam@154: if (err<0) cannam@154: { cannam@154: fprintf(stderr, "failed to set bitrate: %s\n", opus_strerror(err)); cannam@154: return EXIT_FAILURE; cannam@154: } cannam@154: inFile = argv[1]; cannam@154: fin = fopen(inFile, "r"); cannam@154: if (fin==NULL) cannam@154: { cannam@154: fprintf(stderr, "failed to open input file: %s\n", strerror(errno)); cannam@154: return EXIT_FAILURE; cannam@154: } cannam@154: cannam@154: cannam@154: /* Create a new decoder state. */ cannam@154: decoder = opus_decoder_create(SAMPLE_RATE, CHANNELS, &err); cannam@154: if (err<0) cannam@154: { cannam@154: fprintf(stderr, "failed to create decoder: %s\n", opus_strerror(err)); cannam@154: return EXIT_FAILURE; cannam@154: } cannam@154: outFile = argv[2]; cannam@154: fout = fopen(outFile, "w"); cannam@154: if (fout==NULL) cannam@154: { cannam@154: fprintf(stderr, "failed to open output file: %s\n", strerror(errno)); cannam@154: return EXIT_FAILURE; cannam@154: } cannam@154: cannam@154: while (1) cannam@154: { cannam@154: int i; cannam@154: unsigned char pcm_bytes[MAX_FRAME_SIZE*CHANNELS*2]; cannam@154: int frame_size; cannam@154: cannam@154: /* Read a 16 bits/sample audio frame. */ cannam@154: fread(pcm_bytes, sizeof(short)*CHANNELS, FRAME_SIZE, fin); cannam@154: if (feof(fin)) cannam@154: break; cannam@154: /* Convert from little-endian ordering. */ cannam@154: for (i=0;i>8)&0xFF; cannam@154: } cannam@154: /* Write the decoded audio to file. */ cannam@154: fwrite(pcm_bytes, sizeof(short), frame_size*CHANNELS, fout); cannam@154: } cannam@154: /*Destroy the encoder state*/ cannam@154: opus_encoder_destroy(encoder); cannam@154: opus_decoder_destroy(decoder); cannam@154: fclose(fin); cannam@154: fclose(fout); cannam@154: return EXIT_SUCCESS; cannam@154: }