Unfortunately, Mac turns off the optical link when there's no sound for 10 seconds or so. The obvious kludgey solution is to play continously a silence audio stream.
The program below uses CoreAudio to do so. It eats 6% CPU, but nothing is perfect :)
#!/usr/bin/env python
import thread
import coreaudio # http://wiki.python.org/moin/MacPython/CoreAudio
import time
class Ctrl(object):
def callback(self):
# called in another thread context!
return self.silence
def __init__(self):
audio_id, sampling_rate, chunksize = coreaudio.initAudio()
self.silence = [0.0 for i in range(0, chunksize)]
coreaudio.installAudioCallback(audio_id, self)
def stop(self):
coreaudio.stopAudio(self)
# Have to initialize the threading mechanisms in order for PyGIL_Ensure to work
thread.start_new_thread(lambda: None, ())
try:
ctrl = Ctrl()
time.sleep(999999)
except KeyboardInterrupt:
ctrl.stop()
time.sleep(0.1)
This is actually a nice example of how to use CoreAudio in Python, and it was based in a project of mine, PaiMorse. The start_new_thread is there only to make the interpreter thread-aware, because CoreAudio creates threads unbeknownst to Python.