annotate libaf/af_surround.c @ 8678:db0810dcab33

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