Chris@40: /* Chris@40: ** Copyright (C) 1999-2012 Erik de Castro Lopo Chris@40: ** Chris@40: ** This program is free software; you can redistribute it and/or modify Chris@40: ** it under the terms of the GNU General Public License as published by Chris@40: ** the Free Software Foundation; either version 2 of the License, or Chris@40: ** (at your option) any later version. Chris@40: ** Chris@40: ** This program is distributed in the hope that it will be useful, Chris@40: ** but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@40: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@40: ** GNU General Public License for more details. Chris@40: ** Chris@40: ** You should have received a copy of the GNU General Public License Chris@40: ** along with this program; if not, write to the Free Software Chris@40: ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Chris@40: */ Chris@40: Chris@40: #include "sfconfig.h" Chris@40: Chris@40: #include Chris@40: #include Chris@40: #include Chris@40: Chris@40: #if HAVE_UNISTD_H Chris@40: #include Chris@40: #endif Chris@40: Chris@40: #include Chris@40: Chris@40: #include "utils.h" Chris@40: Chris@40: #define BUFFER_SIZE (65536) Chris@40: Chris@40: static unsigned char alaw_encode (int sample) ; Chris@40: static int alaw_decode (unsigned int alawbyte) ; Chris@40: Chris@40: static short short_buffer [BUFFER_SIZE] ; Chris@40: static unsigned char alaw_buffer [BUFFER_SIZE] ; Chris@40: Chris@40: int Chris@40: main (void) Chris@40: { SNDFILE *file ; Chris@40: SF_INFO sfinfo ; Chris@40: const char *filename ; Chris@40: int k ; Chris@40: Chris@40: print_test_name ("alaw_test", "encoder") ; Chris@40: Chris@40: filename = "test.raw" ; Chris@40: Chris@40: sf_info_setup (&sfinfo, SF_FORMAT_RAW | SF_FORMAT_ALAW, 44100, 1) ; Chris@40: Chris@40: if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL) Chris@40: { printf ("sf_open_write failed with error : ") ; Chris@40: fflush (stdout) ; Chris@40: puts (sf_strerror (NULL)) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: /* Generate a file containing all possible 16 bit sample values Chris@40: ** and write it to disk as alaw encoded.frames. Chris@40: */ Chris@40: Chris@40: for (k = 0 ; k < 0x10000 ; k++) Chris@40: short_buffer [k] = k & 0xFFFF ; Chris@40: Chris@40: sf_write_short (file, short_buffer, BUFFER_SIZE) ; Chris@40: sf_close (file) ; Chris@40: Chris@40: /* Now open that file and compare the alaw encoded sample values Chris@40: ** with what they should be. Chris@40: */ Chris@40: Chris@40: if ((file = sf_open (filename, SFM_READ, &sfinfo)) == NULL) Chris@40: { printf ("sf_open_write failed with error : ") ; Chris@40: puts (sf_strerror (NULL)) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: check_log_buffer_or_die (file, __LINE__) ; Chris@40: Chris@40: if (sf_read_raw (file, alaw_buffer, BUFFER_SIZE) != BUFFER_SIZE) Chris@40: { printf ("sf_read_raw : ") ; Chris@40: puts (sf_strerror (file)) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: for (k = 0 ; k < 0x10000 ; k++) Chris@40: if (alaw_encode (short_buffer [k]) != alaw_buffer [k]) Chris@40: { printf ("Encoder error : sample #%d (0x%02X should be 0x%02X)\n", k, alaw_buffer [k], alaw_encode (short_buffer [k])) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: sf_close (file) ; Chris@40: Chris@40: puts ("ok") ; Chris@40: Chris@40: print_test_name ("alaw_test", "decoder") ; Chris@40: /* Now generate a file containing all possible 8 bit encoded Chris@40: ** sample values and write it to disk as alaw encoded.frames. Chris@40: */ Chris@40: Chris@40: if (! (file = sf_open (filename, SFM_WRITE, &sfinfo))) Chris@40: { printf ("sf_open_write failed with error : ") ; Chris@40: puts (sf_strerror (NULL)) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: for (k = 0 ; k < 256 ; k++) Chris@40: alaw_buffer [k] = k & 0xFF ; Chris@40: Chris@40: sf_write_raw (file, alaw_buffer, 256) ; Chris@40: sf_close (file) ; Chris@40: Chris@40: /* Now open that file and compare the alaw decoded sample values Chris@40: ** with what they should be. Chris@40: */ Chris@40: Chris@40: if (! (file = sf_open (filename, SFM_READ, &sfinfo))) Chris@40: { printf ("sf_open_write failed with error : ") ; Chris@40: puts (sf_strerror (NULL)) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: check_log_buffer_or_die (file, __LINE__) ; Chris@40: Chris@40: if (sf_read_short (file, short_buffer, 256) != 256) Chris@40: { printf ("sf_read_short : ") ; Chris@40: puts (sf_strerror (file)) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: Chris@40: for (k = 0 ; k < 256 ; k++) Chris@40: if (short_buffer [k] != alaw_decode (alaw_buffer [k])) Chris@40: { printf ("Decoder error : sample #%d (0x%02X should be 0x%02X)\n", k, short_buffer [k], alaw_decode (alaw_buffer [k])) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: sf_close (file) ; Chris@40: Chris@40: puts ("ok") ; Chris@40: Chris@40: unlink (filename) ; Chris@40: Chris@40: return 0 ; Chris@40: } /* main */ Chris@40: Chris@40: Chris@40: /*================================================================================= Chris@40: ** The following routines came from the sox-12.15 (Sound eXcahcnge) distribution. Chris@40: ** Chris@40: ** This code is not compiled into libsndfile. It is only used to test the Chris@40: ** libsndfile lookup tables for correctness. Chris@40: ** Chris@40: ** I have included the original authors comments. Chris@40: */ Chris@40: Chris@40: /* Chris@40: ** A-law routines by Graeme W. Gill. Chris@40: ** Date: 93/5/7 Chris@40: ** Chris@40: ** References: Chris@40: ** 1) CCITT Recommendation G.711 Chris@40: ** Chris@40: */ Chris@40: Chris@40: #define ACLIP 31744 Chris@40: Chris@40: static Chris@40: unsigned char alaw_encode (int sample) Chris@40: { static int exp_lut [128] = Chris@40: { 1, 1, 2, 2, 3, 3, 3, 3, Chris@40: 4, 4, 4, 4, 4, 4, 4, 4, Chris@40: 5, 5, 5, 5, 5, 5, 5, 5, Chris@40: 5, 5, 5, 5, 5, 5, 5, 5, Chris@40: 6, 6, 6, 6, 6, 6, 6, 6, Chris@40: 6, 6, 6, 6, 6, 6, 6, 6, Chris@40: 6, 6, 6, 6, 6, 6, 6, 6, Chris@40: 6, 6, 6, 6, 6, 6, 6, 6, Chris@40: 7, 7, 7, 7, 7, 7, 7, 7, Chris@40: 7, 7, 7, 7, 7, 7, 7, 7, Chris@40: 7, 7, 7, 7, 7, 7, 7, 7, Chris@40: 7, 7, 7, 7, 7, 7, 7, 7, Chris@40: 7, 7, 7, 7, 7, 7, 7, 7, Chris@40: 7, 7, 7, 7, 7, 7, 7, 7, Chris@40: 7, 7, 7, 7, 7, 7, 7, 7, Chris@40: 7, 7, 7, 7, 7, 7, 7, 7 Chris@40: } ; Chris@40: Chris@40: int sign, exponent, mantissa ; Chris@40: unsigned char Alawbyte ; Chris@40: Chris@40: /* Get the sample into sign-magnitude. */ Chris@40: sign = ((~sample) >> 8) & 0x80 ; /* set aside the sign */ Chris@40: if (sign == 0) Chris@40: sample = -sample ; /* get magnitude */ Chris@40: if (sample > ACLIP) Chris@40: sample = ACLIP ; /* clip the magnitude */ Chris@40: Chris@40: /* Convert from 16 bit linear to ulaw. */ Chris@40: if (sample >= 256) Chris@40: { exponent = exp_lut [(sample >> 8) & 0x7F] ; Chris@40: mantissa = (sample >> (exponent + 3)) & 0x0F ; Chris@40: Alawbyte = ((exponent << 4) | mantissa) ; Chris@40: } Chris@40: else Chris@40: Alawbyte = (sample >> 4) ; Chris@40: Chris@40: Alawbyte ^= (sign ^ 0x55) ; Chris@40: Chris@40: return Alawbyte ; Chris@40: } /* alaw_encode */ Chris@40: Chris@40: static Chris@40: int alaw_decode (unsigned int Alawbyte) Chris@40: { static int exp_lut [8] = { 0, 264, 528, 1056, 2112, 4224, 8448, 16896 } ; Chris@40: int sign, exponent, mantissa, sample ; Chris@40: Chris@40: Alawbyte ^= 0x55 ; Chris@40: sign = (Alawbyte & 0x80) ; Chris@40: Alawbyte &= 0x7f ; /* get magnitude */ Chris@40: if (Alawbyte >= 16) Chris@40: { exponent = (Alawbyte >> 4) & 0x07 ; Chris@40: mantissa = Alawbyte & 0x0F ; Chris@40: sample = exp_lut [exponent] + (mantissa << (exponent + 3)) ; Chris@40: } Chris@40: else Chris@40: sample = (Alawbyte << 4) + 8 ; Chris@40: if (sign == 0) Chris@40: sample = -sample ; Chris@40: Chris@40: return sample ; Chris@40: } /* alaw_decode */ Chris@40: