Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/Ruby_Scripts/200.commands.rb

Parent Directory Parent Directory | Revision Log Revision Log


Revision 82 - (show annotations) (download)
Sun May 28 02:15:58 2017 UTC (6 years, 10 months ago) by toshinagata1964
File size: 6382 byte(s)
'Thin selected events' command is implemented.
1 # Copyright (c) 2010-2017 Toshi Nagata. All rights reserved.
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation version 2 of the License.
6 #
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU General Public License for more details.
11
12 class Sequence
13
14 def change_control_number_ext
15 hash = Dialog.run {
16 layout(1,
17 layout(2,
18 item(:text, :title=>"Old control Number"),
19 item(:textfield, :width=>40, :range=>[0, 127], :tag=>"old"),
20 item(:text, :title=>"New control number"),
21 item(:textfield, :width=>40, :range=>[0, 127], :tag=>"new")),
22 item(:checkbox, :title=>"Change only editable tracks", :tag=>"editable_only"),
23 item(:checkbox, :title=>"Change only selected events", :tag=>"selection_only"))
24 }
25 if hash["status"] == 0
26 old = hash["old"]
27 new = hash["new"]
28 editable_only = hash["editable_only"]
29 selection_only = hash["selection_only"]
30 # puts "old = #{old}, new = #{new}, editable_only = #{editable_only}"
31 each_track { |tr|
32 next if editable_only && !tr.editable?
33 tr.send(selection_only ? :each_selected : :each) { |p|
34 if p.kind == :control && p.code == old
35 p.code = new
36 end
37 }
38 }
39 end
40 end
41
42 def change_timebase
43 timebase = self.timebase
44 hash = Dialog.run("Change Timebase") {
45 layout(1,
46 layout(2,
47 item(:text, :title=>"Current timebase = #{timebase}"),
48 nil,
49 item(:text, :title=>"New timebase"),
50 item(:textfield, :width=>40, :range=>[24, 960], :tag=>"new")))
51 }
52 # p hash
53 if hash[:status] == 0
54 new = hash["new"].to_f
55 mult = new / timebase
56 each_track { |tr|
57 set1 = tr.all_events
58 set2 = tr.eventset { |p| p.kind == :note }
59 set2.modify_duration("*", mult)
60 set1.modify_tick("*", mult)
61 }
62 self.set_timebase(new)
63 end
64 end
65
66 def randomize_ticks
67 wd = (get_global_settings("randomize_tick_width") || "10").to_f
68 if !self.has_selection
69 message_box("No events are selected.", "Error", :ok);
70 return
71 end
72 hash = Dialog.run("Randomize Ticks") {
73 layout(1,
74 layout(1,
75 item(:text, :title=>"Randomize width (in milliseconds, 10-1000)"),
76 item(:textfield, :width=>40, :value=>wd.to_s, :range=>[10, 1000], :tag=>"width")))
77 }
78 if hash[:status] == 0
79 wd = hash["width"].to_f
80 set_global_settings("randomize_tick_width", wd.to_s)
81 each_track { |tr|
82 s = tr.selection
83 t = []
84 s.each { |p|
85 t1 = self.tick_to_time(p.tick)
86 t2 = t1 + (rand() - 0.5) * wd / 1000.0
87 t.push(Integer(self.time_to_tick(t2)))
88 }
89 s.modify_tick(t)
90 }
91 end
92 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
209
210 register_menu("Change timebase...", :change_timebase)
211 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)

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26