-1

I have a list with barline ticks and midi notes that can overlap the barlines. So I made a list of 'barlineticks':

barlinepos = [0, 768.0, 1536.0, 2304.0, 3072.0, 3840.0, 4608.0, 5376.0, 6144.0, 6912.0, 0, 576.0, 1152.0, 1728.0, 2304.0, 2880.0, 3456.0, 4032.0, 4608.0, 5184.0, 5760.0, 6336.0, 6912.0, 7488.0]

And a MidiFile:

{'type': 'time_signature', 'numerator': 4, 'denominator': 4, 'time': 0, 'duration': 768, 'ID': 0}
{'type': 'set_tempo', 'tempo': 500000, 'time': 0, 'ID': 1}
{'type': 'track_name', 'name': 'Tempo Track', 'time': 0, 'ID': 2}
{'type': 'track_name', 'name': 'New Instrument', 'time': 0, 'ID': 3}
{'type': 'note_on', 'time': 0, 'channel': 0, 'note': 48, 'velocity': 100, 'ID': 4, 'duration': 956}
{'type': 'time_signature', 'numerator': 3, 'denominator': 4, 'time': 768, 'duration': 6911, 'ID': 5}
{'type': 'note_on', 'time': 768, 'channel': 0, 'note': 46, 'velocity': 100, 'ID': 6, 'duration': 575}
{'type': 'note_off', 'time': 956, 'channel': 0, 'note': 48, 'velocity': 0, 'ID': 7}
{'type': 'note_off', 'time': 1343, 'channel': 0, 'note': 46, 'velocity': 0, 'ID': 8}
{'type': 'end_of_track', 'time': 7679, 'ID': 9}

And I want to check if the midi note is overlapping a barline. Every note_on message has a 'time' and a 'duration' value. I have to check if one of the barlineticks(in the list) is inside the range of the note('time' and 'duration'). I tried:

if barlinepos in range(0, 956):
    print(True)

Of course this doesn't work because barlinepos is a list. How can I check if one of the values in the list results in True?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • Does this answer your question? [How to find whether a number belongs to a particular range in Python?](https://stackoverflow.com/questions/618093/how-to-find-whether-a-number-belongs-to-a-particular-range-in-python) – mkrieger1 Oct 20 '20 at 18:35
  • Or https://stackoverflow.com/questions/1342601/pythonic-way-of-checking-if-a-condition-holds-for-any-element-of-a-list – mkrieger1 Oct 20 '20 at 18:36
  • Does this answer your question? [Pythonic way of checking if a condition holds for any element of a list](https://stackoverflow.com/questions/1342601/pythonic-way-of-checking-if-a-condition-holds-for-any-element-of-a-list) – Random Davis Oct 20 '20 at 18:37
  • You can iterate over the list, and use `min()` and `max()` to find the extent of the range you wish to check – ti7 Oct 20 '20 at 18:39
  • Thank you for the suggestions. I've looked to it but they are a little different because the search value is not a list. – Philip Bergwerf Oct 21 '20 at 05:42

1 Answers1

1

Simple iteration to solve the requirement:

for i in midifile:
    start, end = i["time"], i["time"]+i["duration"]
    for j in barlinepos:
        if j >= start and j<= end:
            print(True)
            break
    print(False)
Abhinav Mathur
  • 7,791
  • 3
  • 10
  • 24