ulaw_test.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 ulaw_encode (int sample) ;
  29. static int ulaw_decode (unsigned int ulawbyte) ;
  30. static short short_buffer [BUFFER_SIZE] ;
  31. static unsigned char ulaw_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 ("ulaw_test", "encoder") ;
  39. filename = "test.raw" ;
  40. sf_info_setup (&sfinfo, SF_FORMAT_RAW | SF_FORMAT_ULAW, 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 ulaw 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 ulaw 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, ulaw_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 (ulaw_encode (short_buffer [k]) != ulaw_buffer [k])
  70. { printf ("Encoder error : sample #%d (0x%02X should be 0x%02X)\n", k, ulaw_buffer [k], ulaw_encode (short_buffer [k])) ;
  71. exit (1) ;
  72. } ;
  73. sf_close (file) ;
  74. puts ("ok") ;
  75. print_test_name ("ulaw_test", "decoder") ;
  76. /* Now generate a file containing all possible 8 bit encoded
  77. ** sample values and write it to disk as ulaw 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. ulaw_buffer [k] = k & 0xFF ;
  86. sf_write_raw (file, ulaw_buffer, 256) ;
  87. sf_close (file) ;
  88. /* Now open that file and compare the ulaw 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] != ulaw_decode (ulaw_buffer [k]))
  104. { printf ("Decoder error : sample #%d (0x%04X should be 0x%04X)\n", k, short_buffer [k], ulaw_decode (ulaw_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. ** This routine converts from linear to ulaw.
  122. **
  123. ** Craig Reese: IDA/Supercomputing Research Center
  124. ** Joe Campbell: Department of Defense
  125. ** 29 September 1989
  126. **
  127. ** References:
  128. ** 1) CCITT Recommendation G.711 (very difficult to follow)
  129. ** 2) "A New Digital Technique for Implementation of Any
  130. ** Continuous PCM Companding Law," Villeret, Michel,
  131. ** et al. 1973 IEEE Int. Conf. on Communications, Vol 1,
  132. ** 1973, pg. 11.12-11.17
  133. ** 3) MIL-STD-188-113,"Interoperability and Performance Standards
  134. ** for Analog-to_Digital Conversion Techniques,"
  135. ** 17 February 1987
  136. **
  137. ** Input: Signed 16 bit linear sample
  138. ** Output: 8 bit ulaw sample
  139. */
  140. #define uBIAS 0x84 /* define the add-in bias for 16 bit.frames */
  141. #define uCLIP 32635
  142. static
  143. unsigned char ulaw_encode (int sample)
  144. { static int exp_lut [256] =
  145. { 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  146. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  147. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  148. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  149. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  150. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  151. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  152. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  153. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  154. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  155. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  156. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  157. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  158. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  159. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  160. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
  161. } ;
  162. int sign, exponent, mantissa ;
  163. unsigned char ulawbyte ;
  164. /* Get the sample into sign-magnitude. */
  165. sign = (sample >> 8) & 0x80 ; /* set aside the sign */
  166. if (sign != 0)
  167. sample = -sample ; /* get magnitude */
  168. if (sample > uCLIP)
  169. sample = uCLIP ; /* clip the magnitude */
  170. /* Convert from 16 bit linear to ulaw. */
  171. sample = sample + uBIAS ;
  172. exponent = exp_lut [(sample >> 7) & 0xFF] ;
  173. mantissa = (sample >> (exponent + 3)) & 0x0F ;
  174. ulawbyte = ~ (sign | (exponent << 4) | mantissa) ;
  175. return ulawbyte ;
  176. } /* ulaw_encode */
  177. /*
  178. ** This routine converts from ulaw to 16 bit linear.
  179. **
  180. ** Craig Reese: IDA/Supercomputing Research Center
  181. ** 29 September 1989
  182. **
  183. ** References:
  184. ** 1) CCITT Recommendation G.711 (very difficult to follow)
  185. ** 2) MIL-STD-188-113,"Interoperability and Performance Standards
  186. ** for Analog-to_Digital Conversion Techniques,"
  187. ** 17 February 1987
  188. **
  189. ** Input: 8 bit ulaw sample
  190. ** Output: signed 16 bit linear sample
  191. */
  192. static
  193. int ulaw_decode (unsigned int ulawbyte)
  194. { static int exp_lut [8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 } ;
  195. int sign, exponent, mantissa, sample ;
  196. ulawbyte = ~ ulawbyte ;
  197. sign = (ulawbyte & 0x80) ;
  198. exponent = (ulawbyte >> 4) & 0x07 ;
  199. mantissa = ulawbyte & 0x0F ;
  200. sample = exp_lut [exponent] + (mantissa << (exponent + 3)) ;
  201. if (sign != 0)
  202. sample = -sample ;
  203. return sample ;
  204. } /* ulaw_decode */