-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtoolbox.lua
More file actions
executable file
·143 lines (119 loc) · 3.39 KB
/
Copy pathtoolbox.lua
File metadata and controls
executable file
·143 lines (119 loc) · 3.39 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
-- Queries the selection range start and end lines
function selection_line_range()
local line_start, line_end
local pattern_index = renoise.song().selected_pattern_index
local iter = renoise.song().pattern_iterator:lines_in_pattern(pattern_index)
for pos,line in iter do
for _,note_column in pairs(line.note_columns) do
if (note_column.is_selected) then
line_start = line_start or pos.line
line_end = line_end and math.max(line_end, pos.line) or pos.line
end
end
for _,effect_column in pairs(line.effect_columns) do
if (effect_column.is_selected) then
line_start = line_start or pos.line
line_end = line_end and math.max(line_end, pos.line) or pos.line
end
end
end
return line_start, line_end
end
-- PHP style exlpode()
function explode(div,str)
if (div=='') then return false end
local pos,arr = 0,table.create()
-- for each divider found
for st,sp in function() return string.find(str,div,pos,true) end do
table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider
pos = sp + 1 -- Jump past current divider
end
table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider
return arr
end
-- PHP style (int) type casting
function toint(num)
num = tonumber(num)
if num == nil then
return 0
else
return math.floor(num)
end
end
-- PHP style remove trailing and leading whitespace from string
function trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
-- Convert bpm to midi tempo
function bpm_to_tempo(bpm)
return math.floor(60000000/bpm + .5)
end
-- Converts all delta times in track to absolute times
function _delta2Absolute(track)
local mc = track:count()
local last = 0
for i=1,mc do
local msg = explode(' ', track[i])
local t = last + toint(msg[1])
msg[1] = t
track[i] = table.concat(msg, " ")
last = t
end
end
-- Variable length string to int (+repositioning)
function _readVarLen(str, pos)
pos = pos + 1
local value = str:byte(pos)
if bit.band(value, 0x80) > 0 then
local c = nil
value = bit.band(value, 0x7F)
repeat
pos = pos + 1
c = str:byte(pos)
value = bit.lshift(value, 7) + bit.band(c, 0x7F)
until bit.band(c, 0x80) <= 0
end
return value, pos
end
-- int to variable length string
function _writeVarLen(value)
local buf = bit.band(value, 0x7F)
local str = ""
value = bit.rshift(value, 7)
while value > 0 do
buf = bit.lshift(buf, 8)
buf = bit.bor(buf, bit.bor(bit.band(value, 0x7F), 0x80))
value = bit.rshift(value, 7)
end
while true do
str = str .. string.char(buf % 256)
if bit.band(buf, 0x80) > 0 then
buf = bit.rshift(buf, 8)
else
break
end
end
-- print(str:byte())
return str
end
-- int to bytes (length len)
function _getBytes(n, len)
local str = ""
for i = len-1, 0 , -1 do
local tmp = math.floor(n / math.pow(256 , i))
if tmp > 255 then
local tmp2 = toint(tmp / 256) * 256
tmp = tmp - tmp2
end
str = str .. string.char(toint(tmp))
end
return str
end
-- hexstr to binstr
function _hex2bin(hex_str)
local bin_str = ""
for i = 1, hex_str:len(), 2 do
bin_str = bin_str .. string.char(tonumber(hex_str:sub(i, i + 2), 16))
end
return bin_str
end