-
Notifications
You must be signed in to change notification settings - Fork 2
/
visualization.lua
275 lines (215 loc) · 8.4 KB
/
visualization.lua
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
-- MIT License
--
-- Copyright (c) 2018 nabakin
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
local ffi = require("ffi")
--[[ Initialize FFI ]]
ffi.cdef[[
float* fft(float *samples, int nSamples, int tickCount);
]]
local fft = ffi.load(ffi.os == "Windows" and "fft" or "./libfft.dylib")
--[[ Initialize Variables ]]
-- Variables for FFT.
local visualization = {}
local sampling_size = config.sampling_size
local old_sample = 0
local samples_ptr = nil
-- Variables for drawing the visualization.
local visualizer_type = config.visualization
local tick_amplitude_average = 0
local tick_count = 128
local fade_activated = config.fade
local fade_intensity_multiplier = config.fade_intensity_multiplier
--[[ Functions ]]
--- Reloads visualization variables that affect the menu.
-- Necessary for returning to the main menu.
function visualization.reload()
-- Variables for FFT.
old_sample = 0
samples_ptr = nil
-- Variables for drawing the visualization.
tick_amplitude_average = 0
end
--- Runs FFT on music file samples and obtains the next waveform.
-- @return table: Waveform output of FFT.
function visualization.generateMusicWaveform()
local normalized_samples = {}
local channels = audio.getChannels()
--[[ Generates sample input for FFT from sound data. Optimized
to take any number of channels (ex: Mono, Stereo, 5.1, 7.1).
Not completely supported by Love2D yet. ]]
local range = 2*audio.getQueueSize()*audio.getDecoderBuffer()/(audio.getBitDepth()/8)
for i=1, sampling_size do
-- Obtain necessary samples.
local new_sample = 0
for j=0, channels-1 do
local sample_index = range/2-sampling_size*channels/2+(i-1)*channels+j
new_sample = new_sample+audio.music.getSample(sample_index)
end
-- X channels of sound data -> 1 channel of sound data; for FFT input.
local sample_average = new_sample/channels
-- Build FFT input.
table.insert(normalized_samples, sample_average)
end
-- Sample memoization.
old_sample = audio.music.tellSong('samples')
--[[ Samples -> Waveform using FFI ]]
--[[ Allocates and stores samples in memory. Do
NOT destroy or allocation won't be maintained. ]]
samples_ptr = ffi.new("float["..sampling_size.."]", normalized_samples)
-- Lua Variables -> FFI/C Variables
local sample_count_ptr = ffi.new("int", sampling_size)
local tick_count_ptr = ffi.new("int", tick_count)
return fft.fft(samples_ptr, sample_count_ptr, tick_count_ptr)
end
--- Runs FFT on Recording Device samples and obtains the next waveform.
-- @return table: Waveform output of FFT.
function visualization.generateRecordingDeviceWaveform()
local normalized_samples = {}
local channels = audio.getChannels()
--[[ Generates sample input for FFT from sound data. Optimized
to take any number of channels (ex: Mono, Stereo, 5.1, 7.1).
Not completely supported by Love2D yet. ]]
for i=1, sampling_size do
-- Obtain necessary samples.
local new_sample = 0
for j=0, channels-1 do
local sample_index = audio.recordingdevice.getSampleSum()-sampling_size*channels+(i-1)*channels+j
new_sample = new_sample+audio.recordingdevice.getSample(sample_index)
end
-- X channels of sound data -> 1 channel of sound data; for FFT input.
local sample_average = new_sample/channels
-- Build FFT input.
table.insert(normalized_samples, sample_average)
end
--[[ Samples -> Waveform using FFI ]]
--[[ Allocates and stores samples in memory. Do
NOT destroy or allocation won't be maintained. ]]
samples_ptr = ffi.new("float["..sampling_size.."]", normalized_samples)
-- Lua Variables -> FFI/C Variables
local sample_count_ptr = ffi.new("int", sampling_size)
local tick_count_ptr = ffi.new("int", tick_count)
return fft.fft(samples_ptr, sample_count_ptr, tick_count_ptr)
end
--- Handles all drawing of visualization.
-- @param waveform table: Waveform FFT of samples.
function visualization.draw(waveform)
local tick_distance
local tick_width
local graphics_width = gui.graphics.getWidth()
local graphics_height = gui.graphics.getHeight()
-- Scales visualization at a decreasing rate.
local graphics_scaled_height = math.max(71.138*graphics_height^(1/3), graphics_height)
-- Load properties of bar visualization.
if visualizer_type == 1 then
tick_count = 48
tick_distance = graphics_width/(tick_count*2)
tick_width = graphics_width/(tick_count*5.5)
elseif visualizer_type == 2 then
tick_count = 64
tick_distance = graphics_width/(tick_count*2)
tick_width = graphics_width/(tick_count*4.3)
elseif visualizer_type == 3 then
tick_count = 128
local tick_padding = 2
tick_distance = graphics_width/((tick_count+tick_padding)*2)
tick_width = tick_distance
elseif visualizer_type == 4 then
tick_count = 256
tick_distance = graphics_width/(tick_count*2)
tick_width = tick_distance
end
if fade_activated then
gui.graphics.setColor(nil, (.03-tick_amplitude_average)*fade_intensity_multiplier)
else
gui.graphics.setColor()
end
--[[ Draw bar visualization ]]
-- If no waveform, skip drawing of bar visualization.
if not waveform[0] then
tick_count = 0
end
-- Draw bars.
local tick_amplitude_sum = 0
for i=0, tick_count-1 do
local tick_amplitude = waveform[i]
local tick_height = math.max(graphics_scaled_height*tick_amplitude*2, tick_width/2)
love.graphics.rectangle(
'fill', graphics_width/2+i*tick_distance,
graphics_height/2 - tick_height/2,
tick_width, tick_height,
tick_width/2, tick_width/2
)
love.graphics.rectangle(
'fill', graphics_width/2-(i+1)*tick_distance,
graphics_height/2 - tick_height/2,
tick_width, tick_height,
tick_width/2, tick_width/2
)
tick_amplitude_sum = tick_amplitude_sum + tick_amplitude
end
-- Used to manipulate the degree of fade (if enabled).
tick_amplitude_average = tick_amplitude_sum/tick_count
end
--- Sets the type of bar visualization.
-- @param v number: An integer of 1-4. Each changes the bar visualization properties.
function visualization.setType(v)
visualizer_type = v
end
--- Obtains the type of bar visualization.
-- @return number: An integer of 1-4. The type of bar visualization.
function visualization.getType()
return visualizer_type
end
--- Enable/Disable fade.
-- @param f boolean: Fade option.
function visualization.setFade(f)
fade_activated = f
-- If disabled, fade intensity becomes 0.
if not f then
gui.graphics.setColor(nil, 0)
end
end
--- Obtains status of fade activation.
-- @return boolean: A boolean representing the status of fade activation.
function visualization.isFading()
return fade_activated
end
--- Determines if the visualization would change if an FFT was performed.
--- Aka, is there any point to running an FFT?
-- Just a bit of FFT memoization for efficiency.
-- @return boolean: True if the visualization would change. False otherwise.
function visualization.wouldChange()
-- For when playing music from files.
if (audio.music.tellSong('samples') ~= old_sample) then
return true
end
-- For when using a Recording Device.
if audio.recordingdevice.isActive() then
return not audio.isPaused()
end
return false
end
--- Obtains FFT input size of audio being sampled.
-- @returns number: A number representing the sampling size.
function visualization.getSamplingSize()
return sampling_size
end
return visualization