📄 utils.c
字号:
/*** Copyright (C) 2002-2004 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.*//*** Utility functions to make writing the test suite easier.**** The .c and .h files were generated automagically with Autogen from** the files utils.def and utils.tpl.*/#include "config.h"#include <stdio.h>#include <stdlib.h>#ifdef HAVE_UNISTD_H#include <unistd.h>#endif#if (HAVE_DECL_S_IRGRP == 0)#include <sf_unistd.h>#endif#include <errno.h>#include <string.h>#include <ctype.h>#include <math.h>#include <fcntl.h>#include <sys/stat.h>#include <sndfile.h>#include "utils.h"#ifndef M_PI#define M_PI 3.14159265358979323846264338#endif#define SIGNED_SIZEOF(x) ((int) (sizeof (x)))#define LOG_BUFFER_SIZE 2048voidgen_windowed_sine_float (float *data, int len, double maximum){ int k ; memset (data, 0, len * sizeof (float)) ; /* ** Choose a frequency of 1/32 so that it aligns perfectly with a DFT ** bucket to minimise spreading of energy over more than one bucket. ** Also do not want to make the frequency too high as some of the ** codecs (ie gsm610) have a quite severe high frequency roll off. */ len /= 2 ; for (k = 0 ; k < len ; k++) { data [k] = sin (2.0 * k * M_PI * 1.0 / 32.0 + 0.4) ; /* Apply Hanning Window. */ data [k] *= maximum * (0.5 - 0.5 * cos (2.0 * M_PI * k / ((len) - 1))) ; } return ;} /* gen_windowed_sine_float */voidgen_windowed_sine_double (double *data, int len, double maximum){ int k ; memset (data, 0, len * sizeof (double)) ; /* ** Choose a frequency of 1/32 so that it aligns perfectly with a DFT ** bucket to minimise spreading of energy over more than one bucket. ** Also do not want to make the frequency too high as some of the ** codecs (ie gsm610) have a quite severe high frequency roll off. */ len /= 2 ; for (k = 0 ; k < len ; k++) { data [k] = sin (2.0 * k * M_PI * 1.0 / 32.0 + 0.4) ; /* Apply Hanning Window. */ data [k] *= maximum * (0.5 - 0.5 * cos (2.0 * M_PI * k / ((len) - 1))) ; } return ;} /* gen_windowed_sine_double */voidcheck_file_hash_or_die (const char *filename, unsigned int target_hash, int line_num){ static unsigned char buffer [2048] ; unsigned int hash1, hash2 ; FILE *file ; int k, read_count ; memset (buffer, 0xEE, sizeof (buffer)) ; /* The 'b' in the mode string means binary for Win32. */ if (! (file = fopen (filename, "rb"))) { printf ("\n\nLine %d: could not open file '%s'\n\n", line_num, filename) ; exit (1) ; } ; hash1 = hash2 = 0 ; while ((read_count = fread (buffer, 1, sizeof (buffer), file))) { for (k = 0 ; k < read_count ; k++) { hash1 = hash1 + buffer [k] ; hash2 = hash2 ^ (buffer [k] << (k % 25)) ; } ; } ; fclose (file) ; hash1 += hash2 ; if (target_hash == 0) { printf (" 0x%08x ", hash1) ; return ; } ; if (hash1 != target_hash) { printf ("\n\nLine %d: incorrect hash value 0x%08x should be 0x%08x\n\n", line_num, hash1, target_hash) ; exit (1) ; } return ;} /* check_file_hash_or_die */voidprint_test_name (const char *test, const char *filename){ int count ; if (test == NULL || filename == NULL) { printf (__FILE__ ": bad test of filename parameter.\n") ; exit (1) ; } ; printf (" %-25s : %s ", test, filename) ; count = 24 - strlen (filename) ; while (count -- > 0) putchar ('.') ; putchar (' ') ; fflush (stdout) ;} /* print_test_name *//*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/static char octfilename [] = "error.dat" ;intoct_save_short (short *a, short *b, int len){ FILE *file ; int k ; if (! (file = fopen (octfilename, "w"))) return 1 ; fprintf (file, "# Not created by Octave\n") ; fprintf (file, "# name: a\n") ; fprintf (file, "# type: matrix\n") ; fprintf (file, "# rows: %d\n", len) ; fprintf (file, "# columns: 1\n") ; for (k = 0 ; k < len ; k++) fprintf (file, "% d\n", a [k]) ; fprintf (file, "# name: b\n") ; fprintf (file, "# type: matrix\n") ; fprintf (file, "# rows: %d\n", len) ; fprintf (file, "# columns: 1\n") ; for (k = 0 ; k < len ; k++) fprintf (file, "% d\n", b [k]) ; fclose (file) ; return 0 ;} /* oct_save_short */intoct_save_int (int *a, int *b, int len){ FILE *file ; int k ; if (! (file = fopen (octfilename, "w"))) return 1 ; fprintf (file, "# Not created by Octave\n") ; fprintf (file, "# name: a\n") ; fprintf (file, "# type: matrix\n") ; fprintf (file, "# rows: %d\n", len) ; fprintf (file, "# columns: 1\n") ; for (k = 0 ; k < len ; k++) fprintf (file, "% d\n", a [k]) ; fprintf (file, "# name: b\n") ; fprintf (file, "# type: matrix\n") ; fprintf (file, "# rows: %d\n", len) ; fprintf (file, "# columns: 1\n") ; for (k = 0 ; k < len ; k++) fprintf (file, "% d\n", b [k]) ; fclose (file) ; return 0 ;} /* oct_save_int */intoct_save_float (float *a, float *b, int len){ FILE *file ; int k ; if (! (file = fopen (octfilename, "w"))) return 1 ; fprintf (file, "# Not created by Octave\n") ; fprintf (file, "# name: a\n") ; fprintf (file, "# type: matrix\n") ; fprintf (file, "# rows: %d\n", len) ; fprintf (file, "# columns: 1\n") ; for (k = 0 ; k < len ; k++) fprintf (file, "% g\n", a [k]) ; fprintf (file, "# name: b\n") ; fprintf (file, "# type: matrix\n") ; fprintf (file, "# rows: %d\n", len) ; fprintf (file, "# columns: 1\n") ; for (k = 0 ; k < len ; k++) fprintf (file, "% g\n", b [k]) ; fclose (file) ; return 0 ;} /* oct_save_float */intoct_save_double (double *a, double *b, int len){ FILE *file ; int k ; if (! (file = fopen (octfilename, "w"))) return 1 ; fprintf (file, "# Not created by Octave\n") ; fprintf (file, "# name: a\n") ; fprintf (file, "# type: matrix\n") ; fprintf (file, "# rows: %d\n", len) ; fprintf (file, "# columns: 1\n") ; for (k = 0 ; k < len ; k++) fprintf (file, "% g\n", a [k]) ; fprintf (file, "# name: b\n") ; fprintf (file, "# type: matrix\n") ; fprintf (file, "# rows: %d\n", len) ; fprintf (file, "# columns: 1\n") ; for (k = 0 ; k < len ; k++) fprintf (file, "% g\n", b [k]) ; fclose (file) ; return 0 ;} /* oct_save_double */voidcheck_log_buffer_or_die (SNDFILE *file, int line_num){ static char buffer [LOG_BUFFER_SIZE] ; int count ; memset (buffer, 0, LOG_BUFFER_SIZE) ; /* Get the log buffer data. */ count = sf_command (file, SFC_GET_LOG_INFO, buffer, LOG_BUFFER_SIZE) ; if (LOG_BUFFER_SIZE - count < 2) { printf ("\n\nLine %d : Possible long log buffer.\n", line_num) ; exit (1) ; } /* Look for "Should" */ if (strstr (buffer, "ould")) { printf ("\n\nLine %d : Log buffer contains `ould'. Dumping.\n", line_num) ; puts (buffer) ; exit (1) ; } ; /* Look for "**" */ if (strstr (buffer, "*")) { printf ("\n\nLine %d : Log buffer contains `*'. Dumping.\n", line_num) ; puts (buffer) ; exit (1) ; } ; return ;} /* check_log_buffer_or_die */intstring_in_log_buffer (SNDFILE *file, const char *s){ static char buffer [LOG_BUFFER_SIZE] ; int count ; memset (buffer, 0, LOG_BUFFER_SIZE) ; /* Get the log buffer data. */ count = sf_command (file, SFC_GET_LOG_INFO, buffer, LOG_BUFFER_SIZE) ; if (LOG_BUFFER_SIZE - count < 2) { printf ("Possible long log buffer.\n") ; exit (1) ; } /* Look for string */ return strstr (buffer, s) ? SF_TRUE : SF_FALSE ;} /* string_in_log_buffer */voidhexdump_file (const char * filename, sf_count_t offset, sf_count_t length){ FILE * file ; char buffer [16] ; int k, m, ch, readcount ; if (length > 1000000) { printf ("\n\nError : length (%ld) too long.\n\n", SF_COUNT_TO_LONG (offset)) ; exit (1) ; } ; if ((file = fopen (filename, "r")) == NULL) { printf ("\n\nError : hexdump_file (%s) could not open file for read.\n\n", filename) ; exit (1) ; } ; if (fseek (file, offset, SEEK_SET) != 0) { printf ("\n\nError : fseek(file, %ld, SEEK_SET) failed : %s\n\n", SF_COUNT_TO_LONG (offset), strerror (errno)) ; exit (1) ; } ; puts ("\n\n") ; for (k = 0 ; k < length ; k+= sizeof (buffer)) { readcount = fread (buffer, 1, sizeof (buffer), file) ; printf ("%08lx : ", SF_COUNT_TO_LONG (offset + k)) ; for (m = 0 ; m < readcount ; m++) printf ("%02x ", buffer [m] & 0xFF) ; for (m = readcount ; m < SIGNED_SIZEOF (buffer) ; m++) printf (" ") ; printf (" ") ; for (m = 0 ; m < readcount ; m++) { ch = isprint (buffer [m]) ? buffer [m] : '.' ; putchar (ch) ; } ; if (readcount < SIGNED_SIZEOF (buffer)) break ; putchar ('\n') ; } ; puts ("\n") ; fclose (file) ;} /* hexdump_file */voiddump_log_buffer (SNDFILE *file){ static char buffer [LOG_BUFFER_SIZE] ; int count ; memset (buffer, 0, LOG_BUFFER_SIZE) ; /* Get the log buffer data. */ count = sf_command (file, SFC_GET_LOG_INFO, buffer, LOG_BUFFER_SIZE) ; if (strlen (buffer) < 1) puts ("Log buffer empty.\n") ; else puts (buffer) ; return ;} /* dump_log_buffer */SNDFILE *test_open_file_or_die (const char *filename, int mode, SF_INFO *sfinfo, int allow_fd, int line_num){ static int count = 0 ; SNDFILE *file ; const char *modestr, *func_name ; int oflags = 0, omode = 0 ; /* ** Need to test both sf_open() and sf_open_fd(). ** Do so alternately. */ switch (mode) { case SFM_READ : modestr = "SFM_READ" ; oflags = O_RDONLY ; omode = 0 ; break ; case SFM_WRITE : modestr = "SFM_WRITE" ; oflags = O_WRONLY | O_CREAT | O_TRUNC ; omode = S_IRUSR | S_IWUSR | S_IRGRP ; break ; case SFM_RDWR : modestr = "SFM_RDWR" ; oflags = O_RDWR | O_CREAT ; omode = S_IRUSR | S_IWUSR | S_IRGRP ; break ; default : printf ("\n\nLine %d: Bad mode.\n", line_num) ; fflush (stdout) ; exit (1) ; } ;#if (defined (__CYGWIN__) || defined (WIN32) || defined (_WIN32)) /* Stupid fscking windows. */ oflags |= O_BINARY ;#endif if (allow_fd && ((++count) & 1) == 1) { int fd ; if (omode == 0) fd = open (filename, oflags) ; else fd = open (filename, oflags, omode) ; if (fd < 0) { perror ("open") ; exit (1) ; } ; func_name = "sf_open_fd" ; file = sf_open_fd (fd, mode, sfinfo, SF_TRUE) ; } else { func_name = "sf_open" ; file = sf_open (filename, mode, sfinfo) ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -