Module main
[hide private]
[frames] | no frames]

Source Code for Module main

  1  #!/usr/bin/python 
  2  # -*- coding: latin-1 -*- 
  3   
  4  """  
  5  A barebones Python host for Csound 
  6   
  7  @author: Øyvind Brandtsegg 
  8  @contact: obrandts@gmail.com 
  9  @license: GPL 
 10  @requires: csnd 
 11  """ 
 12   
 13  import csnd  
 14  import cs.csMessages 
 15  import cs.csModule 
 16  import time 
 17  import control.eventCaller 
 18  import control.theTime2 
 19  from constants import * 
 20   
 21   
 22  #-------------------------------------------------------------------------------- 
 23   
 24   
 25  #### instantiate eventCaller 
 26  eventCaller = control.eventCaller.EventCaller() 
 27  """The event caller is the central module, communication with all other parts of the application.""" 
 28   
 29  theTime = control.theTime2.TheTime2(eventCaller) 
 30  """theTime is the timed queue used for timed automation of method calls.""" 
 31   
 32  #### running csound 
 33  csThread = cs.csModule.CsoundThreadRoutine(theTime) 
 34  """Instance of the Csound module, setting up and running Csound.""" 
 35  csound = csThread.csound 
 36  """Pointer to the actual Csound instance.""" 
 37  performanceThread = csound#csThread.performanceThread 
 38  """ 
 39  (Would be) Pointer to the C++ thread running Csound.  
 40  The current implementation does not use the performancethread, but the ksmps loop based method of running Csound.  
 41  The pointer to the performance thread has been implemented to make it feasible to change between ksmps-loop and performancethread driven Csound. 
 42  """ 
 43  csMessages = cs.csMessages.CsoundMessages(csound, performanceThread)  
 44  """Instance of csMessages, used for all message passing from python to csound.""" 
 45   
 46  # put pointers to objects into eventCaller 
 47  eventCaller.setPointers(csMessages, theTime) 
 48   
 49  # start csound thread 
 50  csThread.csoundThread.start() 
 51  # start eventCaller and theTime 
 52  eventCaller.initValues() 
 53  print 'eventCaller starting threads' 
 54  eventCaller.startThreads()  
 55  theTime.runClock = True 
 56  """Set theTime clock to run mode.""" 
 57   
 58  print '**************************************************************************' 
 59  print 'available commands:' 
 60  print '     perform(module, KEYWORD)' 
 61  print '     ...where KEYWORD may  be START or STOP' 
 62  print '     setParameter(module, parameter, value)' 
 63  print '     i ... sends a score event to Csound (e.g. "i 1 0 1 -5 60 0.5 0.5")' 
 64  print '     other commands:' 
 65  print '     eventCaller.setTimeBpm(value)' 
 66  print '     eventCaller.recordAudio(START/STOP)' 
 67  print 'stop: stop the barebones system and exit the application' 
 68  print '**************************************************************************' 
 69   
 70   
 71  # main loop 
 72  command = '' 
 73  while command != 'stop': 
 74      command = raw_input() 
 75      if command[:8] == 'perform(': 
 76          args = command[8:] 
 77          if 'START' in args: 
 78              module = args.split('START')[0][:-2] 
 79              try: 
 80                  eventCaller.perform(module, START) 
 81              except: 
 82                  print module, 'is not a valid composition module in eventCaller, skipping command' 
 83          elif 'STOP' in args: 
 84              module = args.split('STOP')[0][:-2] 
 85              try: 
 86                  eventCaller.perform(module, STOP) 
 87              except: 
 88                  print module, 'is not a valid composition module in eventCaller, skipping command' 
 89          else: 
 90              print 'perform must be called with START or STOP argument' 
 91      elif command[:13] == 'setParameter(': 
 92          args = command[13:] 
 93          module, parameter = args.split(',')[:2] 
 94          value = args.partition(parameter)[2] 
 95          while parameter[0] == ' ': parameter = parameter[1:]# remove any leading whitespace from the parameter argument 
 96          while value[0] == ',' or value[0] == ' ': value = value[1:]# remove any leading comma or whitespace from the value argument         
 97          value = value[:-1] # get rid of trailing parens 
 98          eventCaller.setParameter(module, parameter, value) 
 99      elif command[0] == 'i': 
100          csMessages.csoundInputMessage(command) 
101      elif command != 'stop': 
102          # this might facilitate some variant of live coding with barebones 
103          c = compile('r='+command, 'string', 'exec') 
104          try: 
105              exec(c) 
106          except: 
107              print command, 'is not a valid code object, skipping execution of', command 
108      time.sleep(0) 
109       
110  # stop threads 
111  eventCaller.stopThreads()  
112  csThread.stop() 
113