-
Notifications
You must be signed in to change notification settings - Fork 106
/
visualization.cc
177 lines (144 loc) · 3.98 KB
/
visualization.cc
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
/*
* visualization.c
* Copyright 2010-2011 John Lindgren
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions, and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions, and the following disclaimer in the documentation
* provided with the distribution.
*
* This software is provided "as is" and without any warranty, express or
* implied. In no event shall the authors be liable for any damages arising from
* the use of this software.
*/
#include "interface.h"
#include "internal.h"
#include <string.h>
#include "plugin.h"
#include "plugins.h"
#include "runtime.h"
static Index<Visualizer *> visualizers;
static int running = false;
static int num_enabled = 0;
EXPORT void aud_visualizer_add(Visualizer * vis)
{
visualizers.append(vis);
num_enabled++;
if (num_enabled == 1)
vis_runner_enable(true);
}
EXPORT void aud_visualizer_remove(Visualizer * vis)
{
int num_disabled = 0;
auto is_match = [&](Visualizer * vis2) {
if (vis2 != vis)
return false;
num_disabled++;
return true;
};
visualizers.remove_if(is_match, true);
num_enabled -= num_disabled;
if (!num_enabled)
vis_runner_enable(false);
}
void vis_send_clear()
{
for (Visualizer * vis : visualizers)
vis->clear();
}
static void pcm_to_mono(const float * data, float * mono, int channels)
{
if (channels == 1)
memcpy(mono, data, sizeof(float) * 512);
else
{
float * set = mono;
while (set < &mono[512])
{
*set++ = (data[0] + data[1]) / 2;
data += channels;
}
}
}
void vis_send_audio(const float * data, int channels)
{
auto is_active = [](int type_mask) {
for (Visualizer * vis : visualizers)
{
if ((vis->type_mask & type_mask))
return true;
}
return false;
};
float mono[512];
float freq[256];
if (is_active(Visualizer::MonoPCM | Visualizer::Freq))
pcm_to_mono(data, mono, channels);
if (is_active(Visualizer::Freq))
calc_freq(mono, freq);
for (Visualizer * vis : visualizers)
{
if ((vis->type_mask & Visualizer::MonoPCM))
vis->render_mono_pcm(mono);
if ((vis->type_mask & Visualizer::MultiPCM))
vis->render_multi_pcm(data, channels);
if ((vis->type_mask & Visualizer::Freq))
vis->render_freq(freq);
}
}
static bool vis_load(PluginHandle * plugin)
{
AUDINFO("Activating %s.\n", aud_plugin_get_name(plugin));
VisPlugin * header = (VisPlugin *)aud_plugin_get_header(plugin);
if (!header)
return false;
aud_visualizer_add(header);
return true;
}
static void vis_unload(PluginHandle * plugin)
{
AUDINFO("Deactivating %s.\n", aud_plugin_get_name(plugin));
VisPlugin * header = (VisPlugin *)aud_plugin_get_header(plugin);
if (!header)
return;
header->clear();
aud_visualizer_remove(header);
}
void vis_activate(bool activate)
{
if (!activate == !running)
return;
for (PluginHandle * plugin : aud_plugin_list(PluginType::Vis))
{
if (!aud_plugin_get_enabled(plugin))
continue;
if (activate)
vis_load(plugin);
else
vis_unload(plugin);
}
running = activate;
}
bool vis_plugin_start(PluginHandle * plugin)
{
VisPlugin * vp = (VisPlugin *)aud_plugin_get_header(plugin);
if (!vp || !vp->init())
return false;
if (running)
vis_load(plugin);
return true;
}
void vis_plugin_stop(PluginHandle * plugin)
{
VisPlugin * vp = (VisPlugin *)aud_plugin_get_header(plugin);
if (!vp)
return;
if (running)
vis_unload(plugin);
vp->cleanup();
}