-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmidiToBinaryVector.lua
More file actions
130 lines (112 loc) · 3.01 KB
/
midiToBinaryVector.lua
File metadata and controls
130 lines (112 loc) · 3.01 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
-- Importing need libraries
midi = require "MIDI" -- http://www.pjb.com.au/comp/lua/MIDI.html
require "torch" -- http://torch.ch/
-- Set the default tensor type to floats
torch.setdefaulttensortype('torch.FloatTensor')
-- allocate funciton to create a 2D array
allocate_array = function(row_size,col_size)
local out = {}
for i=1,row_size do
out[i] = {}
for j=1,col_size do
out[i][j] = 0
end
end
return out
end
--[[
midiToBinaryVec
input: Takes in a filename
output: spits out a torch float tensor.
--]]
setIntensity = function(binVector,note,i,intensity)
binVector[note][i] = (intensity )--/ 128)
--if(binVector[note])
--print(binVector[note][i])
end
midiToBinaryVec = function(filename)
print(filename)
-- read the file
local f = assert(io.open(filename, "r"))
local t = f:read("*all")
if t == nil then
f:close()
return nil
end
-- Set some local max and min variabes
local min = 100000000
local max = 0
-- This variabe keeps track of the current notes
local notes = {}
-- Concert the read in midi to a score object
m = midi.midi2score(t)
-- get the the total ticks in a midi
local total_ticks = midi.score2stats(m)["nticks"]
-- get the number of channels
numchannels = table.getn(m)
--iterate through the score objects channels and find all notes
for k, v in pairs(m)
do
if type(v)=="table" then
for k2,v2 in pairs(v)
do
if v2[1] == "note"
then
-- Finding the minimum and maximum amount of duration
if max < v2[3] then max = v2[3] end
if min > v2[3] then min = v2[3] end
notes[#notes+1] = v2
end
end
end
end
-- determing the overall array length using total ticks / smallest furation
array_col = total_ticks/50
array_row = 128 -- The number of midis notes, this can be made better.
f:close()
-- need to allocate array to feeat everything into
--local binVector = allocate_array(array_row,array_col)
local binVector = torch.Tensor(array_row,array_col):zero()
--print("NOTES: " .. #notes)
--print(notes)
ma = require "math"
-- fit all notes
for k,n in pairs(notes)
do
--print(k)
--print(n)
local fr = ma.min((n[2])/(min) + 1,array_col)
local to = ma.min((n[2]+n[3])/(min)+1,array_col)
local note = ma.min(ma.max(n[5],0),128)
local intensity = n[6]
--print(binVector[note])
for i=fr,to do
ok,err = pcall(setIntensity,binVector, note, i, intensity)
if( not ok)
then
print ("ERROR: ")
print(err)
print(ok)
return nil
else
--print ("Okay: ")
break
end
--if(intensity/100~=.96) then print(intensity/128) end
end
end
--binVector2 = torch.Tensor(binVector)
return binVector
end
-- A simple test print function for printing out the table representation
printBinaryVector = function(binVec)
local s = ""
for k,v in pairs(binVec)
do
for k2,v2 in pairs(v) do
s = s .. v2
end
print(s)
s = ""
end
end