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