[2295] | 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 | |
---|
[2296] | 32 | int roar_xcoder_speex_init (struct roar_xcoder * state) { |
---|
[2298] | 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 | if ( self == NULL ) |
---|
| 38 | return -1; |
---|
| 39 | |
---|
| 40 | // curruntly only 16 bit mode is supported |
---|
| 41 | if ( info->bits != 16 ) { |
---|
| 42 | free(self); |
---|
| 43 | return -1; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | // curruntly only mono mode is supported |
---|
| 47 | if ( info->channels != 1 ) { |
---|
| 48 | free(self); |
---|
| 49 | return -1; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | memset(self, 0, sizeof(struct roar_xcoder_speex)); |
---|
| 53 | |
---|
| 54 | state->inst = self; |
---|
| 55 | |
---|
| 56 | self->mode = ROAR_SPEEX_MODE_UWB; |
---|
| 57 | |
---|
| 58 | if (state->encode) { |
---|
| 59 | switch (self->mode) { |
---|
| 60 | case ROAR_SPEEX_MODE_NB: self->xcoder = speex_encoder_init(&speex_nb_mode); break; |
---|
| 61 | case ROAR_SPEEX_MODE_WB: self->xcoder = speex_encoder_init(&speex_wb_mode); break; |
---|
| 62 | case ROAR_SPEEX_MODE_UWB: self->xcoder = speex_encoder_init(&speex_uwb_mode); break; |
---|
| 63 | } |
---|
| 64 | tmp = 8; |
---|
| 65 | speex_encoder_ctl(self->xcoder, SPEEX_SET_QUALITY, &tmp); |
---|
| 66 | speex_encoder_ctl(self->xcoder, SPEEX_GET_FRAME_SIZE, &(self->frame_size)); |
---|
| 67 | } else { |
---|
| 68 | self->xcoder = NULL; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | speex_bits_init(&(self->bits)); |
---|
| 72 | |
---|
| 73 | return 0; |
---|
[2296] | 74 | } |
---|
| 75 | |
---|
| 76 | int roar_xcoder_speex_uninit (struct roar_xcoder * state) { |
---|
[2298] | 77 | struct roar_xcoder_speex * self = state->inst; |
---|
| 78 | |
---|
| 79 | if ( self->xcoder != NULL ) { |
---|
| 80 | if (state->encode) { |
---|
| 81 | speex_encoder_destroy(self->xcoder); |
---|
| 82 | } else { |
---|
| 83 | speex_decoder_destroy(self->xcoder); |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | speex_bits_destroy(&(self->bits)); |
---|
| 88 | |
---|
| 89 | free(self); |
---|
| 90 | |
---|
| 91 | return 0; |
---|
[2296] | 92 | } |
---|
| 93 | |
---|
| 94 | int roar_xcoder_speex_packet_size(struct roar_xcoder * state, int samples) { |
---|
[2298] | 95 | struct roar_xcoder_speex * self = state->inst; |
---|
[2300] | 96 | |
---|
| 97 | if (!state->encode) |
---|
| 98 | if (state->stage != ROAR_XCODER_STAGE_OPENED) |
---|
| 99 | return -1; |
---|
[2298] | 100 | |
---|
| 101 | return _16BIT * self->frame_size; |
---|
[2296] | 102 | } |
---|
| 103 | |
---|
| 104 | int roar_xcoder_speex_encode (struct roar_xcoder * state, void * buf, size_t len) { |
---|
[2299] | 105 | struct roar_xcoder_speex * self = state->inst; |
---|
| 106 | uint16_t tmp_net; |
---|
| 107 | int pkg_len; |
---|
| 108 | |
---|
| 109 | if (!state->encode) |
---|
| 110 | return -1; |
---|
| 111 | |
---|
| 112 | ROAR_DBG("roar_xcoder_speex_encode(*): Encoding..."); |
---|
| 113 | |
---|
| 114 | if ( state->stage == ROAR_XCODER_STAGE_INITED ) { |
---|
| 115 | if ( roar_vio_write(state->backend, ROAR_SPEEX_MAGIC, ROAR_SPEEX_MAGIC_LEN) != ROAR_SPEEX_MAGIC_LEN ) |
---|
| 116 | return -1; |
---|
| 117 | state->stage = ROAR_XCODER_STAGE_MAGIC; |
---|
| 118 | ROAR_DBG("roar_xcoder_speex_encode(*): Wrote MAGIC"); |
---|
| 119 | |
---|
| 120 | state->stage = ROAR_XCODER_STAGE_OPENING; |
---|
| 121 | |
---|
| 122 | tmp_net = ROAR_HOST2NET16(self->mode); |
---|
| 123 | if ( roar_vio_write(state->backend, &tmp_net, 2) != 2 ) |
---|
| 124 | return -1; |
---|
| 125 | |
---|
| 126 | state->stage = ROAR_XCODER_STAGE_OPENED; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | speex_bits_reset(&(self->bits)); |
---|
| 130 | |
---|
| 131 | speex_encode_int(self->xcoder, (spx_int16_t *) buf, &(self->bits)); |
---|
| 132 | |
---|
| 133 | pkg_len = speex_bits_write(&(self->bits), self->cc, ROAR_SPEEX_MAX_CC); |
---|
| 134 | |
---|
| 135 | tmp_net = ROAR_HOST2NET16(pkg_len); |
---|
| 136 | |
---|
| 137 | if ( roar_vio_write(state->backend, &tmp_net, 2) != 2 ) |
---|
| 138 | return -1; |
---|
| 139 | |
---|
| 140 | if ( roar_vio_write(state->backend, self->cc, pkg_len) != pkg_len ) |
---|
| 141 | return -1; |
---|
| 142 | |
---|
| 143 | return 0; |
---|
[2296] | 144 | } |
---|
| 145 | |
---|
| 146 | int roar_xcoder_speex_decode (struct roar_xcoder * state, void * buf, size_t len) { |
---|
[2300] | 147 | struct roar_xcoder_speex * self = state->inst; |
---|
| 148 | char magic[ROAR_SPEEX_MAGIC_LEN]; |
---|
| 149 | uint16_t tmp_net; |
---|
| 150 | int pkg_len; |
---|
| 151 | int tmp; |
---|
| 152 | |
---|
| 153 | if ( state->stage == ROAR_XCODER_STAGE_INITED ) { |
---|
| 154 | if ( roar_vio_read(state->backend, magic, ROAR_SPEEX_MAGIC_LEN) != ROAR_SPEEX_MAGIC_LEN ) |
---|
| 155 | return -1; |
---|
| 156 | |
---|
| 157 | if ( memcmp(magic, ROAR_SPEEX_MAGIC, ROAR_SPEEX_MAGIC_LEN) != 0 ) |
---|
| 158 | return -1; |
---|
| 159 | |
---|
| 160 | state->stage = ROAR_XCODER_STAGE_MAGIC; |
---|
| 161 | |
---|
| 162 | if ( roar_vio_read(state->backend, &tmp_net, 2) != 2 ) |
---|
| 163 | return -1; |
---|
| 164 | |
---|
| 165 | self->mode = ROAR_NET2HOST16(tmp_net); |
---|
| 166 | |
---|
| 167 | state->stage = ROAR_XCODER_STAGE_OPENING; |
---|
| 168 | |
---|
| 169 | switch (self->mode) { |
---|
| 170 | case ROAR_SPEEX_MODE_NB: self->xcoder = speex_decoder_init(&speex_nb_mode); break; |
---|
| 171 | case ROAR_SPEEX_MODE_WB: self->xcoder = speex_decoder_init(&speex_wb_mode); break; |
---|
| 172 | case ROAR_SPEEX_MODE_UWB: self->xcoder = speex_decoder_init(&speex_uwb_mode); break; |
---|
| 173 | default: |
---|
| 174 | return -1; |
---|
| 175 | break; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | tmp=1; |
---|
| 179 | speex_decoder_ctl(self->xcoder, SPEEX_SET_ENH, &tmp); |
---|
| 180 | speex_decoder_ctl(self->xcoder, SPEEX_GET_FRAME_SIZE, &(self->frame_size)); |
---|
| 181 | |
---|
| 182 | state->stage = ROAR_XCODER_STAGE_OPENED; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | if ( roar_vio_read(state->backend, &tmp_net, 2) != 2 ) |
---|
| 186 | return -1; |
---|
| 187 | |
---|
| 188 | pkg_len = ROAR_NET2HOST16(tmp_net); |
---|
| 189 | |
---|
| 190 | if ( pkg_len > ROAR_SPEEX_MAX_CC ) |
---|
| 191 | return -1; |
---|
| 192 | |
---|
| 193 | if ( roar_vio_read(state->backend, self->cc, pkg_len) != pkg_len ) |
---|
| 194 | return -1; |
---|
| 195 | |
---|
| 196 | speex_bits_read_from(&(self->bits), self->cc, pkg_len); |
---|
| 197 | |
---|
| 198 | speex_decode_int(self->xcoder, &(self->bits), buf); |
---|
| 199 | |
---|
| 200 | return 0; |
---|
[2296] | 201 | } |
---|
| 202 | |
---|
[2295] | 203 | #endif |
---|
| 204 | |
---|
| 205 | //ll |
---|