forked from cnlohr/colorchord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OutputLinear.c
213 lines (172 loc) · 5.34 KB
/
OutputLinear.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//XXX TODO Figure out why it STILL fails when going around a loop
#include "outdrivers.h"
#include "notefinder.h"
#include <stdio.h>
#include <string.h>
#include "parameters.h"
#include <stdlib.h>
#include "color.h"
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
struct LEDOutDriver
{
int did_init;
int total_leds;
int is_loop;
float light_siding;
float last_led_pos[MAX_LEDS];
float last_led_pos_filter[MAX_LEDS];
float last_led_amp[MAX_LEDS];
int steady_bright;
float led_floor;
float satamp;
int lastadvance;
};
static float lindiff( float a, float b ) //Find the minimum change around a wheel.
{
float diff = a - b;
if( diff < 0 ) diff *= -1;
float otherdiff = (a<b)?(a+1):(a-1);
otherdiff -=b;
if( otherdiff < 0 ) otherdiff *= -1;
if( diff < otherdiff )
return diff;
else
return otherdiff;
}
static void LEDUpdate(void * id, struct NoteFinder*nf)
{
struct LEDOutDriver * led = (struct LEDOutDriver*)id;
//Step 1: Calculate the quantity of all the LEDs we'll want.
int totbins = nf->note_peaks;//nf->dists;
int i, j;
float binvals[totbins];
float binvalsQ[totbins];
float binpos[totbins];
float totalbinval = 0;
// if( totbins > led_bins ) totbins = led_bins;
for( i = 0; i < totbins; i++ )
{
binpos[i] = nf->note_positions[i] / nf->freqbins;
binvals[i] = pow( nf->note_amplitudes2[i], led->light_siding );
// binvals[i] = (binvals[i]<led->led_floor)?0:binvals[i];
// if( nf->note_positions[i] < 0 ) { binvals[i] = 0; binvalsQ[i] = 0; }
binvalsQ[i] =pow( nf->note_amplitudes[i], led->light_siding );
// nf->note_amplitudes[i];//
totalbinval += binvals[i];
}
float newtotal = 0;
for( i = 0; i < totbins; i++ )
{
#define SMOOTHZERO
#ifdef SMOOTHZERO
binvals[i] -= led->led_floor*totalbinval;
if( binvals[i] / totalbinval < 0 )
binvals[i] = binvalsQ[i] = 0;
#else
if( binvals[i] / totalbinval < led->led_floor )
binvals[i] = binvalsQ[i] = 0;
#endif
newtotal += binvals[i];
}
totalbinval = newtotal;
float rledpos[led->total_leds];
float rledamp[led->total_leds];
float rledampQ[led->total_leds];
int rbinout = 0;
for( i = 0; i < totbins; i++ )
{
int nrleds = (int)((binvals[i] / totalbinval) * led->total_leds);
// if( nrleds < 40 ) nrleds = 0;
for( j = 0; j < nrleds && rbinout < led->total_leds; j++ )
{
rledpos[rbinout] = binpos[i];
rledamp[rbinout] = binvals[i];
rledampQ[rbinout] = binvalsQ[i];
rbinout++;
}
}
for( ; rbinout < led->total_leds; rbinout++ )
{
rledpos[rbinout] = rledpos[rbinout-1];
rledamp[rbinout] = rledamp[rbinout-1];
rledampQ[rbinout] = rledampQ[rbinout-1];
}
//Now we have to minimize "advance".
int minadvance = 0;
if( led->is_loop )
{
float mindiff = 1e20;
//Uncomment this for a rotationally continuous surface.
for( i = 0; i < led->total_leds; i++ )
{
float diff = 0;
diff = 0;
for( j = 0; j < led->total_leds; j++ )
{
int r = (j + i) % led->total_leds;
float rd = lindiff( led->last_led_pos_filter[j], rledpos[r]);
diff += rd;//*rd;
}
int advancediff = ( led->lastadvance - i );
if( advancediff < 0 ) advancediff *= -1;
if( advancediff > led->total_leds/2 ) advancediff = led->total_leds - advancediff;
float ad = (float)advancediff/(float)led->total_leds;
diff += ad * ad;// * led->total_leds;
if( diff < mindiff )
{
mindiff = diff;
minadvance = i;
}
}
}
led->lastadvance = minadvance;
// printf( "MA: %d %f\n", minadvance, mindiff );
//Advance the LEDs to this position when outputting the values.
for( i = 0; i < led->total_leds; i++ )
{
int ia = ( i + minadvance + led->total_leds ) % led->total_leds;
float sat = rledamp[ia] * led->satamp;
float satQ = rledampQ[ia] * led->satamp;
if( satQ > 1 ) satQ = 1;
led->last_led_pos[i] = rledpos[ia];
led->last_led_amp[i] = sat;
float sendsat = (led->steady_bright?sat:satQ);
if( sendsat > 1 ) sendsat = 1;
int r = CCtoHEX( led->last_led_pos[i], 1.0, sendsat );
OutLEDs[i*3+0] = r & 0xff;
OutLEDs[i*3+1] = (r>>8) & 0xff;
OutLEDs[i*3+2] = (r>>16) & 0xff;
}
if( led->is_loop )
{
for( i = 0; i < led->total_leds; i++ )
{
led->last_led_pos_filter[i] = led->last_led_pos_filter[i] * .9 + led->last_led_pos[i] * .1;
}
}
}
static void LEDParams(void * id )
{
struct LEDOutDriver * led = (struct LEDOutDriver*)id;
led->satamp = 2; RegisterValue( "satamp", PAFLOAT, &led->satamp, sizeof( led->satamp ) );
led->total_leds = 300; RegisterValue( "leds", PAINT, &led->total_leds, sizeof( led->total_leds ) );
led->led_floor = .1; RegisterValue( "led_floor", PAFLOAT, &led->led_floor, sizeof( led->led_floor ) );
led->light_siding = 1.4;RegisterValue( "light_siding", PAFLOAT, &led->light_siding, sizeof( led->light_siding ) );
led->is_loop = 0; RegisterValue( "is_loop", PAINT, &led->is_loop, sizeof( led->is_loop ) );
led->steady_bright = 1; RegisterValue( "steady_bright", PAINT, &led->steady_bright, sizeof( led->steady_bright ) );
printf( "Found LEDs for output. leds=%d\n", led->total_leds );
}
static struct DriverInstances * OutputLinear()
{
struct DriverInstances * ret = malloc( sizeof( struct DriverInstances ) );
memset( ret, 0, sizeof( struct DriverInstances ) );
struct LEDOutDriver * led = ret->id = malloc( sizeof( struct LEDOutDriver ) );
memset( led, 0, sizeof( struct LEDOutDriver ) );
ret->Func = LEDUpdate;
ret->Params = LEDParams;
LEDParams( led );
return ret;
}
REGISTER_OUT_DRIVER(OutputLinear);