synthlog.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env python3
  2. """Reads a LYNX Test Synth log (tools/testplugin/lynx_test_synth_vst3.cpp) and reports, per plugin instance:
  3. - play head: process calls while playing whose position is not where the previous block ended (repeats / jumps)
  4. - notes: note ons, note offs, keys left sounding, note ons of a key that already sounds (a doubled voice), and the
  5. shortest / longest note in samples (counted from the blocks, independent of the reported position)
  6. usage: synthlog.py <log> [bpm] [rate]"""
  7. import sys, collections
  8. def main():
  9. path = sys.argv[1]
  10. bpm = float(sys.argv[2]) if len(sys.argv) > 2 else 128.0
  11. rate = float(sys.argv[3]) if len(sys.argv) > 3 else 44100.0
  12. qps = bpm / 60.0 / rate # quarters per sample
  13. inst = collections.defaultdict(lambda: dict(prev=None, samples=0, blocks=0, playing_blocks=0, repeats=0, jumps=0,
  14. worst=0.0, on=0, off=0, sounding=collections.Counter(), doubled=0,
  15. starts={}, lengths=[], modes=collections.Counter(), block_at=0))
  16. for line in open(path, errors='replace'):
  17. parts = line.split()
  18. if not parts or (parts[0].startswith('P') and len(parts) < 6) or (parts[0].startswith('E') and len(parts) < 4):
  19. continue # (a line still being written)
  20. tag = parts[0]
  21. if tag.startswith('P'):
  22. d = inst[tag[1:]]
  23. ppq, n, playing, mode = float(parts[1]), int(parts[2]), parts[3] == '1', parts[4]
  24. d['blocks'] += 1
  25. d['block_at'] = d['samples']
  26. if playing:
  27. d['playing_blocks'] += 1
  28. d['modes'][mode] += 1
  29. prev = d['prev']
  30. if prev is not None and prev[2]:
  31. expected = prev[0] + prev[1] * qps
  32. err = abs(ppq - expected)
  33. if err > 1e-6:
  34. if abs(ppq - prev[0]) < 1e-9 and prev[1] > 0:
  35. d['repeats'] += 1
  36. elif err > 0.25:
  37. d['jumps'] += 1
  38. else:
  39. d['worst'] = max(d['worst'], err)
  40. d['prev'] = (ppq, n, playing)
  41. d['samples'] += n
  42. elif tag.startswith('E'):
  43. d = inst[tag[1:]]
  44. kind, key, off = parts[1], int(parts[2]), int(parts[3])
  45. at = d['block_at'] + off
  46. if kind == 'on':
  47. d['on'] += 1
  48. if d['sounding'][key] > 0:
  49. d['doubled'] += 1
  50. d['sounding'][key] += 1
  51. d['starts'].setdefault(key, []).append(at)
  52. else:
  53. d['off'] += 1
  54. if d['sounding'][key] > 0:
  55. d['sounding'][key] -= 1
  56. if d['starts'].get(key):
  57. d['lengths'].append(at - d['starts'][key].pop(0))
  58. for k, d in sorted(inst.items()):
  59. left = sum(v for v in d['sounding'].values() if v > 0)
  60. lengths = sorted(d['lengths'])
  61. print(f"instance {k}: blocks {d['blocks']} (playing {d['playing_blocks']}, modes {dict(d['modes'])}); "
  62. f"play head: {d['repeats']} blocks repeat the previous position, {d['jumps']} jumps, worst drift {d['worst']:.2e} q")
  63. print(f" notes: {d['on']} on, {d['off']} off, {d['doubled']} doubled (on over a sounding key), {left} left sounding; "
  64. f"length min {lengths[0] if lengths else '-'} max {lengths[-1] if lengths else '-'} samples, "
  65. f"{len(lengths)} measured")
  66. if lengths:
  67. hist = collections.Counter(lengths)
  68. print(" most common lengths:", hist.most_common(6))
  69. main()