Chris@0: /* Chris@0: ** Copyright (C) 1999-2011 Erik de Castro Lopo Chris@0: ** Chris@0: ** This program is free software; you can redistribute it and/or modify Chris@0: ** it under the terms of the GNU General Public License as published by Chris@0: ** the Free Software Foundation; either version 2 of the License, or Chris@0: ** (at your option) any later version. Chris@0: ** Chris@0: ** This program is distributed in the hope that it will be useful, Chris@0: ** but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: ** GNU General Public License for more details. Chris@0: ** Chris@0: ** You should have received a copy of the GNU General Public License Chris@0: ** along with this program; if not, write to the Free Software Chris@0: ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Chris@0: */ Chris@0: Chris@0: #include "sfconfig.h" Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: Chris@0: #if HAVE_UNISTD_H Chris@0: #include Chris@0: #endif Chris@0: Chris@0: #include Chris@0: Chris@0: #include "utils.h" Chris@0: Chris@0: #define BUFFER_SIZE (2000) Chris@0: Chris@0: static void old_test (void) ; Chris@0: static void headerless_test (const char * filename, int format, int expected) ; Chris@0: Chris@0: int Chris@0: main (void) Chris@0: { Chris@0: old_test () ; Chris@0: Chris@0: headerless_test ("raw.vox", SF_FORMAT_VOX_ADPCM, SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM) ; Chris@0: headerless_test ("raw.gsm", SF_FORMAT_GSM610, SF_FORMAT_RAW | SF_FORMAT_GSM610) ; Chris@0: Chris@0: headerless_test ("raw.snd", SF_FORMAT_ULAW, SF_FORMAT_RAW | SF_FORMAT_ULAW) ; Chris@0: headerless_test ("raw.au" , SF_FORMAT_ULAW, SF_FORMAT_RAW | SF_FORMAT_ULAW) ; Chris@0: Chris@0: return 0 ; Chris@0: } /* main */ Chris@0: Chris@0: static void Chris@0: headerless_test (const char * filename, int format, int expected) Chris@0: { static short buffer [BUFFER_SIZE] ; Chris@0: SNDFILE *file ; Chris@0: SF_INFO sfinfo ; Chris@0: int k ; Chris@0: Chris@0: format &= SF_FORMAT_SUBMASK ; Chris@0: Chris@0: print_test_name (__func__, filename) ; Chris@0: Chris@0: for (k = 0 ; k < BUFFER_SIZE ; k++) Chris@0: buffer [k] = k ; Chris@0: Chris@0: sfinfo.samplerate = 8000 ; Chris@0: sfinfo.frames = 0 ; Chris@0: sfinfo.channels = 1 ; Chris@0: sfinfo.format = SF_FORMAT_RAW | format ; Chris@0: Chris@0: file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; Chris@0: Chris@0: if ((k = sf_write_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE) Chris@0: { printf ("Line %d: sf_write_short failed with short write (%d => %d).\n", __LINE__, BUFFER_SIZE, k) ; Chris@0: fflush (stdout) ; Chris@0: puts (sf_strerror (file)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: sf_close (file) ; Chris@0: Chris@0: memset (buffer, 0, sizeof (buffer)) ; Chris@0: Chris@0: /* We should be able to detect these so clear sfinfo. */ Chris@0: memset (&sfinfo, 0, sizeof (sfinfo)) ; Chris@0: Chris@0: file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; Chris@0: Chris@0: if (sfinfo.format != expected) Chris@0: { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, expected, sfinfo.format) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (sfinfo.frames < BUFFER_SIZE) Chris@0: { printf ("Line %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (sfinfo.channels != 1) Chris@0: { printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: check_log_buffer_or_die (file, __LINE__) ; Chris@0: Chris@0: sf_close (file) ; Chris@0: Chris@0: printf ("ok\n") ; Chris@0: unlink (filename) ; Chris@0: } /* headerless_test */ Chris@0: Chris@0: static void Chris@0: old_test (void) Chris@0: { static short buffer [BUFFER_SIZE] ; Chris@0: SNDFILE *file ; Chris@0: SF_INFO sfinfo ; Chris@0: int k, filetype ; Chris@0: const char *filename = "headerless.wav" ; Chris@0: Chris@0: print_test_name (__func__, "") ; Chris@0: Chris@0: for (k = 0 ; k < BUFFER_SIZE ; k++) Chris@0: buffer [k] = k ; Chris@0: Chris@0: filetype = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ; Chris@0: Chris@0: sfinfo.samplerate = 32000 ; Chris@0: sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */ Chris@0: sfinfo.channels = 1 ; Chris@0: sfinfo.format = filetype ; Chris@0: Chris@0: file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; Chris@0: Chris@0: if ((k = sf_write_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE) Chris@0: { printf ("Line %d: sf_write_short failed with short write (%d => %d).\n", __LINE__, BUFFER_SIZE, k) ; Chris@0: fflush (stdout) ; Chris@0: puts (sf_strerror (file)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: sf_close (file) ; Chris@0: Chris@0: memset (buffer, 0, sizeof (buffer)) ; Chris@0: Chris@0: /* Read as RAW but get the bit width and endian-ness correct. */ Chris@0: sfinfo.format = filetype = SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_PCM_16 ; Chris@0: Chris@0: file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; Chris@0: Chris@0: if (sfinfo.format != filetype) Chris@0: { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (sfinfo.frames < BUFFER_SIZE) Chris@0: { printf ("Line %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (sfinfo.channels != 1) Chris@0: { printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: check_log_buffer_or_die (file, __LINE__) ; Chris@0: Chris@0: if ((k = sf_read_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE) Chris@0: { printf ("Line %d: short read (%d).\n", __LINE__, k) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: for (k = 0 ; k < BUFFER_SIZE - 22 ; k++) Chris@0: if (buffer [k + 22] != k) Chris@0: { printf ("Line %d: Incorrect sample (#%d : 0x%x => 0x%x).\n", __LINE__, k, k, buffer [k]) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: sf_close (file) ; Chris@0: Chris@0: printf ("ok\n") ; Chris@0: unlink (filename) ; Chris@0: } /* old_test */ Chris@0: