Class: Ramekin::Meta
- Inherits:
-
Object
- Object
- Ramekin::Meta
- Includes:
- Error::Helpers
- Defined in:
- lib/ramekin/meta.rb
Constant Summary collapse
- SURROUND_R =
(-0x7f..0x80)
Instance Attribute Summary collapse
-
#amk ⇒ Object
readonly
Returns the value of attribute amk.
-
#author ⇒ Object
readonly
Returns the value of attribute author.
-
#comment ⇒ Object
readonly
Returns the value of attribute comment.
-
#echo ⇒ Object
readonly
Returns the value of attribute echo.
-
#game ⇒ Object
readonly
Returns the value of attribute game.
-
#instruments ⇒ Object
readonly
Returns the value of attribute instruments.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#readme ⇒ Object
readonly
Returns the value of attribute readme.
-
#sample_groups ⇒ Object
readonly
Returns the value of attribute sample_groups.
-
#tempo ⇒ Object
readonly
Returns the value of attribute tempo.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#volume ⇒ Object
readonly
Returns the value of attribute volume.
Instance Method Summary collapse
- #check_arg(*types) ⇒ Object
- #divisor ⇒ Object
- #done? ⇒ Boolean
- #echo_channel_bits ⇒ Object
- #echo_delay ⇒ Object
- #echo_feedback ⇒ Object
- #echo_fir ⇒ Object
- #echo_hex ⇒ Object
- #echo_volumes ⇒ Object
- #expect_arg(type) ⇒ Object
- #expect_args(*types) ⇒ Object
-
#initialize(elements, sample_path = nil) ⇒ Meta
constructor
A new instance of Meta.
- #next! ⇒ Object
- #pack_info ⇒ Object
- #parse ⇒ Object
- #parse_directive ⇒ Object
- #peek ⇒ Object
- #sample_path ⇒ Object
- #sample_path_unsafe ⇒ Object
- #t_value ⇒ Object
- #t_with_divisor ⇒ Object
Constructor Details
#initialize(elements, sample_path = nil) ⇒ Meta
17 18 19 20 21 22 23 |
# File 'lib/ramekin/meta.rb', line 17 def initialize(elements, sample_path=nil) @elements = elements @instruments = [] @sample_groups = [] @sample_path = sample_path = {} end |
Instance Attribute Details
#amk ⇒ Object (readonly)
Returns the value of attribute amk.
10 11 12 |
# File 'lib/ramekin/meta.rb', line 10 def amk @amk end |
#author ⇒ Object (readonly)
Returns the value of attribute author.
7 8 9 |
# File 'lib/ramekin/meta.rb', line 7 def end |
#comment ⇒ Object (readonly)
Returns the value of attribute comment.
8 9 10 |
# File 'lib/ramekin/meta.rb', line 8 def comment @comment end |
#echo ⇒ Object (readonly)
Returns the value of attribute echo.
16 17 18 |
# File 'lib/ramekin/meta.rb', line 16 def echo @echo end |
#game ⇒ Object (readonly)
Returns the value of attribute game.
6 7 8 |
# File 'lib/ramekin/meta.rb', line 6 def game @game end |
#instruments ⇒ Object (readonly)
Returns the value of attribute instruments.
11 12 13 |
# File 'lib/ramekin/meta.rb', line 11 def instruments @instruments end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
13 14 15 |
# File 'lib/ramekin/meta.rb', line 13 def end |
#readme ⇒ Object (readonly)
Returns the value of attribute readme.
9 10 11 |
# File 'lib/ramekin/meta.rb', line 9 def readme @readme end |
#sample_groups ⇒ Object (readonly)
Returns the value of attribute sample_groups.
12 13 14 |
# File 'lib/ramekin/meta.rb', line 12 def sample_groups @sample_groups end |
#tempo ⇒ Object (readonly)
Returns the value of attribute tempo.
15 16 17 |
# File 'lib/ramekin/meta.rb', line 15 def tempo @tempo end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
5 6 7 |
# File 'lib/ramekin/meta.rb', line 5 def title @title end |
#volume ⇒ Object (readonly)
Returns the value of attribute volume.
14 15 16 |
# File 'lib/ramekin/meta.rb', line 14 def volume @volume end |
Instance Method Details
#check_arg(*types) ⇒ Object
244 245 246 247 248 249 250 251 |
# File 'lib/ramekin/meta.rb', line 244 def check_arg(*types) if peek && types.include?(peek.type) next! @current else nil end end |
#divisor ⇒ Object
91 92 93 |
# File 'lib/ramekin/meta.rb', line 91 def divisor t_with_divisor[1] end |
#done? ⇒ Boolean
115 116 117 |
# File 'lib/ramekin/meta.rb', line 115 def done? @elements.empty? && @peek.nil? end |
#echo_channel_bits ⇒ Object
60 61 62 |
# File 'lib/ramekin/meta.rb', line 60 def echo_channel_bits (@echo_channels ||= Set.new).map { |x| 1 << x }.inject(0, &:|) end |
#echo_delay ⇒ Object
68 69 70 |
# File 'lib/ramekin/meta.rb', line 68 def echo_delay @echo_delay || 0 end |
#echo_feedback ⇒ Object
72 73 74 |
# File 'lib/ramekin/meta.rb', line 72 def echo_feedback (@echo_feedback || 0) & 0xFF end |
#echo_fir ⇒ Object
76 77 78 |
# File 'lib/ramekin/meta.rb', line 76 def echo_fir @echo_fir || 0 end |
#echo_hex ⇒ Object
80 81 82 83 84 85 |
# File 'lib/ramekin/meta.rb', line 80 def echo_hex sprintf("$ef$%02x$%02x$%02x $f1$%02x$%02x$%02x", echo_channel_bits, *echo_volumes, echo_delay, echo_feedback, echo_fir ) end |
#echo_volumes ⇒ Object
64 65 66 |
# File 'lib/ramekin/meta.rb', line 64 def echo_volumes [(@echo_volume_l || 0) & 0xFF, (@echo_volume_r || 0) & 0xFF] end |
#expect_arg(type) ⇒ Object
240 241 242 |
# File 'lib/ramekin/meta.rb', line 240 def expect_arg(type) expect_args(type).first end |
#expect_args(*types) ⇒ Object
229 230 231 232 233 234 235 236 237 238 |
# File 'lib/ramekin/meta.rb', line 229 def expect_args(*types) types.map do |type| next! unless @current.type == type error! "expected #{type} in ##{@current_directive.value} directive" end @current end end |
#next! ⇒ Object
253 254 255 256 257 258 259 260 261 |
# File 'lib/ramekin/meta.rb', line 253 def next! if @peek @current = @peek @peek = nil return @current end @current = @elements.shift end |
#pack_info ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/ramekin/meta.rb', line 39 def pack_info packs = self.instruments.map(&:pack).uniq pack_info = packs.map do |pack| out = StringIO.new out << " #{pack.name}" out << " (posted by #{pack.authors.join(', ')})" if pack..any? out << "\n" out << " - #{pack.url}" out.string end.join("\n\n") end |
#parse ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/ramekin/meta.rb', line 119 def parse loop do break if done? next! case @current.type when :directive then parse_directive when :amk then @amk = @current when :option then [@current.value] = true when :w then @volume = @current when :t, :bpm then @tempo = @current; @t_with_divisor = nil else error! 'invalid token in header' end end end |
#parse_directive ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/ramekin/meta.rb', line 139 def parse_directive @current_directive = @current case @current.value when 'title' then @title = expect_arg(:string) when 'game' then @game = expect_arg(:string) when 'author' then = expect_arg(:string) when 'comment' then @comment = expect_arg(:string) when 'readme' then @readme = expect_arg(:string) when 'pack' then pack_name = expect_arg(:string) @current_pack = SamplePack.find(pack_name.value) \ or error!("can't find sample pack #{pack_name.value.inspect}, try running `ramekin package update`") when 'instrument' name, path = expect_args(:instrument, :string) extensions = [] while (el = check_arg(:adsr, :tuning, :gain, :o)) extensions << el end return unless @current_pack @instruments << Instrument.new(@current_pack, @current_directive, name, path, extensions) when 'default', 'optimized' then @sample_groups << @current when 'echo/channels' channels = @current_directive.values[1] case channels when 'all' @echo_channels = Set.new(0..7) return when 'none' @echo_channels = Set.new return end unless channels && channels =~ /\A\d(,\d)+\z/ error! 'invalid echo channels (list of single digit numbers separated by comma)' end @echo_channels = Set.new(channels.split(',').map(&:to_i)) unless @echo_channels.all? { |c| (0..7).include?(c) } error! 'invalid echo channels (must be 0-7)' end when 'echo/volume' vols = @current_directive.values[1] unless vols && vols =~ /\A-?\h\h?(,\h\h?)?/ error! 'invalid echo volume (one or two hex numbers)' end vols = vols.split(',').map { |v| v.to_i(16) } if vols.size == 1 @echo_volume_l = @echo_volume_r = vols[0] else @echo_volume_l, @echo_volume_r = vols end unless SURROUND_R.include?(@echo_volume_l) && SURROUND_R.include?(@echo_volume_r) error! 'invalid echo volume (-7f to 80, use negative for surround)' end when 'echo/delay' delay = @current_directive.values[1] unless delay && delay =~ /\A[0-7]\z/ error! 'invalid echo delay (single digit 0-7)' end @echo_delay = delay.to_i when 'echo/feedback' feedback = @current_directive.values[1] unless feedback && feedback =~ /\A-?\h\h?\z/ error! 'invalid echo feedback (one or two hex digits)' end @echo_feedback = feedback.to_i(16) unless SURROUND_R.include?(@echo_feedback) error! 'invalid echo feedback (-7F to 80, use negative for surround)' end when 'echo/fir' fir = @current_directive.values[1] unless fir && fir =~ /\A\h\h?\z/ error! 'invalid echo FIR setting (one or two hex digits)' end @echo_fir = fir.to_i(16) else error! 'invalid directive in header' end end |
#peek ⇒ Object
263 264 265 |
# File 'lib/ramekin/meta.rb', line 263 def peek @peek ||= @elements.shift end |
#sample_path ⇒ Object
53 54 55 56 57 58 |
# File 'lib/ramekin/meta.rb', line 53 def sample_path p = sample_path_unsafe return nil if p.nil? p.gsub(/[\\\/.]/, '_').gsub(/\s+/m, ' ') end |
#sample_path_unsafe ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ramekin/meta.rb', line 25 def sample_path_unsafe return @sample_path if @sample_path game = @game&.value title = @title&.value = &.value game = nil if game && game.upcase == "N/A" return "#{game} - #{title}" if game && title return "#{author} - #{title}" if && title return title if title nil end |
#t_value ⇒ Object
87 88 89 |
# File 'lib/ramekin/meta.rb', line 87 def t_value t_with_divisor[0] end |
#t_with_divisor ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ramekin/meta.rb', line 95 def t_with_divisor return [50, 1] if @tempo.nil? @t_with_divisor ||= begin t = case @tempo.type when :t then @tempo.value when :bpm then (@tempo.value.to_f * 8192 / 20025.0) end t = t.to_f divisor = 1 while t > 60 t /= 2 divisor *= 2 end [t.round, divisor] end end |