picadae calibration programs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

convert.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. ##| Copyright: (C) 2019-2020 Kevin Larke <contact AT larke DOT org>
  2. ##| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
  3. import os,json,pickle,csv
  4. from shutil import copyfile
  5. def event_times( eventTimeFn ):
  6. eventL = []
  7. velL = []
  8. with open(eventTimeFn,"r") as f:
  9. rdr = csv.reader(f)
  10. for row in rdr:
  11. if row[0] == 'start':
  12. beginMs = int(row[1])
  13. elif row[0] == 'key_down':
  14. key_downMs = int(row[1]) - beginMs
  15. vel = int(row[3])
  16. elif row[0] == 'key_up':
  17. key_upMs = row[1]
  18. eventL.append( [ key_downMs, key_downMs+1000 ] )
  19. velL.append( vel )
  20. return eventL,velL
  21. def pulse_lengths( pulseLenFn, velL ):
  22. with open(pulseLenFn,'rb') as f:
  23. d = pickle.load(f)
  24. msL = d['msL']
  25. # note: first posn in table is a multiplier
  26. msL = [ msL[i]*msL[0] for i in range(1,len(msL))]
  27. usL = [ msL[vel-1] for vel in velL ]
  28. return usL
  29. def convert( inDir, outDir ):
  30. if not os.path.isdir(outDir):
  31. os.mkdir(outDir)
  32. for dirStr in os.listdir(inDir):
  33. idir = os.path.join( inDir, dirStr )
  34. if os.path.isdir(idir):
  35. id = 0
  36. while True:
  37. eventTimeFn = os.path.join( idir, "labels_%i.csv" % (id) )
  38. if not os.path.isfile( eventTimeFn ):
  39. break
  40. eventTimeL,velL = event_times(eventTimeFn)
  41. pulseTimeFn = os.path.join( idir, "table_%i.pickle" % (id))
  42. pulseUsL = pulse_lengths( pulseTimeFn, velL )
  43. pitch = idir.split("/")[-1]
  44. if not pitch.isdigit():
  45. break
  46. d = {
  47. "pulseUsL":pulseUsL,
  48. "pitchL":[ pitch ],
  49. "noteDurMs":1000,
  50. "pauseDurMs":0,
  51. "holdDutyPct":50,
  52. "eventTimeL":eventTimeL,
  53. "beginMs":0
  54. }
  55. odir = os.path.join( outDir, pitch )
  56. if not os.path.isdir(odir):
  57. os.mkdir(odir)
  58. odir = os.path.join( odir, "%i" % (id) )
  59. if not os.path.isdir(odir):
  60. os.mkdir(odir)
  61. with open(os.path.join( odir, "seq.json" ),"w") as f:
  62. f.write(json.dumps( d ))
  63. copyfile( os.path.join(idir,"audio_%i.wav" % (id)), os.path.join(odir,"audio.wav"))
  64. id += 1
  65. if __name__ == "__main__":
  66. inDir = "/home/kevin/temp/picadae_ac_2/full_map"
  67. outDir = "/home/kevin/temp/p_ac_3_cvt/full_map"
  68. #inDir = "/home/kevin/temp/picadae_ac_2/week_0"
  69. #outDir = "/home/kevin/temp/p_ac_3_cvt/week_0"
  70. convert( inDir, outDir )