-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.lua
87 lines (73 loc) · 1.74 KB
/
utils.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
require 'audio'
require 'image'
--local cfg = dofile 'config'
local function getProcessedData(img, imageSize)
local img = img:resize(1,imageSize,imageSize)
img = img/255
return img
end
local function getImageData(fn, imageSize)
local img = image.load(fn)
local imgData = getProcessedData(img, imageSize)
return imgData
end
local function split(input, sep)
if sep == nil then sep = "%s" end
local t={} ; i=1
for str in string.gmatch(input, "([^"..sep.."]+)") do
t[i] = str
i = i + 1
end
return t
end
local function slice(tbl, first, last, step)
local sliced = {}
for i = first or 1, last or #tbl, step or 1 do
sliced[#sliced+1] = tbl()
end
return sliced
end
local function getLabelTable(genre)
label = {}
for k,v in pairs(cfg.genres) do
if v == genre then label[k] = 1 else label[k] = 0 end
end
return label
end
local function getLabel(genre)
for k,v in pairs(cfg.genres) do
if v == genre then return k end
end
end
local function toInt(val)
return math.floor(val + 0.5)
end
local function isMono(songFile)
local song = audio.load(songFile)
return song:size(2)==1
end
local function splitString(str, delimiter)
result = {};
for match in (str..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result, match);
end
return result;
end
local function contains(table, val)
for i=1,#table do
if table[i] == val then return true end
end
return false
end
local utils = {}
utils.getProcessedData = getProcessedData
utils.getImageData = getImageData
utils.split = split
utils.slice = slice
utils.getLabelTable = getLabelTable
utils.getLabel = getLabel
utils.toInt = toInt
utils.isMono = isMono
utils.contains = contains
utils.splitString = splitString
return utils