| 91 |
end |
end |
| 92 |
end |
end |
| 93 |
|
|
| 94 |
|
def thin_events |
| 95 |
|
info = [] |
| 96 |
|
nev = 0 |
| 97 |
|
interval = (get_global_settings("thin_event_interval") || "10").to_f |
| 98 |
|
self.each_editable_track { |tr| |
| 99 |
|
sel = tr.selection |
| 100 |
|
if sel == nil || sel.count == 0 |
| 101 |
|
next |
| 102 |
|
end |
| 103 |
|
pt = tr.pointer(sel[0]) |
| 104 |
|
info.push([tr, pt, 0, sel, nil]) |
| 105 |
|
nev += sel.count |
| 106 |
|
} |
| 107 |
|
if info.count == 0 |
| 108 |
|
message_box("No events are selected", "Cannot process events", :ok, :error) |
| 109 |
|
return |
| 110 |
|
end |
| 111 |
|
# Check the event kind |
| 112 |
|
typ = nil # Control: 0 to 127, Pitch bend: 128 |
| 113 |
|
info.each { |ip| |
| 114 |
|
ip[3].each { |pt| |
| 115 |
|
if pt.kind == :control |
| 116 |
|
t = pt.code |
| 117 |
|
elsif pt.kind == :pitch_bend |
| 118 |
|
t = 128 |
| 119 |
|
else |
| 120 |
|
message_box("Only control and pitch bend events can be processed.", "Cannot process events", :ok, :error) |
| 121 |
|
return |
| 122 |
|
end |
| 123 |
|
if typ == nil |
| 124 |
|
typ = t |
| 125 |
|
elsif typ != t |
| 126 |
|
message_box("All events must be the same type", "Cannot process events", :ok, :error) |
| 127 |
|
return |
| 128 |
|
end |
| 129 |
|
} |
| 130 |
|
} |
| 131 |
|
msg = "#{nev} events (" + (typ == 128 ? "Pitch bend" : "Control #{typ}") + ") found in #{info.count} track#{info.count > 1 ? "s" : ""}." |
| 132 |
|
hash = Dialog.run("Thin Events") { |
| 133 |
|
layout(1, |
| 134 |
|
item(:text, :title=>msg), |
| 135 |
|
layout(1, |
| 136 |
|
item(:text, :title=>"Minimum event interval (in milliseconds, 10-1000)"), |
| 137 |
|
item(:textfield, :width=>40, :value=>interval.to_s, :range=>[10, 1000], :tag=>"interval"))) |
| 138 |
|
} |
| 139 |
|
return if hash[:status] != 0 |
| 140 |
|
interval = hash["interval"].to_f |
| 141 |
|
set_global_settings("thin_event_interval", interval.to_s) |
| 142 |
|
next_tick = nil |
| 143 |
|
while true |
| 144 |
|
# Look for the earliest event |
| 145 |
|
ip = info.min_by { |ip0| ip0[1].selected? ? ip0[1].tick : 0x7fffffff } |
| 146 |
|
pt = ip[1] |
| 147 |
|
break if !pt.selected? # All events are processed |
| 148 |
|
next_tick ||= pt.tick |
| 149 |
|
if ip[4] == nil || pt.tick >= next_tick |
| 150 |
|
ip[4] ||= [] |
| 151 |
|
new_tick = pt.tick |
| 152 |
|
new_data = pt.data |
| 153 |
|
pt.next_in_selection |
| 154 |
|
else |
| 155 |
|
while pt.next_in_selection && pt.tick < next_tick |
| 156 |
|
end |
| 157 |
|
if pt.selected? |
| 158 |
|
if pt.tick == next_tick |
| 159 |
|
new_tick = pt.tick |
| 160 |
|
new_data = pt.data |
| 161 |
|
pt.next_in_selection |
| 162 |
|
else |
| 163 |
|
# ip[4] is not empty, so we should have ip[4][-2] |
| 164 |
|
# Interpolate the data value |
| 165 |
|
old_tick = ip[4][-2] |
| 166 |
|
old_data = ip[4][-1] |
| 167 |
|
val = Float(pt.data - old_data) / (pt.tick - old_tick) * (next_tick - old_tick) + old_data |
| 168 |
|
new_tick = next_tick |
| 169 |
|
new_data = val # val is left as float |
| 170 |
|
end |
| 171 |
|
else |
| 172 |
|
pt.last_in_selection |
| 173 |
|
new_tick = pt.tick |
| 174 |
|
new_data = pt.data |
| 175 |
|
pt.next_in_selection |
| 176 |
|
end |
| 177 |
|
end |
| 178 |
|
if ip[4].count > 0 |
| 179 |
|
old_data = ip[4][-1] |
| 180 |
|
if old_data.to_i == new_data.to_i |
| 181 |
|
new_tick = nil # Skip this event |
| 182 |
|
end |
| 183 |
|
end |
| 184 |
|
if new_tick |
| 185 |
|
ip[4].push(new_tick, new_data) |
| 186 |
|
end |
| 187 |
|
next_tick = self.time_to_tick(self.tick_to_time(next_tick) + interval / 1000.0) |
| 188 |
|
end |
| 189 |
|
nev_new = 0 |
| 190 |
|
info.each { |ip| |
| 191 |
|
ntr = Track.new |
| 192 |
|
(ip[4].count / 2).times { |i| |
| 193 |
|
tick = ip[4][i * 2] |
| 194 |
|
val = ip[4][i * 2 + 1].to_i |
| 195 |
|
if typ == 128 |
| 196 |
|
ntr.add(tick, :pitch_bend, val) |
| 197 |
|
else |
| 198 |
|
ntr.add(tick, :control, typ, val) |
| 199 |
|
end |
| 200 |
|
} |
| 201 |
|
ip[0].cut(ip[3]) |
| 202 |
|
ip[0].merge(ntr) |
| 203 |
|
nev_new += ntr.nevents |
| 204 |
|
} |
| 205 |
|
message_box("#{nev} events were replaced with #{nev_new} events.", "", :ok) |
| 206 |
|
end |
| 207 |
|
|
| 208 |
end |
end |
| 209 |
|
|
| 210 |
register_menu("Change timebase...", :change_timebase) |
register_menu("Change timebase...", :change_timebase) |
| 211 |
register_menu("Randomize ticks...", :randomize_ticks, 1) |
register_menu("Randomize ticks...", :randomize_ticks, 1) |
| 212 |
|
register_menu("Thin selected events...", :thin_events, 1) |
| 213 |
# register_menu("Change control number...", :change_control_number_ext) |
# register_menu("Change control number...", :change_control_number_ext) |