ulaw_test.c 7.0 KB

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