source: roaraudio/libroardsp/filter_speex_prep.c @ 5546:faff3e9677c4

Last change on this file since 5546:faff3e9677c4 was 5381:430b1d26e12d, checked in by phi, 12 years ago

updated copyright years

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