1b815c7f3Sopenharmony_ci/* 2b815c7f3Sopenharmony_ci** Copyright (C) 2007-2018 Erik de Castro Lopo <erikd@mega-nerd.com> 3b815c7f3Sopenharmony_ci** 4b815c7f3Sopenharmony_ci** This program is free software; you can redistribute it and/or modify 5b815c7f3Sopenharmony_ci** it under the terms of the GNU General Public License as published by 6b815c7f3Sopenharmony_ci** the Free Software Foundation; either version 2 of the License, or 7b815c7f3Sopenharmony_ci** (at your option) any later version. 8b815c7f3Sopenharmony_ci** 9b815c7f3Sopenharmony_ci** This program is distributed in the hope that it will be useful, 10b815c7f3Sopenharmony_ci** but WITHOUT ANY WARRANTY; without even the implied warranty of 11b815c7f3Sopenharmony_ci** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12b815c7f3Sopenharmony_ci** GNU General Public License for more details. 13b815c7f3Sopenharmony_ci** 14b815c7f3Sopenharmony_ci** You should have received a copy of the GNU General Public License 15b815c7f3Sopenharmony_ci** along with this program; if not, write to the Free Software 16b815c7f3Sopenharmony_ci** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17b815c7f3Sopenharmony_ci*/ 18b815c7f3Sopenharmony_ci 19b815c7f3Sopenharmony_ci#include "sfconfig.h" 20b815c7f3Sopenharmony_ci 21b815c7f3Sopenharmony_ci#include <stdio.h> 22b815c7f3Sopenharmony_ci#include <stdlib.h> 23b815c7f3Sopenharmony_ci#include <string.h> 24b815c7f3Sopenharmony_ci#if HAVE_UNISTD_H 25b815c7f3Sopenharmony_ci#include <unistd.h> 26b815c7f3Sopenharmony_ci#else 27b815c7f3Sopenharmony_ci#include "sf_unistd.h" 28b815c7f3Sopenharmony_ci#endif 29b815c7f3Sopenharmony_ci 30b815c7f3Sopenharmony_ci#include <math.h> 31b815c7f3Sopenharmony_ci#include <inttypes.h> 32b815c7f3Sopenharmony_ci#include <sndfile.h> 33b815c7f3Sopenharmony_ci 34b815c7f3Sopenharmony_ci#include "utils.h" 35b815c7f3Sopenharmony_ci 36b815c7f3Sopenharmony_ci#define SAMPLE_RATE 48000 37b815c7f3Sopenharmony_ci#define DATA_LENGTH (SAMPLE_RATE / 8) 38b815c7f3Sopenharmony_ci 39b815c7f3Sopenharmony_citypedef union 40b815c7f3Sopenharmony_ci{ double d [DATA_LENGTH] ; 41b815c7f3Sopenharmony_ci float f [DATA_LENGTH] ; 42b815c7f3Sopenharmony_ci int i [DATA_LENGTH] ; 43b815c7f3Sopenharmony_ci short s [DATA_LENGTH] ; 44b815c7f3Sopenharmony_ci} BUFFER ; 45b815c7f3Sopenharmony_ci 46b815c7f3Sopenharmony_cistatic BUFFER data_out ; 47b815c7f3Sopenharmony_cistatic BUFFER data_in ; 48b815c7f3Sopenharmony_ci 49b815c7f3Sopenharmony_cistatic void 50b815c7f3Sopenharmony_ciogg_opus_short_test (void) 51b815c7f3Sopenharmony_ci{ const char * filename = "ogg_opus_short.opus" ; 52b815c7f3Sopenharmony_ci 53b815c7f3Sopenharmony_ci SNDFILE * file ; 54b815c7f3Sopenharmony_ci SF_INFO sfinfo ; 55b815c7f3Sopenharmony_ci short seek_data [10] ; 56b815c7f3Sopenharmony_ci unsigned k ; 57b815c7f3Sopenharmony_ci 58b815c7f3Sopenharmony_ci print_test_name ("ogg_opus_short_test", filename) ; 59b815c7f3Sopenharmony_ci 60b815c7f3Sopenharmony_ci /* Generate float data. */ 61b815c7f3Sopenharmony_ci gen_windowed_sine_float (data_out.f, ARRAY_LEN (data_out.f), 1.0 * 0x7F00) ; 62b815c7f3Sopenharmony_ci 63b815c7f3Sopenharmony_ci /* Convert to short. */ 64b815c7f3Sopenharmony_ci for (k = 0 ; k < ARRAY_LEN (data_out.s) ; k++) 65b815c7f3Sopenharmony_ci data_out.s [k] = (short) lrintf (data_out.f [k]) ; 66b815c7f3Sopenharmony_ci 67b815c7f3Sopenharmony_ci memset (&sfinfo, 0, sizeof (sfinfo)) ; 68b815c7f3Sopenharmony_ci 69b815c7f3Sopenharmony_ci /* Set up output file type. */ 70b815c7f3Sopenharmony_ci sfinfo.format = SF_FORMAT_OGG | SF_FORMAT_OPUS ; 71b815c7f3Sopenharmony_ci sfinfo.channels = 1 ; 72b815c7f3Sopenharmony_ci sfinfo.samplerate = SAMPLE_RATE ; 73b815c7f3Sopenharmony_ci 74b815c7f3Sopenharmony_ci /* Write the output file. */ 75b815c7f3Sopenharmony_ci file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ; 76b815c7f3Sopenharmony_ci test_write_short_or_die (file, 0, data_out.s, ARRAY_LEN (data_out.s), __LINE__) ; 77b815c7f3Sopenharmony_ci sf_close (file) ; 78b815c7f3Sopenharmony_ci 79b815c7f3Sopenharmony_ci /* Read the file in again. */ 80b815c7f3Sopenharmony_ci memset (&sfinfo, 0, sizeof (sfinfo)) ; 81b815c7f3Sopenharmony_ci 82b815c7f3Sopenharmony_ci file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; 83b815c7f3Sopenharmony_ci test_read_short_or_die (file, 0, data_in.s, ARRAY_LEN (data_in.s), __LINE__) ; 84b815c7f3Sopenharmony_ci sf_close (file) ; 85b815c7f3Sopenharmony_ci 86b815c7f3Sopenharmony_ci puts ("ok") ; 87b815c7f3Sopenharmony_ci 88b815c7f3Sopenharmony_ci /* Test seeking. */ 89b815c7f3Sopenharmony_ci print_test_name ("ogg_opus_seek_test", filename) ; 90b815c7f3Sopenharmony_ci 91b815c7f3Sopenharmony_ci file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; 92b815c7f3Sopenharmony_ci 93b815c7f3Sopenharmony_ci test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ; 94b815c7f3Sopenharmony_ci test_read_short_or_die (file, 0, seek_data, ARRAY_LEN (seek_data), __LINE__) ; 95b815c7f3Sopenharmony_ci compare_short_or_die (seek_data, data_in.s + 10, ARRAY_LEN (seek_data), __LINE__) ; 96b815c7f3Sopenharmony_ci 97b815c7f3Sopenharmony_ci /* Test seek to end of file. */ 98b815c7f3Sopenharmony_ci test_seek_or_die (file, 0, SEEK_END, sfinfo.frames, sfinfo.channels, __LINE__) ; 99b815c7f3Sopenharmony_ci 100b815c7f3Sopenharmony_ci sf_close (file) ; 101b815c7f3Sopenharmony_ci 102b815c7f3Sopenharmony_ci puts ("ok") ; 103b815c7f3Sopenharmony_ci 104b815c7f3Sopenharmony_ci unlink (filename) ; 105b815c7f3Sopenharmony_ci} /* ogg_opus_short_test */ 106b815c7f3Sopenharmony_ci 107b815c7f3Sopenharmony_cistatic void 108b815c7f3Sopenharmony_ciogg_opus_int_test (void) 109b815c7f3Sopenharmony_ci{ const char * filename = "ogg_opus_int.opus" ; 110b815c7f3Sopenharmony_ci 111b815c7f3Sopenharmony_ci SNDFILE * file ; 112b815c7f3Sopenharmony_ci SF_INFO sfinfo ; 113b815c7f3Sopenharmony_ci int seek_data [10] ; 114b815c7f3Sopenharmony_ci unsigned k ; 115b815c7f3Sopenharmony_ci 116b815c7f3Sopenharmony_ci print_test_name ("ogg_opus_int_test", filename) ; 117b815c7f3Sopenharmony_ci 118b815c7f3Sopenharmony_ci /* Generate float data. */ 119b815c7f3Sopenharmony_ci gen_windowed_sine_float (data_out.f, ARRAY_LEN (data_out.f), 1.0 * 0x7FFF0000) ; 120b815c7f3Sopenharmony_ci 121b815c7f3Sopenharmony_ci /* Convert to integer. */ 122b815c7f3Sopenharmony_ci for (k = 0 ; k < ARRAY_LEN (data_out.i) ; k++) 123b815c7f3Sopenharmony_ci data_out.i [k] = lrintf (data_out.f [k]) ; 124b815c7f3Sopenharmony_ci 125b815c7f3Sopenharmony_ci memset (&sfinfo, 0, sizeof (sfinfo)) ; 126b815c7f3Sopenharmony_ci 127b815c7f3Sopenharmony_ci /* Set up output file type. */ 128b815c7f3Sopenharmony_ci sfinfo.format = SF_FORMAT_OGG | SF_FORMAT_OPUS ; 129b815c7f3Sopenharmony_ci sfinfo.channels = 1 ; 130b815c7f3Sopenharmony_ci sfinfo.samplerate = SAMPLE_RATE ; 131b815c7f3Sopenharmony_ci 132b815c7f3Sopenharmony_ci /* Write the output file. */ 133b815c7f3Sopenharmony_ci file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ; 134b815c7f3Sopenharmony_ci test_write_int_or_die (file, 0, data_out.i, ARRAY_LEN (data_out.i), __LINE__) ; 135b815c7f3Sopenharmony_ci sf_close (file) ; 136b815c7f3Sopenharmony_ci 137b815c7f3Sopenharmony_ci /* Read the file in again. */ 138b815c7f3Sopenharmony_ci memset (&sfinfo, 0, sizeof (sfinfo)) ; 139b815c7f3Sopenharmony_ci 140b815c7f3Sopenharmony_ci file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; 141b815c7f3Sopenharmony_ci test_read_int_or_die (file, 0, data_in.i, ARRAY_LEN (data_in.i), __LINE__) ; 142b815c7f3Sopenharmony_ci sf_close (file) ; 143b815c7f3Sopenharmony_ci 144b815c7f3Sopenharmony_ci puts ("ok") ; 145b815c7f3Sopenharmony_ci 146b815c7f3Sopenharmony_ci /* Test seeking. */ 147b815c7f3Sopenharmony_ci print_test_name ("ogg_opus_seek_test", filename) ; 148b815c7f3Sopenharmony_ci 149b815c7f3Sopenharmony_ci file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; 150b815c7f3Sopenharmony_ci 151b815c7f3Sopenharmony_ci test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ; 152b815c7f3Sopenharmony_ci test_read_int_or_die (file, 0, seek_data, ARRAY_LEN (seek_data), __LINE__) ; 153b815c7f3Sopenharmony_ci compare_int_or_die (seek_data, data_in.i + 10, ARRAY_LEN (seek_data), __LINE__) ; 154b815c7f3Sopenharmony_ci 155b815c7f3Sopenharmony_ci sf_close (file) ; 156b815c7f3Sopenharmony_ci 157b815c7f3Sopenharmony_ci puts ("ok") ; 158b815c7f3Sopenharmony_ci 159b815c7f3Sopenharmony_ci unlink (filename) ; 160b815c7f3Sopenharmony_ci} /* ogg_opus_int_test */ 161b815c7f3Sopenharmony_ci 162b815c7f3Sopenharmony_cistatic void 163b815c7f3Sopenharmony_ciogg_opus_float_test (void) 164b815c7f3Sopenharmony_ci{ const char * filename = "ogg_opus_float.opus" ; 165b815c7f3Sopenharmony_ci 166b815c7f3Sopenharmony_ci SNDFILE * file ; 167b815c7f3Sopenharmony_ci SF_INFO sfinfo ; 168b815c7f3Sopenharmony_ci float seek_data [10] ; 169b815c7f3Sopenharmony_ci 170b815c7f3Sopenharmony_ci print_test_name ("ogg_opus_float_test", filename) ; 171b815c7f3Sopenharmony_ci 172b815c7f3Sopenharmony_ci gen_windowed_sine_float (data_out.f, ARRAY_LEN (data_out.f), 0.95) ; 173b815c7f3Sopenharmony_ci 174b815c7f3Sopenharmony_ci memset (&sfinfo, 0, sizeof (sfinfo)) ; 175b815c7f3Sopenharmony_ci 176b815c7f3Sopenharmony_ci /* Set up output file type. */ 177b815c7f3Sopenharmony_ci sfinfo.format = SF_FORMAT_OGG | SF_FORMAT_OPUS ; 178b815c7f3Sopenharmony_ci sfinfo.channels = 1 ; 179b815c7f3Sopenharmony_ci sfinfo.samplerate = SAMPLE_RATE ; 180b815c7f3Sopenharmony_ci 181b815c7f3Sopenharmony_ci /* Write the output file. */ 182b815c7f3Sopenharmony_ci file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ; 183b815c7f3Sopenharmony_ci test_write_float_or_die (file, 0, data_out.f, ARRAY_LEN (data_out.f), __LINE__) ; 184b815c7f3Sopenharmony_ci sf_close (file) ; 185b815c7f3Sopenharmony_ci 186b815c7f3Sopenharmony_ci /* Read the file in again. */ 187b815c7f3Sopenharmony_ci memset (&sfinfo, 0, sizeof (sfinfo)) ; 188b815c7f3Sopenharmony_ci 189b815c7f3Sopenharmony_ci file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; 190b815c7f3Sopenharmony_ci test_read_float_or_die (file, 0, data_in.f, ARRAY_LEN (data_in.f), __LINE__) ; 191b815c7f3Sopenharmony_ci sf_close (file) ; 192b815c7f3Sopenharmony_ci 193b815c7f3Sopenharmony_ci puts ("ok") ; 194b815c7f3Sopenharmony_ci 195b815c7f3Sopenharmony_ci /* Test seeking. */ 196b815c7f3Sopenharmony_ci print_test_name ("ogg_opus_seek_test", filename) ; 197b815c7f3Sopenharmony_ci 198b815c7f3Sopenharmony_ci file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; 199b815c7f3Sopenharmony_ci 200b815c7f3Sopenharmony_ci test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ; 201b815c7f3Sopenharmony_ci test_read_float_or_die (file, 0, seek_data, ARRAY_LEN (seek_data), __LINE__) ; 202b815c7f3Sopenharmony_ci compare_float_or_die (seek_data, data_in.f + 10, ARRAY_LEN (seek_data), __LINE__) ; 203b815c7f3Sopenharmony_ci 204b815c7f3Sopenharmony_ci sf_close (file) ; 205b815c7f3Sopenharmony_ci 206b815c7f3Sopenharmony_ci puts ("ok") ; 207b815c7f3Sopenharmony_ci 208b815c7f3Sopenharmony_ci unlink (filename) ; 209b815c7f3Sopenharmony_ci} /* ogg_opus_float_test */ 210b815c7f3Sopenharmony_ci 211b815c7f3Sopenharmony_cistatic void 212b815c7f3Sopenharmony_ciogg_opus_double_test (void) 213b815c7f3Sopenharmony_ci{ const char * filename = "ogg_opus_double.opus" ; 214b815c7f3Sopenharmony_ci 215b815c7f3Sopenharmony_ci SNDFILE * file ; 216b815c7f3Sopenharmony_ci SF_INFO sfinfo ; 217b815c7f3Sopenharmony_ci double seek_data [10] ; 218b815c7f3Sopenharmony_ci 219b815c7f3Sopenharmony_ci print_test_name ("ogg_opus_double_test", filename) ; 220b815c7f3Sopenharmony_ci 221b815c7f3Sopenharmony_ci gen_windowed_sine_double (data_out.d, ARRAY_LEN (data_out.d), 0.95) ; 222b815c7f3Sopenharmony_ci 223b815c7f3Sopenharmony_ci memset (&sfinfo, 0, sizeof (sfinfo)) ; 224b815c7f3Sopenharmony_ci 225b815c7f3Sopenharmony_ci /* Set up output file type. */ 226b815c7f3Sopenharmony_ci sfinfo.format = SF_FORMAT_OGG | SF_FORMAT_OPUS ; 227b815c7f3Sopenharmony_ci sfinfo.channels = 1 ; 228b815c7f3Sopenharmony_ci sfinfo.samplerate = SAMPLE_RATE ; 229b815c7f3Sopenharmony_ci 230b815c7f3Sopenharmony_ci /* Write the output file. */ 231b815c7f3Sopenharmony_ci file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ; 232b815c7f3Sopenharmony_ci test_write_double_or_die (file, 0, data_out.d, ARRAY_LEN (data_out.d), __LINE__) ; 233b815c7f3Sopenharmony_ci sf_close (file) ; 234b815c7f3Sopenharmony_ci 235b815c7f3Sopenharmony_ci /* Read the file in again. */ 236b815c7f3Sopenharmony_ci memset (&sfinfo, 0, sizeof (sfinfo)) ; 237b815c7f3Sopenharmony_ci 238b815c7f3Sopenharmony_ci file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; 239b815c7f3Sopenharmony_ci test_read_double_or_die (file, 0, data_in.d, ARRAY_LEN (data_in.d), __LINE__) ; 240b815c7f3Sopenharmony_ci sf_close (file) ; 241b815c7f3Sopenharmony_ci 242b815c7f3Sopenharmony_ci puts ("ok") ; 243b815c7f3Sopenharmony_ci 244b815c7f3Sopenharmony_ci /* Test seeking. */ 245b815c7f3Sopenharmony_ci print_test_name ("ogg_opus_seek_test", filename) ; 246b815c7f3Sopenharmony_ci 247b815c7f3Sopenharmony_ci file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; 248b815c7f3Sopenharmony_ci 249b815c7f3Sopenharmony_ci test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ; 250b815c7f3Sopenharmony_ci test_read_double_or_die (file, 0, seek_data, ARRAY_LEN (seek_data), __LINE__) ; 251b815c7f3Sopenharmony_ci compare_double_or_die (seek_data, data_in.d + 10, ARRAY_LEN (seek_data), __LINE__) ; 252b815c7f3Sopenharmony_ci 253b815c7f3Sopenharmony_ci sf_close (file) ; 254b815c7f3Sopenharmony_ci 255b815c7f3Sopenharmony_ci puts ("ok") ; 256b815c7f3Sopenharmony_ci 257b815c7f3Sopenharmony_ci unlink (filename) ; 258b815c7f3Sopenharmony_ci} /* ogg_opus_double_test */ 259b815c7f3Sopenharmony_ci 260b815c7f3Sopenharmony_ci 261b815c7f3Sopenharmony_cistatic void 262b815c7f3Sopenharmony_ciogg_opus_stereo_seek_test (const char * filename, int format) 263b815c7f3Sopenharmony_ci{ static float data [SAMPLE_RATE] ; 264b815c7f3Sopenharmony_ci static float stereo_out [SAMPLE_RATE * 2] ; 265b815c7f3Sopenharmony_ci 266b815c7f3Sopenharmony_ci SNDFILE * file ; 267b815c7f3Sopenharmony_ci SF_INFO sfinfo ; 268b815c7f3Sopenharmony_ci sf_count_t pos ; 269b815c7f3Sopenharmony_ci unsigned k ; 270b815c7f3Sopenharmony_ci 271b815c7f3Sopenharmony_ci print_test_name (__func__, filename) ; 272b815c7f3Sopenharmony_ci 273b815c7f3Sopenharmony_ci gen_windowed_sine_float (data, ARRAY_LEN (data), 0.95) ; 274b815c7f3Sopenharmony_ci for (k = 0 ; k < ARRAY_LEN (data) ; k++) 275b815c7f3Sopenharmony_ci { stereo_out [2 * k] = data [k] ; 276b815c7f3Sopenharmony_ci stereo_out [2 * k + 1] = data [ARRAY_LEN (data) - k - 1] ; 277b815c7f3Sopenharmony_ci } ; 278b815c7f3Sopenharmony_ci 279b815c7f3Sopenharmony_ci memset (&sfinfo, 0, sizeof (sfinfo)) ; 280b815c7f3Sopenharmony_ci 281b815c7f3Sopenharmony_ci /* Set up output file type. */ 282b815c7f3Sopenharmony_ci sfinfo.format = format ; 283b815c7f3Sopenharmony_ci sfinfo.channels = 2 ; 284b815c7f3Sopenharmony_ci sfinfo.samplerate = SAMPLE_RATE ; 285b815c7f3Sopenharmony_ci 286b815c7f3Sopenharmony_ci /* Write the output file. */ 287b815c7f3Sopenharmony_ci file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ; 288b815c7f3Sopenharmony_ci test_write_float_or_die (file, 0, stereo_out, ARRAY_LEN (stereo_out), __LINE__) ; 289b815c7f3Sopenharmony_ci sf_close (file) ; 290b815c7f3Sopenharmony_ci 291b815c7f3Sopenharmony_ci /* Open file in again for reading. */ 292b815c7f3Sopenharmony_ci memset (&sfinfo, 0, sizeof (sfinfo)) ; 293b815c7f3Sopenharmony_ci file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; 294b815c7f3Sopenharmony_ci 295b815c7f3Sopenharmony_ci /* Read in the whole file. */ 296b815c7f3Sopenharmony_ci test_read_float_or_die (file, 0, stereo_out, ARRAY_LEN (stereo_out), __LINE__) ; 297b815c7f3Sopenharmony_ci 298b815c7f3Sopenharmony_ci /* Now hammer seeking code. */ 299b815c7f3Sopenharmony_ci test_seek_or_die (file, 234, SEEK_SET, 234, sfinfo.channels, __LINE__) ; 300b815c7f3Sopenharmony_ci test_readf_float_or_die (file, 0, data, 10, __LINE__) ; 301b815c7f3Sopenharmony_ci compare_float_or_die (data, stereo_out + (234 * sfinfo.channels), 10, __LINE__) ; 302b815c7f3Sopenharmony_ci 303b815c7f3Sopenharmony_ci test_seek_or_die (file, 442, SEEK_SET, 442, sfinfo.channels, __LINE__) ; 304b815c7f3Sopenharmony_ci test_readf_float_or_die (file, 0, data, 10, __LINE__) ; 305b815c7f3Sopenharmony_ci compare_float_or_die (data, stereo_out + (442 * sfinfo.channels), 10, __LINE__) ; 306b815c7f3Sopenharmony_ci 307b815c7f3Sopenharmony_ci test_seek_or_die (file, 12, SEEK_CUR, 442 + 10 + 12, sfinfo.channels, __LINE__) ; 308b815c7f3Sopenharmony_ci test_readf_float_or_die (file, 0, data, 10, __LINE__) ; 309b815c7f3Sopenharmony_ci compare_float_or_die (data, stereo_out + ((442 + 10 + 12) * sfinfo.channels), 10, __LINE__) ; 310b815c7f3Sopenharmony_ci 311b815c7f3Sopenharmony_ci test_seek_or_die (file, 12, SEEK_CUR, 442 + 20 + 24, sfinfo.channels, __LINE__) ; 312b815c7f3Sopenharmony_ci test_readf_float_or_die (file, 0, data, 10, __LINE__) ; 313b815c7f3Sopenharmony_ci compare_float_or_die (data, stereo_out + ((442 + 20 + 24) * sfinfo.channels), 10, __LINE__) ; 314b815c7f3Sopenharmony_ci 315b815c7f3Sopenharmony_ci pos = 500 - sfinfo.frames ; 316b815c7f3Sopenharmony_ci test_seek_or_die (file, pos, SEEK_END, 500, sfinfo.channels, __LINE__) ; 317b815c7f3Sopenharmony_ci test_readf_float_or_die (file, 0, data, 10, __LINE__) ; 318b815c7f3Sopenharmony_ci compare_float_or_die (data, stereo_out + (500 * sfinfo.channels), 10, __LINE__) ; 319b815c7f3Sopenharmony_ci 320b815c7f3Sopenharmony_ci pos = 10 - sfinfo.frames ; 321b815c7f3Sopenharmony_ci test_seek_or_die (file, pos, SEEK_END, 10, sfinfo.channels, __LINE__) ; 322b815c7f3Sopenharmony_ci test_readf_float_or_die (file, 0, data, 10, __LINE__) ; 323b815c7f3Sopenharmony_ci compare_float_or_die (data, stereo_out + (10 * sfinfo.channels), 10, __LINE__) ; 324b815c7f3Sopenharmony_ci 325b815c7f3Sopenharmony_ci sf_close (file) ; 326b815c7f3Sopenharmony_ci 327b815c7f3Sopenharmony_ci puts ("ok") ; 328b815c7f3Sopenharmony_ci unlink (filename) ; 329b815c7f3Sopenharmony_ci} /* ogg_opus_stereo_seek_test */ 330b815c7f3Sopenharmony_ci 331b815c7f3Sopenharmony_ci 332b815c7f3Sopenharmony_cistatic void 333b815c7f3Sopenharmony_ciogg_opus_original_samplerate_test (void) 334b815c7f3Sopenharmony_ci{ const char * filename = "ogg_opus_original_samplerate.opus" ; 335b815c7f3Sopenharmony_ci 336b815c7f3Sopenharmony_ci SNDFILE * file ; 337b815c7f3Sopenharmony_ci SF_INFO sfinfo ; 338b815c7f3Sopenharmony_ci int original_samplerate = 54321 ; 339b815c7f3Sopenharmony_ci sf_count_t frames ; 340b815c7f3Sopenharmony_ci 341b815c7f3Sopenharmony_ci print_test_name ("ogg_opus_original_samplerate_test", filename) ; 342b815c7f3Sopenharmony_ci 343b815c7f3Sopenharmony_ci gen_windowed_sine_double (data_out.d, ARRAY_LEN (data_out.d), 0.95) ; 344b815c7f3Sopenharmony_ci 345b815c7f3Sopenharmony_ci memset (&sfinfo, 0, sizeof (sfinfo)) ; 346b815c7f3Sopenharmony_ci 347b815c7f3Sopenharmony_ci /* Set up output file type. */ 348b815c7f3Sopenharmony_ci sfinfo.format = SF_FORMAT_OGG | SF_FORMAT_OPUS ; 349b815c7f3Sopenharmony_ci sfinfo.channels = 1 ; 350b815c7f3Sopenharmony_ci sfinfo.samplerate = SAMPLE_RATE ; 351b815c7f3Sopenharmony_ci 352b815c7f3Sopenharmony_ci /* Write the output file. */ 353b815c7f3Sopenharmony_ci file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ; 354b815c7f3Sopenharmony_ci if (sf_command (file, SFC_SET_ORIGINAL_SAMPLERATE, &original_samplerate, sizeof (original_samplerate)) != SF_TRUE) 355b815c7f3Sopenharmony_ci { printf ("\nCommand SFC_SET_ORIGINAL_SAMPLERATE failed!\n") ; 356b815c7f3Sopenharmony_ci exit (1) ; 357b815c7f3Sopenharmony_ci } ; 358b815c7f3Sopenharmony_ci test_write_double_or_die (file, 0, data_out.d, ARRAY_LEN (data_out.d), __LINE__) ; 359b815c7f3Sopenharmony_ci if (sf_command (file, SFC_SET_ORIGINAL_SAMPLERATE, &original_samplerate, sizeof (original_samplerate)) != SF_FALSE) 360b815c7f3Sopenharmony_ci { printf ("\nCommand SFC_SET_ORIGINAL_SAMPLERATE succeeded when it should have failed!") ; 361b815c7f3Sopenharmony_ci exit (1) ; 362b815c7f3Sopenharmony_ci } ; 363b815c7f3Sopenharmony_ci sf_close (file) ; 364b815c7f3Sopenharmony_ci 365b815c7f3Sopenharmony_ci /* Read the file in again. */ 366b815c7f3Sopenharmony_ci memset (&sfinfo, 0, sizeof (sfinfo)) ; 367b815c7f3Sopenharmony_ci 368b815c7f3Sopenharmony_ci original_samplerate = 0 ; 369b815c7f3Sopenharmony_ci file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; 370b815c7f3Sopenharmony_ci if (sf_command (file, SFC_GET_ORIGINAL_SAMPLERATE, &original_samplerate, sizeof (original_samplerate)) != SF_TRUE 371b815c7f3Sopenharmony_ci || original_samplerate != 54321) 372b815c7f3Sopenharmony_ci { printf ("\nCommand SFC_GET_ORIGINAL_SAMPLERATE failed!\n") ; 373b815c7f3Sopenharmony_ci exit (1) ; 374b815c7f3Sopenharmony_ci } ; 375b815c7f3Sopenharmony_ci test_read_double_or_die (file, 0, data_in.d, 8, __LINE__) ; 376b815c7f3Sopenharmony_ci if (sf_command (file, SFC_SET_ORIGINAL_SAMPLERATE, &original_samplerate, sizeof (original_samplerate)) == SF_TRUE) 377b815c7f3Sopenharmony_ci { printf ("\nCommand SFC_SET_ORIGINAL_SAMPLERATE succeeded when it should have failed!\n") ; 378b815c7f3Sopenharmony_ci exit (1) ; 379b815c7f3Sopenharmony_ci } ; 380b815c7f3Sopenharmony_ci sf_close (file) ; 381b815c7f3Sopenharmony_ci 382b815c7f3Sopenharmony_ci /* Test changing the decoder. */ 383b815c7f3Sopenharmony_ci file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; 384b815c7f3Sopenharmony_ci frames = sfinfo.frames ; 385b815c7f3Sopenharmony_ci original_samplerate = 16000 ; 386b815c7f3Sopenharmony_ci if (sf_command (file, SFC_SET_ORIGINAL_SAMPLERATE, &original_samplerate, sizeof (original_samplerate)) != SF_TRUE) 387b815c7f3Sopenharmony_ci { printf ("\nCommand SFC_SET_ORIGINAL_SAMPLERATE failed!\n") ; 388b815c7f3Sopenharmony_ci exit (1) ; 389b815c7f3Sopenharmony_ci } ; 390b815c7f3Sopenharmony_ci if (sf_command (file, SFC_GET_CURRENT_SF_INFO, &sfinfo, sizeof (sfinfo))) 391b815c7f3Sopenharmony_ci { printf ("\nCommand SFC_GET_CURRENT_SF_INFO failed!\n") ; 392b815c7f3Sopenharmony_ci exit (1) ; 393b815c7f3Sopenharmony_ci } ; 394b815c7f3Sopenharmony_ci if (frames / (48000 / 16000) != sfinfo.frames) 395b815c7f3Sopenharmony_ci { printf ("\nIncorrect frame count! (%" PRId64 " vs %" PRId64")\n", frames / (48000 / 16000), sfinfo.frames) ; 396b815c7f3Sopenharmony_ci exit (1) ; 397b815c7f3Sopenharmony_ci } ; 398b815c7f3Sopenharmony_ci test_read_double_or_die (file, 0, data_out.d, sfinfo.frames, __LINE__) ; 399b815c7f3Sopenharmony_ci 400b815c7f3Sopenharmony_ci sf_close (file) ; 401b815c7f3Sopenharmony_ci 402b815c7f3Sopenharmony_ci puts ("ok") ; 403b815c7f3Sopenharmony_ci 404b815c7f3Sopenharmony_ci unlink (filename) ; 405b815c7f3Sopenharmony_ci} /* ogg_opus_original_samplerate_test */ 406b815c7f3Sopenharmony_ci 407b815c7f3Sopenharmony_ci 408b815c7f3Sopenharmony_ciint 409b815c7f3Sopenharmony_cimain (void) 410b815c7f3Sopenharmony_ci{ 411b815c7f3Sopenharmony_ci if (HAVE_EXTERNAL_XIPH_LIBS) 412b815c7f3Sopenharmony_ci { ogg_opus_short_test () ; 413b815c7f3Sopenharmony_ci ogg_opus_int_test () ; 414b815c7f3Sopenharmony_ci ogg_opus_float_test () ; 415b815c7f3Sopenharmony_ci ogg_opus_double_test () ; 416b815c7f3Sopenharmony_ci 417b815c7f3Sopenharmony_ci ogg_opus_stereo_seek_test ("ogg_opus_seek.opus", SF_FORMAT_OGG | SF_FORMAT_OPUS) ; 418b815c7f3Sopenharmony_ci ogg_opus_original_samplerate_test () ; 419b815c7f3Sopenharmony_ci } 420b815c7f3Sopenharmony_ci else 421b815c7f3Sopenharmony_ci puts (" No Ogg/Opus tests because Ogg/Opus support was not compiled in.") ; 422b815c7f3Sopenharmony_ci 423b815c7f3Sopenharmony_ci return 0 ; 424b815c7f3Sopenharmony_ci} /* main */ 425