Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Py game talk

293 views

Published on

  • Be the first to comment

  • Be the first to like this

Py game talk

  1. 1. Embedded Real Time Audio Scripting Or: Python Abuse
  2. 2. Scripting in Music <ul><li>Respond to MIDI Events (via callback) </li></ul><ul><li>Audio Rate VS Control Rate </li></ul><ul><li>Control Rate Processing IN Audio Rate </li></ul><ul><li>Callback Example: </li></ul><ul><ul><li>def onMidiMessage(args): </li></ul></ul><ul><ul><li>m = args[‘midi’] </li></ul></ul><ul><ul><li>print m.getNoteNumber() </li></ul></ul>
  3. 3. Practical Uses <ul><li>Performance Effects </li></ul><ul><ul><li>Chord Builder </li></ul></ul><ul><ul><li>Delay (Echo) </li></ul></ul><ul><ul><li>Arpeggiator </li></ul></ul><ul><li>Reproducing Real Instruments </li></ul><ul><ul><li>Repetition </li></ul></ul><ul><ul><li>Overtones (f.ex. Piano) </li></ul></ul><ul><ul><li>Portamento </li></ul></ul>
  4. 4. Realtime Concepts <ul><li>No blocking allowed! </li></ul><ul><li>But what is blocking? </li></ul><ul><li>“ Realtime safe” message queues </li></ul><ul><li>OK & Not OK code blocks </li></ul><ul><ul><li>OK: basic logic, simple API </li></ul></ul><ul><ul><li>NOT OK: time.wait(), disk IO, recursion </li></ul></ul><ul><li>Avoid bottlenecks with Async processing </li></ul>
  5. 5. “ Safe” For Audio Work <ul><li>Conceptual Requirements </li></ul><ul><ul><li>Perfect theoretical reliability </li></ul></ul><ul><ul><li>Absolutely no blocking </li></ul></ul><ul><ul><li>High performance caching </li></ul></ul><ul><ul><li>Blah blah blah </li></ul></ul><ul><li>Actual Requirements </li></ul><ul><ul><li>Audio just needs to sound good </li></ul></ul><ul><ul><li>Video just needs to look good </li></ul></ul>
  6. 6. “Safe” For Audio Work <ul><li>“ Python is not safe for realtime work” </li></ul><ul><ul><li>Wait, what?!? </li></ul></ul><ul><ul><li>A common phrase, but what does it mean? </li></ul></ul><ul><ul><li>Calls to malloc/free are “slow” </li></ul></ul><ul><ul><li>Global Interpreter Lock effectively disables true multithreading </li></ul></ul><ul><ul><li>But a fast malloc / free combination helps </li></ul></ul><ul><ul><li>A single script thread or process doesn’t need the GIL, so it relies soley on language speed </li></ul></ul>
  7. 7. “Safe” For Audio Work <ul><li>Empirical Evidence (drum roll…) </li></ul><ul><ul><li>Python is fast! </li></ul></ul><ul><ul><li>Shark Profile (PyEval_EvalFrame: 0.4%) </li></ul></ul><ul><ul><li>The GIL SUCKS! (only at low latencies) </li></ul></ul><ul><ul><li>Troubleshooting: </li></ul></ul><ul><ul><ul><li>Comment out code </li></ul></ul></ul><ul><ul><ul><li>Comment out API calls </li></ul></ul></ul><ul><ul><ul><li>Disable scripting all together </li></ul></ul></ul>
  8. 8. App / Engine Separation <ul><li>Basic audio app: (2 Threads) </li></ul><ul><ul><li>Thread 1: Application GUI </li></ul></ul><ul><ul><li>Thread 2: Audio Engine </li></ul></ul><ul><li>Advanced mixing app (n Threads): </li></ul><ul><ul><li>Thread 1: Application GUI </li></ul></ul><ul><ul><li>Thread 2: Audio Engine (Track 1) </li></ul></ul><ul><ul><li>Thread n: Audio Engine (Track n) </li></ul></ul>
  9. 9. Audio or Scripting Thread? <ul><li>Sync VS Async Processing </li></ul><ul><ul><li>Sync: Python IN audio thread </li></ul></ul><ul><ul><li>Async: Python in own thread </li></ul></ul><ul><ul><li>Async processing opens possibilities for easy refactoring for proc migration </li></ul></ul><ul><li>Engine “performance paranoia” </li></ul><ul><ul><li>Audio code is hard to debug </li></ul></ul><ul><ul><li>Avoid blame from managers who love freaking out </li></ul></ul>
  10. 10. Audio or Scripting Thread? <ul><li>Solution: Moving to one new thread </li></ul><ul><ul><li>Move burden of poor performance to scripting accuracy, not audio quality </li></ul></ul><ul><ul><li>Avoids “performance paranoia” </li></ul></ul><ul><li>Solution: Moving to many new processes </li></ul><ul><ul><li>One (thin) scripting daemon per audio thread, with lock-step processing </li></ul></ul><ul><ul><li>Great idea, difficult implementation </li></ul></ul><ul><ul><li>Burdened by IPC overhead, complex proc mgmt </li></ul></ul><ul><ul><li>Google Chrome, AJAX, HTML5 </li></ul></ul>
  11. 11. Multiprocessing Caveats <ul><li>Increased Performance means: </li></ul><ul><ul><li>Decreased Latency </li></ul></ul><ul><ul><li>Code speed is key </li></ul></ul><ul><ul><li>GIL competition comes back to haunt you </li></ul></ul>

×