Saturday, February 23, 2019

ex1. poscil and linen opcodes

We use poscil (precision oscillator) with our sine table (giSine). First instrument uses default phase of 0, second phase of 0.5 (which will create cosine waves), and third with an envelope.


# ex1.py
# poscil and linen opcodes

from csd import CSD

csd = CSD(__file__)

csd.s1()

csd.add('giSine ftgen 0, 0, 2^10, 10, 1')

ins = '''
    instr 1
ifrq = 440
iamp = 0.5
aOsc poscil iamp, ifrq, giSine
outs aOsc, aOsc
    endin
'''

csd.add(ins)

ins = '''
    instr 2
ifrq = 440
iamp = 0.5
iphs = 0.5
aOsc poscil iamp, ifrq, giSine, iphs
outs aOsc, aOsc
    endin
'''

csd.add(ins)

ins = '''
    instr 3
ifrq = 440
iamp = 0.5
iphs = 0.5
kEnv linen 1,p3/4,p3,p3/4
aOsc poscil iamp, ifrq, giSine, iphs
outs kEnv * aOsc, kEnv * aOsc
    endin
'''

csd.add(ins)

csd.s2()

score = '''
i 1 0 1
i 2 2 1
i 3 4 1
'''

csd.add(score)

csd.s3()

We use this file:


# csd.py

import os
import time

class CSD:

    def __init__(self, fnam):
        self.lines = []
        self.fnam = fnam.split(os.path.sep)[-1][:-3]

    def add(self, line):
        self.lines.append(line)

    def s1(self, sr=44100, ksmps = 32, nchnls=2, dbfs=1):
        self.add('<CsoundSynthesizer>')
        self.add('<CsOptions>')
        self.add('-o %s.wav -W' % self.fnam)
        self.add('</CsOptions>')
        self.add('<CsInstruments>')
        self.add('sr = %s' % sr)
        self.add('ksmps = %s' % ksmps)
        self.add('nchnls = %s' % nchnls)
        self.add('0dbfs = %s' % dbfs)

    def s2(self):
        self.add('</CsInstruments>')
        self.add('<CsScore>')

    def s3(self):
        self.add('</CsScore>')
        self.add('</CsoundSynthesizer>')
        string = '\n'.join(self.lines)
        fout = open(self.fnam + '.csd', 'w')
        fout.write(string)
        fout.close()
        time.sleep(0.5)
        status = os.system('csound %s.csd' % self.fnam)
        if status!=0: raise Exception ('Could not write wav file')

We have the video here.

No comments:

Post a Comment