#include <stdio.h>
#include <sndfile.h>

// Prints out information about a sound file.
// Written by Mark Wilde - 6/27/2003

int
main (int argc, char *argv[])
{
    SF_INFO soundInfo;          // holds the info in the header of the sound
                                // file
    SNDFILE *sndFile;           // pointer to the sound file
    SF_FORMAT_INFO format_info; // holds information about the sound file format

    int format = 0;             // the format of this file
    int errno = 0;              // an error number associated with closing the
                                // file
    int i = 0;

    if (argc <= 1)
    {
        printf ("You need to give one sound file "
                "name to extract the header information from.\n");
    }
    else
    {
        for (i = 0; i < argc - 1; i++)
        {
            if ((sndFile = sf_open (argv[i + 1], SFM_READ, &soundInfo)) == NULL)
            {
                puts ("Not able to open sound file.");  // print an error
                                                        // message
                perror (sf_strerror (sndFile)); // print the error message
                                                // libsndfile gave
                exit (EXIT_FAILURE);
            }

            format_info.format = soundInfo.format;

            if (sf_command (sndFile, SFC_GET_FORMAT_INFO,
                            &format_info, sizeof (format_info)) != 0)
            {
                puts ("There was an error identifying the format of this file.");
                perror (sf_strerror (sndFile)); // print the error message
                                                // libsndfile gave
                exit (EXIT_FAILURE);
            }

            printf ("%s ", format_info.name);
            printf ("- %d channel(s) of ", soundInfo.channels);
            printf ("%d samples ", soundInfo.frames);
            printf ("recorded at %d Hz\n", soundInfo.samplerate);

            if ((errno = sf_close (sndFile)) != 0)
            {
                perror ("Not able to close file properly.");
                perror (sf_error_number (errno));
                exit (EXIT_FAILURE);
            }
        }
    }
}