Giter Club home page Giter Club logo

Comments (4)

flagstone78 avatar flagstone78 commented on September 24, 2024

This is my work around method:
I parse the file with pattern= midi.read_midifile("/path/to/file"). This gives a list with the note off event.
I do a for statement that goes through the list and adds up the ticks as it goes.
When it gets to a noteOnEvent, it adds the note to a 'activenotelist' and records the current tick with it.
when it gets to a noteOffEvent, it checks the 'activenotelist' for the note and calculates the duration by subtracting the tick that was recorded with the note in the 'activenotelist' from the current tick value.

from python-midi.

tdhsmith avatar tdhsmith commented on September 24, 2024

_I only got the NoteOnEvent, where is the NoteOffEvent?_

Some MIDIfiles (including the provided example mary.mid) might not actually use note off commands to stop notes from playing. Check out the beginning of the track dump for mary.mid:

midi.NoteOnEvent(tick=0, channel=0, data=[64, 72]),
midi.NoteOnEvent(tick=0, channel=0, data=[55, 70]),
midi.NoteOnEvent(tick=231, channel=0, data=[64, 0]),

Notice how note 64 (E4) plays at a velocity of 72, and then two events later it plays at a velocity of 0. Since each scale note can only be playing once (per channel/per track), this overrides the previous value and essentially turns off the note by playing again, but at 0 volume. This is technically a valid way to stop a note's sound, though it's certainly frustrating to anyone who likes clear semantic categories.

_can you show me how to get the start time and the duration of a NoteEvent?_

@flagstone78 described this pretty well already. The crux of the matter is that you'll need to keep track of what tick you're at as you traverse the file, then, if you are interested in a particular note, save its initial tick (this is the start time*). Keep traversing until you hit another NoteOn or NoteOff event with the same pitch and channel as your first one, then subtract the start time from your current tick at this second event to get the duration. Note that I check for both ons and offs because of the issue above (stopping notes by giving them 0 velocity) and because one might consider a new NoteOnEvent as a semantically distinct note from the first, regardless of whether the note was off in between or not.

* though these are all assuming you want ticks. If you want seconds/etc, you'll have to convert; the main readme has some details.

EDIT: just discovered there is a Track.make_ticks_abs() that will convert tick values for you, though there is an open issue #20 so be careful to use it only once.

from python-midi.

greenbeen avatar greenbeen commented on September 24, 2024

Can someone explain how to get the tick and channel values? In Flagstone78's example above, I can complete the "pattern= midi.read_midifile("/path/to/file")" part just fine. I can create a for loop and extract the pitches using event.get_pitch(), but I can't figure out how to access the tick or channel values.
Thanks for you help!

from python-midi.

tdhsmith avatar tdhsmith commented on September 24, 2024

They can be accessed directly using event.tick and event.channel, rather than through a getter method.

(Note that not all event types will not have a channel though.)

from python-midi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.