Wednesday, March 6, 2019

ex8. pluck csound opcode

The csound pluck opcode generates a sound based on the Karpus Strong Algorithm.


We use a python file (rand_gen.py) to generate a random signal of 16k samples.


# rand_gen.py
# random signal of length N

N = 16 * 1024

import numpy as np
from scipy.io import wavfile

amp = 30000 # <= 32768
rate = 44100

sig = np.random.randint(-amp,amp, size=N).astype('int16')
wavfile.write('rand.wav', rate, sig)

We use this file (ex8.py) to generate instrument 1 which we play 200 times, with different parameters. We use the table genertated by rand.wav file.


# ex8.py
# pluck opcode

import random

from csd import CSD
import table
from opcodes import envelope, output, source

csd = CSD(__file__)

csd.begin()

csd.add_instrument(
    num = 1,
    opcodes = [
        envelope.adsr(
            attack = 'p5*idur',
            sustain = 0.99,
            sig_out = 'kadsr'
            ),
        source.pluck(
            sig_out = 'asig',
            amp = 0.5,
            freq = 'p4',
            table = 1,
            method = 1,
            iparam1 = 0,
            iparam2 = 0
        ),
        output.outs(
            sig_in = ['kadsr*asig','kadsr*asig']
        )
               ]
    )

csd.middle()

t = 0
for i in range(200):
    at = 0.01 + .09 * random.random()
    t = t + .5 + random.random()/5
    f = 220 + 440 * random.random()
    csd.add('i 1 %.4f 0.5 %.4f %.4f' % (t, f, at))

csd.add_tables([table.sample(
                    num=1,
                    name='rand.wav'
                    )
                ])

csd.end()

We use pluck from updated source.py file:


# source.py
# ver 1

import sys

#1. lfo
def lfo(**dic):
    ''' A low frequency oscillator of various shapes.
    itype = 0 - sine, itype = 1 - triangles,
    itype = 2 - square (bipolar), itype = 3 - square (unipolar),
    itype = 4 - saw-tooth, itype = 5 - saw-tooth(down)
    sig_out, amp, freq, type '''
    sig_out = dic.get('sig_out','')
    if sig_out=='':
        print('We need sig_out for lfo')
        sys.exit(-1)
    amp = dic.get('amp', 1)
    freq = dic.get('freq', 440)
    itype = dic.get('itype', 0)
    return '%s lfo %s, %s, %s' % (sig_out,amp,freq,itype)

#2. pluck
def pluck(**dic):
    ''' Produces a naturally decaying plucked string or drum sound.
    - sig_out, amp, freq, table
    - method, method of natural decay. There are 6, some of which use
    - the 2 parameters values that follow.
    - 1: simple averaging. A simple smoothing process, uninfluenced by
    - parameter values.
    - 2: stretched averaging. As above, with smoothing time stretched by
    - a factor of iparm1 (>=1).
    - 3: simple drum. The range from pitch to noise is controlled by a
    - 'roughness factor' in iparm1 (0 to 1). Zero gives the plucked string
    - effect, while 1 reverses the polarity of every sample (octave down,
    - odd harmonics). The setting .5 gives an optimum snare drum.
    - 4: stretched drum. Combines both roughness and stretch factors.
    - iparm1 is roughness (0 to 1), and iparm2 the stretch factor (>=1).
    - 5: weighted averaging. As method 1, with iparm1 weighting the
    - current sample (the status quo) and iparm2 weighting the previous
    - adjacent one. iparm1 + iparm2 must be <= 1.
    - 6: 1st order recursive filter, with coefs .5. Unaffected by
    - parameter values. '''
    sig_out = dic.get('sig_out', '')
    if sig_out == '':
        print('We need sig_out for pluck')
        sys.exit(-1)
    amp = dic.get('amp', 1)
    freq = dic.get('freq', 440)
    table = dic.get('table', 1)
    method = dic.get('method', 1)
    iparam1 = dic.get('iparam1', 0)
    iparam2 = dic.get('iparam2', 0)
    t = sig_out,amp,freq,freq,table,method,iparam1,iparam2
    return '%s pluck %s,%s,%s,%s,%s,%s,%s' % t

#3. poscil
def poscil(**dic):
    ''' High precision oscillator
    sig_out, amp, freq, and table '''
    sig_out = dic.get('sig_out','')
    if sig_out == '':
        print('We need sig_out for poscil')
        return
    amp = dic.get('amp', 1)
    freq = dic.get('freq', 440)
    table = dic.get('table', 1)
    return '%s poscil %s, %s, %d' % (sig_out,amp,freq,table)

The link to the audio is here.

1 comment:

  1. Ukraine is one of the most popular destinations for outsourcing software development. Thus, I would highly recommend checking out this article if you’re looking to assemble your software developers in Ukraine.

    ReplyDelete