示例#1
0
def CreateMidifile(
    input_file_name,
    data,
    output_file,
    options):
  size = len(data)
  page_size = options.page_size
  delay = options.delay
  _, input_file_name = os.path.split(input_file_name)
  comments = [
      'Warning: contains OS data!',
      'Created from %(input_file_name)s' % locals(),
      'Size: %(size)d' % locals(),
      'Page size: %(page_size)d' % locals(),
      'Delay: %(delay)d ms' % locals()]
  m = midifile.Writer()
  if options.write_comments:
    for comment in comments:
      m.AddTrack().AddEvent(0, midifile.TextEvent(comment))
  t = m.AddTrack()
  t.AddEvent(0, midifile.TempoEvent(120.0))
  page_size *= 2  # Converts from words to bytes
  # The first SysEx block must not start at 0! Sequencers like Logic play the
  # first SysEx block everytime stop/play is pressed.
  time = 1
  syx_data = []
  for i in xrange(0, size, page_size):
    block = ''.join(map(chr, data[i:i+page_size]))
    padding = page_size - len(block)
    block += '\x00' * padding
    mfr_id = options.manufacturer_id if not \
        options.force_obsolete_manufacturer_id else '\x00\x21\x02'
    event = midifile.SysExEvent(
        mfr_id,
        struct.pack('>h', options.device_id),
        options.update_command + midifile.Nibblize(block))
    t.AddEvent(time, event)
    syx_data.append(event.raw_message)
    # ms -> s -> beats -> ticks
    time += int(delay / 1000.0 / 0.5 * 96)
  event = midifile.SysExEvent(
      mfr_id,
      struct.pack('>h', options.device_id),
      options.reset_command)
  t.AddEvent(time, event)
  syx_data.append(event.raw_message)
  
  f = file(output_file, 'wb')
  if options.syx:
    f.write(''.join(syx_data))
  else:
    m.Write(f, format=1)
  f.close()
def CreateMidifile(input_file_name, data, output_file, options):
    size = len(data)
    _, input_file_name = os.path.split(input_file_name)
    comments = [
        'Contains wavetable data for Shruthi-1',
        'Created from %(input_file_name)s' % locals(),
        'Size: %(size)d' % locals()
    ]
    m = midifile.Writer()
    if options.write_comments:
        for comment in comments:
            m.AddTrack().AddEvent(0, midifile.TextEvent(comment))
    t = m.AddTrack()
    t.AddEvent(0, midifile.TempoEvent(120.0))
    event = midifile.SysExEvent(
        options.manufacturer_id, options.device_id,
        options.update_command + midifile.Nibblize(data))
    t.AddEvent(1, event)
    f = file(output_file, 'wb')
    if options.syx:
        f.write(''.join(event.raw_message))
    else:
        m.Write(f, format=1)
    f.close()