From 1168a29ecda217d5192c37ee8b993433f8c945c4 Mon Sep 17 00:00:00 2001 From: Ralph Giles Date: Sat, 30 May 2020 21:17:36 -0700 Subject: [PATCH 1/3] trivial_example: open raw pcm files in binary mode. The simple codec round-trip example file in the doc directory opens an input and output pcm file. It was working fine on POSIX systems, but not on Windows, which treats text files differently. This is confusing in a example, so it's better to add an explicit binary flag to the fopen() calls. This does nothing on unix-like systems, but should make the example work for developers on Windows. Thanks to Wavesonics who reported this on irc. Signed-off-by: Mark Harris --- doc/trivial_example.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/trivial_example.c b/doc/trivial_example.c index 047ca0a2..abeba1c2 100644 --- a/doc/trivial_example.c +++ b/doc/trivial_example.c @@ -85,7 +85,7 @@ int main(int argc, char **argv) return EXIT_FAILURE; } inFile = argv[1]; - fin = fopen(inFile, "r"); + fin = fopen(inFile, "rb"); if (fin==NULL) { fprintf(stderr, "failed to open input file: %s\n", strerror(errno)); @@ -101,7 +101,7 @@ int main(int argc, char **argv) return EXIT_FAILURE; } outFile = argv[2]; - fout = fopen(outFile, "w"); + fout = fopen(outFile, "wb"); if (fout==NULL) { fprintf(stderr, "failed to open output file: %s\n", strerror(errno)); -- GitLab From fefcad3797de1beaba2784bb229679b743846cdc Mon Sep 17 00:00:00 2001 From: Ralph Giles Date: Sun, 31 May 2020 17:56:46 -0700 Subject: [PATCH 2/3] 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 --- doc/trivial_example.c | 15 +++++++++++++-- 1 file 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 Date: Sat, 30 May 2020 21:22:09 -0700 Subject: [PATCH 3/3] Build trivial_example by default. Add doc/trivial_example.c to the autotools build so we get some minimal verification that the code compiles. Signed-off-by: Mark Harris --- .gitignore | 1 + Makefile.am | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 31e29014..837619f9 100644 --- a/.gitignore +++ b/.gitignore @@ -38,6 +38,7 @@ opus_demo repacketizer_demo stamp-h1 test-driver +trivial_example *.sw* *.o *.lo diff --git a/Makefile.am b/Makefile.am index 470b8e70..6d579fe4 100644 --- a/Makefile.am +++ b/Makefile.am @@ -103,7 +103,8 @@ noinst_PROGRAMS = celt/tests/test_unit_cwrs32 \ tests/test_opus_decode \ tests/test_opus_encode \ tests/test_opus_padding \ - tests/test_opus_projection + tests/test_opus_projection \ + trivial_example TESTS = celt/tests/test_unit_cwrs32 \ celt/tests/test_unit_dft \ @@ -131,6 +132,9 @@ repacketizer_demo_LDADD = libopus.la $(NE10_LIBS) $(LIBM) opus_compare_SOURCES = src/opus_compare.c opus_compare_LDADD = $(LIBM) +trivial_example_SOURCES = doc/trivial_example.c +trivial_example_LDADD = libopus.la $(LIBM) + tests_test_opus_api_SOURCES = tests/test_opus_api.c tests/test_opus_common.h tests_test_opus_api_LDADD = libopus.la $(NE10_LIBS) $(LIBM) -- GitLab