Alexandre Lision | 7c6f4a6 | 2013-09-05 13:27:01 -0400 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com> |
| 3 | ** |
| 4 | ** This program is free software; you can redistribute it and/or modify |
| 5 | ** it under the terms of the GNU General Public License as published by |
| 6 | ** the Free Software Foundation; either version 2 of the License, or |
| 7 | ** (at your option) any later version. |
| 8 | ** |
| 9 | ** This program is distributed in the hope that it will be useful, |
| 10 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | ** GNU General Public License for more details. |
| 13 | ** |
| 14 | ** You should have received a copy of the GNU General Public License |
| 15 | ** along with this program; if not, write to the Free Software |
| 16 | ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #include "sfconfig.h" |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <math.h> |
| 25 | |
| 26 | #if HAVE_UNISTD_H |
| 27 | #include <unistd.h> |
| 28 | #endif |
| 29 | |
| 30 | #include <sndfile.h> |
| 31 | |
| 32 | #include "utils.h" |
| 33 | |
| 34 | #define BUFFER_SIZE (2000) |
| 35 | |
| 36 | static void old_test (void) ; |
| 37 | static void headerless_test (const char * filename, int format, int expected) ; |
| 38 | |
| 39 | int |
| 40 | main (void) |
| 41 | { |
| 42 | old_test () ; |
| 43 | |
| 44 | headerless_test ("raw.vox", SF_FORMAT_VOX_ADPCM, SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM) ; |
| 45 | headerless_test ("raw.gsm", SF_FORMAT_GSM610, SF_FORMAT_RAW | SF_FORMAT_GSM610) ; |
| 46 | |
| 47 | headerless_test ("raw.snd", SF_FORMAT_ULAW, SF_FORMAT_RAW | SF_FORMAT_ULAW) ; |
| 48 | headerless_test ("raw.au" , SF_FORMAT_ULAW, SF_FORMAT_RAW | SF_FORMAT_ULAW) ; |
| 49 | |
| 50 | return 0 ; |
| 51 | } /* main */ |
| 52 | |
| 53 | static void |
| 54 | headerless_test (const char * filename, int format, int expected) |
| 55 | { static short buffer [BUFFER_SIZE] ; |
| 56 | SNDFILE *file ; |
| 57 | SF_INFO sfinfo ; |
| 58 | int k ; |
| 59 | |
| 60 | format &= SF_FORMAT_SUBMASK ; |
| 61 | |
| 62 | print_test_name (__func__, filename) ; |
| 63 | |
| 64 | for (k = 0 ; k < BUFFER_SIZE ; k++) |
| 65 | buffer [k] = k ; |
| 66 | |
| 67 | sfinfo.samplerate = 8000 ; |
| 68 | sfinfo.frames = 0 ; |
| 69 | sfinfo.channels = 1 ; |
| 70 | sfinfo.format = SF_FORMAT_RAW | format ; |
| 71 | |
| 72 | file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; |
| 73 | |
| 74 | if ((k = sf_write_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE) |
| 75 | { printf ("Line %d: sf_write_short failed with short write (%d => %d).\n", __LINE__, BUFFER_SIZE, k) ; |
| 76 | fflush (stdout) ; |
| 77 | puts (sf_strerror (file)) ; |
| 78 | exit (1) ; |
| 79 | } ; |
| 80 | |
| 81 | sf_close (file) ; |
| 82 | |
| 83 | memset (buffer, 0, sizeof (buffer)) ; |
| 84 | |
| 85 | /* We should be able to detect these so clear sfinfo. */ |
| 86 | memset (&sfinfo, 0, sizeof (sfinfo)) ; |
| 87 | |
| 88 | file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; |
| 89 | |
| 90 | if (sfinfo.format != expected) |
| 91 | { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, expected, sfinfo.format) ; |
| 92 | exit (1) ; |
| 93 | } ; |
| 94 | |
| 95 | if (sfinfo.frames < BUFFER_SIZE) |
| 96 | { printf ("Line %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ; |
| 97 | exit (1) ; |
| 98 | } ; |
| 99 | |
| 100 | if (sfinfo.channels != 1) |
| 101 | { printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ; |
| 102 | exit (1) ; |
| 103 | } ; |
| 104 | |
| 105 | check_log_buffer_or_die (file, __LINE__) ; |
| 106 | |
| 107 | sf_close (file) ; |
| 108 | |
| 109 | printf ("ok\n") ; |
| 110 | unlink (filename) ; |
| 111 | } /* headerless_test */ |
| 112 | |
| 113 | static void |
| 114 | old_test (void) |
| 115 | { static short buffer [BUFFER_SIZE] ; |
| 116 | SNDFILE *file ; |
| 117 | SF_INFO sfinfo ; |
| 118 | int k, filetype ; |
| 119 | const char *filename = "headerless.wav" ; |
| 120 | |
| 121 | print_test_name (__func__, "") ; |
| 122 | |
| 123 | for (k = 0 ; k < BUFFER_SIZE ; k++) |
| 124 | buffer [k] = k ; |
| 125 | |
| 126 | filetype = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ; |
| 127 | |
| 128 | sfinfo.samplerate = 32000 ; |
| 129 | sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */ |
| 130 | sfinfo.channels = 1 ; |
| 131 | sfinfo.format = filetype ; |
| 132 | |
| 133 | file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; |
| 134 | |
| 135 | if ((k = sf_write_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE) |
| 136 | { printf ("Line %d: sf_write_short failed with short write (%d => %d).\n", __LINE__, BUFFER_SIZE, k) ; |
| 137 | fflush (stdout) ; |
| 138 | puts (sf_strerror (file)) ; |
| 139 | exit (1) ; |
| 140 | } ; |
| 141 | |
| 142 | sf_close (file) ; |
| 143 | |
| 144 | memset (buffer, 0, sizeof (buffer)) ; |
| 145 | |
| 146 | /* Read as RAW but get the bit width and endian-ness correct. */ |
| 147 | sfinfo.format = filetype = SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_PCM_16 ; |
| 148 | |
| 149 | file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; |
| 150 | |
| 151 | if (sfinfo.format != filetype) |
| 152 | { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ; |
| 153 | exit (1) ; |
| 154 | } ; |
| 155 | |
| 156 | if (sfinfo.frames < BUFFER_SIZE) |
| 157 | { printf ("Line %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ; |
| 158 | exit (1) ; |
| 159 | } ; |
| 160 | |
| 161 | if (sfinfo.channels != 1) |
| 162 | { printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ; |
| 163 | exit (1) ; |
| 164 | } ; |
| 165 | |
| 166 | check_log_buffer_or_die (file, __LINE__) ; |
| 167 | |
| 168 | if ((k = sf_read_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE) |
| 169 | { printf ("Line %d: short read (%d).\n", __LINE__, k) ; |
| 170 | exit (1) ; |
| 171 | } ; |
| 172 | |
| 173 | for (k = 0 ; k < BUFFER_SIZE - 22 ; k++) |
| 174 | if (buffer [k + 22] != k) |
| 175 | { printf ("Line %d: Incorrect sample (#%d : 0x%x => 0x%x).\n", __LINE__, k, k, buffer [k]) ; |
| 176 | exit (1) ; |
| 177 | } ; |
| 178 | |
| 179 | sf_close (file) ; |
| 180 | |
| 181 | printf ("ok\n") ; |
| 182 | unlink (filename) ; |
| 183 | } /* old_test */ |
| 184 | |