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