1b815c7f3Sopenharmony_ci/* 2b815c7f3Sopenharmony_ci** Copyright (C) 2002-2014 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#include <math.h> 25b815c7f3Sopenharmony_ci#include <inttypes.h> 26b815c7f3Sopenharmony_ci 27b815c7f3Sopenharmony_ci#if HAVE_UNISTD_H 28b815c7f3Sopenharmony_ci#include <unistd.h> 29b815c7f3Sopenharmony_ci#else 30b815c7f3Sopenharmony_ci#include "sf_unistd.h" 31b815c7f3Sopenharmony_ci#endif 32b815c7f3Sopenharmony_ci 33b815c7f3Sopenharmony_ci#include <sndfile.h> 34b815c7f3Sopenharmony_ci 35b815c7f3Sopenharmony_ci#include "utils.h" 36b815c7f3Sopenharmony_ci 37b815c7f3Sopenharmony_ci#define BUFFER_LEN (1 << 10) 38b815c7f3Sopenharmony_ci#define LOG_BUFFER_SIZE 1024 39b815c7f3Sopenharmony_ci 40b815c7f3Sopenharmony_cistatic void raw_offset_test (const char *filename, int typeminor) ; 41b815c7f3Sopenharmony_cistatic void bad_raw_test (void) ; 42b815c7f3Sopenharmony_ci 43b815c7f3Sopenharmony_ci/* Force the start of this buffer to be double aligned. Sparc-solaris will 44b815c7f3Sopenharmony_ci** choke if its not. 45b815c7f3Sopenharmony_ci*/ 46b815c7f3Sopenharmony_cistatic short data [BUFFER_LEN] ; 47b815c7f3Sopenharmony_ci 48b815c7f3Sopenharmony_ciint 49b815c7f3Sopenharmony_cimain (void) 50b815c7f3Sopenharmony_ci{ 51b815c7f3Sopenharmony_ci raw_offset_test ("offset.raw", SF_FORMAT_PCM_16) ; 52b815c7f3Sopenharmony_ci bad_raw_test () ; 53b815c7f3Sopenharmony_ci 54b815c7f3Sopenharmony_ci return 0 ; 55b815c7f3Sopenharmony_ci} /* main */ 56b815c7f3Sopenharmony_ci 57b815c7f3Sopenharmony_ci/*============================================================================================ 58b815c7f3Sopenharmony_ci** Here are the test functions. 59b815c7f3Sopenharmony_ci*/ 60b815c7f3Sopenharmony_ci 61b815c7f3Sopenharmony_cistatic void 62b815c7f3Sopenharmony_ciraw_offset_test (const char *filename, int typeminor) 63b815c7f3Sopenharmony_ci{ SNDFILE *sndfile ; 64b815c7f3Sopenharmony_ci SF_INFO sfinfo ; 65b815c7f3Sopenharmony_ci sf_count_t start ; 66b815c7f3Sopenharmony_ci int k ; 67b815c7f3Sopenharmony_ci 68b815c7f3Sopenharmony_ci print_test_name ("raw_offset_test", filename) ; 69b815c7f3Sopenharmony_ci 70b815c7f3Sopenharmony_ci sfinfo.samplerate = 44100 ; 71b815c7f3Sopenharmony_ci sfinfo.format = SF_FORMAT_RAW | typeminor ; 72b815c7f3Sopenharmony_ci sfinfo.channels = 1 ; 73b815c7f3Sopenharmony_ci sfinfo.frames = 0 ; 74b815c7f3Sopenharmony_ci 75b815c7f3Sopenharmony_ci sndfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ; 76b815c7f3Sopenharmony_ci 77b815c7f3Sopenharmony_ci start = 0 ; 78b815c7f3Sopenharmony_ci sf_command (sndfile, SFC_FILE_TRUNCATE, &start, sizeof (start)) ; 79b815c7f3Sopenharmony_ci 80b815c7f3Sopenharmony_ci for (k = 0 ; k < BUFFER_LEN ; k++) 81b815c7f3Sopenharmony_ci data [k] = k ; 82b815c7f3Sopenharmony_ci test_write_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ; 83b815c7f3Sopenharmony_ci 84b815c7f3Sopenharmony_ci sf_close (sndfile) ; 85b815c7f3Sopenharmony_ci 86b815c7f3Sopenharmony_ci sndfile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; 87b815c7f3Sopenharmony_ci check_log_buffer_or_die (sndfile, __LINE__) ; 88b815c7f3Sopenharmony_ci 89b815c7f3Sopenharmony_ci if (ABS (BUFFER_LEN - sfinfo.frames) > 1) 90b815c7f3Sopenharmony_ci { printf ("\n\nLine %d : Incorrect sample count (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, BUFFER_LEN) ; 91b815c7f3Sopenharmony_ci dump_log_buffer (sndfile) ; 92b815c7f3Sopenharmony_ci exit (1) ; 93b815c7f3Sopenharmony_ci } ; 94b815c7f3Sopenharmony_ci 95b815c7f3Sopenharmony_ci memset (data, 0 , sizeof (data)) ; 96b815c7f3Sopenharmony_ci test_read_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ; 97b815c7f3Sopenharmony_ci for (k = 0 ; k < BUFFER_LEN ; k++) 98b815c7f3Sopenharmony_ci if (data [k] != k) 99b815c7f3Sopenharmony_ci printf ("Error : line %d\n", __LINE__) ; 100b815c7f3Sopenharmony_ci 101b815c7f3Sopenharmony_ci /* Set dataoffset to 2 bytes from beginning of file. */ 102b815c7f3Sopenharmony_ci start = 2 ; 103b815c7f3Sopenharmony_ci sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ; 104b815c7f3Sopenharmony_ci 105b815c7f3Sopenharmony_ci /* Seek to new start */ 106b815c7f3Sopenharmony_ci test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ; 107b815c7f3Sopenharmony_ci 108b815c7f3Sopenharmony_ci memset (data, 0 , sizeof (data)) ; 109b815c7f3Sopenharmony_ci test_read_short_or_die (sndfile, 0, data, BUFFER_LEN - 1, __LINE__) ; 110b815c7f3Sopenharmony_ci for (k = 0 ; k < BUFFER_LEN - 1 ; k++) 111b815c7f3Sopenharmony_ci if (data [k] != k + 1) 112b815c7f3Sopenharmony_ci { printf ("Error : line %d\n", __LINE__) ; 113b815c7f3Sopenharmony_ci exit (1) ; 114b815c7f3Sopenharmony_ci } ; 115b815c7f3Sopenharmony_ci 116b815c7f3Sopenharmony_ci /* Set dataoffset to 4 bytes from beginning of file. */ 117b815c7f3Sopenharmony_ci start = 4 ; 118b815c7f3Sopenharmony_ci sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ; 119b815c7f3Sopenharmony_ci 120b815c7f3Sopenharmony_ci /* Seek to new start */ 121b815c7f3Sopenharmony_ci test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ; 122b815c7f3Sopenharmony_ci 123b815c7f3Sopenharmony_ci memset (data, 0 , sizeof (data)) ; 124b815c7f3Sopenharmony_ci test_read_short_or_die (sndfile, 0, data, BUFFER_LEN - 2, __LINE__) ; 125b815c7f3Sopenharmony_ci for (k = 0 ; k < BUFFER_LEN - 2 ; k++) 126b815c7f3Sopenharmony_ci if (data [k] != k + 2) 127b815c7f3Sopenharmony_ci { printf ("Error : line %d\n", __LINE__) ; 128b815c7f3Sopenharmony_ci exit (1) ; 129b815c7f3Sopenharmony_ci } ; 130b815c7f3Sopenharmony_ci 131b815c7f3Sopenharmony_ci /* Set dataoffset back to 0 bytes from beginning of file. */ 132b815c7f3Sopenharmony_ci start = 0 ; 133b815c7f3Sopenharmony_ci sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ; 134b815c7f3Sopenharmony_ci 135b815c7f3Sopenharmony_ci /* Seek to new start */ 136b815c7f3Sopenharmony_ci test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ; 137b815c7f3Sopenharmony_ci 138b815c7f3Sopenharmony_ci memset (data, 0 , sizeof (data)) ; 139b815c7f3Sopenharmony_ci test_read_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ; 140b815c7f3Sopenharmony_ci for (k = 0 ; k < BUFFER_LEN ; k++) 141b815c7f3Sopenharmony_ci if (data [k] != k) 142b815c7f3Sopenharmony_ci { printf ("Error : line %d\n", __LINE__) ; 143b815c7f3Sopenharmony_ci exit (1) ; 144b815c7f3Sopenharmony_ci } ; 145b815c7f3Sopenharmony_ci 146b815c7f3Sopenharmony_ci sf_close (sndfile) ; 147b815c7f3Sopenharmony_ci unlink (filename) ; 148b815c7f3Sopenharmony_ci 149b815c7f3Sopenharmony_ci puts ("ok") ; 150b815c7f3Sopenharmony_ci} /* raw_offset_test */ 151b815c7f3Sopenharmony_ci 152b815c7f3Sopenharmony_cistatic void 153b815c7f3Sopenharmony_cibad_raw_test (void) 154b815c7f3Sopenharmony_ci{ FILE *textfile ; 155b815c7f3Sopenharmony_ci SNDFILE *file ; 156b815c7f3Sopenharmony_ci SF_INFO sfinfo ; 157b815c7f3Sopenharmony_ci const char *errorstr, *filename = "bad.raw" ; 158b815c7f3Sopenharmony_ci 159b815c7f3Sopenharmony_ci print_test_name ("bad_raw_test", filename) ; 160b815c7f3Sopenharmony_ci 161b815c7f3Sopenharmony_ci if ((textfile = fopen (filename, "w")) == NULL) 162b815c7f3Sopenharmony_ci { printf ("\n\nLine %d : not able to open text file for write.\n", __LINE__) ; 163b815c7f3Sopenharmony_ci exit (1) ; 164b815c7f3Sopenharmony_ci } ; 165b815c7f3Sopenharmony_ci 166b815c7f3Sopenharmony_ci fprintf (textfile, "This is not a valid file.\n") ; 167b815c7f3Sopenharmony_ci fclose (textfile) ; 168b815c7f3Sopenharmony_ci 169b815c7f3Sopenharmony_ci sfinfo.samplerate = 44100 ; 170b815c7f3Sopenharmony_ci sfinfo.format = SF_FORMAT_RAW | 0xABCD ; 171b815c7f3Sopenharmony_ci sfinfo.channels = 1 ; 172b815c7f3Sopenharmony_ci 173b815c7f3Sopenharmony_ci if ((file = sf_open (filename, SFM_READ, &sfinfo)) != NULL) 174b815c7f3Sopenharmony_ci { printf ("\n\nLine %d : Error, file should not have opened.\n", __LINE__ - 1) ; 175b815c7f3Sopenharmony_ci exit (1) ; 176b815c7f3Sopenharmony_ci } ; 177b815c7f3Sopenharmony_ci 178b815c7f3Sopenharmony_ci errorstr = sf_strerror (file) ; 179b815c7f3Sopenharmony_ci 180b815c7f3Sopenharmony_ci if (strstr (errorstr, "Bad format field in SF_INFO struct") == NULL) 181b815c7f3Sopenharmony_ci { printf ("\n\nLine %d : Error bad error string : %s.\n", __LINE__ - 1, errorstr) ; 182b815c7f3Sopenharmony_ci exit (1) ; 183b815c7f3Sopenharmony_ci } ; 184b815c7f3Sopenharmony_ci 185b815c7f3Sopenharmony_ci unlink (filename) ; 186b815c7f3Sopenharmony_ci 187b815c7f3Sopenharmony_ci puts ("ok") ; 188b815c7f3Sopenharmony_ci} /* bad_raw_test */ 189b815c7f3Sopenharmony_ci 190