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 alaw_encode (int sample) ; cannam@125: static int alaw_decode (unsigned int alawbyte) ; cannam@125: cannam@125: static short short_buffer [BUFFER_SIZE] ; cannam@125: static unsigned char alaw_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 ("alaw_test", "encoder") ; cannam@125: cannam@125: filename = "test.raw" ; cannam@125: cannam@125: sf_info_setup (&sfinfo, SF_FORMAT_RAW | SF_FORMAT_ALAW, 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 alaw 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 alaw 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, alaw_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 (alaw_encode (short_buffer [k]) != alaw_buffer [k]) cannam@125: { printf ("Encoder error : sample #%d (0x%02X should be 0x%02X)\n", k, alaw_buffer [k], alaw_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 ("alaw_test", "decoder") ; cannam@125: /* Now generate a file containing all possible 8 bit encoded cannam@125: ** sample values and write it to disk as alaw 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: alaw_buffer [k] = k & 0xFF ; cannam@125: cannam@125: sf_write_raw (file, alaw_buffer, 256) ; cannam@125: sf_close (file) ; cannam@125: cannam@125: /* Now open that file and compare the alaw 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] != alaw_decode (alaw_buffer [k])) cannam@125: { printf ("Decoder error : sample #%d (0x%02X should be 0x%02X)\n", k, short_buffer [k], alaw_decode (alaw_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: ** A-law routines by Graeme W. Gill. cannam@125: ** Date: 93/5/7 cannam@125: ** cannam@125: ** References: cannam@125: ** 1) CCITT Recommendation G.711 cannam@125: ** cannam@125: */ cannam@125: cannam@125: #define ACLIP 31744 cannam@125: cannam@125: static cannam@125: unsigned char alaw_encode (int sample) cannam@125: { static int exp_lut [128] = cannam@125: { 1, 1, 2, 2, 3, 3, 3, 3, cannam@125: 4, 4, 4, 4, 4, 4, 4, 4, cannam@125: 5, 5, 5, 5, 5, 5, 5, 5, cannam@125: 5, 5, 5, 5, 5, 5, 5, 5, cannam@125: 6, 6, 6, 6, 6, 6, 6, 6, cannam@125: 6, 6, 6, 6, 6, 6, 6, 6, cannam@125: 6, 6, 6, 6, 6, 6, 6, 6, cannam@125: 6, 6, 6, 6, 6, 6, 6, 6, cannam@125: 7, 7, 7, 7, 7, 7, 7, 7, cannam@125: 7, 7, 7, 7, 7, 7, 7, 7, cannam@125: 7, 7, 7, 7, 7, 7, 7, 7, cannam@125: 7, 7, 7, 7, 7, 7, 7, 7, cannam@125: 7, 7, 7, 7, 7, 7, 7, 7, cannam@125: 7, 7, 7, 7, 7, 7, 7, 7, cannam@125: 7, 7, 7, 7, 7, 7, 7, 7, cannam@125: 7, 7, 7, 7, 7, 7, 7, 7 cannam@125: } ; cannam@125: cannam@125: int sign, exponent, mantissa ; cannam@125: unsigned char Alawbyte ; 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 > ACLIP) cannam@125: sample = ACLIP ; /* clip the magnitude */ cannam@125: cannam@125: /* Convert from 16 bit linear to ulaw. */ cannam@125: if (sample >= 256) cannam@125: { exponent = exp_lut [(sample >> 8) & 0x7F] ; cannam@125: mantissa = (sample >> (exponent + 3)) & 0x0F ; cannam@125: Alawbyte = ((exponent << 4) | mantissa) ; cannam@125: } cannam@125: else cannam@125: Alawbyte = (sample >> 4) ; cannam@125: cannam@125: Alawbyte ^= (sign ^ 0x55) ; cannam@125: cannam@125: return Alawbyte ; cannam@125: } /* alaw_encode */ cannam@125: cannam@125: static cannam@125: int alaw_decode (unsigned int Alawbyte) cannam@125: { static int exp_lut [8] = { 0, 264, 528, 1056, 2112, 4224, 8448, 16896 } ; cannam@125: int sign, exponent, mantissa, sample ; cannam@125: cannam@125: Alawbyte ^= 0x55 ; cannam@125: sign = (Alawbyte & 0x80) ; cannam@125: Alawbyte &= 0x7f ; /* get magnitude */ cannam@125: if (Alawbyte >= 16) cannam@125: { exponent = (Alawbyte >> 4) & 0x07 ; cannam@125: mantissa = Alawbyte & 0x0F ; cannam@125: sample = exp_lut [exponent] + (mantissa << (exponent + 3)) ; cannam@125: } cannam@125: else cannam@125: sample = (Alawbyte << 4) + 8 ; cannam@125: if (sign == 0) cannam@125: sample = -sample ; cannam@125: cannam@125: return sample ; cannam@125: } /* alaw_decode */ cannam@125: