mkarduino.py 1.1 KB

1234567891011121314151617181920212223242526272829
  1. #IV to arduino array computer
  2. import xml.etree.ElementTree as ET
  3. import sys
  4. iv = ET.parse(sys.argv[1]).getroot()
  5. streams = iv.findall('./streams/stream[@type="ns"]')
  6. if len(streams) > 3:
  7. print 'WARNING: Too many streams'
  8. for i in xrange(min(3, len(streams))):
  9. stream = streams[i]
  10. notes = stream.findall('note')
  11. # First, the header
  12. sys.stdout.write('const uint16_t track%d[] PROGMEM = {\n'%(i,))
  13. # For the first note, write out the delay needed to get there
  14. if notes[0].get('time') > 0:
  15. sys.stdout.write('\t%d, 0,\n'%(int(float(notes[0].get('time'))*1000),))
  16. for idx, note in enumerate(notes):
  17. sys.stdout.write('\t%d, FREQ(%d),\n'%(int(float(note.get('dur'))*1000), int(440.0 * 2**((float(note.get('pitch'))-69)/12.0))))
  18. if idx < len(notes)-1 and float(note.get('time'))+float(note.get('dur')) < float(notes[idx+1].get('time')):
  19. sys.stdout.write('\t%d, 0,\n'%(int(1000*(float(notes[idx+1].get('time')) - (float(note.get('time')) + float(note.get('dur'))))),))
  20. # Finish up the stream
  21. sys.stdout.write('};\n\n')