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