alaw_test.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. ** Copyright (C) 1999-2012 Erik de Castro Lopo <[email protected]>
  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. #else
  25. #include "sf_unistd.h"
  26. #endif
  27. #include <sndfile.h>
  28. #include "utils.h"
  29. #define BUFFER_SIZE (65536)
  30. static unsigned char alaw_encode (int sample) ;
  31. static int alaw_decode (unsigned int alawbyte) ;
  32. static short short_buffer [BUFFER_SIZE] ;
  33. static unsigned char alaw_buffer [BUFFER_SIZE] ;
  34. int
  35. main (void)
  36. { SNDFILE *file ;
  37. SF_INFO sfinfo ;
  38. const char *filename ;
  39. int k ;
  40. print_test_name ("alaw_test", "encoder") ;
  41. filename = "alaw_test.raw" ;
  42. sf_info_setup (&sfinfo, SF_FORMAT_RAW | SF_FORMAT_ALAW, 44100, 1) ;
  43. if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL)
  44. { printf ("sf_open_write failed with error : ") ;
  45. fflush (stdout) ;
  46. puts (sf_strerror (NULL)) ;
  47. exit (1) ;
  48. } ;
  49. /* Generate a file containing all possible 16 bit sample values
  50. ** and write it to disk as alaw encoded.frames.
  51. */
  52. for (k = 0 ; k < 0x10000 ; k++)
  53. short_buffer [k] = k & 0xFFFF ;
  54. sf_write_short (file, short_buffer, BUFFER_SIZE) ;
  55. sf_close (file) ;
  56. /* Now open that file and compare the alaw encoded sample values
  57. ** with what they should be.
  58. */
  59. if ((file = sf_open (filename, SFM_READ, &sfinfo)) == NULL)
  60. { printf ("sf_open_write failed with error : ") ;
  61. puts (sf_strerror (NULL)) ;
  62. exit (1) ;
  63. } ;
  64. check_log_buffer_or_die (file, __LINE__) ;
  65. if (sf_read_raw (file, alaw_buffer, BUFFER_SIZE) != BUFFER_SIZE)
  66. { printf ("sf_read_raw : ") ;
  67. puts (sf_strerror (file)) ;
  68. exit (1) ;
  69. } ;
  70. for (k = 0 ; k < 0x10000 ; k++)
  71. if (alaw_encode (short_buffer [k]) != alaw_buffer [k])
  72. { printf ("Encoder error : sample #%d (0x%02X should be 0x%02X)\n", k, alaw_buffer [k], alaw_encode (short_buffer [k])) ;
  73. exit (1) ;
  74. } ;
  75. sf_close (file) ;
  76. puts ("ok") ;
  77. print_test_name ("alaw_test", "decoder") ;
  78. /* Now generate a file containing all possible 8 bit encoded
  79. ** sample values and write it to disk as alaw encoded.frames.
  80. */
  81. if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
  82. { printf ("sf_open_write failed with error : ") ;
  83. puts (sf_strerror (NULL)) ;
  84. exit (1) ;
  85. } ;
  86. for (k = 0 ; k < 256 ; k++)
  87. alaw_buffer [k] = k & 0xFF ;
  88. sf_write_raw (file, alaw_buffer, 256) ;
  89. sf_close (file) ;
  90. /* Now open that file and compare the alaw decoded sample values
  91. ** with what they should be.
  92. */
  93. if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
  94. { printf ("sf_open_write failed with error : ") ;
  95. puts (sf_strerror (NULL)) ;
  96. exit (1) ;
  97. } ;
  98. check_log_buffer_or_die (file, __LINE__) ;
  99. if (sf_read_short (file, short_buffer, 256) != 256)
  100. { printf ("sf_read_short : ") ;
  101. puts (sf_strerror (file)) ;
  102. exit (1) ;
  103. } ;
  104. for (k = 0 ; k < 256 ; k++)
  105. if (short_buffer [k] != alaw_decode (alaw_buffer [k]))
  106. { printf ("Decoder error : sample #%d (0x%02X should be 0x%02X)\n", k, short_buffer [k], alaw_decode (alaw_buffer [k])) ;
  107. exit (1) ;
  108. } ;
  109. sf_close (file) ;
  110. puts ("ok") ;
  111. unlink (filename) ;
  112. return 0 ;
  113. } /* main */
  114. /*=================================================================================
  115. ** The following routines came from the sox-12.15 (Sound eXcahcnge) distribution.
  116. **
  117. ** This code is not compiled into libsndfile. It is only used to test the
  118. ** libsndfile lookup tables for correctness.
  119. **
  120. ** I have included the original authors comments.
  121. */
  122. /*
  123. ** A-law routines by Graeme W. Gill.
  124. ** Date: 93/5/7
  125. **
  126. ** References:
  127. ** 1) CCITT Recommendation G.711
  128. **
  129. */
  130. #define ACLIP 31744
  131. static
  132. unsigned char alaw_encode (int sample)
  133. { static int exp_lut [128] =
  134. { 1, 1, 2, 2, 3, 3, 3, 3,
  135. 4, 4, 4, 4, 4, 4, 4, 4,
  136. 5, 5, 5, 5, 5, 5, 5, 5,
  137. 5, 5, 5, 5, 5, 5, 5, 5,
  138. 6, 6, 6, 6, 6, 6, 6, 6,
  139. 6, 6, 6, 6, 6, 6, 6, 6,
  140. 6, 6, 6, 6, 6, 6, 6, 6,
  141. 6, 6, 6, 6, 6, 6, 6, 6,
  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. 7, 7, 7, 7, 7, 7, 7, 7,
  149. 7, 7, 7, 7, 7, 7, 7, 7
  150. } ;
  151. int sign, exponent, mantissa ;
  152. unsigned char Alawbyte ;
  153. /* Get the sample into sign-magnitude. */
  154. sign = ((~sample) >> 8) & 0x80 ; /* set aside the sign */
  155. if (sign == 0)
  156. sample = -sample ; /* get magnitude */
  157. if (sample > ACLIP)
  158. sample = ACLIP ; /* clip the magnitude */
  159. /* Convert from 16 bit linear to ulaw. */
  160. if (sample >= 256)
  161. { exponent = exp_lut [(sample >> 8) & 0x7F] ;
  162. mantissa = (sample >> (exponent + 3)) & 0x0F ;
  163. Alawbyte = ((exponent << 4) | mantissa) ;
  164. }
  165. else
  166. Alawbyte = (sample >> 4) ;
  167. Alawbyte ^= (sign ^ 0x55) ;
  168. return Alawbyte ;
  169. } /* alaw_encode */
  170. static
  171. int alaw_decode (unsigned int Alawbyte)
  172. { static int exp_lut [8] = { 0, 264, 528, 1056, 2112, 4224, 8448, 16896 } ;
  173. int sign, exponent, mantissa, sample ;
  174. Alawbyte ^= 0x55 ;
  175. sign = (Alawbyte & 0x80) ;
  176. Alawbyte &= 0x7f ; /* get magnitude */
  177. if (Alawbyte >= 16)
  178. { exponent = (Alawbyte >> 4) & 0x07 ;
  179. mantissa = Alawbyte & 0x0F ;
  180. sample = exp_lut [exponent] + (mantissa << (exponent + 3)) ;
  181. }
  182. else
  183. sample = (Alawbyte << 4) + 8 ;
  184. if (sign == 0)
  185. sample = -sample ;
  186. return sample ;
  187. } /* alaw_decode */