source: roaraudio/libroardsp/transcode_speex.c @ 2314:93120f7a0387

Last change on this file since 2314:93120f7a0387 was 2314:93120f7a0387, checked in by phi, 15 years ago

don't set sample rate...

File size: 7.6 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/*
72// TODO: foind out if this works
73  tmp = info->rate;
74  speex_encoder_ctl(self->xcoder, SPEEX_SET_SAMPLING_RATE, &tmp);
75*/
76  speex_encoder_ctl(self->xcoder, SPEEX_GET_FRAME_SIZE,    &(self->frame_size));
77 } else {
78  self->xcoder = NULL;
79 }
80
81 speex_bits_init(&(self->bits));
82
83 return 0;
84}
85
86int roar_xcoder_speex_uninit     (struct roar_xcoder * state) {
87 struct roar_xcoder_speex * self = state->inst;
88
89 if ( self->xcoder != NULL ) {
90  if (state->encode) {
91   speex_encoder_destroy(self->xcoder);
92  } else {
93   speex_decoder_destroy(self->xcoder);
94  }
95 }
96
97 speex_bits_destroy(&(self->bits));
98
99 free(self);
100
101 return 0;
102}
103
104int roar_xcoder_speex_packet_size(struct roar_xcoder * state, int samples) {
105 struct roar_xcoder_speex * self = state->inst;
106
107 if (!state->encode)
108  if (state->stage != ROAR_XCODER_STAGE_OPENED)
109   return -1;
110
111 return _16BIT * self->frame_size * (self->stereo ? 2 : 1);
112}
113
114int roar_xcoder_speex_encode     (struct roar_xcoder * state, void * buf, size_t len) {
115 struct roar_xcoder_speex * self = state->inst;
116 uint16_t tmp_net;
117 int pkg_len;
118
119 if (!state->encode)
120  return -1;
121
122 ROAR_DBG("roar_xcoder_speex_encode(*): Encoding...");
123
124 if ( state->stage == ROAR_XCODER_STAGE_INITED ) {
125  if ( roar_vio_write(state->backend, ROAR_SPEEX_MAGIC, ROAR_SPEEX_MAGIC_LEN) != ROAR_SPEEX_MAGIC_LEN )
126   return -1;
127  state->stage = ROAR_XCODER_STAGE_MAGIC;
128  ROAR_DBG("roar_xcoder_speex_encode(*): Wrote MAGIC");
129
130  state->stage = ROAR_XCODER_STAGE_OPENING;
131
132  tmp_net = ROAR_HOST2NET16(self->mode);
133  if ( roar_vio_write(state->backend, &tmp_net, 2) != 2 )
134   return -1;
135
136  state->stage = ROAR_XCODER_STAGE_OPENED;
137 }
138
139 speex_bits_reset(&(self->bits));
140
141 if ( self->stereo )
142  speex_encode_stereo_int((spx_int16_t *) buf, self->frame_size, &(self->bits));
143
144 speex_encode_int(self->xcoder, (spx_int16_t *) buf, &(self->bits));
145
146 pkg_len = speex_bits_write(&(self->bits), self->cc, ROAR_SPEEX_MAX_CC);
147
148 tmp_net = ROAR_HOST2NET16(pkg_len);
149
150 if ( roar_vio_write(state->backend, &tmp_net, 2) != 2 )
151  return -1;
152
153 if ( roar_vio_write(state->backend, self->cc, pkg_len) != pkg_len )
154   return -1;
155
156 return 0;
157}
158
159// TODO: move all the init thingys into a seperate function
160int roar_xcoder_speex_decode     (struct roar_xcoder * state, void * buf, size_t len) {
161 struct roar_xcoder_speex * self = state->inst;
162 char magic[ROAR_SPEEX_MAGIC_LEN];
163 uint16_t tmp_net;
164 int pkg_len;
165 int tmp;
166 SpeexStereoState stereo = SPEEX_STEREO_STATE_INIT;
167 SpeexCallback callback;
168
169 ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len);
170
171 if ( state->stage == ROAR_XCODER_STAGE_INITED ) {
172  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = INITED", state, buf, (unsigned long)len);
173  if ( roar_vio_read(state->backend, magic, ROAR_SPEEX_MAGIC_LEN) != ROAR_SPEEX_MAGIC_LEN )
174   return -1;
175
176  if ( memcmp(magic, ROAR_SPEEX_MAGIC, ROAR_SPEEX_MAGIC_LEN) != 0 )
177   return -1;
178
179  state->stage = ROAR_XCODER_STAGE_MAGIC;
180  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = MAGIC", state, buf, (unsigned long)len);
181
182  if ( roar_vio_read(state->backend, &tmp_net, 2) != 2 )
183   return -1;
184
185  self->mode = ROAR_NET2HOST16(tmp_net);
186
187  state->stage = ROAR_XCODER_STAGE_OPENING;
188  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = OPENING", state, buf, (unsigned long)len);
189
190  switch (self->mode) {
191   case ROAR_SPEEX_MODE_NB:  self->xcoder = speex_decoder_init(&speex_nb_mode);  break;
192   case ROAR_SPEEX_MODE_WB:  self->xcoder = speex_decoder_init(&speex_wb_mode);  break;
193   case ROAR_SPEEX_MODE_UWB: self->xcoder = speex_decoder_init(&speex_uwb_mode); break;
194   default:
195     return -1;
196    break;
197  }
198
199  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): opened decoder state", state, buf, (unsigned long)len);
200
201  tmp=1;
202  speex_decoder_ctl(self->xcoder, SPEEX_SET_ENH, &tmp);
203/*
204// FIXME: why does this make the decoder segfaul?
205  tmp = state->info.pcm.rate;
206  speex_encoder_ctl(self->xcoder, SPEEX_SET_SAMPLING_RATE, &tmp);
207*/
208  speex_decoder_ctl(self->xcoder, SPEEX_GET_FRAME_SIZE, &(self->frame_size));
209
210  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): self->stereo = %i", state, buf, (unsigned long)len, self->stereo);
211
212  if ( self->stereo ) {
213   memcpy(&(self->stereo_state), &stereo, sizeof(self->stereo_state));
214
215   callback.callback_id = SPEEX_INBAND_STEREO;
216   callback.func = speex_std_stereo_request_handler;
217   callback.data = &(self->stereo_state);
218
219   speex_decoder_ctl(self->xcoder, SPEEX_SET_HANDLER, &callback);
220  }
221
222  state->stage = ROAR_XCODER_STAGE_OPENED;
223  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = OPENED", state, buf, (unsigned long)len);
224 }
225
226 ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = %s", state, buf, (unsigned long)len,
227          state->stage == ROAR_XCODER_STAGE_OPENED ? "OPENED" : "???"
228         );
229
230 if ( roar_vio_read(state->backend, &tmp_net, 2) != 2 )
231  return -1;
232
233 pkg_len = ROAR_NET2HOST16(tmp_net);
234
235 if ( pkg_len > ROAR_SPEEX_MAX_CC )
236  return -1;
237
238 ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len);
239
240 if ( roar_vio_read(state->backend, self->cc, pkg_len) != pkg_len )
241  return -1;
242
243 speex_bits_read_from(&(self->bits), self->cc, pkg_len);
244
245 speex_decode_int(self->xcoder, &(self->bits), buf);
246
247 if ( self->stereo ) {
248  speex_decode_stereo_int(buf, self->frame_size, &(self->stereo_state));
249 }
250
251 ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu) = 0", state, buf, (unsigned long)len);
252 return 0;
253}
254
255#endif
256
257//ll
Note: See TracBrowser for help on using the repository browser.