⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 command_test.c

📁 Audacity是一款用於錄音和編輯聲音的、免費的開放源碼軟體。它可以執行於Mac OS X、Microsoft Windows、GNU/Linux和其它作業系統
💻 C
📖 第 1 页 / 共 2 页
字号:
/*** Copyright (C) 2001-2002 Erik de Castro Lopo <erikd@mega-nerd.com>**** This program is free software; you can redistribute it and/or modify** it under the terms of the GNU General Public License as published by** the Free Software Foundation; either version 2 of the License, or** (at your option) any later version.**** This program is distributed in the hope that it will be useful,** but WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the** GNU General Public License for more details.**** You should have received a copy of the GNU General Public License** along with this program; if not, write to the Free Software** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/#include "config.h"#include <stdio.h>#include <stdlib.h>#include <string.h>#ifdef HAVE_UNISTD_H#include <unistd.h>#endif#include <math.h>#include <sndfile.h>#include "utils.h"#define	BUFFER_LEN		(1<<10)#define LOG_BUFFER_SIZE	1024static	void	float_norm_test		(const char *filename) ;static	void	double_norm_test	(const char *filename) ;static	void	format_tests		(void) ;static	void	calc_peak_test		(int filetype, const char *filename) ;static	void	truncate_test		(const char *filename, int filetype) ;/* Force the start of this buffer to be double aligned. Sparc-solaris will** choke if its not.*/static	float	float_data	[BUFFER_LEN] ;static	double	double_data	[BUFFER_LEN] ;intmain (int argc, char *argv []){	/*-char	*filename ;-*/	int		do_all = 0 ;	int		test_count = 0 ;	if (argc != 2)	{	printf ("Usage : %s <test>\n", argv [0]) ;		printf ("    Where <test> is one of the following:\n") ;		printf ("           ver    - test sf_command (SFC_GETLIB_VERSION)\n") ;		/*-printf ("           text  - test adding of text strings\n") ;-*/		printf ("           norm  - test floating point normalisation\n") ;		printf ("           format - test format string commands\n") ;		printf ("           peak   - test peak calculation\n") ;		printf ("           trunc  - test file truncation\n") ;		printf ("           all   - perform all tests\n") ;		exit (1) ;		} ;	do_all =! strcmp (argv [1], "all") ;	if (do_all || ! strcmp (argv [1], "ver"))	{	char buffer [128] ;		buffer [0] = 0 ;		sf_command (NULL, SFC_GET_LIB_VERSION, buffer, sizeof (buffer)) ;		if (strlen (buffer) < 1)		{	printf ("Line %d: could not retrieve lib version.\n", __LINE__) ;			exit (1) ;			} ;		test_count++ ;		} ;	if (do_all || ! strcmp (argv [1], "norm"))	{	/*	Preliminary float/double normalisation tests. More testing		**	is done in the program 'floating_point_test'.		*/		float_norm_test		("float.wav") ;		double_norm_test	("double.wav") ;		test_count++ ;		} ;	if (do_all || ! strcmp (argv [1], "peak"))	{	calc_peak_test (SF_ENDIAN_BIG		| SF_FORMAT_RAW, "be-peak.raw") ;		calc_peak_test (SF_ENDIAN_LITTLE	| SF_FORMAT_RAW, "le-peak.raw") ;		test_count++ ;		} ;	if (do_all || ! strcmp (argv [1], "format"))	{	format_tests () ;		test_count++ ;		} ;	if (do_all || ! strcmp (argv [1], "trunc"))	{	truncate_test ("truncate.raw", SF_FORMAT_RAW | SF_FORMAT_PCM_32) ;		truncate_test ("truncate.au" , SF_FORMAT_AU | SF_FORMAT_PCM_16) ;		test_count++ ;		} ;	if (test_count == 0)	{	printf ("Mono : ************************************\n") ;		printf ("Mono : *  No '%s' test defined.\n", argv [1]) ;		printf ("Mono : ************************************\n") ;		return 1 ;		} ;	return 0 ;} /* main *//*============================================================================================**	Here are the test functions.*/static voidfloat_norm_test (const char *filename){	SNDFILE			*file ;	SF_INFO			sfinfo ;	unsigned int	k ;	print_test_name ("float_norm_test", filename) ;	sfinfo.samplerate	= 44100 ;	sfinfo.format		= (SF_FORMAT_RAW | SF_FORMAT_PCM_16) ;	sfinfo.channels		= 1 ;	sfinfo.frames		= BUFFER_LEN ;	/* Create float_data with all values being less than 1.0. */	for (k = 0 ; k < BUFFER_LEN / 2 ; k++)		float_data [k] = (k + 5) / (2.0 * BUFFER_LEN) ;	for (k = BUFFER_LEN / 2 ; k < BUFFER_LEN ; k++)		float_data [k] = (k + 5) ;	if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))	{	printf ("Line %d: sf_open_write failed with error : ", __LINE__) ;		fflush (stdout) ;		puts (sf_strerror (NULL)) ;		exit (1) ;		} ;	/* Normalisation is on by default so no need to do anything here. */	if ((k = sf_write_float (file, float_data, BUFFER_LEN / 2)) != BUFFER_LEN / 2)	{	printf ("Line %d: sf_write_float failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;		exit (1) ;		} ;	/* Turn normalisation off. */	sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;	if ((k = sf_write_float (file, float_data + BUFFER_LEN / 2, BUFFER_LEN / 2)) != BUFFER_LEN / 2)	{	printf ("Line %d: sf_write_float failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;		exit (1) ;		} ;	sf_close (file) ;	/* sfinfo struct should still contain correct data. */	if (! (file = sf_open (filename, SFM_READ, &sfinfo)))	{	printf ("Line %d: sf_open_read failed with error : ", __LINE__) ;		fflush (stdout) ;		puts (sf_strerror (NULL)) ;		exit (1) ;		} ;	if (sfinfo.format != (SF_FORMAT_RAW | SF_FORMAT_PCM_16))	{	printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, (SF_FORMAT_RAW | SF_FORMAT_PCM_16), sfinfo.format) ;		exit (1) ;		} ;	if (sfinfo.frames != BUFFER_LEN)	{	printf ("\n\nLine %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_LEN, SF_COUNT_TO_LONG (sfinfo.frames)) ;		exit (1) ;		} ;	if (sfinfo.channels != 1)	{	printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;		exit (1) ;		} ;	/* Read float_data and check that it is normalised (ie default). */	if ((k = sf_read_float (file, float_data, BUFFER_LEN)) != BUFFER_LEN)	{	printf ("\n\nLine %d: sf_read_float failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;		exit (1) ;		} ;	for (k = 0 ; k < BUFFER_LEN ; k++)		if (float_data [k] >= 1.0)		{	printf ("\n\nLine %d: float_data [%d] == %f which is greater than 1.0\n", __LINE__, k, float_data [k]) ;			exit (1) ;			} ;	/* Seek to start of file, turn normalisation off, read float_data and check again. */	sf_seek (file, 0, SEEK_SET) ;	sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;	if ((k = sf_read_float (file, float_data, BUFFER_LEN)) != BUFFER_LEN)	{	printf ("\n\nLine %d: sf_read_float failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;		exit (1) ;		} ;	for (k = 0 ; k < BUFFER_LEN ; k++)		if (float_data [k] < 1.0)		{	printf ("\n\nLine %d: float_data [%d] == %f which is less than 1.0\n", __LINE__, k, float_data [k]) ;			exit (1) ;			} ;	/* Seek to start of file, turn normalisation on, read float_data and do final check. */	sf_seek (file, 0, SEEK_SET) ;	sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_TRUE) ;	if ((k = sf_read_float (file, float_data, BUFFER_LEN)) != BUFFER_LEN)	{	printf ("\n\nLine %d: sf_read_float failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;		exit (1) ;		} ;	for (k = 0 ; k < BUFFER_LEN ; k++)		if (float_data [k] > 1.0)		{	printf ("\n\nLine %d: float_data [%d] == %f which is greater than 1.0\n", __LINE__, k, float_data [k]) ;			exit (1) ;			} ;	sf_close (file) ;	unlink (filename) ;	printf ("ok\n") ;} /* float_norm_test */static voiddouble_norm_test (const char *filename){	SNDFILE			*file ;	SF_INFO			sfinfo ;	unsigned int	k ;	print_test_name ("double_norm_test", filename) ;	sfinfo.samplerate	= 44100 ;	sfinfo.format		= (SF_FORMAT_RAW | SF_FORMAT_PCM_16) ;	sfinfo.channels		= 1 ;	sfinfo.frames		= BUFFER_LEN ;	/* Create double_data with all values being less than 1.0. */	for (k = 0 ; k < BUFFER_LEN / 2 ; k++)		double_data [k] = (k + 5) / (2.0 * BUFFER_LEN) ;	for (k = BUFFER_LEN / 2 ; k < BUFFER_LEN ; k++)		double_data [k] = (k + 5) ;	if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))	{	printf ("Line %d: sf_open_write failed with error : ", __LINE__) ;		fflush (stdout) ;		puts (sf_strerror (NULL)) ;		exit (1) ;		} ;	/* Normailsation is on by default so no need to do anything here. */	/*-sf_command (file, "set-norm-double", "true", 0) ;-*/	if ((k = sf_write_double (file, double_data, BUFFER_LEN / 2)) != BUFFER_LEN / 2)	{	printf ("Line %d: sf_write_double failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;		exit (1) ;		} ;	/* Turn normalisation off. */	sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;	if ((k = sf_write_double (file, double_data + BUFFER_LEN / 2, BUFFER_LEN / 2)) != BUFFER_LEN / 2)	{	printf ("Line %d: sf_write_double failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;		exit (1) ;		} ;	sf_close (file) ;	if (! (file = sf_open (filename, SFM_READ, &sfinfo)))	{	printf ("Line %d: sf_open_read failed with error : ", __LINE__) ;		fflush (stdout) ;		puts (sf_strerror (NULL)) ;		exit (1) ;		} ;	if (sfinfo.format != (SF_FORMAT_RAW | SF_FORMAT_PCM_16))	{	printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, (SF_FORMAT_RAW | SF_FORMAT_PCM_16), sfinfo.format) ;		exit (1) ;		} ;	if (sfinfo.frames != BUFFER_LEN)	{	printf ("\n\nLine %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_LEN, SF_COUNT_TO_LONG (sfinfo.frames)) ;		exit (1) ;		} ;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -