class_docstr: MIDI File.
  
  Parameters
  ----------
  filename : str
      MIDI file name.
  file_format : int, optional
      MIDI file format (0, 1, 2).
  ticks_per_beat : int, optional
      Resolution (i.e. ticks per quarter note) of the MIDI file.
  unit : str, optional
      Unit of all MIDI messages, can be one of the following:
  
      - 'ticks', 't': use native MIDI ticks as unit,
      - 'seconds', 's': use seconds as unit,
      - 'beats', 'b' : use beats as unit.
  
  timing : str, optional
      Timing of all MIDI messages, can be one of the following:
  
      - 'absolute', 'abs', 'a': use absolute timing.
      - 'relative', 'rel', 'r': use relative timing, i.e. delta to
        previous message.
  
  Examples
  --------
  Create a MIDI file from an array with notes. The format of the note array
  is: 'onset time', 'pitch', 'duration', 'velocity', 'channel'. The last
  column can be omitted, assuming channel 0.
  
  >>> notes = np.array([[0, 50, 1, 60], [0.5, 62, 0.5, 90]])
  >>> m = MIDIFile.from_notes(notes)
  >>> m  # doctest: +ELLIPSIS
  <madmom.io.midi.MIDIFile object at 0x...>
  
  The notes can be accessed as a numpy array in various formats (default is
  seconds):
  
  >>> m.notes
  array([[ 0. , 50. ,  1. , 60. ,  0. ],
         [ 0.5, 62. ,  0.5, 90. ,  0. ]])
  >>> m.unit ='ticks'
  >>> m.notes
  array([[  0.,  50., 960.,  60.,   0.],
         [480.,  62., 480.,  90.,   0.]])
  >>> m.unit = 'seconds'
  >>> m.notes
  array([[ 0. , 50. ,  1. , 60. ,  0. ],
         [ 0.5, 62. ,  0.5, 90. ,  0. ]])
  >>> m.unit = 'beats'
  >>> m.notes
  array([[ 0., 50.,  2., 60.,  0.],
         [ 1., 62.,  1., 90.,  0.]])
  
  >>> m = MIDIFile.from_notes(notes, tempo=60)
  >>> m.notes
  array([[ 0. , 50. ,  1. , 60. ,  0. ],
         [ 0.5, 62. ,  0.5, 90. ,  0. ]])
  >>> m.unit = 'ticks'
  >>> m.notes
  array([[  0.,  50., 480.,  60.,   0.],
         [240.,  62., 240.,  90.,   0.]])
  >>> m.unit = 'beats'
  >>> m.notes
  array([[ 0. , 50. ,  1. , 60. ,  0. ],
         [ 0.5, 62. ,  0.5, 90. ,  0. ]])
  
  >>> m = MIDIFile.from_notes(notes, time_signature=(2, 2))
  >>> m.notes
  array([[ 0. , 50. ,  1. , 60. ,  0. ],
         [ 0.5, 62. ,  0.5, 90. ,  0. ]])
  >>> m.unit = 'ticks'
  >>> m.notes
  array([[   0.,   50., 1920.,   60.,    0.],
         [ 960.,   62.,  960.,   90.,    0.]])
  >>> m.unit = 'beats'
  >>> m.notes
  array([[ 0., 50.,  2., 60.,  0.],
         [ 1., 62.,  1., 90.,  0.]])
  
  >>> m = MIDIFile.from_notes(notes, tempo=60, time_signature=(2, 2))
  >>> m.notes
  array([[ 0. , 50. ,  1. , 60. ,  0. ],
         [ 0.5, 62. ,  0.5, 90. ,  0. ]])
  >>> m.unit = 'ticks'
  >>> m.notes
  array([[  0.,  50., 960.,  60.,   0.],
         [480.,  62., 480.,  90.,   0.]])
  >>> m.unit = 'beats'
  >>> m.notes
  array([[ 0. , 50. ,  1. , 60. ,  0. ],
         [ 0.5, 62. ,  0.5, 90. ,  0. ]])
  
  >>> m = MIDIFile.from_notes(notes, tempo=240, time_signature=(3, 8))
  >>> m.notes
  array([[ 0. , 50. ,  1. , 60. ,  0. ],
         [ 0.5, 62. ,  0.5, 90. ,  0. ]])
  >>> m.unit = 'ticks'
  >>> m.notes
  array([[  0.,  50., 960.,  60.,   0.],
         [480.,  62., 480.,  90.,   0.]])
  >>> m.unit = 'beats'
  >>> m.notes
  array([[ 0., 50.,  4., 60.,  0.],
         [ 2., 62.,  2., 90.,  0.]])
class_docstr_tok: ["MIDI","File",".","Parameters","--","--","--","--","--","filename",":","str","MIDI","file","name",".","file_format",":","int",",","optional","MIDI","file","format","(","0",",","1",",","2",")",".","ticks_per_beat",":","int",",","optional","Resolution","(","i.e",".","ticks","per","quarter","note",")","of","the","MIDI","file",".","unit",":","str",",","optional","Unit","of","all","MIDI","messages",",","can","be","one","of","the","following",":","-","'ticks","'",",","'t","'",":","use","native","MIDI","ticks","as","unit",",","-","'seconds","'",",","'s","'",":","use","seconds","as","unit",",","-","'beats","'",",","'","b","'",":","use","beats","as","unit",".","timing",":","str",",","optional","Timing","of","all","MIDI","messages",",","can","be","one","of","the","following",":","-","'absolute","'",",","'abs","'",",","'","a","'",":","use","absolute","timing",".","-","'relative","'",",","'rel","'",",","'","r","'",":","use","relative","timing",",","i.e",".","delta","to","previous","message",".","Examples","--","--","--","--","Create","a","MIDI","file","from","an","array","with","notes",".","The","format","of","the","note","array","is",":","'onset","time","'",",","'pitch","'",",","'duration","'",",","'velocity","'",",","'channel","'",".","The","last","column","can","be","omitted",",","assuming","channel","0",".",">",">",">","notes","=","np.array","(","[","[","0",",","50",",","1",",","60","]",",","[","0.5",",","62",",","0.5",",","90","]","]",")",">",">",">","m","=","MIDIFile.from_notes","(","notes",")",">",">",">","m","#","doctest",":","+ELLIPSIS","<","madmom.io.midi.MIDIFile","object","at","0x","...",">","The","notes","can","be","accessed","as","a","numpy","array","in","various","formats","(","default","is","seconds",")",":",">",">",">","m.notes","array","(","[","[","0.",",","50.",",","1.",",","60.",",","0",".","]",",","[","0.5",",","62.",",","0.5",",","90.",",","0",".","]","]",")",">",">",">","m.unit","='ticks'",">",">",">","m.notes","array","(","[","[","0.",",","50.",",","960.",",","60.",",","0",".","]",",","[","480.",",","62.",",","480.",",","90.",",","0",".","]","]",")",">",">",">","m.unit","=","'seconds'",">",">",">","m.notes","array","(","[","[","0.",",","50.",",","1.",",","60.",",","0",".","]",",","[","0.5",",","62.",",","0.5",",","90.",",","0",".","]","]",")",">",">",">","m.unit","=","'beats'",">",">",">","m.notes","array","(","[","[","0.",",","50.",",","2.",",","60.",",","0",".","]",",","[","1.",",","62.",",","1.",",","90.",",","0",".","]","]",")",">",">",">","m","=","MIDIFile.from_notes","(","notes",",","tempo=60",")",">",">",">","m.notes","array","(","[","[","0.",",","50.",",","1.",",","60.",",","0",".","]",",","[","0.5",",","62.",",","0.5",",","90.",",","0",".","]","]",")",">",">",">","m.unit","=","'ticks'",">",">",">","m.notes","array","(","[","[","0.",",","50.",",","480.",",","60.",",","0",".","]",",","[","240.",",","62.",",","240.",",","90.",",","0",".","]","]",")",">",">",">","m.unit","=","'beats'",">",">",">","m.notes","array","(","[","[","0.",",","50.",",","1.",",","60.",",","0",".","]",",","[","0.5",",","62.",",","0.5",",","90.",",","0",".","]","]",")",">",">",">","m","=","MIDIFile.from_notes","(","notes",",","time_signature=","(","2",",","2",")",")",">",">",">","m.notes","array","(","[","[","0.",",","50.",",","1.",",","60.",",","0",".","]",",","[","0.5",",","62.",",","0.5",",","90.",",","0",".","]","]",")",">",">",">","m.unit","=","'ticks'",">",">",">","m.notes","array","(","[","[","0.",",","50.",",","1920.",",","60.",",","0",".","]",",","[","960.",",","62.",",","960.",",","90.",",","0",".","]","]",")",">",">",">","m.unit","=","'beats'",">",">",">","m.notes","array","(","[","[","0.",",","50.",",","2.",",","60.",",","0",".","]",",","[","1.",",","62.",",","1.",",","90.",",","0",".","]","]",")",">",">",">","m","=","MIDIFile.from_notes","(","notes",",","tempo=60",",","time_signature=","(","2",",","2",")",")",">",">",">","m.notes","array","(","[","[","0.",",","50.",",","1.",",","60.",",","0",".","]",",","[","0.5",",","62.",",","0.5",",","90.",",","0",".","]","]",")",">",">",">","m.unit","=","'ticks'",">",">",">","m.notes","array","(","[","[","0.",",","50.",",","960.",",","60.",",","0",".","]",",","[","480.",",","62.",",","480.",",","90.",",","0",".","]","]",")",">",">",">","m.unit","=","'beats'",">",">",">","m.notes","array","(","[","[","0.",",","50.",",","1.",",","60.",",","0",".","]",",","[","0.5",",","62.",",","0.5",",","90.",",","0",".","]","]",")",">",">",">","m","=","MIDIFile.from_notes","(","notes",",","tempo=240",",","time_signature=","(","3",",","8",")",")",">",">",">","m.notes","array","(","[","[","0.",",","50.",",","1.",",","60.",",","0",".","]",",","[","0.5",",","62.",",","0.5",",","90.",",","0",".","]","]",")",">",">",">","m.unit","=","'ticks'",">",">",">","m.notes","array","(","[","[","0.",",","50.",",","960.",",","60.",",","0",".","]",",","[","480.",",","62.",",","480.",",","90.",",","0",".","]","]",")",">",">",">","m.unit","=","'beats'",">",">",">","m.notes","array","(","[","[","0.",",","50.",",","4.",",","60.",",","0",".","]",",","[","2.",",","62.",",","2.",",","90.",",","0",".","]","]",")"]
docstr: Create a MIDIFile from the given notes.
  
  Parameters
  ----------
  notes : numpy array
      Array with notes, one per row. The columns are defined as:
      (onset time, pitch, duration, velocity, [channel]).
  unit : str, optional
      Unit of `notes`, can be one of the following:
  
      - 'seconds', 's': use seconds as unit,
      - 'ticks', 't': use native MIDI ticks as unit,
      - 'beats', 'b' : use beats as unit.
  
  tempo : float, optional
      Tempo of the MIDI track, given in bpm or microseconds per quarter
      note. The unit is determined automatically by the value:
  
      - `tempo` <= 1000: bpm
      - `tempo` > 1000: microseconds per quarter note
  
  time_signature : tuple, optional
      Time signature of the track, e.g. (4, 4) for 4/4.
  ticks_per_beat : int, optional
      Resolution (i.e. ticks per quarter note) of the MIDI file.
  
  Returns
  -------
  :class:`MIDIFile` instance
      :class:`MIDIFile` instance with all notes collected in one track.
  
  Notes
  -----
  All note events (including the generated tempo and time signature
  events) are written into a single track (i.e. MIDI file format 0).
