2
0

alaw_test.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. ** Copyright (C) 1999-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
  3. **
  4. ** This program is free software; you can redistribute it and/or modify
  5. ** it under the terms of the GNU General Public License as published by
  6. ** the Free Software Foundation; either version 2 of the License, or
  7. ** (at your option) any later version.
  8. **
  9. ** This program is distributed in the hope that it will be useful,
  10. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ** GNU General Public License for more details.
  13. **
  14. ** You should have received a copy of the GNU General Public License
  15. ** along with this program; if not, write to the Free Software
  16. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include "sfconfig.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #if HAVE_UNISTD_H
  23. #include <unistd.h>
  24. #endif
  25. #include <sndfile.h>
  26. #include "utils.h"
  27. #define BUFFER_SIZE (65536)
  28. static unsigned char alaw_encode (int sample) ;
  29. static int alaw_decode (unsigned int alawbyte) ;
  30. static short short_buffer [BUFFER_SIZE] ;
  31. static unsigned char alaw_buffer [BUFFER_SIZE] ;
  32. int
  33. main (void)
  34. { SNDFILE *file ;
  35. SF_INFO sfinfo ;
  36. const char *filename ;
  37. int k ;
  38. print_test_name ("alaw_test", "encoder") ;
  39. filename = "test.raw" ;
  40. sf_info_setup (&sfinfo, SF_FORMAT_RAW | SF_FORMAT_ALAW, 44100, 1) ;
  41. if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL)
  42. { printf ("sf_open_write failed with error : ") ;
  43. fflush (stdout) ;
  44. puts (sf_strerror (NULL)) ;
  45. exit (1) ;
  46. } ;
  47. /* Generate a file containing all possible 16 bit sample values
  48. ** and write it to disk as alaw encoded.frames.
  49. */
  50. for (k = 0 ; k < 0x10000 ; k++)
  51. short_buffer [k] = k & 0xFFFF ;
  52. sf_write_short (file, short_buffer, BUFFER_SIZE) ;
  53. sf_close (file) ;
  54. /* Now open that file and compare the alaw encoded sample values
  55. ** with what they should be.
  56. */
  57. if ((file = sf_open (filename, SFM_READ, &sfinfo)) == NULL)
  58. { printf ("sf_open_write failed with error : ") ;
  59. puts (sf_strerror (NULL)) ;
  60. exit (1) ;
  61. } ;
  62. check_log_buffer_or_die (file, __LINE__) ;
  63. if (sf_read_raw (file, alaw_buffer, BUFFER_SIZE) != BUFFER_SIZE)
  64. { printf ("sf_read_raw : ") ;
  65. puts (sf_strerror (file)) ;
  66. exit (1) ;
  67. } ;
  68. for (k = 0 ; k < 0x10000 ; k++)
  69. if (alaw_encode (short_buffer [k]) != alaw_buffer [k])
  70. { printf ("Encoder error : sample #%d (0x%02X should be 0x%02X)\n", k, alaw_buffer [k], alaw_encode (short_buffer [k])) ;
  71. exit (1) ;
  72. } ;
  73. sf_close (file) ;
  74. puts ("ok") ;
  75. print_test_name ("alaw_test", "decoder") ;
  76. /* Now generate a file containing all possible 8 bit encoded
  77. ** sample values and write it to disk as alaw encoded.frames.
  78. */
  79. if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
  80. { printf ("sf_open_write failed with error : ") ;
  81. puts (sf_strerror (NULL)) ;
  82. exit (1) ;
  83. } ;
  84. for (k = 0 ; k < 256 ; k++)
  85. alaw_buffer [k] = k & 0xFF ;
  86. sf_write_raw (file, alaw_buffer, 256) ;
  87. sf_close (file) ;
  88. /* Now open that file and compare the alaw decoded sample values
  89. ** with what they should be.
  90. */
  91. if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
  92. { printf ("sf_open_write failed with error : ") ;
  93. puts (sf_strerror (NULL)) ;
  94. exit (1) ;
  95. } ;
  96. check_log_buffer_or_die (file, __LINE__) ;
  97. if (sf_read_short (file, short_buffer, 256) != 256)
  98. { printf ("sf_read_short : ") ;
  99. puts (sf_strerror (file)) ;
  100. exit (1) ;
  101. } ;
  102. for (k = 0 ; k < 256 ; k++)
  103. if (short_buffer [k] != alaw_decode (alaw_buffer [k]))
  104. { printf ("Decoder error : sample #%d (0x%02X should be 0x%02X)\n", k, short_buffer [k], alaw_decode (alaw_buffer [k])) ;
  105. exit (1) ;
  106. } ;
  107. sf_close (file) ;
  108. puts ("ok") ;
  109. unlink (filename) ;
  110. return 0 ;
  111. } /* main */
  112. /*=================================================================================
  113. ** The following routines came from the sox-12.15 (Sound eXcahcnge) distribution.
  114. **
  115. ** This code is not compiled into libsndfile. It is only used to test the
  116. ** libsndfile lookup tables for correctness.
  117. **
  118. ** I have included the original authors comments.
  119. */
  120. /*
  121. ** A-law routines by Graeme W. Gill.
  122. ** Date: 93/5/7
  123. **
  124. ** References:
  125. ** 1) CCITT Recommendation G.711
  126. **
  127. */
  128. #define ACLIP 31744
  129. static
  130. unsigned char alaw_encode (int sample)
  131. { static int exp_lut [128] =
  132. { 1, 1, 2, 2, 3, 3, 3, 3,
  133. 4, 4, 4, 4, 4, 4, 4, 4,
  134. 5, 5, 5, 5, 5, 5, 5, 5,
  135. 5, 5, 5, 5, 5, 5, 5, 5,
  136. 6, 6, 6, 6, 6, 6, 6, 6,
  137. 6, 6, 6, 6, 6, 6, 6, 6,
  138. 6, 6, 6, 6, 6, 6, 6, 6,
  139. 6, 6, 6, 6, 6, 6, 6, 6,
  140. 7, 7, 7, 7, 7, 7, 7, 7,
  141. 7, 7, 7, 7, 7, 7, 7, 7,
  142. 7, 7, 7, 7, 7, 7, 7, 7,
  143. 7, 7, 7, 7, 7, 7, 7, 7,
  144. 7, 7, 7, 7, 7, 7, 7, 7,
  145. 7, 7, 7, 7, 7, 7, 7, 7,
  146. 7, 7, 7, 7, 7, 7, 7, 7,
  147. 7, 7, 7, 7, 7, 7, 7, 7
  148. } ;
  149. int sign, exponent, mantissa ;
  150. unsigned char Alawbyte ;
  151. /* Get the sample into sign-magnitude. */
  152. sign = ((~sample) >> 8) & 0x80 ; /* set aside the sign */
  153. if (sign == 0)
  154. sample = -sample ; /* get magnitude */
  155. if (sample > ACLIP)
  156. sample = ACLIP ; /* clip the magnitude */
  157. /* Convert from 16 bit linear to ulaw. */
  158. if (sample >= 256)
  159. { exponent = exp_lut [(sample >> 8) & 0x7F] ;
  160. mantissa = (sample >> (exponent + 3)) & 0x0F ;
  161. Alawbyte = ((exponent << 4) | mantissa) ;
  162. }
  163. else
  164. Alawbyte = (sample >> 4) ;
  165. Alawbyte ^= (sign ^ 0x55) ;
  166. return Alawbyte ;
  167. } /* alaw_encode */
  168. static
  169. int alaw_decode (unsigned int Alawbyte)
  170. { static int exp_lut [8] = { 0, 264, 528, 1056, 2112, 4224, 8448, 16896 } ;
  171. int sign, exponent, mantissa, sample ;
  172. Alawbyte ^= 0x55 ;
  173. sign = (Alawbyte & 0x80) ;
  174. Alawbyte &= 0x7f ; /* get magnitude */
  175. if (Alawbyte >= 16)
  176. { exponent = (Alawbyte >> 4) & 0x07 ;
  177. mantissa = Alawbyte & 0x0F ;
  178. sample = exp_lut [exponent] + (mantissa << (exponent + 3)) ;
  179. }
  180. else
  181. sample = (Alawbyte << 4) + 8 ;
  182. if (sign == 0)
  183. sample = -sample ;
  184. return sample ;
  185. } /* alaw_decode */