cst_wave.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*************************************************************************/
  2. /* */
  3. /* Language Technologies Institute */
  4. /* Carnegie Mellon University */
  5. /* Copyright (c) 1999 */
  6. /* All Rights Reserved. */
  7. /* */
  8. /* Permission is hereby granted, free of charge, to use and distribute */
  9. /* this software and its documentation without restriction, including */
  10. /* without limitation the rights to use, copy, modify, merge, publish, */
  11. /* distribute, sublicense, and/or sell copies of this work, and to */
  12. /* permit persons to whom this work is furnished to do so, subject to */
  13. /* the following conditions: */
  14. /* 1. The code must retain the above copyright notice, this list of */
  15. /* conditions and the following disclaimer. */
  16. /* 2. Any modifications must be clearly marked as such. */
  17. /* 3. Original authors' names are not deleted. */
  18. /* 4. The authors' names are not used to endorse or promote products */
  19. /* derived from this software without specific prior written */
  20. /* permission. */
  21. /* */
  22. /* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */
  23. /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
  24. /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
  25. /* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */
  26. /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
  27. /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
  28. /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
  29. /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
  30. /* THIS SOFTWARE. */
  31. /* */
  32. /*************************************************************************/
  33. /* Author: Alan W Black (awb@cs.cmu.edu) */
  34. /* Date: August 2000 */
  35. /*************************************************************************/
  36. /* */
  37. /* Waveforms */
  38. /* */
  39. /*************************************************************************/
  40. #ifndef _CST_WAVE_H__
  41. #define _CST_WAVE_H__
  42. #include "cst_file.h"
  43. #include "cst_error.h"
  44. #include "cst_alloc.h"
  45. #include "cst_endian.h"
  46. #include "cst_file.h"
  47. #include "cst_val.h"
  48. typedef struct cst_wave_struct {
  49. const char *type;
  50. int sample_rate;
  51. int num_samples;
  52. int num_channels;
  53. short *samples;
  54. } cst_wave;
  55. typedef struct cst_wave_header_struct {
  56. const char *type;
  57. int hsize;
  58. int num_bytes;
  59. int sample_rate;
  60. int num_samples;
  61. int num_channels;
  62. } cst_wave_header;
  63. cst_wave *new_wave();
  64. cst_wave *copy_wave(const cst_wave *w);
  65. void delete_wave(cst_wave *val);
  66. cst_wave *concat_wave(cst_wave *dest, const cst_wave *src);
  67. #define cst_wave_num_samples(w) (w?w->num_samples:0)
  68. #define cst_wave_num_channels(w) (w?w->num_channels:0)
  69. #define cst_wave_sample_rate(w) (w?w->sample_rate:0)
  70. #define cst_wave_samples(w) (w->samples)
  71. #define cst_wave_set_num_samples(w,s) w->num_samples=s
  72. #define cst_wave_set_num_channels(w,s) w->num_channels=s
  73. #define cst_wave_set_sample_rate(w,s) w->sample_rate=s
  74. int cst_wave_save(cst_wave *w, const char *filename, const char *type);
  75. int cst_wave_save_riff(cst_wave *w, const char *filename);
  76. int cst_wave_save_raw(cst_wave *w, const char *filename);
  77. int cst_wave_append_riff(cst_wave *w,const char *filename);
  78. int cst_wave_save_riff_fd(cst_wave *w, cst_file fd);
  79. int cst_wave_save_raw_fd(cst_wave *w, cst_file fd);
  80. int cst_wave_load(cst_wave *w, const char *filename, const char *type);
  81. int cst_wave_load_riff(cst_wave *w, const char *filename);
  82. int cst_wave_load_raw(cst_wave *w, const char *filename,
  83. const char *bo, int sample_rate);
  84. int cst_wave_load_riff_header(cst_wave_header *header,cst_file fd);
  85. int cst_wave_load_riff_fd(cst_wave *w, cst_file fd);
  86. int cst_wave_load_raw_fd (cst_wave *w, cst_file fd,
  87. const char *bo, int sample_rate);
  88. void cst_wave_resize(cst_wave *w,int samples, int num_channels);
  89. void cst_wave_resample(cst_wave *w, int sample_rate);
  90. void cst_wave_rescale(cst_wave *w, int factor);
  91. /* Resampling code */
  92. typedef struct cst_rateconv_struct {
  93. int channels; /* what do you think? */
  94. int up, down; /* up/down sampling ratio */
  95. double gain; /* output gain */
  96. int lag; /* lag time (in samples) */
  97. int *sin, *sout, *coep; /* filter buffers, coefficients */
  98. /* n.b. outsize is the minimum buffer size for
  99. cst_rateconv_out() when streaming */
  100. int insize, outsize; /* size of filter buffers */
  101. int incount; /* amount of input data */
  102. int len; /* size of filter */
  103. /* internal foo coefficients */
  104. double fsin, fgk, fgg;
  105. /* internal counters */
  106. int inbaseidx, inoffset, cycctr, outidx;
  107. } cst_rateconv;
  108. cst_rateconv * new_rateconv(int up, int down, int channels);
  109. void delete_rateconv(cst_rateconv *filt);
  110. int cst_rateconv_in(cst_rateconv *filt, const short *inptr, int max);
  111. int cst_rateconv_leadout(cst_rateconv *filt);
  112. int cst_rateconv_out(cst_rateconv *filt, short *outptr, int max);
  113. /* File format cruft. */
  114. #define RIFF_FORMAT_PCM 0x0001
  115. #define RIFF_FORMAT_ADPCM 0x0002
  116. #define RIFF_FORMAT_MULAW 0x0006
  117. #define RIFF_FORMAT_ALAW 0x0007
  118. /* Sun/Next header, short and sweet, note its always BIG_ENDIAN though */
  119. typedef struct {
  120. unsigned int magic; /* magic number */
  121. unsigned int hdr_size; /* size of this header */
  122. int data_size; /* length of data (optional) */
  123. unsigned int encoding; /* data encoding format */
  124. unsigned int sample_rate; /* samples per second */
  125. unsigned int channels; /* number of interleaved channels */
  126. } snd_header;
  127. #define CST_SND_MAGIC (unsigned int)0x2e736e64
  128. #define CST_SND_ULAW 1
  129. #define CST_SND_UCHAR 2
  130. #define CST_SND_SHORT 3
  131. /* Convertion functions */
  132. unsigned char cst_short_to_ulaw(short sample);
  133. short cst_ulaw_to_short(unsigned char ulawbyte);
  134. #define CST_G721_LEADIN 8
  135. unsigned char *cst_g721_decode(int *actual_size,int size,
  136. const unsigned char *packed_residual);
  137. unsigned char *cst_g721_encode(int *packed_size,int actual_size,
  138. const unsigned char *unpacked_residual);
  139. CST_VAL_USER_TYPE_DCLS(wave,cst_wave)
  140. #endif