docstr_tok: ["Create","a","MIDIFile","from","the","given","notes",".","Parameters","--","--","--","--","--","notes",":","numpy","array","Array","with","notes",",","one","per","row",".","The","columns","are","defined","as",":","(","onset","time",",","pitch",",","duration",",","velocity",",","[","channel","]",")",".","unit",":","str",",","optional","Unit","of","`","notes","`",",","can","be","one","of","the","following",":","-","'seconds","'",",","'s","'",":","use","seconds","as","unit",",","-","'ticks","'",",","'t","'",":","use","native","MIDI","ticks","as","unit",",","-","'beats","'",",","'","b","'",":","use","beats","as","unit",".","tempo",":","float",",","optional","Tempo","of","the","MIDI","track",",","given","in","bpm","or","microseconds","per","quarter","note",".","The","unit","is","determined","automatically","by","the","value",":","-","`","tempo","`","<","=","1000",":","bpm","-","`","tempo","`",">","1000",":","microseconds","per","quarter","note","time_signature",":","tuple",",","optional","Time","signature","of","the","track",",","e.g",".","(","4",",","4",")","for","4\/4",".","ticks_per_beat",":","int",",","optional","Resolution","(","i.e",".","ticks","per","quarter","note",")","of","the","MIDI","file",".","Returns","--","--","--","-",":","class",":","`","MIDIFile","`","instance",":","class",":","`","MIDIFile","`","instance","with","all","notes","collected","in","one","track",".","Notes","--","--","-","All","note","events","(","including","the","generated","tempo","and","time","signature","events",")","are","written","into","a","single","track","(","i.e",".","MIDI","file","format","0",")","."]
code: def from_notes(
      cls,
      notes,
      unit="seconds",
      tempo=DEFAULT_TEMPO,
      time_signature=DEFAULT_TIME_SIGNATURE,
      ticks_per_beat=DEFAULT_TICKS_PER_BEAT,
  ):
      """
      Create a MIDIFile from the given notes.
  
      Parameters
      ----------
      notes : numpy array
          Array with notes, one per row. The columns are defined as:
          (onset time, pitch, duration, velocity, [channel]).
      unit : str, optional
          Unit of `notes`, can be one of the following:
  
          - 'seconds', 's': use seconds as unit,
          - 'ticks', 't': use native MIDI ticks as unit,
          - 'beats', 'b' : use beats as unit.
  
      tempo : float, optional
          Tempo of the MIDI track, given in bpm or microseconds per quarter
          note. The unit is determined automatically by the value:
  
          - `tempo` <= 1000: bpm
          - `tempo` > 1000: microseconds per quarter note
  
      time_signature : tuple, optional
          Time signature of the track, e.g. (4, 4) for 4/4.
      ticks_per_beat : int, optional
          Resolution (i.e. ticks per quarter note) of the MIDI file.
  
      Returns
      -------
      :class:`MIDIFile` instance
          :class:`MIDIFile` instance with all notes collected in one track.
  
      Notes
      -----
      All note events (including the generated tempo and time signature
      events) are written into a single track (i.e. MIDI file format 0).
  
      """
      # create new MIDI file
      midi_file = cls(
          file_format=0,
          ticks_per_beat=ticks_per_beat,
          unit=unit,
          timing="absolute",
      )
      # convert tempo
      if tempo <= 1000:
          # convert from bpm to tempo
          tempo = bpm2tempo(tempo, time_signature)
      else:
          # tempo given in ticks per quarter note
          # i.e. we have to adjust according to the time signature
          tempo = int(tempo * time_signature[1] / 4)
      # create new track and add tempo and time signature information
      track = midi_file.add_track()
      track.append(mido.MetaMessage("set_tempo", tempo=tempo))
      track.append(
          mido.MetaMessage(
              "time_signature",
              numerator=time_signature[0],
              denominator=time_signature[1],
          )
      )
      # create note on/off messages with absolute timing
      messages = []
      for note in notes:
          try:
              onset, pitch, duration, velocity, channel = note
          except ValueError:
              onset, pitch, duration, velocity = note
              channel = 0
          pitch = int(pitch)
          velocity = int(velocity)
          channel = int(channel)
          offset = onset + duration
          # create MIDI messages
          onset = second2tick(onset, ticks_per_beat, tempo)
          note_on = mido.Message(
              "note_on",
              time=onset,
              note=pitch,
              velocity=velocity,
              channel=channel,
          )
          offset = second2tick(offset, ticks_per_beat, tempo)
          note_off = mido.Message(
              "note_off", time=offset, note=pitch, channel=channel
          )
          # append to list
          messages.extend([note_on, note_off])
      # sort them, convert to relative timing and append to track
      messages.sort(key=lambda msg: msg.time)
      messages = mido.midifiles.tracks._to_reltime(messages)
      track.extend(messages)
      # return MIDI file
      return midi_file
  
imports: from __future__ import absolute_import, division, print_function
  import warnings
  import mido
  import numpy
label_desc: Use image node_id 9 for calling the MIDIFile obj's underlying member method code with example usage: obj.from_notes(cls, notes, unit, tempo, time_signature, ticks_per_beat) and returns: midi_file