1 | //fader.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2010 |
---|
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 | #define _CHECK_BASIC() if ( state == NULL ) return -1 |
---|
29 | #define _CHECK_CALCPCM() _CHECK_BASIC(); if ( frames == 0 ) return 0; if ( data == NULL ) return -1; |
---|
30 | |
---|
31 | int roar_fader_init (struct roar_fader_state * state, float * poly, int coeff) { |
---|
32 | _CHECK_BASIC(); |
---|
33 | |
---|
34 | if ( poly == NULL ) |
---|
35 | return -1; |
---|
36 | |
---|
37 | if ( coeff < 2 ) |
---|
38 | return -1; |
---|
39 | |
---|
40 | if ( coeff > ROAR_FADER_MAX_COEFF ) |
---|
41 | return -1; |
---|
42 | |
---|
43 | memset(state, 0, sizeof(struct roar_fader_state)); |
---|
44 | |
---|
45 | state->rate = -1; |
---|
46 | state->coeff = coeff; |
---|
47 | |
---|
48 | state->start = -1; |
---|
49 | state->stop = -1; |
---|
50 | |
---|
51 | memcpy(state->poly, poly, sizeof(float)*coeff); |
---|
52 | |
---|
53 | return 0; |
---|
54 | } |
---|
55 | |
---|
56 | int roar_fader_set_rate (struct roar_fader_state * state, int rate) { |
---|
57 | _CHECK_BASIC(); |
---|
58 | |
---|
59 | state->rate = rate; |
---|
60 | |
---|
61 | return 0; |
---|
62 | } |
---|
63 | |
---|
64 | int roar_fader_set_startstop(struct roar_fader_state * state, ssize_t start, ssize_t stop) { |
---|
65 | _CHECK_BASIC(); |
---|
66 | |
---|
67 | if ( start >= 0 ) |
---|
68 | state->start = start; |
---|
69 | |
---|
70 | if ( stop >= 0 ) |
---|
71 | state->stop = stop; |
---|
72 | |
---|
73 | return 0; |
---|
74 | } |
---|
75 | |
---|
76 | int roar_fader_has_started (struct roar_fader_state * state) { |
---|
77 | if ( state == NULL ) |
---|
78 | return -1; |
---|
79 | |
---|
80 | if ( state->start == -1 ) |
---|
81 | return 0; |
---|
82 | |
---|
83 | if ( state->start <= state->pcmoffset ) |
---|
84 | return 1; |
---|
85 | |
---|
86 | return 0; |
---|
87 | } |
---|
88 | |
---|
89 | int roar_fader_has_ended (struct roar_fader_state * state) { |
---|
90 | if ( state == NULL ) |
---|
91 | return -1; |
---|
92 | |
---|
93 | if ( state->stop == -1 ) |
---|
94 | return 0; |
---|
95 | |
---|
96 | if ( state->stop > state->pcmoffset ) |
---|
97 | return 1; |
---|
98 | |
---|
99 | return 0; |
---|
100 | } |
---|
101 | |
---|
102 | int roar_fader_calcpcm_i16n(struct roar_fader_state * state, int16_t * data, size_t frames, int channels) { |
---|
103 | _CHECK_CALCPCM(); |
---|
104 | |
---|
105 | switch (channels) { |
---|
106 | case 1: return roar_fader_calcpcm_i161(state, data, frames); |
---|
107 | } |
---|
108 | |
---|
109 | return -1; |
---|
110 | } |
---|
111 | |
---|
112 | int roar_fader_calcpcm_i161(struct roar_fader_state * state, int16_t * data, size_t frames) { |
---|
113 | size_t start, stop, cur; |
---|
114 | int i, i_s, i_e; |
---|
115 | float t_cur, t_step; |
---|
116 | float g_cur; |
---|
117 | |
---|
118 | _CHECK_BASIC(); |
---|
119 | |
---|
120 | if ( state->start == -1 ) { |
---|
121 | start = state->pcmoffset; |
---|
122 | } else { |
---|
123 | start = state->start; |
---|
124 | } |
---|
125 | |
---|
126 | if ( state->stop == -1 ) { |
---|
127 | stop = state->pcmoffset + frames; |
---|
128 | } else { |
---|
129 | stop = state->stop; |
---|
130 | } |
---|
131 | |
---|
132 | cur = state->pcmoffset; |
---|
133 | |
---|
134 | i_s = 0; |
---|
135 | i_e = frames; |
---|
136 | |
---|
137 | if ( start > cur ) |
---|
138 | i_s = start - cur; |
---|
139 | |
---|
140 | if ( stop < (cur + frames) ) |
---|
141 | i_e = stop - cur; |
---|
142 | |
---|
143 | t_step = (float)(state->coeff - 1)/(float)(stop - start); |
---|
144 | |
---|
145 | if ( start < cur ) { |
---|
146 | t_cur = (cur - start)*t_step; |
---|
147 | } else { |
---|
148 | t_cur = 0; |
---|
149 | } |
---|
150 | |
---|
151 | ROAR_DBG("roar_fader_calcpcm_i161(*): i_s->i_e: %i->%i", i_s, i_e); |
---|
152 | |
---|
153 | for (i = i_s; i < i_e; i++, cur++) { |
---|
154 | t_cur += t_step; |
---|
155 | g_cur = roar_math_cvpoly(state->poly, t_cur, state->coeff); |
---|
156 | // ROAR_DBG("roar_fader_calcpcm_i161(*): i=%i, g_cur=%f", i, g_cur); |
---|
157 | data[i] = (float)data[i] * g_cur; |
---|
158 | } |
---|
159 | |
---|
160 | state->pcmoffset = cur; |
---|
161 | |
---|
162 | return -1; |
---|
163 | } |
---|
164 | |
---|
165 | //ll |
---|