def testcsoundapi(dur=20, nchnls=2, backend=None, sr=None): backend = backend or csound.get_default_backend() sr = sr or csound.get_sr(backend) cs = ctcsound.Csound() orc = f""" sr = {sr} ksmps = 128 nchnls = {nchnls} instr 1 iperiod = 1 kchn init -1 ktrig metro 1/idur kchn = (kchn + ktrig) % nchnls anoise pinker outch kchn+1, anoise printk2 kchn endin schedule(1, 0, {dur}) """ orc = textwrap.dedent(orc) options = ["-d", "-odac", "-+rtaudio=%s" % backend, "-m 0"] for opt in options: cs.setOption(opt) cs.compileOrc(orc) cs.start() pt = ctcsound.CsoundPerformanceThread(cs.csound()) pt.play() return pt
def compile_and_start(self): logger.debug("Starting Sampler") # self.cs.setStringChannel("gSname", self.sample_path) self.cs.compileCsdText(self.csd) self.cs.start() self.pt = ctcsound.CsoundPerformanceThread(self.cs.csound()) self.pt.play()
def startEngine(self, sr=44100, ksmps=32, nchnls=2, zerodbfs=1.0, dac='dac', adc='', port=0, bufferSize=0): """Start an ICsound engine. The user can specify values for sr, ksmps, nchnls, zerodbfs, dac, adc, a port number, and the messages buffer size. If a port number is given, this engine will listen to that port for csound code and events. """ if self._csPerf: print("CsoundMagics: Csound already running") return if self._clientAddr or self._clientPort: self._clientAddr = None self._clientPort = None self._debugPrint( "Closing existing client connection before starting engine") self._sr = sr self._ksmps = ksmps self._nchnls = nchnls self._0dbfs = zerodbfs self._dac = dac self._adc = adc self.createMessageBuffer(0) self.setOption('-o' + self._dac) self._bufferSize = bufferSize if self._adc: self.setOption('-i' + self._adc) if port > 0: self.setOption("--port={}".format(port)) if self._bufferSize: self.setOption("-B{}".format(self._bufferSize)) self.setOption("-b{}".format(self._bufferSize)) orc = ''' sr = {} ksmps = {} nchnls = {} 0dbfs = {} '''.format(self._sr, self._ksmps, self._nchnls, self._0dbfs) self.compileOrc(orc) self.start() self._csPerf = ctcsound.CsoundPerformanceThread(self.csound()) self._csPerf.play() self._flushMessages() if self._csPerf.status() == 0: print("Csound engine started at slot#: {}.".format(self.slotNum)) if port > 0: print("Listening to port {}".format(port)) else: print("Error starting server. Maybe port is in use?")
def createEngine(self): self.cs = ctcsound.Csound() res = self.cs.compileOrc(code) self.cs.setOption('-odac') if res == 0: self.cs.start() self.cs.setControlChannel('pitch', 1.0) self.cs.setControlChannel('volume', 1.0) self.perf = ctcsound.CsoundPerformanceThread(self.cs.csound()) self.perf.play() return True else: return False
def start(self, instr, instr_bank): print "Nebulae Starting" if self.currentInstr != self.new_instr: reset_settings_flag = True else: reset_settings_flag = False self.currentInstr = instr if self.c is None: self.c = ctcsound.Csound() self.log.spill_basic_info() floader = fileloader.FileLoader() floader.reload() self.orc_handle.generate_orc(instr, instr_bank) configData = self.orc_handle.getConfigDict() self.c.setOption("-iadc:hw:0,0") self.c.setOption("-odac:hw:0,0") # Set option for Csound if configData.has_key("-B"): self.c.setOption("-B" + str(configData.get("-B")[0])) else: self.c.setOption("-B512") # Liberal Buffer if configData.has_key("-b"): self.c.setOption("-b" + str(configData.get("-b")[0])) self.c.setOption("--realtime") self.c.setOption("-+rtaudio=alsa") # Set option for Csound if debug is True: self.c.setOption("-m7") else: self.c.setOption("-m0") # Set option for Csound self.c.setOption("-d") self.c.compileOrc( self.orc_handle.curOrc) # Compile Orchestra from String self.c.readScore( self.orc_handle.curSco) # Read in Score generated from notes self.c.start() # Start Csound self.c_handle = ch.ControlHandler( self.c, self.orc_handle.numFiles(), configData, self.new_instr, bank=self.new_bank) # Create handler for all csound comm. self.loadUI() self.pt = ctcsound.CsoundPerformanceThread( self.c.csound()) # Create CsoundPerformanceThread self.c_handle.setCsoundPerformanceThread(self.pt) self.pt.play() # Begin Performing the Score in the perforamnce thread self.c_handle.updateAll( ) # Update all values to ensure their at their initial state. if reset_settings_flag == True: print("Changing Instr File -- Resetting Secondary Settings") self.c_handle.restoreAltToDefault()
def _start_csound(self): cs = ctcsound.Csound() orc = self._csdstr.format(sr=self.sr, ksmps=128, backend=self.backend, numosc=self._numosc) options = ["-d", "-odac", "-+rtaudio=%s" % self.backend, "-m 0"] for opt in options: cs.setOption(opt) cs.compileOrc(orc) cs.start() pt = ctcsound.CsoundPerformanceThread(cs.csound()) pt.play() self._cs = cs self._pt = pt
def _startCsound(self): cs = ctcsound.Csound() orc = self._csdstr.format(sr=self.sr, ksmps=self.ksmps, nchnls=2, backend=self.backend, a4=self.a4) options = ["-d", "-odac", "-+rtaudio=%s" % self.backend, "-m 0"] for opt in options: cs.setOption(opt) logger.debug(orc) cs.compileOrc(orc) cs.start() pt = ctcsound.CsoundPerformanceThread(cs.csound()) pt.play() self._cs = cs self._pt = pt
def _start_csound(self): cs = ctcsound.Csound() csd = _csd_sinesynth.format( freq=self.freq, amp=self.amp, sr=self.sr, backend=self.backend, freqport=self.freqport, gain=self.gain) error = cs.compileCsdText(csd) if error: raise RuntimeError("Could not compile csound source") cs.start() pt = ctcsound.CsoundPerformanceThread(cs.csound()) pt.play() self._cs = cs self._pt = pt self._setControlChannel = ctcsound.libcsound.csoundSetControlChannel
def __init__(self): ctcsound.csoundInitialize(ctcsound.CSOUNDINIT_NO_SIGNAL_HANDLER) self._csnd = ctcsound.Csound() self._csnd.compileCsd(Config.PLUGIN_UNIVORC) self._csnd.setDebug(False) self._csnd.start() self._perfThread = ctcsound.CsoundPerformanceThread( self._csnd.csound()) self._perfThread.play() # TODO # sc_initialize(Config.PLUGIN_UNIVORC, Config.PLUGIN_DEBUG, # Config.PLUGIN_VERBOSE, Config.PLUGIN_RATE) self.on = False self.setMasterVolume(100.0) self.periods_per_buffer = 2 global _loop_default # TODO # _loop_default = self.loopCreate() self.instrumentDB = InstrumentDB.getRef() # temporyary dictionary of loopId: loopNumTicks, # while I wait for james to implement it properly self.jamesSux = {}
non_has_filename: prints "MasterOutput i %9.4f t %9.4f d %9.4f k %9.4f v %9.4f p %9.4f #%3d\\n", p1, p2, p3, p4, p5, p1/6, active(p1) kstatus, kchan, kdata1, kdata2 midiin ;printf " midi in s %4d c %4d %4d %4d\\n", kdata2, kstatus, kchan, kdata1, kdata2 endin ''' # The "f 0" statement prevents an abrupt cutoff. sco = "f 0 90\n" + musx_csound.to_csound_score(f) print(sco) csound = ctcsound.Csound() csound.setOption("-+msg_color=0") csound.setOption("-d") csound.setOption("-m195") csound.setOption("-f") # Change this for your actual audio configuration, try "aplay -l" to see what they are. csound.setOption("-odac:plughw:1,0") # Can also be a soundfile. # csound.setOption("-otest.wav") csound.compileOrc(orc) csound.readScore(sco) csound.start() # Probably runs a bit smoother in an independent thread (this is a native thread, # not a Python thread). thread = ctcsound.CsoundPerformanceThread(csound.csound()) thread.play() thread.join()
def setUpClass(self): self.cs = ctcsound.Csound() self.cs.compile_("csoundPerformanceThread", "simple.csd") self.pt = ctcsound.CsoundPerformanceThread(self.cs.csound())
kres line 1, idur, 0 ; so tail does not leave DC offset hanging. asig = asig * kres if (nchnls == 2) then ; stereo out aL, aR pan2 asig, (kloc % 180) * 180 ; pan stereo (0=hardL,1=hardR) outs aL, aR else out asig ; mono out endif endin """ # import ctcsoud API ; instantiate & initialize the Csound() class import ctcsound cs = ctcsound.Csound() # instantiate the Csound() class cs.setOption("-odac") # DAC output cs.setOption("-b 4096") # output buffer size cs.setOption("-d") # suppress displays ; test! # --------- instantiate the RT perf thread ------------------ csPerf = ctcsound.CsoundPerformanceThread(cs.csound()) cs.readScore("f 0 z \n") # keep Csound running for a long time... # -------- sound "fonts" for "rendering" --------------------- cs.readScore("f 20 0 0 1 \"sounds/agogo1-H.wav\" 0 0 0 \n") cs.compileOrc(myOrcOptions) # SR, NrChans cs.start() # start CS synth csPerf.play() # start separate performance thread cs.compileOrc(pluckIt) # compile the pluckIt instrument def passCallback(): pass # suppress all terminal output from CS TRACE = False if not TRACE:
def worker(self): self.oldFile = os.listdir(self.source_path) for file in self.oldFile: if not (file == ".DS_Store" or file == "README.md"): filePath = self.source_path + '\\'+file print(filePath,'-------------------------------') shutil.move(filePath, self.dest_path) print ('*******************************************') print ('RHAPSODY MODULE-1 OUTPUT') print ('*******************************************') # Initializing Csound cs = ctcsound.Csound() file = os.path.abspath(os.path.join(os.path.dirname(__file__),"Rhapsody.csd")) ret = cs.compile_("csound", "-o", "dac", file) if ret == ctcsound.CSOUND_SUCCESS: cs.start() pt = ctcsound.CsoundPerformanceThread(cs.csound()) pt.play() while not cs.performBuffer() and self.threadactive: # SEARCHING FOR NEW CSV FILES ON THE 'Recordings' DIRECTORY self.newFile = os.listdir(self.source_path) beat_array = [] origBeatTimes = [] tempoBeatTimes = [] recording_tempo = 1 data = [] # GETTING THE TEMPO for file in self.newFile: if file.endswith(".txt"): filePath = self.source_path + '\\'+file openFile = open(filePath) text_file = csv.reader(openFile) i = 0 for line in text_file: if i == 0: data.append(float(line[0])) i += 1 else: data.append(int(line[0])) recordingTempo = data[0] openFile.close() for file in self.newFile: if file.endswith(".csv"): self.ver = self.ver + 1 pt.scoreEvent(False, 'i', (101, 0, 0.0001, self.ver)) # GETTING DATA FROM CSV FILE filePath = self.source_path + '\\'+file openFile = open(filePath) csv_file = csv.reader(openFile) for row in csv_file: print(type(row),row,'%%%%%%%%%%%%%%%%%%%%%%%%%%%%') if row: beat_array.append(row[0]) openFile.close() for beat in beat_array: origBeatTimes.append(float(beat)) n = 1 print ('===========================================') print ('SENDING TO CSOUND') print ('===========================================') print ('Getting data from:', file) print ('Beat onset times:', origBeatTimes) print ('Csound score lines:') for time in origBeatTimes: s_per_beat = 60 / recordingTempo s_per_measure = s_per_beat * len(beat_array) loop_length = s_per_measure * 1 modified_time = recordingTempo*time/60 if (loop_length <= 6) and (time == origBeatTimes[len(origBeatTimes)-1]): continue if (loop_length - modified_time) < 0: continue if 6 - modified_time < 0.8: pt.scoreEvent(False, 'i', (100, modified_time, 1, 0, self.cpspch_array[data[n]], self.ver, 1, recordingTempo, loop_length)) print (100, modified_time, 1, 0, self.cpspch_array[data[n]], self.ver, 1, recordingTempo, loop_length) else: pt.scoreEvent(False, 'i', (100, modified_time, 1, 0.2, self.cpspch_array[data[n]], self.ver, 1, recordingTempo, loop_length)) print (100, modified_time, 1, 0.2, self.cpspch_array[data[n]], self.ver, 1, recordingTempo, loop_length) n = n+1 print ('===========================================') print ('END') print ('===========================================') # MOVING ALL EXISTING FILES TO A Backup DIRECTORY #directory = os.path.abspath(os.path.join(os.path.dirname(__file__))) '''self.oldFile = os.listdir(self.source_path) for file in self.oldFile: if not (file == ".DS_Store" or file == "README.md"): filePath = self.source_path + '\\'+file shutil.move(filePath, self.dest_path)''' cs.sleep(4000) pt.stop() pt.join() del cs self.finished.emit()
def startThread(self): if self.compile_("csoundSession", self.csd) == 0 : self.createMessageBuffer(0) self.pt = ctcsound.CsoundPerformanceThread(self.cs) self.pt.play()
#c.setOption("-B256") # Liberal Buffer c.setOption("-+rtaudio=alsa") # Set option for Csound #c.setOption("--sched") c.setOption("-m7") # Set option for Csound c.compileOrc(orc_handle.curOrc) # Compile Orchestra from String c.readScore(orc_handle.curSco) # Read in Score generated from notes c.start() # Start Csound # Temp CSound csd playback #c.compileCsd("tests/test.csd") #c.start() c_handle = ch.ControlHandler( c, orc_handle.numFiles()) # Create handler for all csound comm. ui = ui.UserInterface(c_handle) # Create User Interface pt = ctcsound.CsoundPerformanceThread( c.csound()) # Create CsoundPerformanceThread pt.play() # Begin Performing the Score in the perforamnce thread c_handle.updateAll( ) # Update all values to ensure their at their initial state. while (pt.status() == 0 ): # Run a loop to poll for messages, and handle the UI. #while(True): c_handle.updateAll() ui.update() #pt.stop() #pt.join() ui.cleanup() print("\nexited. . .\nGood Bye")
def compileAndStart(self): csd = f''' <CsoundSynthesizer> <CsOptions> -d -M0 -o dac -m0 -+rtmidi=portmidi ;-+rtmidi=virtual ;use this option will abort --midi-key-cps=6 --midi-velocity-amp=4 </CsOptions> <CsInstruments> sr = 44100 ksmps = 32 nchnls = 2 0dbfs = 1.0 massign 0, 1 instr 1 ; Sampler Sname = "{self.sample_path}" iNum notnum {self.stringPitch2File()} ivol = p4 ipb = 1 inchs = filenchnls(Sname) if inchs == 1 then aLeft diskin2 Sname, ipb aL = aLeft*p4 aR = aLeft*p4 else aLeft, aRight diskin2 Sname, ipb aL = aLeft*p4 aR = aRight*p4 endif outs aL,aR endin </CsInstruments> <CsScore> f 0 3600 ; 1 hour long empty score </CsScore> </CsoundSynthesizer> ''' self.cs.compileCsdText(csd) self.cs.start() self.pt = ctcsound.CsoundPerformanceThread(self.cs.csound()) self.pt.play()