src/mediastreamer/mstruespeechencoder.c

branch
gaim
changeset 20470
77693555855f
parent 13071
b98e72d4089a
parent 20469
b2836a24d81e
child 20471
1966704b3e42
equal deleted inserted replaced
13071:b98e72d4089a 20470:77693555855f
1 /*
2 Copyright 2003 Robert W. Brewer <rbrewer at op.net>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19
20 #include "mstruespeechencoder.h"
21 #include "mscodec.h"
22
23 #define TRUESPEECH_CBSIZE 32
24
25 extern MSCodecInfo TrueSpeechinfo;
26
27 static MSTrueSpeechEncoderClass *ms_truespeechencoder_class = 0;
28
29 /* FOR INTERNAL USE*/
30 void ms_truespeechencoder_init(MSTrueSpeechEncoder *r);
31 void ms_truespeechencoder_class_init(MSTrueSpeechEncoderClass *klass);
32 void ms_truespeechencoder_destroy(MSTrueSpeechEncoder *obj);
33 void ms_truespeechencoder_process(MSTrueSpeechEncoder *r);
34
35 MSFilter * ms_truespeechencoder_new(void)
36 {
37 MSTrueSpeechEncoder *r = 0;
38
39 if (!ms_truespeechencoder_class)
40 {
41 ms_truespeechencoder_class = g_new(MSTrueSpeechEncoderClass, 1);
42 ms_truespeechencoder_class_init(ms_truespeechencoder_class);
43 }
44
45 r = g_new(MSTrueSpeechEncoder, 1);
46 MS_FILTER(r)->klass = MS_FILTER_CLASS(ms_truespeechencoder_class);
47 ms_truespeechencoder_init(r);
48 return MS_FILTER(r);
49 }
50
51
52 /* FOR INTERNAL USE*/
53 void ms_truespeechencoder_init(MSTrueSpeechEncoder *r)
54 {
55 ms_filter_init(MS_FILTER(r));
56 MS_FILTER(r)->infifos = r->f_inputs;
57 MS_FILTER(r)->outfifos = r->f_outputs;
58
59 WAVEFORMATEX* wf = ms_truespeechencoder_wf_create();
60
61 r->codec = win32codec_create(wf, 1);
62 free(wf);
63
64 MS_FILTER(r)->r_mingran = r->codec->min_insize;
65
66 MS_FILTER_CLASS(ms_truespeechencoder_class)->r_maxgran =
67 r->codec->min_insize;
68 MS_FILTER_CLASS(ms_truespeechencoder_class)->w_maxgran =
69 r->codec->min_outsize;
70
71 memset(r->f_inputs, 0, sizeof(MSFifo*) * MS_TRUESPEECH_CODEC_MAX_IN_OUT);
72 memset(r->f_outputs, 0, sizeof(MSFifo*) * MS_TRUESPEECH_CODEC_MAX_IN_OUT);
73 }
74
75 void ms_truespeechencoder_class_init(MSTrueSpeechEncoderClass *klass)
76 {
77 ms_filter_class_init(MS_FILTER_CLASS(klass));
78 ms_filter_class_set_name(MS_FILTER_CLASS(klass), "TrueSpeechEncoder");
79 MS_FILTER_CLASS(klass)->max_finputs = MS_TRUESPEECH_CODEC_MAX_IN_OUT;
80 MS_FILTER_CLASS(klass)->max_foutputs = MS_TRUESPEECH_CODEC_MAX_IN_OUT;
81 MS_FILTER_CLASS(klass)->r_maxgran = 0; /* filled in by first instance */
82 MS_FILTER_CLASS(klass)->w_maxgran = 0; /* filled in by first instance */
83 MS_FILTER_CLASS(klass)->destroy = (MSFilterDestroyFunc)ms_truespeechencoder_destroy;
84 MS_FILTER_CLASS(klass)->process = (MSFilterProcessFunc)ms_truespeechencoder_process;
85 MS_FILTER_CLASS(klass)->info = MS_FILTER_INFO(&TrueSpeechinfo);
86 klass->driver = win32codec_create_driver(TRUESPEECH_DLL,
87 TRUESPEECH_FORMAT_TAG, 1);
88 }
89
90 void ms_truespeechencoder_process(MSTrueSpeechEncoder *r)
91 {
92 MSFifo *fi,*fo;
93 int err1;
94 void *s,*d;
95
96 /* process output fifos, but there is only one for this class of filter*/
97
98 fi = r->f_inputs[0];
99 fo = r->f_outputs[0];
100 if (fi)
101 {
102 err1 = ms_fifo_get_read_ptr(fi, r->codec->min_insize, &s);
103 if (err1 > 0)
104 {
105 err1 = ms_fifo_get_write_ptr(fo, r->codec->min_outsize, &d);
106 if (d)
107 {
108 signed long n;
109
110 n = win32codec_convert(r->codec,
111 s, r->codec->min_insize,
112 d, r->codec->min_outsize);
113 }
114 }
115
116 }
117 }
118
119
120
121 void ms_truespeechencoder_uninit(MSTrueSpeechEncoder *obj)
122 {
123 win32codec_destroy(obj->codec);
124 }
125
126 void ms_truespeechencoder_destroy(MSTrueSpeechEncoder *obj)
127 {
128 ms_truespeechencoder_uninit(obj);
129 g_free(obj);
130 }
131
132
133 WAVEFORMATEX* ms_truespeechencoder_wf_create()
134 {
135 WAVEFORMATEX* ts_wf = 0;
136 long* iptr = 0;
137
138 ts_wf = malloc(sizeof(WAVEFORMATEX) + TRUESPEECH_CBSIZE);
139 if (!ts_wf)
140 {
141 return 0;
142 }
143
144 memset(ts_wf, 0, sizeof(*ts_wf) + TRUESPEECH_CBSIZE);
145
146 ts_wf->wFormatTag = TRUESPEECH_FORMAT_TAG;
147 ts_wf->nChannels = 1;
148 ts_wf->nSamplesPerSec = 8000;
149 ts_wf->wBitsPerSample = 1;
150 ts_wf->nBlockAlign = 32;
151 ts_wf->nAvgBytesPerSec = 1067;
152 ts_wf->cbSize = TRUESPEECH_CBSIZE;
153
154 /* write extra data needed by TrueSpeech codec found
155 from examining a TrueSpeech .wav file header
156 */
157 iptr = (long*)(ts_wf + 1);
158 *iptr = 0x00f00001;
159
160 return ts_wf;
161 }

mercurial