collab/PineapplePad
git clone https://git.julia.blue/collab/PineapplePad
118022e
master
1import json 2import sys 3 4# to run: python3 extract.py <infile>.json <name> 5 6infile = sys .argv [1 ] 7name = sys .argv [2 ] 8track = 1 9 10f = open (name + '.h' ,'w' ) 11f .write ('//' + name + '\n' ) 12f .write ("#include <stdint.h>\n\n" ) 13f .write ("const uint32_t pitch[] = {" ) 14 15music = json .load (open (infile )) 16divisors = [] 17durations = [] 18velocities = [] 19time = 0 20restVelocity = '0' 21restDuration = '100' 22restdivisor = '0' 23for note in music ["tracks" ][track ]["notes" ]: 24noteTime = note ["time" ] 25if (time < noteTime ): 26divisors .append (restdivisor ) 27velocities .append (restVelocity ) 28durations .append (str (int (100 * float ("{0:.2f}" .format (noteTime - time ))))) 29time = noteTime 30velocity = str (int (100 * float ("{0:.2f}" .format (note ['velocity' ])))) 31duration = str (int (100 * float ("{0:.2f}" .format (note ['duration' ])))) 32frequency = (440 / 32 )* (pow (2 , ((note ["midi" ]- 9 )/ 12 ))) 33divisor = int (80e6 / (64 * frequency )) 34divisors .append (str (divisor )) 35durations .append (duration ) 36velocities .append (velocity ) 37 38divisors .append ('0' ) 39durations .append ('1' ) 40velocities .append ('0' ) 41divisors .append ('0' ) 42durations .append ('1' ) 43velocities .append ('0' ) 44 45# print(divisors) 46f .write (',' .join (divisors )) 47f .write ('};\n\nconst uint16_t dur[] = {' ) 48f .write (',' .join (durations )) 49f .write ('};\n\nconst uint8_t velocity[] = {' ) 50f .write (',' .join (velocities )) 51f .write ('};' ) 52 53f .close ()