8678
|
1 /*
|
|
2 This is an ao2 plugin to do simple decoding of matrixed surround
|
|
3 sound. This will provide a (basic) surround-sound effect from
|
|
4 audio encoded for Dolby Surround, Pro Logic etc.
|
|
5
|
|
6 * This program is free software; you can redistribute it and/or modify
|
|
7 * it under the terms of the GNU General Public License as published by
|
|
8 * the Free Software Foundation; either version 2 of the License, or
|
|
9 * (at your option) any later version.
|
|
10 *
|
|
11 * This program is distributed in the hope that it will be useful,
|
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 * GNU General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License
|
|
17 * along with this program; if not, write to the Free Software
|
|
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
19
|
|
20 Original author: Steve Davies <steve@daviesfam.org>
|
|
21 */
|
|
22
|
|
23 /* The principle: Make rear channels by extracting anti-phase data
|
|
24 from the front channels, delay by 20msec and feed to rear in anti-phase
|
|
25 */
|
|
26
|
|
27
|
|
28 // SPLITREAR: Define to decode two distinct rear channels -
|
|
29 // this doesn't work so well in practice because
|
|
30 // separation in a passive matrix is not high.
|
|
31 // C (dialogue) to Ls and Rs 14dB or so -
|
|
32 // so dialogue leaks to the rear.
|
|
33 // Still - give it a try and send feedback.
|
|
34 // comment this define for old behaviour of a single
|
|
35 // surround sent to rear in anti-phase
|
|
36 #define SPLITREAR
|
|
37
|
|
38 #include <stdio.h>
|
|
39 #include <stdlib.h>
|
|
40 #include <string.h>
|
|
41 #include <unistd.h>
|
|
42
|
|
43 #include "af.h"
|
|
44 #include "dsp.h"
|
|
45
|
|
46 // instance data
|
|
47 typedef struct af_surround_s
|
|
48 {
|
|
49 float msecs; // Rear channel delay in milliseconds
|
|
50 float* Ls_delaybuf; // circular buffer to be used for delaying Ls audio
|
|
51 float* Rs_delaybuf; // circular buffer to be used for delaying Rs audio
|
|
52 int delaybuf_len; // delaybuf buffer length in samples
|
|
53 int delaybuf_rpos; // offset in buffer where we are reading
|
|
54 int delaybuf_wpos; // offset in buffer where we are writing
|
|
55 float filter_coefs_surround[32]; // FIR filter coefficients for surround sound 7kHz lowpass
|
|
56 } af_surround_t;
|
|
57
|
|
58 // Initialization and runtime control
|
|
59 static int control(struct af_instance_s* af, int cmd, void* arg)
|
|
60 {
|
|
61 af_surround_t *instance=af->setup;
|
|
62 switch(cmd){
|
|
63 case AF_CONTROL_REINIT:{
|
|
64 float cutoff;
|
|
65 af->data->rate = ((af_data_t*)arg)->rate;
|
|
66 af->data->nch = ((af_data_t*)arg)->nch*2;
|
|
67 af->data->format = ((af_data_t*)arg)->format;
|
|
68 af->data->bps = ((af_data_t*)arg)->bps;
|
|
69 af_msg(AF_MSG_DEBUG0, "[surround]: rear delay=%0.2fms.\n", instance->msecs);
|
|
70 if (af->data->nch != 4){
|
|
71 af_msg(AF_MSG_ERROR,"Only Stereo input is supported, filter disabled.\n");
|
|
72 return AF_DETACH;
|
|
73 }
|
|
74 // Figure out buffer space (in int16_ts) needed for the 15msec delay
|
|
75 // Extra 31 samples allow for lowpass filter delay (taps-1)
|
|
76 // Double size to make virtual ringbuffer easier
|
|
77 instance->delaybuf_len = ((af->data->rate * instance->msecs / 1000)+31)*2;
|
|
78 // Free old buffers
|
|
79 if (instance->Ls_delaybuf != NULL)
|
|
80 free(instance->Ls_delaybuf);
|
|
81 if (instance->Rs_delaybuf != NULL)
|
|
82 free(instance->Rs_delaybuf);
|
|
83 // Allocate new buffers
|
|
84 instance->Ls_delaybuf=(void*)calloc(instance->delaybuf_len,sizeof(*instance->Ls_delaybuf));
|
|
85 instance->Rs_delaybuf=(void*)calloc(instance->delaybuf_len,sizeof(*instance->Rs_delaybuf));
|
|
86 af_msg(AF_MSG_DEBUG1, "Delay buffers are %d samples each.\n", instance->delaybuf_len);
|
|
87 instance->delaybuf_wpos = 0;
|
|
88 instance->delaybuf_rpos = 32; // compensate for fir delay
|
|
89 // Surround filer coefficients
|
|
90 cutoff = af->data->rate/7000;
|
|
91 if (-1 == design_fir(32, instance->filter_coefs_surround, &cutoff, LP|KAISER, 10.0)) {
|
|
92 af_msg(AF_MSG_ERROR,"[surround] Unable to design prototype filter.\n");
|
|
93 return AF_ERROR;
|
|
94 }
|
|
95
|
|
96 return AF_OK;
|
|
97 }
|
|
98 case AF_CONTROL_COMMAND_LINE:{
|
|
99 float d = 0;
|
|
100 sscanf((char*)arg,"%f",&d);
|
|
101 if (d<0){
|
|
102 af_msg(AF_MSG_ERROR,"Error setting rear delay length in af_surround. Delay has to be positive.\n");
|
|
103 return AF_ERROR;
|
|
104 }
|
|
105 instance->msecs=d;
|
|
106 return AF_OK;
|
|
107 }
|
|
108 }
|
|
109 return AF_UNKNOWN;
|
|
110 }
|
|
111
|
|
112 // Deallocate memory
|
|
113 static void uninit(struct af_instance_s* af)
|
|
114 {
|
|
115 af_surround_t *instance=af->setup;
|
|
116 if(af->data->audio)
|
|
117 free(af->data->audio);
|
|
118 if(af->data)
|
|
119 free(af->data);
|
|
120 if(instance->Ls_delaybuf)
|
|
121 free(instance->Ls_delaybuf);
|
|
122 if(instance->Rs_delaybuf)
|
|
123 free(instance->Rs_delaybuf);
|
|
124 free(af->setup);
|
|
125 }
|
|
126
|
|
127 // The beginnings of an active matrix...
|
|
128 static double steering_matrix[][12] = {
|
|
129 // LL RL LR RR LS RS
|
|
130 // LLs RLs LRs RRs LC RC
|
|
131 {.707, .0, .0, .707, .5, -.5,
|
|
132 .5878, -.3928, .3928, -.5878, .5, .5},
|
|
133 };
|
|
134
|
|
135 // Experimental moving average dominances
|
|
136 //static int amp_L = 0, amp_R = 0, amp_C = 0, amp_S = 0;
|
|
137
|
|
138 // Filter data through filter
|
|
139 static af_data_t* play(struct af_instance_s* af, af_data_t* data){
|
|
140 af_surround_t* instance = (af_surround_t*)af->setup;
|
|
141 int16_t* in = data->audio;
|
|
142 int16_t* out;
|
|
143 int i, samples;
|
|
144 double *matrix = steering_matrix[0]; // later we'll index based on detected dominance
|
|
145
|
|
146 if (AF_OK != RESIZE_LOCAL_BUFFER(af, data))
|
|
147 return NULL;
|
|
148
|
|
149 out = af->data->audio;
|
|
150
|
|
151 // fprintf(stderr, "pl_surround: play %d bytes, %d samples\n", ao_plugin_data.len, samples);
|
|
152
|
|
153 samples = data->len / (data->nch * sizeof(int16_t));
|
|
154
|
|
155 // Testing - place a 1kHz tone on Lt and Rt in anti-phase: should decode in S
|
|
156 //sinewave(in, samples, pl_surround.input_channels, 1000, 0.0, pl_surround.rate);
|
|
157 //sinewave(&in[1], samples, pl_surround.input_channels, 1000, PI, pl_surround.rate);
|
|
158
|
|
159 for (i=0; i<samples; i++) {
|
|
160
|
|
161 // Dominance:
|
|
162 //abs(in[0]) abs(in[1]);
|
|
163 //abs(in[0]+in[1]) abs(in[0]-in[1]);
|
|
164 //10 * log( abs(in[0]) / (abs(in[1])|1) );
|
|
165 //10 * log( abs(in[0]+in[1]) / (abs(in[0]-in[1])|1) );
|
|
166
|
|
167 // About volume balancing...
|
|
168 // Surround encoding does the following:
|
|
169 // Lt=L+.707*C+.707*S, Rt=R+.707*C-.707*S
|
|
170 // So S should be extracted as:
|
|
171 // (Lt-Rt)
|
|
172 // But we are splitting the S to two output channels, so we
|
|
173 // must take 3dB off as we split it:
|
|
174 // Ls=Rs=.707*(Lt-Rt)
|
|
175 // Trouble is, Lt could be +32767, Rt -32768, so possibility that S will
|
|
176 // overflow. So to avoid that, we cut L/R by 3dB (*.707), and S by 6dB (/2).
|
|
177 // this keeps the overall balance, but guarantees no overflow.
|
|
178
|
|
179 // output front left and right
|
|
180 out[0] = matrix[0]*in[0] + matrix[1]*in[1];
|
|
181 out[1] = matrix[2]*in[0] + matrix[3]*in[1];
|
|
182 // output Ls and Rs - from 20msec ago, lowpass filtered @ 7kHz
|
|
183 out[2] = fir(32, instance->filter_coefs_surround,
|
|
184 &instance->Ls_delaybuf[instance->delaybuf_rpos +
|
|
185 instance->delaybuf_len/2]);
|
|
186 #ifdef SPLITREAR
|
|
187 out[3] = fir(32, instance->filter_coefs_surround,
|
|
188 &instance->Rs_delaybuf[instance->delaybuf_rpos +
|
|
189 instance->delaybuf_len/2]);
|
|
190 #else
|
|
191 out[3] = -out[2];
|
|
192 #endif
|
|
193 // calculate and save surround for 20msecs time
|
|
194 #ifdef SPLITREAR
|
|
195 instance->Ls_delaybuf[instance->delaybuf_wpos] =
|
|
196 instance->Ls_delaybuf[instance->delaybuf_wpos + instance->delaybuf_len/2] =
|
|
197 matrix[6]*in[0] + matrix[7]*in[1];
|
|
198 instance->Rs_delaybuf[instance->delaybuf_wpos] =
|
|
199 instance->Rs_delaybuf[instance->delaybuf_wpos++ + instance->delaybuf_len/2] =
|
|
200 matrix[8]*in[0] + matrix[9]*in[1];
|
|
201 #else
|
|
202 instance->Ls_delaybuf[instance->delaybuf_wpos] =
|
|
203 instance->Ls_delaybuf[instance->delaybuf_wpos++ + instance->delaybuf_len/2] =
|
|
204 matrix[4]*in[0] + matrix[5]*in[1];
|
|
205 #endif
|
|
206 instance->delaybuf_rpos++;
|
|
207 instance->delaybuf_wpos %= instance->delaybuf_len/2;
|
|
208 instance->delaybuf_rpos %= instance->delaybuf_len/2;
|
|
209
|
|
210 // next samples...
|
|
211 in = &in[data->nch]; out = &out[af->data->nch];
|
|
212 }
|
|
213
|
|
214 // Show some state
|
|
215 //printf("\npl_surround: delaybuf_pos=%d, samples=%d\r\033[A", pl_surround.delaybuf_pos, samples);
|
|
216
|
|
217 // Set output data
|
|
218 data->audio = af->data->audio;
|
|
219 data->len = (data->len*af->mul.n)/af->mul.d;
|
|
220 data->nch = af->data->nch;
|
|
221
|
|
222 return data;
|
|
223 }
|
|
224
|
|
225 static int open(af_instance_t* af){
|
|
226 af_surround_t *pl_surround;
|
|
227 af->control=control;
|
|
228 af->uninit=uninit;
|
|
229 af->play=play;
|
|
230 af->mul.n=2;
|
|
231 af->mul.d=1;
|
|
232 af->data=calloc(1,sizeof(af_data_t));
|
|
233 af->setup=pl_surround=calloc(1,sizeof(af_surround_t));
|
|
234 pl_surround->msecs=20;
|
|
235 if(af->data == NULL || af->setup == NULL)
|
|
236 return AF_ERROR;
|
|
237 return AF_OK;
|
|
238 }
|
|
239
|
|
240 af_info_t af_info_surround =
|
|
241 {
|
|
242 "Surround decoder filter",
|
|
243 "surround",
|
|
244 "Steve Davies <steve@daviesfam.org>",
|
|
245 "",
|
|
246 AF_FLAGS_REENTRANT,
|
|
247 open
|
|
248 };
|