cannam@85: /* cannam@85: ** Copyright (C) 1999-2011 Erik de Castro Lopo cannam@85: ** cannam@85: ** This program is free software; you can redistribute it and/or modify cannam@85: ** it under the terms of the GNU General Public License as published by cannam@85: ** the Free Software Foundation; either version 2 of the License, or cannam@85: ** (at your option) any later version. cannam@85: ** cannam@85: ** This program is distributed in the hope that it will be useful, cannam@85: ** but WITHOUT ANY WARRANTY; without even the implied warranty of cannam@85: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the cannam@85: ** GNU General Public License for more details. cannam@85: ** cannam@85: ** You should have received a copy of the GNU General Public License cannam@85: ** along with this program; if not, write to the Free Software cannam@85: ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. cannam@85: */ cannam@85: cannam@85: #include "sfconfig.h" cannam@85: cannam@85: #include cannam@85: #include cannam@85: #include cannam@85: cannam@85: #if HAVE_UNISTD_H cannam@85: #include cannam@85: #endif cannam@85: cannam@85: #include cannam@85: cannam@85: #include "utils.h" cannam@85: cannam@85: #define BUFFER_SIZE (65536) cannam@85: cannam@85: static unsigned char ulaw_encode (int sample) ; cannam@85: static int ulaw_decode (unsigned int ulawbyte) ; cannam@85: cannam@85: static short short_buffer [BUFFER_SIZE] ; cannam@85: static unsigned char ulaw_buffer [BUFFER_SIZE] ; cannam@85: cannam@85: int cannam@85: main (void) cannam@85: { SNDFILE *file ; cannam@85: SF_INFO sfinfo ; cannam@85: const char *filename ; cannam@85: int k ; cannam@85: cannam@85: print_test_name ("ulaw_test", "encoder") ; cannam@85: cannam@85: filename = "test.raw" ; cannam@85: cannam@85: sf_info_setup (&sfinfo, SF_FORMAT_RAW | SF_FORMAT_ULAW, 44100, 1) ; cannam@85: cannam@85: if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL) cannam@85: { printf ("sf_open_write failed with error : ") ; cannam@85: fflush (stdout) ; cannam@85: puts (sf_strerror (NULL)) ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: /* Generate a file containing all possible 16 bit sample values cannam@85: ** and write it to disk as ulaw encoded.frames. cannam@85: */ cannam@85: cannam@85: for (k = 0 ; k < 0x10000 ; k++) cannam@85: short_buffer [k] = k & 0xFFFF ; cannam@85: cannam@85: sf_write_short (file, short_buffer, BUFFER_SIZE) ; cannam@85: sf_close (file) ; cannam@85: cannam@85: /* Now open that file and compare the ulaw encoded sample values cannam@85: ** with what they should be. cannam@85: */ cannam@85: cannam@85: if ((file = sf_open (filename, SFM_READ, &sfinfo)) == NULL) cannam@85: { printf ("sf_open_write failed with error : ") ; cannam@85: puts (sf_strerror (NULL)) ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: check_log_buffer_or_die (file, __LINE__) ; cannam@85: cannam@85: if (sf_read_raw (file, ulaw_buffer, BUFFER_SIZE) != BUFFER_SIZE) cannam@85: { printf ("sf_read_raw : ") ; cannam@85: puts (sf_strerror (file)) ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: for (k = 0 ; k < 0x10000 ; k++) cannam@85: if (ulaw_encode (short_buffer [k]) != ulaw_buffer [k]) cannam@85: { printf ("Encoder error : sample #%d (0x%02X should be 0x%02X)\n", k, ulaw_buffer [k], ulaw_encode (short_buffer [k])) ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: sf_close (file) ; cannam@85: cannam@85: puts ("ok") ; cannam@85: cannam@85: print_test_name ("ulaw_test", "decoder") ; cannam@85: cannam@85: /* Now generate a file containing all possible 8 bit encoded cannam@85: ** sample values and write it to disk as ulaw encoded.frames. cannam@85: */ cannam@85: cannam@85: if (! (file = sf_open (filename, SFM_WRITE, &sfinfo))) cannam@85: { printf ("sf_open_write failed with error : ") ; cannam@85: puts (sf_strerror (NULL)) ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: for (k = 0 ; k < 256 ; k++) cannam@85: ulaw_buffer [k] = k & 0xFF ; cannam@85: cannam@85: sf_write_raw (file, ulaw_buffer, 256) ; cannam@85: sf_close (file) ; cannam@85: cannam@85: /* Now open that file and compare the ulaw decoded sample values cannam@85: ** with what they should be. cannam@85: */ cannam@85: cannam@85: if (! (file = sf_open (filename, SFM_READ, &sfinfo))) cannam@85: { printf ("sf_open_write failed with error : ") ; cannam@85: puts (sf_strerror (NULL)) ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: check_log_buffer_or_die (file, __LINE__) ; cannam@85: cannam@85: if (sf_read_short (file, short_buffer, 256) != 256) cannam@85: { printf ("sf_read_short : ") ; cannam@85: puts (sf_strerror (file)) ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: cannam@85: for (k = 0 ; k < 256 ; k++) cannam@85: if (short_buffer [k] != ulaw_decode (ulaw_buffer [k])) cannam@85: { printf ("Decoder error : sample #%d (0x%04X should be 0x%04X)\n", k, short_buffer [k], ulaw_decode (ulaw_buffer [k])) ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: sf_close (file) ; cannam@85: cannam@85: puts ("ok") ; cannam@85: cannam@85: unlink (filename) ; cannam@85: cannam@85: return 0 ; cannam@85: } /* main */ cannam@85: cannam@85: cannam@85: /*================================================================================= cannam@85: ** The following routines came from the sox-12.15 (Sound eXcahcnge) distribution. cannam@85: ** cannam@85: ** This code is not compiled into libsndfile. It is only used to test the cannam@85: ** libsndfile lookup tables for correctness. cannam@85: ** cannam@85: ** I have included the original authors comments. cannam@85: */ cannam@85: cannam@85: /* cannam@85: ** This routine converts from linear to ulaw. cannam@85: ** cannam@85: ** Craig Reese: IDA/Supercomputing Research Center cannam@85: ** Joe Campbell: Department of Defense cannam@85: ** 29 September 1989 cannam@85: ** cannam@85: ** References: cannam@85: ** 1) CCITT Recommendation G.711 (very difficult to follow) cannam@85: ** 2) "A New Digital Technique for Implementation of Any cannam@85: ** Continuous PCM Companding Law," Villeret, Michel, cannam@85: ** et al. 1973 IEEE Int. Conf. on Communications, Vol 1, cannam@85: ** 1973, pg. 11.12-11.17 cannam@85: ** 3) MIL-STD-188-113,"Interoperability and Performance Standards cannam@85: ** for Analog-to_Digital Conversion Techniques," cannam@85: ** 17 February 1987 cannam@85: ** cannam@85: ** Input: Signed 16 bit linear sample cannam@85: ** Output: 8 bit ulaw sample cannam@85: */ cannam@85: cannam@85: #define uBIAS 0x84 /* define the add-in bias for 16 bit.frames */ cannam@85: #define uCLIP 32635 cannam@85: cannam@85: static cannam@85: unsigned char ulaw_encode (int sample) cannam@85: { static int exp_lut [256] = cannam@85: { 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, cannam@85: 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, cannam@85: 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, cannam@85: 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, cannam@85: 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, cannam@85: 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, cannam@85: 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, cannam@85: 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, cannam@85: 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, cannam@85: 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, cannam@85: 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, cannam@85: 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, cannam@85: 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, cannam@85: 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, cannam@85: 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, cannam@85: 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 cannam@85: } ; cannam@85: cannam@85: int sign, exponent, mantissa ; cannam@85: unsigned char ulawbyte ; cannam@85: cannam@85: /* Get the sample into sign-magnitude. */ cannam@85: sign = (sample >> 8) & 0x80 ; /* set aside the sign */ cannam@85: if ( sign != 0 ) cannam@85: sample = -sample ; /* get magnitude */ cannam@85: if ( sample > uCLIP ) cannam@85: sample = uCLIP ; /* clip the magnitude */ cannam@85: cannam@85: /* Convert from 16 bit linear to ulaw. */ cannam@85: sample = sample + uBIAS ; cannam@85: exponent = exp_lut [( sample >> 7 ) & 0xFF] ; cannam@85: mantissa = (sample >> ( exponent + 3 ) ) & 0x0F ; cannam@85: ulawbyte = ~ (sign | ( exponent << 4 ) | mantissa) ; cannam@85: cannam@85: return ulawbyte ; cannam@85: } /* ulaw_encode */ cannam@85: cannam@85: cannam@85: /* cannam@85: ** This routine converts from ulaw to 16 bit linear. cannam@85: ** cannam@85: ** Craig Reese: IDA/Supercomputing Research Center cannam@85: ** 29 September 1989 cannam@85: ** cannam@85: ** References: cannam@85: ** 1) CCITT Recommendation G.711 (very difficult to follow) cannam@85: ** 2) MIL-STD-188-113,"Interoperability and Performance Standards cannam@85: ** for Analog-to_Digital Conversion Techniques," cannam@85: ** 17 February 1987 cannam@85: ** cannam@85: ** Input: 8 bit ulaw sample cannam@85: ** Output: signed 16 bit linear sample cannam@85: */ cannam@85: cannam@85: static cannam@85: int ulaw_decode (unsigned int ulawbyte) cannam@85: { static int exp_lut [8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 } ; cannam@85: int sign, exponent, mantissa, sample ; cannam@85: cannam@85: ulawbyte = ~ ulawbyte ; cannam@85: sign = (ulawbyte & 0x80) ; cannam@85: exponent = (ulawbyte >> 4) & 0x07 ; cannam@85: mantissa = ulawbyte & 0x0F ; cannam@85: sample = exp_lut [exponent] + (mantissa << (exponent + 3)) ; cannam@85: if (sign != 0) cannam@85: sample = -sample ; cannam@85: cannam@85: return sample ; cannam@85: } /* ulaw_decode */ cannam@85: