Class: Webmidi::SMF::Reader::StringStream
- Inherits:
-
Object
- Object
- Webmidi::SMF::Reader::StringStream
- Defined in:
- lib/webmidi/smf/reader.rb
Instance Attribute Summary collapse
-
#position ⇒ Object
readonly
Returns the value of attribute position.
Instance Method Summary collapse
-
#initialize(data) ⇒ StringStream
constructor
A new instance of StringStream.
- #peek_byte ⇒ Object
- #read_byte ⇒ Object
- #read_bytes(n) ⇒ Object
- #read_raw_bytes(n) ⇒ Object
- #read_uint16 ⇒ Object
- #read_uint32 ⇒ Object
- #read_vlq ⇒ Object
- #skip(n) ⇒ Object
- #with_limit(limit) ⇒ Object
Constructor Details
#initialize(data) ⇒ StringStream
Returns a new instance of StringStream.
150 151 152 153 |
# File 'lib/webmidi/smf/reader.rb', line 150 def initialize(data) @data = data @position = 0 end |
Instance Attribute Details
#position ⇒ Object (readonly)
Returns the value of attribute position.
148 149 150 |
# File 'lib/webmidi/smf/reader.rb', line 148 def position @position end |
Instance Method Details
#peek_byte ⇒ Object
176 177 178 179 |
# File 'lib/webmidi/smf/reader.rb', line 176 def peek_byte ensure_available!(1) @data.getbyte(@position) end |
#read_byte ⇒ Object
169 170 171 172 173 174 |
# File 'lib/webmidi/smf/reader.rb', line 169 def read_byte ensure_available!(1) byte = @data.getbyte(@position) @position += 1 byte end |
#read_bytes(n) ⇒ Object
155 156 157 158 159 160 |
# File 'lib/webmidi/smf/reader.rb', line 155 def read_bytes(n) ensure_available!(n) result = @data[@position, n] @position += n result end |
#read_raw_bytes(n) ⇒ Object
162 163 164 165 166 167 |
# File 'lib/webmidi/smf/reader.rb', line 162 def read_raw_bytes(n) ensure_available!(n) result = @data[@position, n].bytes @position += n result end |
#read_uint16 ⇒ Object
194 195 196 197 198 199 |
# File 'lib/webmidi/smf/reader.rb', line 194 def read_uint16 ensure_available!(2) val = (@data.getbyte(@position) << 8) | @data.getbyte(@position + 1) @position += 2 val end |
#read_uint32 ⇒ Object
201 202 203 204 205 206 207 208 209 |
# File 'lib/webmidi/smf/reader.rb', line 201 def read_uint32 ensure_available!(4) val = (@data.getbyte(@position) << 24) | (@data.getbyte(@position + 1) << 16) | (@data.getbyte(@position + 2) << 8) | @data.getbyte(@position + 3) @position += 4 val end |
#read_vlq ⇒ Object
211 212 213 214 215 216 217 218 219 220 |
# File 'lib/webmidi/smf/reader.rb', line 211 def read_vlq value = 0 4.times do byte = read_byte value = (value << 7) | (byte & 0x7F) return value unless (byte & 0x80) != 0 end raise InvalidSMFError, "VLQ exceeds 4 bytes" end |
#skip(n) ⇒ Object
181 182 183 184 |
# File 'lib/webmidi/smf/reader.rb', line 181 def skip(n) ensure_available!(n) @position += n end |
#with_limit(limit) ⇒ Object
186 187 188 189 190 191 192 |
# File 'lib/webmidi/smf/reader.rb', line 186 def with_limit(limit) @limits ||= [] @limits.push(limit) yield ensure @limits.pop end |