Chris@0: /* Chris@0: ** Copyright (C) 1999-2011 Erik de Castro Lopo Chris@0: ** Chris@0: ** All rights reserved. Chris@0: ** Chris@0: ** Redistribution and use in source and binary forms, with or without Chris@0: ** modification, are permitted provided that the following conditions are Chris@0: ** met: Chris@0: ** Chris@0: ** * Redistributions of source code must retain the above copyright Chris@0: ** notice, this list of conditions and the following disclaimer. Chris@0: ** * Redistributions in binary form must reproduce the above copyright Chris@0: ** notice, this list of conditions and the following disclaimer in Chris@0: ** the documentation and/or other materials provided with the Chris@0: ** distribution. Chris@0: ** * Neither the author nor the names of any contributors may be used Chris@0: ** to endorse or promote products derived from this software without Chris@0: ** specific prior written permission. Chris@0: ** Chris@0: ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS Chris@0: ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED Chris@0: ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR Chris@0: ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR Chris@0: ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, Chris@0: ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, Chris@0: ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; Chris@0: ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, Chris@0: ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR Chris@0: ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF Chris@0: ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Chris@0: */ Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: Chris@0: #include Chris@0: Chris@0: #include "common.h" Chris@0: Chris@0: #define BUFFER_LEN (1 << 16) Chris@0: Chris@0: #if (defined (WIN32) || defined (_WIN32)) Chris@0: #include Chris@0: #endif Chris@0: Chris@0: static void print_version (void) ; Chris@0: static void usage_exit (const char *progname) ; Chris@0: Chris@0: static void info_dump (const char *filename) ; Chris@0: static int instrument_dump (const char *filename) ; Chris@0: static int broadcast_dump (const char *filename) ; Chris@0: static int chanmap_dump (const char *filename) ; Chris@0: static void total_dump (void) ; Chris@0: Chris@0: static double total_seconds = 0.0 ; Chris@0: Chris@0: int Chris@0: main (int argc, char *argv []) Chris@0: { int k ; Chris@0: Chris@0: print_version () ; Chris@0: Chris@0: if (argc < 2 || strcmp (argv [1], "--help") == 0 || strcmp (argv [1], "-h") == 0) Chris@0: { usage_exit (program_name (argv [0])) ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: if (strcmp (argv [1], "-i") == 0) Chris@0: { int error = 0 ; Chris@0: Chris@0: for (k = 2 ; k < argc ; k++) Chris@0: error += instrument_dump (argv [k]) ; Chris@0: return error ; Chris@0: } ; Chris@0: Chris@0: if (strcmp (argv [1], "-b") == 0) Chris@0: { int error = 0 ; Chris@0: Chris@0: for (k = 2 ; k < argc ; k++) Chris@0: error += broadcast_dump (argv [k]) ; Chris@0: return error ; Chris@0: } ; Chris@0: Chris@0: if (strcmp (argv [1], "-c") == 0) Chris@0: { int error = 0 ; Chris@0: Chris@0: for (k = 2 ; k < argc ; k++) Chris@0: error += chanmap_dump (argv [k]) ; Chris@0: return error ; Chris@0: } ; Chris@0: Chris@0: for (k = 1 ; k < argc ; k++) Chris@0: info_dump (argv [k]) ; Chris@0: Chris@0: if (argc > 2) Chris@0: total_dump () ; Chris@0: Chris@0: return 0 ; Chris@0: } /* main */ Chris@0: Chris@0: /*============================================================================== Chris@0: ** Print version and usage. Chris@0: */ Chris@0: Chris@0: static double data [BUFFER_LEN] ; Chris@0: Chris@0: static void Chris@0: print_version (void) Chris@0: { char buffer [256] ; Chris@0: Chris@0: sf_command (NULL, SFC_GET_LIB_VERSION, buffer, sizeof (buffer)) ; Chris@0: printf ("\nVersion : %s\n\n", buffer) ; Chris@0: } /* print_version */ Chris@0: Chris@0: Chris@0: static void Chris@0: usage_exit (const char *progname) Chris@0: { printf ("Usage :\n %s ...\n", progname) ; Chris@0: printf (" Prints out information about one or more sound files.\n\n") ; Chris@0: printf (" %s -i \n", progname) ; Chris@0: printf (" Prints out the instrument data for the given file.\n\n") ; Chris@0: printf (" %s -b \n", progname) ; Chris@0: printf (" Prints out the broadcast WAV info for the given file.\n\n") ; Chris@0: #if (defined (_WIN32) || defined (WIN32)) Chris@0: printf ("This is a Unix style command line application which\n" Chris@0: "should be run in a MSDOS box or Command Shell window.\n\n") ; Chris@0: printf ("Sleeping for 5 seconds before exiting.\n\n") ; Chris@0: fflush (stdout) ; Chris@0: Chris@0: /* This is the officially blessed by microsoft way but I can't get Chris@0: ** it to link. Chris@0: ** Sleep (15) ; Chris@0: ** Instead, use this: Chris@0: */ Chris@0: Sleep (5 * 1000) ; Chris@0: #endif Chris@0: printf ("Using %s.\n\n", sf_version_string ()) ; Chris@0: exit (0) ; Chris@0: } /* usage_exit */ Chris@0: Chris@0: /*============================================================================== Chris@0: ** Dumping of sndfile info. Chris@0: */ Chris@0: Chris@0: static double data [BUFFER_LEN] ; Chris@0: Chris@0: static double Chris@0: get_signal_max (SNDFILE *file) Chris@0: { double max ; Chris@0: Chris@0: sf_command (file, SFC_CALC_SIGNAL_MAX, &max, sizeof (max)) ; Chris@0: Chris@0: return max ; Chris@0: } /* get_signal_max */ Chris@0: Chris@0: static double Chris@0: calc_decibels (SF_INFO * sfinfo, double max) Chris@0: { double decibels ; Chris@0: Chris@0: switch (sfinfo->format & SF_FORMAT_SUBMASK) Chris@0: { case SF_FORMAT_PCM_U8 : Chris@0: case SF_FORMAT_PCM_S8 : Chris@0: decibels = max / 0x80 ; Chris@0: break ; Chris@0: Chris@0: case SF_FORMAT_PCM_16 : Chris@0: decibels = max / 0x8000 ; Chris@0: break ; Chris@0: Chris@0: case SF_FORMAT_PCM_24 : Chris@0: decibels = max / 0x800000 ; Chris@0: break ; Chris@0: Chris@0: case SF_FORMAT_PCM_32 : Chris@0: decibels = max / 0x80000000 ; Chris@0: break ; Chris@0: Chris@0: case SF_FORMAT_FLOAT : Chris@0: case SF_FORMAT_DOUBLE : Chris@0: decibels = max / 1.0 ; Chris@0: break ; Chris@0: Chris@0: default : Chris@0: decibels = max / 0x8000 ; Chris@0: break ; Chris@0: } ; Chris@0: Chris@0: return 20.0 * log10 (decibels) ; Chris@0: } /* calc_decibels */ Chris@0: Chris@0: static const char * Chris@0: format_duration_str (double seconds) Chris@0: { static char str [128] ; Chris@0: int hrs, min ; Chris@0: double sec ; Chris@0: Chris@0: memset (str, 0, sizeof (str)) ; Chris@0: Chris@0: hrs = (int) (seconds / 3600.0) ; Chris@0: min = (int) ((seconds - (hrs * 3600.0)) / 60.0) ; Chris@0: sec = seconds - (hrs * 3600.0) - (min * 60.0) ; Chris@0: Chris@0: snprintf (str, sizeof (str) - 1, "%02d:%02d:%06.3f", hrs, min, sec) ; Chris@0: Chris@0: return str ; Chris@0: } /* format_duration_str */ Chris@0: Chris@0: static const char * Chris@0: generate_duration_str (SF_INFO *sfinfo) Chris@0: { Chris@0: double seconds ; Chris@0: Chris@0: if (sfinfo->samplerate < 1) Chris@0: return NULL ; Chris@0: Chris@0: if (sfinfo->frames / sfinfo->samplerate > 0x7FFFFFFF) Chris@0: return "unknown" ; Chris@0: Chris@0: seconds = (1.0 * sfinfo->frames) / sfinfo->samplerate ; Chris@0: Chris@0: /* Accumulate the total of all known file durations */ Chris@0: total_seconds += seconds ; Chris@0: Chris@0: return format_duration_str (seconds) ; Chris@0: } /* generate_duration_str */ Chris@0: Chris@0: static void Chris@0: info_dump (const char *filename) Chris@0: { static char strbuffer [BUFFER_LEN] ; Chris@0: SNDFILE *file ; Chris@0: SF_INFO sfinfo ; Chris@0: double signal_max, decibels ; Chris@0: Chris@0: memset (&sfinfo, 0, sizeof (sfinfo)) ; Chris@0: Chris@0: if ((file = sf_open (filename, SFM_READ, &sfinfo)) == NULL) Chris@0: { printf ("Error : Not able to open input file %s.\n", filename) ; Chris@0: fflush (stdout) ; Chris@0: memset (data, 0, sizeof (data)) ; Chris@0: sf_command (file, SFC_GET_LOG_INFO, strbuffer, BUFFER_LEN) ; Chris@0: puts (strbuffer) ; Chris@0: puts (sf_strerror (NULL)) ; Chris@0: return ; Chris@0: } ; Chris@0: Chris@0: printf ("========================================\n") ; Chris@0: sf_command (file, SFC_GET_LOG_INFO, strbuffer, BUFFER_LEN) ; Chris@0: puts (strbuffer) ; Chris@0: printf ("----------------------------------------\n") ; Chris@0: Chris@0: printf ("Sample Rate : %d\n", sfinfo.samplerate) ; Chris@0: Chris@0: if (sfinfo.frames == SF_COUNT_MAX) Chris@0: printf ("Frames : unknown\n") ; Chris@0: else Chris@0: printf ("Frames : %" PRId64 "\n", sfinfo.frames) ; Chris@0: Chris@0: printf ("Channels : %d\n", sfinfo.channels) ; Chris@0: printf ("Format : 0x%08X\n", sfinfo.format) ; Chris@0: printf ("Sections : %d\n", sfinfo.sections) ; Chris@0: printf ("Seekable : %s\n", (sfinfo.seekable ? "TRUE" : "FALSE")) ; Chris@0: printf ("Duration : %s\n", generate_duration_str (&sfinfo)) ; Chris@0: Chris@0: if (sfinfo.frames < 100 * 1024 * 1024) Chris@0: { /* Do not use sf_signal_max because it doesn't work for non-seekable files . */ Chris@0: signal_max = get_signal_max (file) ; Chris@0: decibels = calc_decibels (&sfinfo, signal_max) ; Chris@0: printf ("Signal Max : %g (%4.2f dB)\n", signal_max, decibels) ; Chris@0: } ; Chris@0: putchar ('\n') ; Chris@0: Chris@0: sf_close (file) ; Chris@0: Chris@0: } /* info_dump */ Chris@0: Chris@0: /*============================================================================== Chris@0: ** Dumping of SF_INSTRUMENT data. Chris@0: */ Chris@0: Chris@0: static const char * Chris@0: str_of_type (int mode) Chris@0: { switch (mode) Chris@0: { case SF_LOOP_NONE : return "none" ; Chris@0: case SF_LOOP_FORWARD : return "fwd " ; Chris@0: case SF_LOOP_BACKWARD : return "back" ; Chris@0: case SF_LOOP_ALTERNATING : return "alt " ; Chris@0: default : break ; Chris@0: } ; Chris@0: Chris@0: return "????" ; Chris@0: } /* str_of_mode */ Chris@0: Chris@0: static int Chris@0: instrument_dump (const char *filename) Chris@0: { SNDFILE *file ; Chris@0: SF_INFO sfinfo ; Chris@0: SF_INSTRUMENT inst ; Chris@0: int got_inst, k ; Chris@0: Chris@0: memset (&sfinfo, 0, sizeof (sfinfo)) ; Chris@0: Chris@0: if ((file = sf_open (filename, SFM_READ, &sfinfo)) == NULL) Chris@0: { printf ("Error : Not able to open input file %s.\n", filename) ; Chris@0: fflush (stdout) ; Chris@0: memset (data, 0, sizeof (data)) ; Chris@0: puts (sf_strerror (NULL)) ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: got_inst = sf_command (file, SFC_GET_INSTRUMENT, &inst, sizeof (inst)) ; Chris@0: sf_close (file) ; Chris@0: Chris@0: if (got_inst == SF_FALSE) Chris@0: { printf ("Error : File '%s' does not contain instrument data.\n\n", filename) ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: printf ("Instrument : %s\n\n", filename) ; Chris@0: printf (" Gain : %d\n", inst.gain) ; Chris@0: printf (" Base note : %d\n", inst.basenote) ; Chris@0: printf (" Velocity : %d - %d\n", (int) inst.velocity_lo, (int) inst.velocity_hi) ; Chris@0: printf (" Key : %d - %d\n", (int) inst.key_lo, (int) inst.key_hi) ; Chris@0: printf (" Loop points : %d\n", inst.loop_count) ; Chris@0: Chris@0: for (k = 0 ; k < inst.loop_count ; k++) Chris@0: printf (" %-2d Mode : %s Start : %6d End : %6d Count : %6d\n", k, str_of_type (inst.loops [k].mode), inst.loops [k].start, inst.loops [k].end, inst.loops [k].count) ; Chris@0: Chris@0: putchar ('\n') ; Chris@0: return 0 ; Chris@0: } /* instrument_dump */ Chris@0: Chris@0: static int Chris@0: broadcast_dump (const char *filename) Chris@0: { SNDFILE *file ; Chris@0: SF_INFO sfinfo ; Chris@0: SF_BROADCAST_INFO_2K bext ; Chris@0: double time_ref_sec ; Chris@0: int got_bext ; Chris@0: Chris@0: memset (&sfinfo, 0, sizeof (sfinfo)) ; Chris@0: Chris@0: if ((file = sf_open (filename, SFM_READ, &sfinfo)) == NULL) Chris@0: { printf ("Error : Not able to open input file %s.\n", filename) ; Chris@0: fflush (stdout) ; Chris@0: memset (data, 0, sizeof (data)) ; Chris@0: puts (sf_strerror (NULL)) ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: memset (&bext, 0, sizeof (SF_BROADCAST_INFO_2K)) ; Chris@0: Chris@0: got_bext = sf_command (file, SFC_GET_BROADCAST_INFO, &bext, sizeof (bext)) ; Chris@0: sf_close (file) ; Chris@0: Chris@0: if (got_bext == SF_FALSE) Chris@0: { printf ("Error : File '%s' does not contain broadcast information.\n\n", filename) ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: /* Chris@0: ** From : http://www.ebu.ch/en/technical/publications/userguides/bwf_user_guide.php Chris@0: ** Chris@0: ** Time Reference: Chris@0: ** This field is a count from midnight in samples to the first sample Chris@0: ** of the audio sequence. Chris@0: */ Chris@0: Chris@0: time_ref_sec = ((pow (2.0, 32) * bext.time_reference_high) + (1.0 * bext.time_reference_low)) / sfinfo.samplerate ; Chris@0: Chris@0: printf ("Description : %.*s\n", (int) sizeof (bext.description), bext.description) ; Chris@0: printf ("Originator : %.*s\n", (int) sizeof (bext.originator), bext.originator) ; Chris@0: printf ("Origination ref : %.*s\n", (int) sizeof (bext.originator_reference), bext.originator_reference) ; Chris@0: printf ("Origination date : %.*s\n", (int) sizeof (bext.origination_date), bext.origination_date) ; Chris@0: printf ("Origination time : %.*s\n", (int) sizeof (bext.origination_time), bext.origination_time) ; Chris@0: Chris@0: if (bext.time_reference_high == 0 && bext.time_reference_low == 0) Chris@0: printf ("Time ref : 0\n") ; Chris@0: else Chris@0: printf ("Time ref : 0x%x%08x (%.6f seconds)\n", bext.time_reference_high, bext.time_reference_low, time_ref_sec) ; Chris@0: Chris@0: printf ("BWF version : %d\n", bext.version) ; Chris@0: printf ("UMID : %.*s\n", (int) sizeof (bext.umid), bext.umid) ; Chris@0: printf ("Coding history : %.*s\n", bext.coding_history_size, bext.coding_history) ; Chris@0: Chris@0: return 0 ; Chris@0: } /* broadcast_dump */ Chris@0: Chris@0: static int Chris@0: chanmap_dump (const char *filename) Chris@0: { SNDFILE *file ; Chris@0: SF_INFO sfinfo ; Chris@0: int * channel_map ; Chris@0: int got_chanmap, k ; Chris@0: Chris@0: memset (&sfinfo, 0, sizeof (sfinfo)) ; Chris@0: Chris@0: if ((file = sf_open (filename, SFM_READ, &sfinfo)) == NULL) Chris@0: { printf ("Error : Not able to open input file %s.\n", filename) ; Chris@0: fflush (stdout) ; Chris@0: memset (data, 0, sizeof (data)) ; Chris@0: puts (sf_strerror (NULL)) ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: if ((channel_map = calloc (sfinfo.channels, sizeof (int))) == NULL) Chris@0: { printf ("Error : malloc failed.\n\n") ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: got_chanmap = sf_command (file, SFC_GET_CHANNEL_MAP_INFO, channel_map, sfinfo.channels * sizeof (int)) ; Chris@0: sf_close (file) ; Chris@0: Chris@0: if (got_chanmap == SF_FALSE) Chris@0: { printf ("Error : File '%s' does not contain channel map information.\n\n", filename) ; Chris@0: free (channel_map) ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: printf ("File : %s\n\n", filename) ; Chris@0: Chris@0: puts (" Chan Position") ; Chris@0: for (k = 0 ; k < sfinfo.channels ; k ++) Chris@0: { const char * name ; Chris@0: Chris@0: #define CASE_NAME(x) case x : name = #x ; break ; Chris@0: switch (channel_map [k]) Chris@0: { CASE_NAME (SF_CHANNEL_MAP_INVALID) ; Chris@0: CASE_NAME (SF_CHANNEL_MAP_MONO) ; Chris@0: CASE_NAME (SF_CHANNEL_MAP_LEFT) ; Chris@0: CASE_NAME (SF_CHANNEL_MAP_RIGHT) ; Chris@0: CASE_NAME (SF_CHANNEL_MAP_CENTER) ; Chris@0: CASE_NAME (SF_CHANNEL_MAP_FRONT_LEFT) ; Chris@0: CASE_NAME (SF_CHANNEL_MAP_FRONT_RIGHT) ; Chris@0: CASE_NAME (SF_CHANNEL_MAP_FRONT_CENTER) ; Chris@0: CASE_NAME (SF_CHANNEL_MAP_REAR_CENTER) ; Chris@0: CASE_NAME (SF_CHANNEL_MAP_REAR_LEFT) ; Chris@0: CASE_NAME (SF_CHANNEL_MAP_REAR_RIGHT) ; Chris@0: CASE_NAME (SF_CHANNEL_MAP_LFE) ; Chris@0: CASE_NAME (SF_CHANNEL_MAP_FRONT_LEFT_OF_CENTER) ; Chris@0: CASE_NAME (SF_CHANNEL_MAP_FRONT_RIGHT_OF_CENTER) ; Chris@0: CASE_NAME (SF_CHANNEL_MAP_SIDE_LEFT) ; Chris@0: CASE_NAME (SF_CHANNEL_MAP_SIDE_RIGHT) ; Chris@0: CASE_NAME (SF_CHANNEL_MAP_TOP_CENTER) ; Chris@0: CASE_NAME (SF_CHANNEL_MAP_TOP_FRONT_LEFT) ; Chris@0: CASE_NAME (SF_CHANNEL_MAP_TOP_FRONT_RIGHT) ; Chris@0: CASE_NAME (SF_CHANNEL_MAP_TOP_FRONT_CENTER) ; Chris@0: CASE_NAME (SF_CHANNEL_MAP_TOP_REAR_LEFT) ; Chris@0: CASE_NAME (SF_CHANNEL_MAP_TOP_REAR_RIGHT) ; Chris@0: CASE_NAME (SF_CHANNEL_MAP_TOP_REAR_CENTER) ; Chris@0: CASE_NAME (SF_CHANNEL_MAP_MAX) ; Chris@0: default : name = "default" ; Chris@0: break ; Chris@0: } ; Chris@0: Chris@0: printf (" %3d %s\n", k, name) ; Chris@0: } ; Chris@0: Chris@0: putchar ('\n') ; Chris@0: free (channel_map) ; Chris@0: Chris@0: return 0 ; Chris@0: } /* chanmap_dump */ Chris@0: Chris@0: static void Chris@0: total_dump (void) Chris@0: { printf ("========================================\n") ; Chris@0: printf ("Total Duration : %s\n", format_duration_str (total_seconds)) ; Chris@0: } /* total_dump */