Refactor audio out of code, better resource management

Fixes #739 and fixes #777