source: roaraudio/libroardsp/filter_speex_prep.c @ 3268:56a8d2d3dbb7

Last change on this file since 3268:56a8d2d3dbb7 was 3014:c4dc5270a335, checked in by phi, 15 years ago

units...

File size: 4.8 KB
Line 
1//filter_speex_prep.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 _SPEEX_TYPES_H
28
29#if defined(ROAR_HAVE_LIBSPEEX) && !defined(ROAR_HAVE_LIBSPEEXDSP)
30#define _SPEEX_API_OLD
31#define _SPEEX_INT int
32#elif defined(ROAR_HAVE_LIBSPEEX) && defined(ROAR_HAVE_LIBSPEEXDSP)
33#define _SPEEX_API_NEW
34#define _SPEEX_INT spx_int32_t
35#endif
36
37#define _on  1
38#define _off 2
39
40#define _CBVM(opt) (ROARDSP_SPEEX_PREP_CBV((opt), ROARDSP_SPEEX_PREP_MASK))
41
42// TODO: check parameters we allready know:
43int roardsp_speex_prep_init   (struct roardsp_filter * filter, struct roar_stream * stream, int id) {
44 struct roardsp_speex_prep * self;
45
46 if ( filter->channels != 1 )
47  return -1;
48
49 if ( filter->bits != 16 )
50  return -1;
51
52 self = roar_mm_malloc(sizeof(struct roardsp_speex_prep));
53
54 if ( self == NULL )
55  return -1;
56
57 memset(self, 0, sizeof(struct roardsp_speex_prep));
58
59 filter->inst = self;
60
61 return 0;
62}
63
64int roardsp_speex_prep_uninit (struct roardsp_filter * filter) {
65 struct roardsp_speex_prep * self = filter->inst;
66
67 if ( self->preprocess != NULL )
68  speex_preprocess_state_destroy(self->preprocess);
69
70 roar_mm_free(self);
71
72 return 0;
73}
74
75int roardsp_speex_prep_calc161(struct roardsp_filter * filter, void * data, size_t samples) {
76 struct roardsp_speex_prep * self = filter->inst;
77
78 if ( self->preprocess == NULL )
79  return -1;
80
81 if ( samples != ((self->frame_size * filter->bits) / 8) )
82  return -1;
83
84#ifdef _SPEEX_API_OLD
85 speex_preprocess(self->preprocess, data, NULL);
86#elif defined(_SPEEX_API_NEW)
87 speex_preprocess_run(self->preprocess, data);
88#else
89 return -1;
90#endif
91
92 return 0;
93}
94
95int roardsp_speex_prep_ctl    (struct roardsp_filter * filter, int cmd, void * data) {
96 struct roardsp_speex_prep * self = filter->inst;
97 union {
98  size_t  size;
99  int32_t i32;
100 } * val = data;
101 _SPEEX_INT si;
102
103 switch (cmd) {
104  case ROARDSP_FCTL_MODE:
105    if ( self->preprocess == NULL )
106     return -1;
107
108    ROAR_DBG("roardsp_speex_prep_ctl(*): val->i32 = 0x%.8x", val->i32);
109    ROAR_DBG("roardsp_speex_prep_ctl(*): _CBVM(ROARDSP_SPEEX_PREP_DENOISE) = 0x%.8x", _CBVM(ROARDSP_SPEEX_PREP_DENOISE));
110
111    if ( val->i32 & _CBVM(ROARDSP_SPEEX_PREP_DENOISE) ) {
112     ROAR_DBG("roardsp_speex_prep_ctl(*): ROARDSP_SPEEX_PREP_CTB(ROARDSP_SPEEX_PREP_DENOISE, val->i32) = 0x%.8x", ROARDSP_SPEEX_PREP_CTB(ROARDSP_SPEEX_PREP_DENOISE, val->i32));
113     switch (ROARDSP_SPEEX_PREP_CTB(ROARDSP_SPEEX_PREP_DENOISE, val->i32)) {
114      case ROARDSP_SPEEX_PREP_ON:  si = _on;  break;
115      case ROARDSP_SPEEX_PREP_OFF: si = _off; break;
116      default: return -1;
117     }
118     speex_preprocess_ctl(self->preprocess, SPEEX_PREPROCESS_SET_DENOISE, &si);
119     val->i32 -= val->i32 & _CBVM(ROARDSP_SPEEX_PREP_DENOISE);
120    }
121
122    if ( val->i32 & _CBVM(ROARDSP_SPEEX_PREP_AGC) ) {
123     switch (ROARDSP_SPEEX_PREP_CTB(ROARDSP_SPEEX_PREP_DENOISE, val->i32)) {
124      case ROARDSP_SPEEX_PREP_ON:  si = _on;  break;
125      case ROARDSP_SPEEX_PREP_OFF: si = _off; break;
126      default: return -1;
127     }
128     speex_preprocess_ctl(self->preprocess, SPEEX_PREPROCESS_SET_AGC, &si);
129     val->i32 -= val->i32 & _CBVM(ROARDSP_SPEEX_PREP_AGC);
130    }
131
132    if ( val->i32 & _CBVM(ROARDSP_SPEEX_PREP_VAD) ) {
133     switch (ROARDSP_SPEEX_PREP_CTB(ROARDSP_SPEEX_PREP_DENOISE, val->i32)) {
134      case ROARDSP_SPEEX_PREP_ON:  si = _on;  break;
135      case ROARDSP_SPEEX_PREP_OFF: si = _off; break;
136      default: return -1;
137     }
138     speex_preprocess_ctl(self->preprocess, SPEEX_PREPROCESS_SET_VAD, &si);
139     val->i32 -= val->i32 & _CBVM(ROARDSP_SPEEX_PREP_VAD);
140    }
141
142    // any other options left? error:
143    if ( val->i32 )
144     return -1;
145
146    return 0;
147   break;
148  case ROARDSP_FCTL_PACKET_SIZE:
149    self->frame_size = val->size;
150
151    self->preprocess = speex_preprocess_state_init(self->frame_size, filter->rate);
152    if ( self->preprocess == NULL )
153     return -1;
154
155    return 0;
156   break;
157 }
158
159 return -1;
160}
161
162int roardsp_speex_prep_reset  (struct roardsp_filter * filter, int what) {
163 if ( what == ROARDSP_RESET_NONE )
164  return 0;
165
166 return -1;
167}
168#endif
169
170//ll
Note: See TracBrowser for help on using the repository browser.