diff options
author | James <olpc@localhost.localdomain> | 2007-02-20 00:14:18 (GMT) |
---|---|---|
committer | James <olpc@localhost.localdomain> | 2007-02-20 00:14:18 (GMT) |
commit | f172a86bf7a1d0328dcda10ea06761fe4cb70522 (patch) | |
tree | a0c46c94a04890570dd4185eef67ec55a36b97c9 /Util/CSoundClient.py | |
parent | 5a183c571cb4d7074d994a82c0f889efde730411 (diff) |
sound feedback on note edit, global singleton csoundclient
Diffstat (limited to 'Util/CSoundClient.py')
-rw-r--r-- | Util/CSoundClient.py | 197 |
1 files changed, 32 insertions, 165 deletions
diff --git a/Util/CSoundClient.py b/Util/CSoundClient.py index a1242a3..4d3bfef 100644 --- a/Util/CSoundClient.py +++ b/Util/CSoundClient.py @@ -10,166 +10,21 @@ import array import Config from Generation.GenerationConstants import GenerationConstants +from Util.Clooper.sclient import * +from Util import NoteDB -#---------------------------------------------------------------------- -# A CSound client used to send messages to the CSound server -# CSoundClient is a singleton -#---------------------------------------------------------------------- -CSoundClient = None -class CSoundClientBase: - def setMasterVolume(self, volume): - self.sendText("csound.SetChannel('masterVolume', %f)\n" % volume) - - def micRecording( self, table ): - mess = Config.MIC_RECORDING_COMMAND % table - self.sendText( mess ) - - def load_mic_instrument( self, inst ): - fileName = Config.PREF_DIR + '/' + inst - instrumentId = Config.INSTRUMENT_TABLE_OFFSET + int(fileName[-1]) + 6 - mess = Config.LOAD_INSTRUMENT_COMMAND % ( instrumentId, fileName ) - self.sendText( mess ) - - def load_instruments( self ): - for instrumentSoundFile in Config.INSTRUMENTS.keys(): - if instrumentSoundFile[0:3] == 'mic' or instrumentSoundFile[0:3] == 'lab': - fileName = Config.PREF_DIR + '/' + instrumentSoundFile - else: - fileName = Config.SOUNDS_DIR + "/" + instrumentSoundFile - instrumentId = Config.INSTRUMENT_TABLE_OFFSET + Config.INSTRUMENTS[ instrumentSoundFile ].instrumentId - mess = Config.LOAD_INSTRUMENT_COMMAND % ( instrumentId, fileName ) - self.sendText( mess ) - - def startTime(self): - raise 'dont do this anymore' - self.sendText("perf.InputMessage('i 5999 0.0 60000000')") - # if any other message arrives to csound at the same time as this one, - # then the global variables will not be set up right in the orcestra - # - # NB: match this to the constant in the instrument 5777 of the csound orcestra - time.sleep(0.1) - - def sendText(self, txt): - raise 'noImpl' - - def connect(self, flag): - raise 'noImpl' - - def destroy(self): - pass - - - -class CSoundClientSocket( CSoundClientBase ): - def __init__( self, serverAddress, serverPort, clientId ): - self.clientId = clientId - self.serverInfo = ( serverAddress, serverPort ) - self.connected = False - - def sendText( self, text ): - #print "Sending to CSound server: %s" % text - try: - self.socket.send( text ) - except socket.error: - if Config.SERVER_REQUIRED : - print 'ERROR: no CSound server. Ignoring message: %s' % text - - def connect( self, init = True ): - if init : - n = Config.INIT_ATTEMPTS - self.socket = socket.socket() - self.connected = False - while n > 0 and not self.connected: - try: - self.socket.connect( self.serverInfo ) - self.connected = True - self.load_instruments() - except socket.error: - if Config.SERVER_REQUIRED : - print 'ERROR: no CSound server. Ignoring connection request.' - time.sleep(Config.INIT_DELAY) - n = n - 1 - else : #un-init - self.sendText( Config.UNLOAD_TABLES_COMMAND ) - del self.socket - -class CSoundClientPerf( CSoundClientBase ): +class _CSoundClientPlugin: def __init__(self, orc): - self.orc = orc + sc_initialize(orc) self.on = False - self.csound = csnd.Csound() - def connect( self, init = True ): - if init: - if self.on : return - self.on = True - self.perf = csnd.CsoundPerformanceThread(self.csound) - self.csound.Compile( self.orc ) - self.perf.Play() - self.load_instruments() - print 'CSoundClient = True' - else: - if not self.on : return - self.on = False - #self.csound.SetChannel('udprecv.0.on', 0) - #print Config.UNLOAD_TABLES_COMMAND - self.sendText( Config.UNLOAD_TABLES_COMMAND ) - #print 'PERF STOP' - self.perf.Stop() - #print 'SLEEP' - #time.sleep(1) - #print 'JOIN' - #time.sleep(1) - rval = self.perf.Join() - #print 'Join returned ', rval - #del self.perf - #time.sleep(1) - #print 'STOP' - #self.csound.Stop() - #print 'RESET' - self.csound.Reset() - print 'CSoundClient = False' - #careful how much cleaning up we do... don't cause a segault! - # better to leave a segfault for the automatic cleanning at the end of the prog - - #self.csound.Cleanup() - #print 'STOPPED' - #time.sleep(1) - #del self.csound - #print 'DELETED' - #time.sleep(1) - def setMasterVolume(self, volume): - self.csound.SetChannel('masterVolume',volume ) + self.masterVolume = 80.0 - def sendText(self, txt): - #print txt - perf = self.perf - csound = self.csound - if 'csound' in txt: - print txt - import sys - sys.exit(0) - exec txt - -class CSoundClientPipe( CSoundClientBase ): - def __init__(self, orc): - self.orc = orc - def connect( self, init = True ): - if init: - (self.child_out, self.child_in) = os.popen2('csound ' + self.orc) - else: - self.child_in.close() - self.child_out.close() + def __del__(self): + self.connect(False) + sc_destroy() - def sendText(self, txt): - str = txt[19:-3] - if len(str) == 0: return - #print 'tosend:[%s]' % (str,) - self.child_out.write(str) -from Util.Clooper.sclient import * -from Util import NoteDB -class CSoundClientPlugin( CSoundClientBase ): def setMasterVolume(self, volume): self.masterVolume = volume if self.on: @@ -191,11 +46,6 @@ class CSoundClientPlugin( CSoundClientBase ): instrumentId = Config.INSTRUMENT_TABLE_OFFSET + int(fileName[-1]) + 6 sc_inputMessage(Config.CSOUND_LOAD_INSTRUMENT % (instrumentId, fileName)) - def __init__(self, orc): - sc_initialize(orc) - self.on = False - self.masterVolume = 80.0 - def load_instruments( self ): for instrumentSoundFile in Config.INSTRUMENTS.keys(): if instrumentSoundFile[0:3] == 'mic' or instrumentSoundFile[0:3] == 'lab': @@ -308,14 +158,13 @@ class CSoundClientPlugin( CSoundClientBase ): print 'ERROR: loopUpdate(): unsupported parameter change' def loopPlay(self, dbnote): qid = (dbnote.page << 16) + dbnote.id - print qid sc_loop_addScoreEvent( qid, 1, 'i', self.csnote_to_array(dbnote.cs)) - def play(self, dbnote, secs_per_tick): - a = self.csnote_to_array(dbnote.cs) - a[1] = a[0] * secs_per_tick - a[8] = max(a[8]*a[1], 0.002) - a[9] = max(a[9]*a[1], 0.002) - cs_addScoreEvent( 'i', a) + def play(self, csnote, secs_per_tick): + a = self.csnote_to_array(csnote) + a[self.DURATION] = a[self.DURATION] * secs_per_tick + a[self.ATTACK] = max(a[self.ATTACK]*a[self.DURATION], 0.002) + a[self.DECAY] = max(a[self.DECAY]*a[self.DURATION], 0.002) + sc_scoreEvent( 'i', a) def csnote_to_array(self, csnote): return self.csnote_to_array1( @@ -335,6 +184,13 @@ class CSoundClientPlugin( CSoundClientBase ): csnote.overlap, csnote.instrumentFlag) + INSTR_TRACK=0 + ONSET=1 + DURATION=2 + PITCH=3 + AMPLITUDE=5 + ATTACK=8 + DECAY=9 def csnote_to_array1( self, onset, pitch, amplitude, @@ -402,3 +258,14 @@ class CSoundClientPlugin( CSoundClientBase ): Config.INSTRUMENTS[ instr ].crossDur ]) return a + +_Client = None + +def new_csound_client(): + global _Client + if _Client == None: + _Client = _CSoundClientPlugin( Config.TAM_TAM_ROOT + '/Resources/univorc.csd' ) + _Client.connect(True) + _Client.setMasterVolume(100.0) + _Client.load_instruments() + return _Client |