io-sparc.c
Upload User: hbbaokai
Upload Date: 2009-07-13
Package Size: 328k
Code Size: 4k
Category:

VOIP program

Development Platform:

Visual C++

  1. /*************************************************************************/
  2. /*                                                                       */
  3. /*                            LD-CELP  G.728                             */
  4. /*                                                                       */
  5. /*    Low-Delay Code Excitation Linear Prediction speech compression.    */
  6. /*                                                                       */
  7. /*                 Copyright: Analog Devices, Inc., 1993                 */
  8. /*                                                                       */
  9. /*                         Author: Alex Zatsman.                         */
  10. /*                                                                       */
  11. /*  This program was written mostly for testing  Analog Devices' g21k C  */
  12. /*  compiler for the  ADSP21000 architecture  family. While the program  */
  13. /*  works  on  Sparc and ADSP21020, it  has  NOT  been  tested with the  */
  14. /*  official test data from CCITT/ITU.                                   */
  15. /*                                                                       */
  16. /*  The program  is   distributed as  is,  WITHOUT ANY WARRANTY, EITHER  */
  17. /*  EXPLICIT OR IMPLIED.                                                 */
  18. /*                                                                       */
  19. /*************************************************************************/
  20. #include <fcntl.h>
  21. #include <stdio.h>
  22. #include <multimedia/libaudio.h>
  23. #include <multimedia/ulaw2linear.h>
  24. #include "common.h"
  25. #include "prototyp.h"
  26. extern int fprintf();
  27. extern void audio_read_filehdr();
  28. extern int read(int, char*, int);
  29. real rscale=0.1;  /* Scaling factor for input */
  30. char * xfile_name;
  31. #ifdef CODER
  32. int oxfd = 0; /* output file (codebook indices) */
  33. int ifd  = 1; /* input file */
  34. char *ifile_name;
  35. void
  36. init_input()
  37. {
  38.     Audio_hdr hdr;
  39.     int hdr_len = 100;
  40.     
  41.     if ((ifd=open(ifile_name, O_RDONLY)) < 0) {
  42. (void) fprintf(stderr, "Can't open "%s"n", ifile_name);
  43. exit(1);
  44.     }
  45.     audio_read_filehdr(ifd, &hdr, 0, &hdr_len);
  46. #ifdef TEST
  47.     fprintf(stderr, "sample_ratet= %dn", hdr.sample_rate);
  48.     fprintf(stderr, "samples_per_unitt= %dn", hdr.samples_per_unit);
  49.     fprintf(stderr, "bytes_per_unitt= %dn", hdr.bytes_per_unit);
  50.     fprintf(stderr, "channelst= %dn", hdr.channels);
  51.     fprintf(stderr, "encodingt= %dn", hdr.encoding);
  52.     fprintf(stderr, "data_sizet= %dn", hdr.data_size);
  53. #endif 
  54.     
  55.     if ((oxfd=open(xfile_name, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) {
  56. (void) fprintf(stderr, "Can't open "%s"n", xfile_name);
  57.     }
  58. }
  59. void
  60. put_index(int x)
  61. {
  62.     short sx = x;
  63.     write(oxfd, &sx, 2);
  64. }
  65. #endif
  66. #ifdef DECODER
  67. char * ofile_name;
  68. static int ofd=1; /* Outpu file */
  69. static ixfd = 0; /* Input file (codebook indices) */
  70. int sound_overflow = 0;
  71. void init_output()
  72. {
  73.     sound_overflow = 0;
  74.     if ((ofd=open(ofile_name, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) {
  75. extern  int errno;
  76. extern char *sys_errlist[];
  77. int ee = errno;
  78. (void) fprintf(stderr, "Can't open "%s" for outputn", ofile_name);
  79. printf(sys_errlist[ee]);
  80. exit(1);
  81.     }
  82.     if ((ixfd = open(xfile_name, O_RDONLY)) < 0) {
  83. (void) fprintf(stderr, "Can't open "%s"n", xfile_name);
  84. exit(3);
  85.     }
  86. }
  87. int get_index()
  88. {
  89.     short sx;
  90.     if (read(ixfd, (char*)&sx, sizeof(sx)) < sizeof(sx))
  91. return -1;
  92.     return (int)sx;
  93. }
  94. #endif
  95. /* Return Number of Samples Read */
  96. #ifdef CODER
  97. int read_sound_buffer(int n, real buf[])
  98. {
  99.     unsigned char ch;
  100.     short s;
  101.     int i, c=0;
  102.     
  103.     for (i=0; i<n && read(ifd, &ch, 1) > 0; i++) {
  104. s = audio_u2s(ch);
  105. buf[c++] =  rscale * (real) s;
  106.     }
  107.     return c;
  108. }
  109. #endif
  110. #ifdef DECODER
  111. int
  112. write_sound_buffer(int n, real buf[])
  113. {
  114.     unsigned char ch;
  115.     int i, c=0;
  116.     
  117.     for (i=0; i<n; i++) {
  118. float xx = buf[i]/rscale;
  119. short s;
  120. if (xx < - 0x7fff || xx > 0x7fff)
  121.     sound_overflow = 1;
  122. s = (short) xx;
  123. ch = audio_s2u(s);
  124. write(ofd, &ch, 1);
  125.     }
  126.     return c;
  127. }
  128. #endif