/***************************************************************************** Perceptual Evaluation of Speech Quality (PESQ) ITU-T Draft Recommendation P.862. Version 1.1 - 15 November 2000. NOTICE The Perceptual Evaluation of Speech Quality (PESQ) algorithm and the copyright therein is the property of British Telecommunications plc and Royal KPN NV, and is protected by UK, US and other patents. Permission is granted to use PESQ for the purpose of evaluation of ITU-T recommendation P.862. Any other use of this software or the PESQ algorithm requires a license, which may be obtained from: OPTICOM GmbH Michael Keyhl, Am Weichselgarten 7, D- 91058 Erlangen, Germany Phone: +49 9131 691 160 Fax: +49 9131 691 325 E-mail: info@opticom.de PsyTechnics Limited Richard Reynolds, B54 Adastral Park, Ipswich IP5 3RE, UK Phone: +44 1473 644 730 or +44 7730 426 251 Fax: +44 1473 645 663 E-mail: richard.reynolds@psytechnics.com Patent-only licences should be obtained from Opticom. PsyTechnics or Opticom can provide licences, and further information, for other PESQ products. Further information is also available from: www.pesq.org By using this software you acknowledge that PESQ is protected by copyright and by patents and is being made available to you for the purpose of evaluation of ITU-T Recommendation P.862. You must not use PESQ for any other purpose without first obtaining a written license from British Telecommunications plc and Royal KPN NV, from their agents listed above. You must not disclose, reproduce or otherwise release PESQ to any third party without the prior written permission of British Telecommunications plc and Royal KPN NV. Authors: Antony Rix (BT) Mike Hollier (BT) Andries Hekstra (KPN Research) John Beerends (KPN Research) *****************************************************************************/ #include #include "pesq.h" #include "dsp.h" void make_stereo_file (char *stereo_path_name, SIGNAL_INFO *ref_info, SIGNAL_INFO *deg_info) { make_stereo_file2 (stereo_path_name, ref_info, deg_info-> data); } void make_stereo_file2 (char *stereo_path_name, SIGNAL_INFO *ref_info, float *deg) { long i; long h; short *buffer; FILE *outputFile; long n; n = ref_info-> Nsamples + DATAPADDING_MSECS * (Fs / 1000) - 2 * SEARCHBUFFER * Downsample; buffer = (short *) safe_malloc (2 * n * sizeof (short)); if ((outputFile = fopen (stereo_path_name, "wb")) == NULL) { printf ("MakeStereoFile : cannot open output file %s!", stereo_path_name); return; } for (i = 0; i < n; i++) { h = (int) ref_info-> data [SEARCHBUFFER * Downsample + i] / 2; if (h < -32767) h = -32767; if (h > 32767) h = 32767; h = (short) h; buffer [2*i] = (short) h; h = (int) deg [SEARCHBUFFER * Downsample + i] / 2; if (h < -32767) h = -32767; if (h > 32767) h = 32767; h = (short) h; buffer [2*i + 1] = (short) h; } fwrite (buffer, sizeof (short) * 2, n, outputFile); fclose (outputFile); safe_free (buffer); } extern float InIIR_Hsos_16k []; extern float InIIR_Hsos_8k []; extern long InIIR_Nsos; void select_rate( long sample_rate, long * Error_Flag, char ** Error_Type ) { if( Fs == sample_rate ) return; if( Fs_16k == sample_rate ) { Fs = Fs_16k; Downsample = Downsample_16k; InIIR_Hsos = InIIR_Hsos_16k; InIIR_Nsos = InIIR_Nsos_16k; Align_Nfft = Align_Nfft_16k; return; } if( Fs_8k == sample_rate ) { Fs = Fs_8k; Downsample = Downsample_8k; InIIR_Hsos = InIIR_Hsos_8k; InIIR_Nsos = InIIR_Nsos_8k; Align_Nfft = Align_Nfft_8k; return; } (*Error_Flag) = -1; (*Error_Type) = "Invalid sample rate specified"; } int file_exist( char * fname ) { FILE * fp = fopen( fname, "rb" ); if( fp == NULL ) return 0; else { fclose( fp ); return 1; } } void load_src( long * Error_Flag, char ** Error_Type, SIGNAL_INFO * sinfo) { long name_len; long file_size; long header_size = 0; long Nsamples; long to_read; long read_count; long count; float *read_ptr; short *input_data; short *p_input; char s; char *p_byte; FILE *Src_file = fopen( sinfo-> path_name, "rb" ); input_data = (short *) safe_malloc( 16384 * sizeof(short) ); if( input_data == NULL ) { *Error_Flag = 1; *Error_Type = "Could not allocate storage for file reading"; printf ("%s!\n", *Error_Type); fclose( Src_file ); return; } if( Src_file == NULL ) { *Error_Flag = 1; *Error_Type = "Could not open source file"; printf ("%s!\n", *Error_Type); safe_free( input_data ); return; } if( fseek( Src_file, 0L, SEEK_END ) != 0 ) { *Error_Flag = 1; *Error_Type = "Could not reach end of source file"; safe_free( input_data ); printf ("%s!\n", *Error_Type); fclose( Src_file ); return; } file_size = ftell( Src_file ); if( file_size < 0L ) { *Error_Flag = 1; *Error_Type = "Could not measure length of source file"; safe_free( input_data ); printf ("%s!\n", *Error_Type); fclose( Src_file ); return; } if( fseek( Src_file, 0L, SEEK_SET ) != 0 ) { *Error_Flag = 1; *Error_Type = "Could not reach start of source file"; safe_free( input_data ); printf ("%s!\n", *Error_Type); fclose( Src_file ); return; } name_len = strlen( sinfo-> path_name ); if( name_len > 4 ) { if( strcmp( sinfo-> path_name + name_len - 4, ".wav" ) == 0 ) header_size = 22; if( strcmp( sinfo-> path_name + name_len - 4, ".WAV" ) == 0 ) header_size = 22; if( strcmp( sinfo-> path_name + name_len - 4, ".raw" ) == 0 ) header_size = 0; if( strcmp( sinfo-> path_name + name_len - 4, ".src" ) == 0 ) header_size = 0; } if( name_len > 2 ) { if( strcmp( sinfo-> path_name + name_len - 2, ".s" ) == 0 ) header_size = 0; } if( header_size > 0 ) fread( input_data, 2, header_size, Src_file ); Nsamples = (file_size / 2) - header_size; sinfo-> Nsamples = Nsamples + 2 * SEARCHBUFFER * Downsample; sinfo-> data = (float *) safe_malloc( (sinfo-> Nsamples + DATAPADDING_MSECS * (Fs / 1000)) * sizeof(float) ); if( sinfo-> data == NULL ) { *Error_Flag = 1; *Error_Type = "Failed to allocate memory for source file"; safe_free( input_data ); printf ("%s!\n", *Error_Type); fclose( Src_file ); return; } read_ptr = sinfo-> data; for( read_count = SEARCHBUFFER*Downsample; read_count > 0; read_count-- ) *(read_ptr++) = 0.0f; to_read = Nsamples; while( to_read > 16384 ) { read_count = fread( input_data, sizeof(short), 16384, Src_file ); if( read_count < 16384 ) { *Error_Flag = 1; *Error_Type = "Error reading source file."; printf ("%s!\n", *Error_Type); safe_free( input_data ); safe_free( sinfo-> data ); sinfo-> data = NULL; fclose( Src_file ); return; } if( sinfo-> apply_swap ) { p_byte = (char *)input_data; for( count = 0L; count < read_count; count++ ) { s = p_byte[count << 1]; p_byte[count << 1] = p_byte[(count << 1)+1]; p_byte[(count << 1)+1] = s; } } to_read -= read_count; p_input = input_data; while( read_count > 0 ) { read_count--; *(read_ptr++) = (float)(*(p_input++)); } } read_count = fread( input_data, sizeof(short), to_read, Src_file ); if( read_count < to_read ) { *Error_Flag = 1; *Error_Type = "Error reading source file"; printf ("%s!\n", *Error_Type); safe_free( input_data ); safe_free( sinfo-> data ); sinfo-> data = NULL; fclose( Src_file ); return; } if( sinfo-> apply_swap ) { p_byte = (char *)input_data; for( count = 0L; count < read_count; count++ ) { s = p_byte[count << 1]; p_byte[count << 1] = p_byte[(count << 1)+1]; p_byte[(count << 1)+1] = s; } } p_input = input_data; while( read_count > 0 ) { read_count--; *(read_ptr++) = (float)(*(p_input++)); } for( read_count = DATAPADDING_MSECS * (Fs / 1000) + SEARCHBUFFER * Downsample; read_count > 0; read_count-- ) *(read_ptr++) = 0.0f; fclose( Src_file ); safe_free( input_data ); sinfo-> VAD = safe_malloc( sinfo-> Nsamples * sizeof(float) / Downsample ); sinfo-> logVAD = safe_malloc( sinfo-> Nsamples * sizeof(float) / Downsample ); if( (sinfo-> VAD == NULL) || (sinfo-> logVAD == NULL)) { *Error_Flag = 1; *Error_Type = "Failed to allocate memory for VAD"; printf ("%s!\n", *Error_Type); return; } } void alloc_other( SIGNAL_INFO * ref_info, SIGNAL_INFO * deg_info, long * Error_Flag, char ** Error_Type, float ** ftmp) { *ftmp = (float *)safe_malloc( max( max( (*ref_info).Nsamples + DATAPADDING_MSECS * (Fs / 1000), (*deg_info).Nsamples + DATAPADDING_MSECS * (Fs / 1000) ), 12 * Align_Nfft) * sizeof(float) ); if( (*ftmp) == NULL ) { *Error_Flag = 2; *Error_Type = "Failed to allocate memory for temporary storage."; printf ("%s!\n", *Error_Type); return; } } /* END OF FILE */