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 | |
---|
32 | int 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 | 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; |
---|
74 | } |
---|
75 | |
---|
76 | int roar_xcoder_speex_uninit (struct roar_xcoder * state) { |
---|
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; |
---|
92 | } |
---|
93 | |
---|
94 | int roar_xcoder_speex_packet_size(struct roar_xcoder * state, int samples) { |
---|
95 | struct roar_xcoder_speex * self = state->inst; |
---|
96 | if (!state->encode) { |
---|
97 | return -1; |
---|
98 | } |
---|
99 | |
---|
100 | return _16BIT * self->frame_size; |
---|
101 | } |
---|
102 | |
---|
103 | int roar_xcoder_speex_encode (struct roar_xcoder * state, void * buf, size_t len) { |
---|
104 | struct roar_xcoder_speex * self = state->inst; |
---|
105 | uint16_t tmp_net; |
---|
106 | int pkg_len; |
---|
107 | |
---|
108 | if (!state->encode) |
---|
109 | return -1; |
---|
110 | |
---|
111 | ROAR_DBG("roar_xcoder_speex_encode(*): Encoding..."); |
---|
112 | |
---|
113 | if ( state->stage == ROAR_XCODER_STAGE_INITED ) { |
---|
114 | if ( roar_vio_write(state->backend, ROAR_SPEEX_MAGIC, ROAR_SPEEX_MAGIC_LEN) != ROAR_SPEEX_MAGIC_LEN ) |
---|
115 | return -1; |
---|
116 | state->stage = ROAR_XCODER_STAGE_MAGIC; |
---|
117 | ROAR_DBG("roar_xcoder_speex_encode(*): Wrote MAGIC"); |
---|
118 | |
---|
119 | state->stage = ROAR_XCODER_STAGE_OPENING; |
---|
120 | |
---|
121 | tmp_net = ROAR_HOST2NET16(self->mode); |
---|
122 | if ( roar_vio_write(state->backend, &tmp_net, 2) != 2 ) |
---|
123 | return -1; |
---|
124 | |
---|
125 | state->stage = ROAR_XCODER_STAGE_OPENED; |
---|
126 | } |
---|
127 | |
---|
128 | speex_bits_reset(&(self->bits)); |
---|
129 | |
---|
130 | speex_encode_int(self->xcoder, (spx_int16_t *) buf, &(self->bits)); |
---|
131 | |
---|
132 | pkg_len = speex_bits_write(&(self->bits), self->cc, ROAR_SPEEX_MAX_CC); |
---|
133 | |
---|
134 | tmp_net = ROAR_HOST2NET16(pkg_len); |
---|
135 | |
---|
136 | if ( roar_vio_write(state->backend, &tmp_net, 2) != 2 ) |
---|
137 | return -1; |
---|
138 | |
---|
139 | if ( roar_vio_write(state->backend, self->cc, pkg_len) != pkg_len ) |
---|
140 | return -1; |
---|
141 | |
---|
142 | return 0; |
---|
143 | } |
---|
144 | |
---|
145 | int roar_xcoder_speex_decode (struct roar_xcoder * state, void * buf, size_t len) { |
---|
146 | return -1; |
---|
147 | } |
---|
148 | |
---|
149 | #endif |
---|
150 | |
---|
151 | //ll |
---|