Skip to content

Commit

Permalink
Raise when requiring IOIs with too few notes
Browse files Browse the repository at this point in the history
Resolves #36.

When estimating a single global tempo, there needs to be at least two
notes so that at least one inner-onset-interval can be computed.  This
raises a ValueError whenever estimate_tempo is called when there are
fewer than two notes.  Also, in order to estimate the beat start, there
needs to be at least one note, so we raise a ValueError there too.  We
don't need to raise a ValueError in estimate_tempo because it just
returns empty np.ndarrays.
  • Loading branch information
craffel committed Aug 3, 2016
1 parent 0039778 commit b733357
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pretty_midi/pretty_midi.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,11 @@ def estimate_tempo(self):
Estimated tempo, in bpm
"""
return self.estimate_tempi()[0][0]
tempi = self.estimate_tempi()[0]
if tempi.size == 0:
raise ValueError("Can't provide a global tempo estimate when there"
" are fewer than two notes.")
return tempi[0][0]

def get_beats(self, start_time=0.):
"""Return a list of beat locations, according to MIDI tempo changes.
Expand Down Expand Up @@ -592,6 +596,9 @@ def estimate_beat_start(self, candidates=10, tolerance=.025):
"""
# Get a sorted list of all notes from all instruments
note_list = [n for i in self.instruments for n in i.notes]
if not note_list:
raise ValueError(
"Can't estimate beat start when there are no notes.")
note_list.sort(key=lambda note: note.start)
# List of possible beat trackings
beat_candidates = []
Expand Down

0 comments on commit b733357

Please sign in to comment.