source: roaraudio/libroardsp/transcode_speex.c @ 2311:883f5e3c33f0

Last change on this file since 2311:883f5e3c33f0 was 2311:883f5e3c33f0, checked in by phi, 15 years ago

added a lot debug lions, commented out parts making the speex xcoder unusable

File size: 7.5 KB
Line 
1//transcode_speex.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009
5 *
6 *  This file is part of libroardsp a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroardsp is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25#include "libroardsp.h"
26
27#ifdef ROAR_HAVE_LIBSPEEX
28
29#define _16BIT (16/8)
30#define _SIZE_LEN 2
31
32int roar_xcoder_speex_init       (struct roar_xcoder * state) {
33 struct roar_xcoder_speex * self = malloc(sizeof(struct roar_xcoder_speex));
34 struct roar_audio_info  * info = &(state->info.pcm);
35 int tmp;
36
37 ROAR_DBG("roar_xcoder_speex_init(*): sizeof(*self) = %lu", (unsigned long)sizeof(*self));
38
39 if ( self == NULL )
40  return -1;
41
42 // curruntly only 16 bit mode is supported
43 if ( info->bits != 16 ) {
44  free(self);
45  return -1;
46 }
47
48 // only mono/stereo mode is supported
49 switch (info->channels) {
50  case 1: self->stereo = 0; break;
51  case 2: self->stereo = 1; break;
52  default:
53    free(self);
54    return -1;
55 }
56
57 memset(self, 0, sizeof(struct roar_xcoder_speex));
58
59 state->inst = self;
60
61 self->mode  = ROAR_SPEEX_MODE_UWB;
62
63 if (state->encode) {
64  switch (self->mode) {
65   case ROAR_SPEEX_MODE_NB:  self->xcoder = speex_encoder_init(&speex_nb_mode);  break;
66   case ROAR_SPEEX_MODE_WB:  self->xcoder = speex_encoder_init(&speex_wb_mode);  break;
67   case ROAR_SPEEX_MODE_UWB: self->xcoder = speex_encoder_init(&speex_uwb_mode); break;
68  }
69  tmp = 8;
70  speex_encoder_ctl(self->xcoder, SPEEX_SET_QUALITY,       &tmp);
71  tmp = info->rate;
72  speex_encoder_ctl(self->xcoder, SPEEX_SET_SAMPLING_RATE, &tmp);
73  speex_encoder_ctl(self->xcoder, SPEEX_GET_FRAME_SIZE,    &(self->frame_size));
74 } else {
75  self->xcoder = NULL;
76 }
77
78 speex_bits_init(&(self->bits));
79
80 return 0;
81}
82
83int roar_xcoder_speex_uninit     (struct roar_xcoder * state) {
84 struct roar_xcoder_speex * self = state->inst;
85
86 if ( self->xcoder != NULL ) {
87  if (state->encode) {
88   speex_encoder_destroy(self->xcoder);
89  } else {
90   speex_decoder_destroy(self->xcoder);
91  }
92 }
93
94 speex_bits_destroy(&(self->bits));
95
96 free(self);
97
98 return 0;
99}
100
101int roar_xcoder_speex_packet_size(struct roar_xcoder * state, int samples) {
102 struct roar_xcoder_speex * self = state->inst;
103
104 if (!state->encode)
105  if (state->stage != ROAR_XCODER_STAGE_OPENED)
106   return -1;
107
108 return _16BIT * self->frame_size * (self->stereo ? 2 : 1);
109}
110
111int roar_xcoder_speex_encode     (struct roar_xcoder * state, void * buf, size_t len) {
112 struct roar_xcoder_speex * self = state->inst;
113 uint16_t tmp_net;
114 int pkg_len;
115
116 if (!state->encode)
117  return -1;
118
119 ROAR_DBG("roar_xcoder_speex_encode(*): Encoding...");
120
121 if ( state->stage == ROAR_XCODER_STAGE_INITED ) {
122  if ( roar_vio_write(state->backend, ROAR_SPEEX_MAGIC, ROAR_SPEEX_MAGIC_LEN) != ROAR_SPEEX_MAGIC_LEN )
123   return -1;
124  state->stage = ROAR_XCODER_STAGE_MAGIC;
125  ROAR_DBG("roar_xcoder_speex_encode(*): Wrote MAGIC");
126
127  state->stage = ROAR_XCODER_STAGE_OPENING;
128
129  tmp_net = ROAR_HOST2NET16(self->mode);
130  if ( roar_vio_write(state->backend, &tmp_net, 2) != 2 )
131   return -1;
132
133  state->stage = ROAR_XCODER_STAGE_OPENED;
134 }
135
136 speex_bits_reset(&(self->bits));
137
138 if ( self->stereo )
139  speex_encode_stereo_int((spx_int16_t *) buf, self->frame_size, &(self->bits));
140
141 speex_encode_int(self->xcoder, (spx_int16_t *) buf, &(self->bits));
142
143 pkg_len = speex_bits_write(&(self->bits), self->cc, ROAR_SPEEX_MAX_CC);
144
145 tmp_net = ROAR_HOST2NET16(pkg_len);
146
147 if ( roar_vio_write(state->backend, &tmp_net, 2) != 2 )
148  return -1;
149
150 if ( roar_vio_write(state->backend, self->cc, pkg_len) != pkg_len )
151   return -1;
152
153 return 0;
154}
155
156// TODO: move all the init thingys into a seperate function
157int roar_xcoder_speex_decode     (struct roar_xcoder * state, void * buf, size_t len) {
158 struct roar_xcoder_speex * self = state->inst;
159 char magic[ROAR_SPEEX_MAGIC_LEN];
160 uint16_t tmp_net;
161 int pkg_len;
162 int tmp;
163 SpeexStereoState stereo = SPEEX_STEREO_STATE_INIT;
164 SpeexCallback callback;
165
166 ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len);
167
168 if ( state->stage == ROAR_XCODER_STAGE_INITED ) {
169  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = INITED", state, buf, (unsigned long)len);
170  if ( roar_vio_read(state->backend, magic, ROAR_SPEEX_MAGIC_LEN) != ROAR_SPEEX_MAGIC_LEN )
171   return -1;
172
173  if ( memcmp(magic, ROAR_SPEEX_MAGIC, ROAR_SPEEX_MAGIC_LEN) != 0 )
174   return -1;
175
176  state->stage = ROAR_XCODER_STAGE_MAGIC;
177  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = MAGIC", state, buf, (unsigned long)len);
178
179  if ( roar_vio_read(state->backend, &tmp_net, 2) != 2 )
180   return -1;
181
182  self->mode = ROAR_NET2HOST16(tmp_net);
183
184  state->stage = ROAR_XCODER_STAGE_OPENING;
185  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = OPENING", state, buf, (unsigned long)len);
186
187  switch (self->mode) {
188   case ROAR_SPEEX_MODE_NB:  self->xcoder = speex_decoder_init(&speex_nb_mode);  break;
189   case ROAR_SPEEX_MODE_WB:  self->xcoder = speex_decoder_init(&speex_wb_mode);  break;
190   case ROAR_SPEEX_MODE_UWB: self->xcoder = speex_decoder_init(&speex_uwb_mode); break;
191   default:
192     return -1;
193    break;
194  }
195
196  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): opened decoder state", state, buf, (unsigned long)len);
197
198  tmp=1;
199  speex_decoder_ctl(self->xcoder, SPEEX_SET_ENH, &tmp);
200/*
201// FIXME: why does this make the decoder segfaul?
202  tmp = state->info.pcm.rate;
203  speex_encoder_ctl(self->xcoder, SPEEX_SET_SAMPLING_RATE, &tmp);
204*/
205  speex_decoder_ctl(self->xcoder, SPEEX_GET_FRAME_SIZE, &(self->frame_size));
206
207  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): self->stereo = %i", state, buf, (unsigned long)len, self->stereo);
208
209  if ( self->stereo ) {
210   memcpy(&(self->stereo_state), &stereo, sizeof(self->stereo_state));
211
212   callback.callback_id = SPEEX_INBAND_STEREO;
213   callback.func = speex_std_stereo_request_handler;
214   callback.data = &(self->stereo_state);
215
216   speex_decoder_ctl(self->xcoder, SPEEX_SET_HANDLER, &callback);
217  }
218
219  state->stage = ROAR_XCODER_STAGE_OPENED;
220  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = OPENED", state, buf, (unsigned long)len);
221 }
222
223 ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = %s", state, buf, (unsigned long)len,
224          state->stage == ROAR_XCODER_STAGE_OPENED ? "OPENED" : "???"
225         );
226
227 if ( roar_vio_read(state->backend, &tmp_net, 2) != 2 )
228  return -1;
229
230 pkg_len = ROAR_NET2HOST16(tmp_net);
231
232 if ( pkg_len > ROAR_SPEEX_MAX_CC )
233  return -1;
234
235 ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len);
236
237 if ( roar_vio_read(state->backend, self->cc, pkg_len) != pkg_len )
238  return -1;
239
240 speex_bits_read_from(&(self->bits), self->cc, pkg_len);
241
242 speex_decode_int(self->xcoder, &(self->bits), buf);
243
244 if ( self->stereo ) {
245  speex_decode_stereo_int(buf, self->frame_size, &(self->stereo_state));
246 }
247
248 ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu) = 0", state, buf, (unsigned long)len);
249 return 0;
250}
251
252#endif
253
254//ll
Note: See TracBrowser for help on using the repository browser.