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