source: roaraudio/libroardsp/filter_speex_prep.c @ 3005:8a2821b526bb

Last change on this file since 3005:8a2821b526bb was 3005:8a2821b526bb, checked in by phi, 15 years ago

make basic options settable

File size: 4.4 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 return 0;
60}
61
62int roardsp_speex_prep_uninit (struct roardsp_filter * filter) {
63 struct roardsp_speex_prep * self = filter->inst;
64
65 if ( self->preprocess != NULL )
66  speex_preprocess_state_destroy(self->preprocess);
67
68 roar_mm_free(self);
69
70 return 0;
71}
72
73int roardsp_speex_prep_calc161(struct roardsp_filter * filter, void * data, size_t samples) {
74 struct roardsp_speex_prep * self = filter->inst;
75
76 if ( self->preprocess == NULL )
77  return -1;
78
79 if ( samples != self->frame_size )
80  return -1;
81
82#ifdef _SPEEX_API_OLD
83 speex_preprocess(self->preprocess, data, NULL);
84#elif defined(_SPEEX_API_NEW)
85 speex_preprocess_run(self->preprocess, data);
86#else
87 return -1;
88#endif
89
90 return 0;
91}
92
93int roardsp_speex_prep_ctl    (struct roardsp_filter * filter, int cmd, void * data) {
94 struct roardsp_speex_prep * self = filter->inst;
95 union {
96  size_t  size;
97  int32_t i32;
98 } * val = data;
99 _SPEEX_INT si;
100
101 switch (cmd) {
102  case ROARDSP_FCTL_MODE:
103    if ( self->preprocess == NULL )
104     return -1;
105
106    if ( val->i32 & _CBVM(ROARDSP_SPEEX_PREP_DENOISE) ) {
107     val->i32 -= val->i32 & _CBVM(ROARDSP_SPEEX_PREP_DENOISE);
108     switch (ROARDSP_SPEEX_PREP_CTB(ROARDSP_SPEEX_PREP_DENOISE, val->i32)) {
109      case ROARDSP_SPEEX_PREP_ON:  si = _on;  break;
110      case ROARDSP_SPEEX_PREP_OFF: si = _off; break;
111      default: return -1;
112     }
113     speex_preprocess_ctl(self->preprocess, SPEEX_PREPROCESS_SET_DENOISE, &si);
114    }
115
116    if ( val->i32 & _CBVM(ROARDSP_SPEEX_PREP_AGC) ) {
117     val->i32 -= val->i32 & _CBVM(ROARDSP_SPEEX_PREP_AGC);
118     switch (ROARDSP_SPEEX_PREP_CTB(ROARDSP_SPEEX_PREP_DENOISE, val->i32)) {
119      case ROARDSP_SPEEX_PREP_ON:  si = _on;  break;
120      case ROARDSP_SPEEX_PREP_OFF: si = _off; break;
121      default: return -1;
122     }
123     speex_preprocess_ctl(self->preprocess, SPEEX_PREPROCESS_SET_AGC, &si);
124    }
125
126    if ( val->i32 & _CBVM(ROARDSP_SPEEX_PREP_VAD) ) {
127     val->i32 -= val->i32 & _CBVM(ROARDSP_SPEEX_PREP_VAD);
128     switch (ROARDSP_SPEEX_PREP_CTB(ROARDSP_SPEEX_PREP_DENOISE, val->i32)) {
129      case ROARDSP_SPEEX_PREP_ON:  si = _on;  break;
130      case ROARDSP_SPEEX_PREP_OFF: si = _off; break;
131      default: return -1;
132     }
133     speex_preprocess_ctl(self->preprocess, SPEEX_PREPROCESS_SET_VAD, &si);
134    }
135
136    // any other options left? error:
137    if ( val->i32 )
138     return -1;
139
140    return 0;
141   break;
142  case ROARDSP_FCTL_PACKET_SIZE:
143    self->frame_size = val->size;
144
145    self->preprocess = speex_preprocess_state_init(self->frame_size, filter->rate);
146    if ( self->preprocess == NULL )
147     return -1;
148
149    return 0;
150   break;
151 }
152
153 return -1;
154}
155
156int roardsp_speex_prep_reset  (struct roardsp_filter * filter, int what) {
157 if ( what == ROARDSP_RESET_NONE )
158  return 0;
159
160 return -1;
161}
162#endif
163
164//ll
Note: See TracBrowser for help on using the repository browser.