Esempio n. 1
0
static void	
test_float_peak (char *str, char *filename, int typemajor)
{	SNDFILE		*file ;
	SF_INFO		sfinfo ;
	int			k, frames, count ;

	printf ("    test_float_peak : %s ... ", str) ;
	
	sfinfo.samplerate  = 44100 ;
	sfinfo.format 	   = (typemajor | SF_FORMAT_FLOAT) ;
	sfinfo.channels    = 4 ;
	sfinfo.frames     = 0 ;

	frames = BUFFER_LEN / sfinfo.channels ;
	
	/* Create some random data with a peak value of 0.66. */
	for (k = 0 ; k < BUFFER_LEN ; k++)
		data [k] = (rand () % 2000) / 3000.0 ;
		
	/* Insert some larger peaks a know locations. */
	data [4 * (frames / 8) + 0] = (frames / 8) * 0.01 ;	/* First channel */
	data [4 * (frames / 6) + 1] = (frames / 6) * 0.01 ;	/* Second channel */
	data [4 * (frames / 4) + 2] = (frames / 4) * 0.01 ;	/* Third channel */
	data [4 * (frames / 2) + 3] = (frames / 2) * 0.01 ;	/* Fourth channel */
		
	if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
	{	printf ("Line %d: sf_open_write failed with error : ", __LINE__) ;
		sf_perror (NULL) ;
		exit (1) ;
		} ;

	/*	Write the data in four passed. The data is designed so that peaks will 
	**	be written in the different calls to sf_write_double ().
	*/
	for (count = 0 ; count < 4 ; count ++)
	{	if ((k = sf_write_double (file, data + count * BUFFER_LEN / 4, BUFFER_LEN / 4)) != BUFFER_LEN / 4)
		{	printf ("Line %d: sf_write_double # %d failed with short write (%d ->%d)\n", __LINE__, count, BUFFER_LEN / 4, k) ;
			exit (1) ;
			} ;
		} ;
	
	sf_close (file) ;
	
	if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
	{	printf ("Line %d: sf_open_read failed with error : ", __LINE__) ;
		sf_perror (NULL) ;
		exit (1) ;
		} ;
	
	if (sfinfo.format != (typemajor | SF_FORMAT_FLOAT))
	{	printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, (typemajor | SF_FORMAT_FLOAT), sfinfo.format) ;
		exit (1) ;
		} ;
	
	if (sfinfo.frames != frames)
	{	printf ("Line %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, frames, (long) sfinfo.frames) ;
		exit (1) ;
		} ;
	
	if (sfinfo.channels != 4)
	{	printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
		exit (1) ;
		} ;

	/* Get the log buffer data. */
	log_buffer [0] = 0 ;
	sf_command	(file, SFC_GET_LOG_INFO, log_buffer, LOG_BUFFER_SIZE) ;
	
	if (strlen (log_buffer) == 0)
	{	printf ("Line %d: Empty log buffer,\n", __LINE__) ;
		exit (1) ;
		} ;

	check_logged_peaks (log_buffer) ;

	sf_close (file) ;

	unlink (filename) ;
	printf ("ok\n") ;
} /* test_float_peak */
Esempio n. 2
0
static void
test_float_peak (const char *filename, int filetype)
{   SNDFILE		*file ;
    SF_INFO		sfinfo ;
    int			k, frames, count ;

    print_test_name ("test_float_peak", filename) ;

    memset (&sfinfo, 0, sizeof (sfinfo)) ;
    sfinfo.samplerate	= 44100 ;
    sfinfo.format		= filetype ;
    sfinfo.channels		= 4 ;
    sfinfo.frames		= 0 ;

    frames = BUFFER_LEN / sfinfo.channels ;

    /* Create some random data with a peak value of 0.66. */
    for (k = 0 ; k < BUFFER_LEN ; k++)
        data [k] = (rand () % 2000) / 3000.0 ;

    /* Insert some larger peaks a know locations. */
    data [4 * (frames / 8) + 0] = (frames / 8) * 0.01 ;	/* First channel */
    data [4 * (frames / 6) + 1] = (frames / 6) * 0.01 ;	/* Second channel */
    data [4 * (frames / 4) + 2] = (frames / 4) * 0.01 ;	/* Third channel */
    data [4 * (frames / 2) + 3] = (frames / 2) * 0.01 ;	/* Fourth channel */

    /* Write a file with PEAK chunks. */
    file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, 0, __LINE__) ;

    /* Try to confuse the header writer by adding a removing the PEAK chunk. */
    sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ;
    sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;
    sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ;

    /*	Write the data in four passed. The data is designed so that peaks will
    **	be written in the different calls to sf_write_double ().
    */
    for (count = 0 ; count < 4 ; count ++)
        test_write_double_or_die (file, 0, data + count * BUFFER_LEN / 4, BUFFER_LEN / 4, BUFFER_LEN / 4) ;

    sf_close (file) ;

    file = test_open_file_or_die (filename, SFM_READ, &sfinfo, 0, __LINE__) ;

    if (sfinfo.format != filetype)
    {   printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
        exit (1) ;
    } ;

    if (sfinfo.frames != frames)
    {   printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__, frames, (long) sfinfo.frames) ;
        exit (1) ;
    } ;

    if (sfinfo.channels != 4)
    {   printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
        exit (1) ;
    } ;

    /* Check these two commands. */
    if (sf_command (file, SFC_GET_SIGNAL_MAX, data, sizeof (double)) == SF_FALSE)
    {   printf ("\n\nLine %d: Command should have returned SF_TRUE.\n", __LINE__) ;
        exit (1) ;
    } ;

    if (fabs (data [0] - (frames / 2) * 0.01) > 0.01)
    {   printf ("\n\nLine %d: Bad peak value (%f should be %f) for command SFC_GET_SIGNAL_MAX.\n", __LINE__, data [0], (frames / 2) * 0.01) ;
        exit (1) ;
    } ;

    if (sf_command (file, SFC_GET_MAX_ALL_CHANNELS, data, sizeof (double) * sfinfo.channels) == SF_FALSE)
    {   printf ("\n\nLine %d: Command should have returned SF_TRUE.\n", __LINE__) ;
        exit (1) ;
    } ;

    if (fabs (data [3] - (frames / 2) * 0.01) > 0.01)
    {   printf ("\n\nLine %d: Bad peak value (%f should be %f) for command SFC_GET_MAX_ALL_CHANNELS.\n", __LINE__, data [0], (frames / 2) * 0.01) ;
        exit (1) ;
    } ;

    /* Get the log buffer data. */
    log_buffer [0] = 0 ;
    sf_command	(file, SFC_GET_LOG_INFO, log_buffer, LOG_BUFFER_SIZE) ;

    if (strlen (log_buffer) == 0)
    {   printf ("\n\nLine %d: Empty log buffer,\n", __LINE__) ;
        exit (1) ;
    } ;

    check_logged_peaks (log_buffer) ;

    sf_close (file) ;

    /* Write a file ***without*** PEAK chunks. */
    file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, 0, __LINE__) ;

    /* Try to confuse the header writer by adding a removing the PEAK chunk. */
    sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;
    sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ;
    sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;

    /*	Write the data in four passed. The data is designed so that peaks will
    **	be written in the different calls to sf_write_double ().
    */
    for (count = 0 ; count < 4 ; count ++)
        test_write_double_or_die (file, 0, data + count * BUFFER_LEN / 4, BUFFER_LEN / 4, BUFFER_LEN / 4) ;

    sf_close (file) ;

    file = test_open_file_or_die (filename, SFM_READ, &sfinfo, 0, __LINE__) ;

    /* Check these two commands. */
    if (sf_command (file, SFC_GET_SIGNAL_MAX, data, sizeof (double)))
    {   printf ("\n\nLine %d: Command should have returned SF_FALSE.\n", __LINE__) ;
        exit (1) ;
    } ;

    if (sf_command (file, SFC_GET_MAX_ALL_CHANNELS, data, sizeof (double) * sfinfo.channels))
    {   printf ("\n\nLine %d: Command should have returned SF_FALSE.\n", __LINE__) ;
        exit (1) ;
    } ;

    /* Get the log buffer data. */
    log_buffer [0] = 0 ;
    sf_command	(file, SFC_GET_LOG_INFO, log_buffer, LOG_BUFFER_SIZE) ;

    if (strlen (log_buffer) == 0)
    {   printf ("\n\nLine %d: Empty log buffer,\n", __LINE__) ;
        exit (1) ;
    } ;

    if (strstr (log_buffer, "PEAK :") != NULL)
    {   printf ("\n\nLine %d: Should not have a PEAK chunk in this file.\n\n", __LINE__) ;
        puts (log_buffer) ;
        exit (1) ;
    } ;

    sf_close (file) ;

    unlink (filename) ;
    printf ("ok\n") ;
} /* test_float_peak */