Repository navigation
Expand file tree
/
Copy pathglobals.lua
More file actions
400 lines (340 loc) · 11.5 KB
/
Copy pathglobals.lua
File metadata and controls
400 lines (340 loc) · 11.5 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
local json = require( "json" )
local timer = require( "timer" )
local util = require( "util" )
local mouse = require( "mouse" )
math.randomseed( os.time() )
-- ffi
ffi = require( "ffi" )
user32 = ffi.load( "user32" )
kernel32 = ffi.load( "kernel32" )
comdlg32 = ffi.load( "Comdlg32" )
glfw = ffi.load( "glfw3" )
ffi.cdef( lovr.filesystem.read( "cdef.c" ) )
-- shader
shader = lovr.graphics.newShader( "shader.vs", "shader.fs", { flags = { glow = false, normalMap = false, vertexTangents = false, tonemap = true } } )
environmentMap = lovr.graphics.newTexture( "out_ibl.ktx" )
sphericalHarmonics = lovr.graphics.newBuffer( { 'vec3', layout = 'std140' }, {
{ 0.215583562850952, 0.215583562850952, 0.215583562850952 },
{ 0.043746661394835, 0.043746661394835, 0.043746661394835 },
{ -0.032223708927631, -0.032223708927631, -0.032223708927631 },
{ -0.144690990447998, -0.144690990447998, -0.144690990447998 },
{ -0.056381676346064, -0.056381676346064, -0.056381676346064 },
{ 0.006616474594921, 0.006616474594921, 0.006616474594921 },
{ -0.035705659538507, -0.035705659538507, -0.035705659538507 },
{ -0.017640553414822, -0.017640553414822, -0.017640553414822 },
{ 0.101310119032860, 0.101310119032860, 0.101310119032860 },
} )
-- globals
e_background_mode = { shader = 1, background = 2, solid = 3 }
e_drawer_state = { fully_open = 1, fully_closed = 2, sliding = 3 }
metrics = {
case_height = 0.18,
feet_height = 0.01,
btn_min = 0.175,
btn_max = 0.177,
btn_inner_min = 0.168,
btn_inner_max = 0.170,
pixel_density_title = 1.2,
pixel_density_small_lcd = 1.4,
pixel_density_monostereo = 1.4,
pixel_density_track_list = 1.0,
ppm = 2000,
track_height_pixels = 58,
half_track_height_pixels = 29,
num_visible_tracks = 8,
drawer_range_x = 0.11107,
drawer_speed = 0.5,
num_visible_presets = 9,
timeout_launch = 1,
inactivity_interval = 5,
camera_zoom_speed = 0.08,
camera_zoom_min = -0.25,
camera_zoom_max = -10
}
winamp_info = {
hwnd = nil,
track_kbps = 0,
track_khz = 0,
mono_stereo = 2, -- 1 = mono, 2 = stereo
shuffle = 1,
repeatval = 1,
playing_state = 0, -- 0 = stopped, 1 = playing, 3 = paused
eq_on = 1,
eq_auto = 1,
eq_preamp = 31.5,
eq_bands = { 31.5, 31.5, 31.5, 31.5, 31.5, 31.5, 31.5, 31.5, 31.5, 31.5 },
time_display_mode = 0, -- 0 = elapsed, 1 = remaining
elapsed_secs = -1,
elapsed_mins = -1,
remaining_secs = -1,
remaining_mins = -1,
cur_track_length_secs = 0,
cur_track_length_mins = 0,
cur_track_title = "",
spectrum = nil,
spectrum_averaged = nil,
seek_position = 0,
map_handle = nil,
p_data = nil,
num_mapping_attempts = 0
}
passes = {}
world = lovr.physics.newWorld()
curve = lovr.math.newCurve( 64 )
vis_shaders = { active_idx = 3 }
backgrounds = { active_idx = 1 }
font = lovr.graphics.getDefaultFont()
window = { handle = nil, last_width = 0, last_height = 0, last_x = 0, last_y = 0 }
camera = { zoom = -0.72, pan = { x = 0, y = 0.18 }, pan_prev = { x = 0, y = 0 }, angle = 0.5, angle_prev = 0.5 }
presets_list = { scroll = 0, ray_hit_y = 0, selected_idx = 1, items = {}, should_open = false, state = e_drawer_state.fully_closed }
playlist = { scroll = 0, tracks = {}, ray_hit_y = 0, selected_idx = 1, refresh = true, num_items_checked = 0, total_time_hours = 0, total_time_mins = 0, total_time_secs = 0 }
timer_sync = timer( true )
timer_scroll_title = timer( true )
timer_sa_bars = timer( true )
timer_flash_time = timer( true )
timer_inactivity = timer( true )
timer_mapping = timer( true )
local about_str = require( "about_str" )
about_app = {
wavy_tex = nil,
wavy_pass = nil,
wavy_shader = lovr.graphics.newShader( "fill", "wavy.fs" ),
show = false,
scroll = 0,
scroll_pause = false,
text = about_str
}
sync_interval = 0.2
units_per_pixel = 0
-- global functions
function is_winamp_panel_visible( num, settings, controls, skin )
-- eq = 0, pl = 1
local hwnd = ffi.cast( "HWND", user32.SendMessageA( winamp_info.hwnd, ffi.C.WM_WA_IPC, num, ffi.C.IPC_GETWND ) )
if hwnd == nil or hwnd == ffi.cast( "HWND", 0 ) then
return false
end
local res = user32.IsWindowVisible( hwnd )
return res ~= 0
end
function create_sa_frames( skin )
local texture = skin.sa_bars.texture
for i = 1, 14 do
local mt = lovr.graphics.newMaterial( { texture = texture, uvScale = { 1 / 14, -1 }, uvShift = { (1 / 14) * (i - 1), 0 } } )
table.insert( skin.sa_bars, mt )
end
for i = 1, 19 do
table.insert( skin.sa_bars.data, 14 )
table.insert( skin.sa_bars.peaks, { y = 1, frames = 0 } )
end
end
function create_digit_frames( skin )
local texture = skin.digits.texture
for i = 1, 11 do
local mt = lovr.graphics.newMaterial( { texture = texture, uvScale = { 1 / 11, -1 }, uvShift = { (1 / 11) * (i - 1), 0 } } )
table.insert( skin.digits, mt )
end
end
function update_curve()
for i = 1, 9 do
local idx = 1 + (i - 1) * 7
local idx1 = idx
local idx2 = idx + 1
local idx3 = idx + 2
local idx4 = idx + 3
local idx5 = idx + 4
local idx6 = idx + 5
local idx7 = idx + 6
local idx8 = idx + 7
local y1 = winamp_info.eq_bands[ i + 0 ]
local y4 = winamp_info.eq_bands[ i + 1 ]
y1 = util.map_range( 0, 63, 3, 73, y1 )
y8 = util.map_range( 0, 63, 3, 73, y4 )
local y2, y3, y4 = y1, y1, y1
local y5, y6, y7 = y8, y8, y8
curve:setPoint( idx1, 8 + ((i - 1) * 48.44), y1, 0 )
curve:setPoint( idx2, 8 + ((i - 0.9) * 48.44), y2, 0 )
curve:setPoint( idx3, 8 + ((i - 0.8) * 48.44), y3, 0 )
curve:setPoint( idx4, 8 + ((i - 0.7) * 48.44), y4, 0 )
curve:setPoint( idx5, 8 + ((i - 0.3) * 48.44), y5, 0 )
curve:setPoint( idx6, 8 + ((i - 0.2) * 48.44), y6, 0 )
curve:setPoint( idx7, 8 + ((i - 0.1) * 48.44), y7, 0 )
curve:setPoint( idx8, 8 + (i * 48.44), y8, 0 )
end
end
function refresh_playlist( winamp_num_tracks, hProcess )
playlist.tracks = {}
for i = 1, winamp_num_tracks do
local remote_ptr = user32.SendMessageA( winamp_info.hwnd, ffi.C.WM_WA_IPC, i - 1, 213 )
if remote_ptr ~= 0 then
local wbuf = util.read_wstring( hProcess, ffi.cast( "void*", remote_ptr ), 512 )
local path = util.utf16_to_utf8( wbuf )
playlist.tracks[ #playlist.tracks + 1 ] = path
end
end
end
function get_winamp_spectrum( settings )
local SHM_NAME = "winamp_spectrum_analyzer_data"
-- open memory mapping created by plugin
winamp_info.map_handle = kernel32.OpenFileMappingA( ffi.C.FILE_MAP_READ, 0, SHM_NAME )
if winamp_info.map_handle == nil then
settings.session.show_fake_sa = true
else
settings.session.show_fake_sa = false
end
-- map memory
winamp_info.p_data = kernel32.MapViewOfFile( winamp_info.map_handle, ffi.C.FILE_MAP_READ, 0, 0, 150 )
if winamp_info.p_data == nil then
settings.session.show_fake_sa = true
kernel32.CloseHandle( winamp_info.map_handle )
winamp_info.spectrum = {}
for i = 0, 149 do
winamp_info.spectrum[ i ] = 0
end
else
settings.session.show_fake_sa = false
-- cast pointer to array
winamp_info.spectrum = ffi.cast( "unsigned char*", winamp_info.p_data )
end
end
function average_frequencies()
local scaling_factor = 16
local num_indices = { 4, 4, 2, 2, 2, 2, 2, 2, 3, 4, 5, 5, 6, 6, 6, 7, 7, 4, 2 }
winamp_info.spectrum_averaged = {}
local cur = 0
for i = 1, 19 do
local sum = 0
for j = 1, num_indices[ i ] do
local val = winamp_info.spectrum[ (j + cur) - 1 ]
if val <= 1 then val = 0 end -- cut very low values
sum = sum + val
end
cur = cur + num_indices[ i ]
local avg = sum / num_indices[ i ]
winamp_info.spectrum_averaged[ #winamp_info.spectrum_averaged + 1 ] = avg * scaling_factor
scaling_factor = scaling_factor + 0.6
end
end
function update_sa_bars( skin )
if timer_sa_bars:get_elapsed() > 0.029 then
timer_sa_bars:reset()
for i = 1, 19 do
skin.sa_bars.data[ i ] = math.floor( util.map_range( 0, 255, 0, 14, winamp_info.spectrum_averaged[ i ] ) )
skin.sa_bars.data[ i ] = util.clamp( 0, 14, skin.sa_bars.data[ i ] )
end
end
end
function update_peaks( skin, dt )
if winamp_info.playing_state == 1 or winamp_info.playing_state == 3 then
for i = 1, 19 do
if skin.sa_bars.peaks[ i ].y < skin.sa_bars.data[ i ] then
skin.sa_bars.peaks[ i ].y = skin.sa_bars.data[ i ]
skin.sa_bars.peaks[ i ].frames = 0
else
skin.sa_bars.peaks[ i ].frames = skin.sa_bars.peaks[ i ].frames + dt
end
if skin.sa_bars.peaks[ i ].frames > 0.82 then
skin.sa_bars.peaks[ i ].y = skin.sa_bars.peaks[ i ].y - 10 * dt
end
end
end
end
function generate_fake_sa()
if winamp_info.playing_state == 0 then return end
for i = 0, 149 do
local val = math.random( 0, 8 )
winamp_info.spectrum[ i ] = val
end
end
function load_vis_shaders()
local folders = lovr.filesystem.getDirectoryItems( "vis_shaders" )
for i = 1, #folders do
local items = lovr.filesystem.getDirectoryItems( "vis_shaders/" .. folders[ i ] )
local entry = { name = folders[ i ], shader = nil, textures = {} }
for j = 1, #items do
if string.sub( items[ j ], -3 ) == ".fs" and not entry.shader then
entry.shader = lovr.graphics.newShader( "fill", "vis_shaders/" .. folders[ i ] .. "/" .. items[ j ] )
elseif string.sub( items[ j ], -4 ) == ".png" and #entry.textures < 4 then
local str = items[ j ]
str = str:sub( 1, -5 )
local tex = { name = str, texture = lovr.graphics.newTexture( "vis_shaders/" .. folders[ i ] .. "/" .. items[ j ] ) }
entry.textures[ #entry.textures + 1 ] = tex
end
end
vis_shaders[ #vis_shaders + 1 ] = entry
end
end
function load_backgrounds()
local items = lovr.filesystem.getDirectoryItems( "backgrounds" )
for i = 1, #items do
local bg = lovr.graphics.newTexture( "backgrounds/" .. items[ i ] )
backgrounds[ #backgrounds + 1 ] = bg
end
end
function try_launch_winamp( settings )
if not settings.session.launch_winamp then
return
end
if winamp_info.hwnd ~= nil then
-- already running
return
end
-- check for wine
local on_wine = false
local res, ntdll = pcall( ffi.load, "ntdll.dll" )
if res then
on_wine = pcall( function() return ntdll.wine_get_version end )
end
local path = settings.session.winamp_path .. "\\winamp.exe"
local start = lovr.timer.getTime()
local f = io.open( path, "r" )
if f then
f:close()
local cmd = string.format( 'start /min "" "%s"', path )
if on_wine then
cmd = string.format( 'start "" "%s"', path )
end
os.execute( cmd )
while lovr.timer.getTime() - start < metrics.timeout_launch do
winamp_info.hwnd = util.find_window( "Winamp v1.x", nil )
if winamp_info.hwnd ~= nil then
return
end
end
print( "timeout exceeded" )
return
else
print( "file not found" )
return
end
end
function add_to_playlist( file_path )
if winamp_info.hwnd == nil then return end
local w_path, char_count = util.utf8_to_utf16( file_path )
if not w_path then return end
local cds = ffi.new( "COPYDATASTRUCT" )
cds.dwData = 1100
cds.cbData = char_count * ffi.sizeof( "wchar_t" )
cds.lpData = w_path
local lParam = ffi.cast( "intptr_t", ffi.cast( "void*", cds ) )
user32.SendMessageW( winamp_info.hwnd, 0x004A, 0, lParam )
-- set playlist pos to last added and play
local count = user32.SendMessageA( winamp_info.hwnd, ffi.C.WM_WA_IPC, 0, 124 );
user32.SendMessageA( winamp_info.hwnd, ffi.C.WM_WA_IPC, tonumber( count ) - 1, 121 )
local res = user32.PostMessageA( winamp_info.hwnd, ffi.C.WM_COMMAND, 40045, 0 )
end
function retry_mapping( settings )
if winamp_info.map_handle == nil and winamp_info.num_mapping_attempts < 20 then
if timer_mapping:get_elapsed() > 0.5 then
timer_mapping:reset()
winamp_info.num_mapping_attempts = winamp_info.num_mapping_attempts + 1
get_winamp_spectrum( settings )
end
end
end
function on_file_drop( window, count, paths )
for i = 0, count - 1 do
local path = ffi.string( paths[ i ] )
add_to_playlist( path )
end
end
drop_callback = ffi.cast( "GLFWdropfun", on_file_drop )