Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.xiph.org/xiph/opus.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorRalph Giles <giles@thaumas.net>2020-06-01 03:56:46 +0300
committerRalph Giles <giles@thaumas.net>2020-06-01 10:48:49 +0300
commitfefcad3797de1beaba2784bb229679b743846cdc (patch)
treeccb3771aba14b4448280e53da070c3b13672fc75 /doc
parent1168a29ecda217d5192c37ee8b993433f8c945c4 (diff)
trivial_example: Check the return value of fread().
Silence a gcc warning by checking the return value of the fread() call instead of the feof() guard. This prevents an infinite loop in the case of a read error. Otherwise, when end-of-file is reached fread() will certainly return a smaller number of elements read than requested, so both cases are handled now. Add a comment to clarify that we're dropping a partial frame on purpose to keep the code simple. Also add more braces around conditional bodies for less error-prone style. Signed-off-by: Mark Harris <mark.hsj@gmail.com>
Diffstat (limited to 'doc')
-rw-r--r--doc/trivial_example.c15
1 files changed, 13 insertions, 2 deletions
diff --git a/doc/trivial_example.c b/doc/trivial_example.c
index abeba1c2..9cf435b4 100644
--- a/doc/trivial_example.c
+++ b/doc/trivial_example.c
@@ -113,14 +113,25 @@ int main(int argc, char **argv)
int i;
unsigned char pcm_bytes[MAX_FRAME_SIZE*CHANNELS*2];
int frame_size;
+ size_t samples;
/* Read a 16 bits/sample audio frame. */
- fread(pcm_bytes, sizeof(short)*CHANNELS, FRAME_SIZE, fin);
- if (feof(fin))
+ samples = fread(pcm_bytes, sizeof(short)*CHANNELS, FRAME_SIZE, fin);
+
+ /* For simplicity, only read whole frames. In a real application,
+ * we'd pad the final partial frame with zeroes, record the exact
+ * duration, and trim the decoded audio to match.
+ */
+ if (samples != FRAME_SIZE)
+ {
break;
+ }
+
/* Convert from little-endian ordering. */
for (i=0;i<CHANNELS*FRAME_SIZE;i++)
+ {
in[i]=pcm_bytes[2*i+1]<<8|pcm_bytes[2*i];
+ }
/* Encode the frame. */
nbBytes = opus_encode(encoder, in, FRAME_SIZE, cbits, MAX_PACKET_SIZE);