annotate src/opus-1.3/doc/trivial_example.c @ 169:223a55898ab9 tip default

Add null config files
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 02 Mar 2020 14:03:47 +0000
parents 4664ac0c1032
children
rev   line source
cannam@154 1 /* Copyright (c) 2013 Jean-Marc Valin */
cannam@154 2 /*
cannam@154 3 Redistribution and use in source and binary forms, with or without
cannam@154 4 modification, are permitted provided that the following conditions
cannam@154 5 are met:
cannam@154 6
cannam@154 7 - Redistributions of source code must retain the above copyright
cannam@154 8 notice, this list of conditions and the following disclaimer.
cannam@154 9
cannam@154 10 - Redistributions in binary form must reproduce the above copyright
cannam@154 11 notice, this list of conditions and the following disclaimer in the
cannam@154 12 documentation and/or other materials provided with the distribution.
cannam@154 13
cannam@154 14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
cannam@154 15 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
cannam@154 16 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
cannam@154 17 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
cannam@154 18 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
cannam@154 19 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
cannam@154 20 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
cannam@154 21 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
cannam@154 22 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
cannam@154 23 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
cannam@154 24 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
cannam@154 25 */
cannam@154 26
cannam@154 27 /* This is meant to be a simple example of encoding and decoding audio
cannam@154 28 using Opus. It should make it easy to understand how the Opus API
cannam@154 29 works. For more information, see the full API documentation at:
cannam@154 30 https://www.opus-codec.org/docs/ */
cannam@154 31
cannam@154 32 #include <stdlib.h>
cannam@154 33 #include <errno.h>
cannam@154 34 #include <string.h>
cannam@154 35 #include <opus.h>
cannam@154 36 #include <stdio.h>
cannam@154 37
cannam@154 38 /*The frame size is hardcoded for this sample code but it doesn't have to be*/
cannam@154 39 #define FRAME_SIZE 960
cannam@154 40 #define SAMPLE_RATE 48000
cannam@154 41 #define CHANNELS 2
cannam@154 42 #define APPLICATION OPUS_APPLICATION_AUDIO
cannam@154 43 #define BITRATE 64000
cannam@154 44
cannam@154 45 #define MAX_FRAME_SIZE 6*960
cannam@154 46 #define MAX_PACKET_SIZE (3*1276)
cannam@154 47
cannam@154 48 int main(int argc, char **argv)
cannam@154 49 {
cannam@154 50 char *inFile;
cannam@154 51 FILE *fin;
cannam@154 52 char *outFile;
cannam@154 53 FILE *fout;
cannam@154 54 opus_int16 in[FRAME_SIZE*CHANNELS];
cannam@154 55 opus_int16 out[MAX_FRAME_SIZE*CHANNELS];
cannam@154 56 unsigned char cbits[MAX_PACKET_SIZE];
cannam@154 57 int nbBytes;
cannam@154 58 /*Holds the state of the encoder and decoder */
cannam@154 59 OpusEncoder *encoder;
cannam@154 60 OpusDecoder *decoder;
cannam@154 61 int err;
cannam@154 62
cannam@154 63 if (argc != 3)
cannam@154 64 {
cannam@154 65 fprintf(stderr, "usage: trivial_example input.pcm output.pcm\n");
cannam@154 66 fprintf(stderr, "input and output are 16-bit little-endian raw files\n");
cannam@154 67 return EXIT_FAILURE;
cannam@154 68 }
cannam@154 69
cannam@154 70 /*Create a new encoder state */
cannam@154 71 encoder = opus_encoder_create(SAMPLE_RATE, CHANNELS, APPLICATION, &err);
cannam@154 72 if (err<0)
cannam@154 73 {
cannam@154 74 fprintf(stderr, "failed to create an encoder: %s\n", opus_strerror(err));
cannam@154 75 return EXIT_FAILURE;
cannam@154 76 }
cannam@154 77 /* Set the desired bit-rate. You can also set other parameters if needed.
cannam@154 78 The Opus library is designed to have good defaults, so only set
cannam@154 79 parameters you know you need. Doing otherwise is likely to result
cannam@154 80 in worse quality, but better. */
cannam@154 81 err = opus_encoder_ctl(encoder, OPUS_SET_BITRATE(BITRATE));
cannam@154 82 if (err<0)
cannam@154 83 {
cannam@154 84 fprintf(stderr, "failed to set bitrate: %s\n", opus_strerror(err));
cannam@154 85 return EXIT_FAILURE;
cannam@154 86 }
cannam@154 87 inFile = argv[1];
cannam@154 88 fin = fopen(inFile, "r");
cannam@154 89 if (fin==NULL)
cannam@154 90 {
cannam@154 91 fprintf(stderr, "failed to open input file: %s\n", strerror(errno));
cannam@154 92 return EXIT_FAILURE;
cannam@154 93 }
cannam@154 94
cannam@154 95
cannam@154 96 /* Create a new decoder state. */
cannam@154 97 decoder = opus_decoder_create(SAMPLE_RATE, CHANNELS, &err);
cannam@154 98 if (err<0)
cannam@154 99 {
cannam@154 100 fprintf(stderr, "failed to create decoder: %s\n", opus_strerror(err));
cannam@154 101 return EXIT_FAILURE;
cannam@154 102 }
cannam@154 103 outFile = argv[2];
cannam@154 104 fout = fopen(outFile, "w");
cannam@154 105 if (fout==NULL)
cannam@154 106 {
cannam@154 107 fprintf(stderr, "failed to open output file: %s\n", strerror(errno));
cannam@154 108 return EXIT_FAILURE;
cannam@154 109 }
cannam@154 110
cannam@154 111 while (1)
cannam@154 112 {
cannam@154 113 int i;
cannam@154 114 unsigned char pcm_bytes[MAX_FRAME_SIZE*CHANNELS*2];
cannam@154 115 int frame_size;
cannam@154 116
cannam@154 117 /* Read a 16 bits/sample audio frame. */
cannam@154 118 fread(pcm_bytes, sizeof(short)*CHANNELS, FRAME_SIZE, fin);
cannam@154 119 if (feof(fin))
cannam@154 120 break;
cannam@154 121 /* Convert from little-endian ordering. */
cannam@154 122 for (i=0;i<CHANNELS*FRAME_SIZE;i++)
cannam@154 123 in[i]=pcm_bytes[2*i+1]<<8|pcm_bytes[2*i];
cannam@154 124
cannam@154 125 /* Encode the frame. */
cannam@154 126 nbBytes = opus_encode(encoder, in, FRAME_SIZE, cbits, MAX_PACKET_SIZE);
cannam@154 127 if (nbBytes<0)
cannam@154 128 {
cannam@154 129 fprintf(stderr, "encode failed: %s\n", opus_strerror(nbBytes));
cannam@154 130 return EXIT_FAILURE;
cannam@154 131 }
cannam@154 132
cannam@154 133
cannam@154 134 /* Decode the data. In this example, frame_size will be constant because
cannam@154 135 the encoder is using a constant frame size. However, that may not
cannam@154 136 be the case for all encoders, so the decoder must always check
cannam@154 137 the frame size returned. */
cannam@154 138 frame_size = opus_decode(decoder, cbits, nbBytes, out, MAX_FRAME_SIZE, 0);
cannam@154 139 if (frame_size<0)
cannam@154 140 {
cannam@154 141 fprintf(stderr, "decoder failed: %s\n", opus_strerror(frame_size));
cannam@154 142 return EXIT_FAILURE;
cannam@154 143 }
cannam@154 144
cannam@154 145 /* Convert to little-endian ordering. */
cannam@154 146 for(i=0;i<CHANNELS*frame_size;i++)
cannam@154 147 {
cannam@154 148 pcm_bytes[2*i]=out[i]&0xFF;
cannam@154 149 pcm_bytes[2*i+1]=(out[i]>>8)&0xFF;
cannam@154 150 }
cannam@154 151 /* Write the decoded audio to file. */
cannam@154 152 fwrite(pcm_bytes, sizeof(short), frame_size*CHANNELS, fout);
cannam@154 153 }
cannam@154 154 /*Destroy the encoder state*/
cannam@154 155 opus_encoder_destroy(encoder);
cannam@154 156 opus_decoder_destroy(decoder);
cannam@154 157 fclose(fin);
cannam@154 158 fclose(fout);
cannam@154 159 return EXIT_SUCCESS;
cannam@154 160 }