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