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 
43 #include "cst_file.h"
44 #include "cst_error.h"
45 #include "cst_alloc.h"
46 #include "cst_endian.h"
47 #include "cst_file.h"
48 #include "cst_val.h"
49 
50 typedef struct  cst_wave_struct {
51     const char *type;
52     int sample_rate;
53     int num_samples;
54     int num_channels;
55     short *samples;
56 } cst_wave;
57 
58 typedef struct  cst_wave_header_struct {
59     const char *type;
60     int hsize;
61     int num_bytes;
62     int sample_rate;
63     int num_samples;
64     int num_channels;
65 } cst_wave_header;
66 
67 cst_wave *new_wave();
68 cst_wave *copy_wave(const cst_wave *w);
69 void delete_wave(cst_wave *val);
70 cst_wave *concat_wave(cst_wave *dest, const cst_wave *src);
71 
72 #define cst_wave_num_samples(w) (w?w->num_samples:0)
73 #define cst_wave_num_channels(w) (w?w->num_channels:0)
74 #define cst_wave_sample_rate(w) (w?w->sample_rate:0)
75 #define cst_wave_samples(w) (w->samples)
76 
77 #define cst_wave_set_num_samples(w,s) w->num_samples=s
78 #define cst_wave_set_num_channels(w,s) w->num_channels=s
79 #define cst_wave_set_sample_rate(w,s) w->sample_rate=s
80 
81 int cst_wave_save(cst_wave *w, const char *filename, const char *type);
82 int cst_wave_save_riff(cst_wave *w, const char *filename);
83 int cst_wave_save_raw(cst_wave *w, const char *filename);
84 int cst_wave_append_riff(cst_wave *w,const char *filename);
85 
86 int cst_wave_save_riff_fd(cst_wave *w, cst_file fd);
87 int cst_wave_save_raw_fd(cst_wave *w, cst_file fd);
88 
89 int cst_wave_load(cst_wave *w, const char *filename, const char *type);
90 int cst_wave_load_riff(cst_wave *w, const char *filename);
91 int cst_wave_load_raw(cst_wave *w, const char *filename,
92 				const char *bo, int sample_rate);
93 
94 int cst_wave_load_riff_header(cst_wave_header *header,cst_file fd);
95 int cst_wave_load_riff_fd(cst_wave *w, cst_file fd);
96 int cst_wave_load_raw_fd (cst_wave *w, cst_file fd,
97 				    const char *bo, int sample_rate);
98 
99 void cst_wave_resize(cst_wave *w,int samples, int num_channels);
100 void cst_wave_resample(cst_wave *w, int sample_rate);
101 void cst_wave_rescale(cst_wave *w, int factor);
102 
103 /* Resampling code */
104 typedef struct cst_rateconv_struct {
105 	int channels;           /* what do you think? */
106 	int up, down;           /* up/down sampling ratio */
107 
108 	double gain;            /* output gain */
109 	int lag;                /* lag time (in samples) */
110 	int *sin, *sout, *coep; /* filter buffers, coefficients */
111 
112 	/* n.b. outsize is the minimum buffer size for
113            cst_rateconv_out() when streaming */
114 	int insize, outsize;    /* size of filter buffers */
115 	int incount;		/* amount of input data */
116 	int len;		/* size of filter */
117 
118 	/* internal foo coefficients */
119 	double fsin, fgk, fgg;
120 	/* internal counters */
121 	int inbaseidx, inoffset, cycctr, outidx;
122 } cst_rateconv;
123 
124 cst_rateconv * new_rateconv(int up, int down, int channels);
125 void delete_rateconv(cst_rateconv *filt);
126 int cst_rateconv_in(cst_rateconv *filt, const short *inptr, int max);
127 int cst_rateconv_leadout(cst_rateconv *filt);
128 int cst_rateconv_out(cst_rateconv *filt, short *outptr, int max);
129 
130 /* File format cruft. */
131 
132 #define RIFF_FORMAT_PCM    0x0001
133 #define RIFF_FORMAT_ADPCM  0x0002
134 #define RIFF_FORMAT_MULAW  0x0006
135 #define RIFF_FORMAT_ALAW   0x0007
136 
137 /* Sun/Next header, short and sweet, note its always BIG_ENDIAN though */
138 typedef struct {
139     unsigned int    magic;	/* magic number */
140     unsigned int    hdr_size;	/* size of this header */
141     int             data_size;	/* length of data (optional) */
142     unsigned int    encoding;	/* data encoding format */
143     unsigned int    sample_rate; /* samples per second */
144     unsigned int    channels;	 /* number of interleaved channels */
145 } snd_header;
146 
147 #define CST_SND_MAGIC (unsigned int)0x2e736e64
148 #define CST_SND_ULAW  1
149 #define CST_SND_UCHAR 2
150 #define CST_SND_SHORT 3
151 
152 /* Convertion functions */
153 unsigned char cst_short_to_ulaw(short sample);
154 short cst_ulaw_to_short(unsigned char ulawbyte);
155 
156 #define CST_G721_LEADIN 8
157 unsigned char *cst_g721_decode(int *actual_size,int size,
158                                const unsigned char *packed_residual);
159 unsigned char *cst_g721_encode(int *packed_size,int actual_size,
160                                const unsigned char *unpacked_residual);
161 
162 CST_VAL_USER_TYPE_DCLS(wave,cst_wave)
163 
164 #endif
165