Module: Webmidi::SMF::Reader

Defined in:
lib/webmidi/smf/reader.rb

Defined Under Namespace

Classes: StringStream

Class Method Summary collapse

Class Method Details

.parse(binary, skip_unknown_chunks: true, stop_at_end_of_track: true) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/webmidi/smf/reader.rb', line 17

def parse(binary, skip_unknown_chunks: true, stop_at_end_of_track: true)
  binary = binary.b if binary.encoding != Encoding::ASCII_8BIT
  stream = StringStream.new(binary)

  format, num_tracks, ppqn = read_header(stream)
  sequence = Sequence.new(format: format, ppqn: ppqn)

  num_tracks.times do
    track = read_track(stream, skip_unknown_chunks: skip_unknown_chunks,
      stop_at_end_of_track: stop_at_end_of_track)
    sequence.add_track(track)
  end

  sequence
end

.read(path_or_io, **options) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/webmidi/smf/reader.rb', line 8

def read(path_or_io, **options)
  data = if path_or_io.respond_to?(:read)
    path_or_io.read
  else
    File.binread(path_or_io)
  end
  parse(data, **options)
end