Skip to content

Commit

Permalink
First draft
Browse files Browse the repository at this point in the history
  • Loading branch information
Fukurokudzu committed Feb 23, 2023
1 parent 8edad88 commit 71a9b43
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
Binary file added examples/ex2.mid
Binary file not shown.
45 changes: 45 additions & 0 deletions lib/midilib/sequence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,51 @@ def beats_per_minute
event = @tracks.first.events.detect { |e| e.is_a?(MIDI::Tempo) }
event ? Tempo.mpq_to_bpm(event.tempo) : DEFAULT_TEMPO
end

def get_tempo_parts
tempo_parts = {}
Array(@tracks).each do |track|
track.events.map do |e|
tempo_parts[e.time_from_start] = Tempo.mpq_to_bpm(e.tempo) if e.is_a?(MIDI::Tempo)
end
end
tempo_parts
end

def avg_beats_per_minute
return DEFAULT_TEMPO if @tracks.nil? || @tracks.empty?

bpm_min = tempo_parts.min
bpm_max = tempo_parts.max
tempo_events ? Tempo.mpq_to_bpm(event.tempo) : DEFAULT_TEMPO
# parts_lenght(tempo_parts.keys, self.get_measures.last.end)
end

def beats_per_minute_now

end

# def avg_beats_per_minute
# return DEFAULT_TEMPO if @tracks.nil? || @tracks.empty?
# tempo_parts = {}
# Array(@tracks).each do |track|
# track.events.map do |e|
# tempo_parts[e.time_from_start] = Tempo.mpq_to_bpm(e.tempo) if e.is_a?(MIDI::Tempo)
# end
# end
# # tempo_events ? Tempo.mpq_to_bpm(event.tempo) : DEFAULT_TEMPO
# # parts_lenght(tempo_parts.keys, self.get_measures.last.end)
# end

# def parts_lenght(start_points, track_ends)
# parts_lenght = []
# start_points.each_with_index do |start_point, i|
# start_points[i+1].nil? ? part_end = track_ends : part_end = start_points[i+1]
# parts_lenght << part_end - start_point
# end
# parts_lenght
# end

alias bpm beats_per_minute
alias tempo beats_per_minute

Expand Down
19 changes: 19 additions & 0 deletions testing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require_relative('lib/midilib')

# Create a new, empty sequence.
seq = MIDI::Sequence.new()

# Read the contents of a MIDI file into the sequence.
File.open('examples/ex2.mid', 'rb') { | file |
seq.read(file) { | track, num_tracks, i |
Array(track).each do |event|
# if MIDI::Tempo === event
# p event.data
# end

end

}
seq.avg_beats_per_minute
# p MIDI::META_TRACK_END
}

0 comments on commit 71a9b43

Please sign in to comment.