Thursday, 20 March 2014

Journey - Further Experiments 7

It was about time I did some Further Experiments. This one is in high resolution resonance and phase modulation synthesis.

The video of 'Journey'

The theme of Sython Song seems to be random walks and experimentation in generative techniques rather than hard core synthesis; so, despite 'Journey' being created in Python I have grouped it under Further Experiments In Extreme Synthesis. It feels really good to be able to bring this piece to life not because it is somehow amazing but because it was so easy achieved computationally.

Of late I have been trying to create 'Perfect Fi' - in other words, synthesis without artefacts. Clearly that is impossible. However, it makes a good target to aim for. One of the biggest problems for digital synthesis is aliasing producing low frequency inharmonics. These build up until then form a low frequency 'swell' of sound which is something cool be usually horrible.

There are three ways to reduce this effect. 
1) Don't make the aliased frequencies in the first place
2) Reduce them through filtering
3) Reduce them by lifting the Nyquist frequency

Using more additive synthesis helps with 1. Distortion synthesis has a nasty habit of creating aliased frequencies. For example, distorting a sine wave into a sawtooth generates frequencies all the way up to the sample rate; fully half of these will be aliased. Producing the saw tooth by adding frequencies up to the Nyquist limit produces a much cleaner sound.

The 'Clean' decimating filter helps with 2. Being more careful with filtering as one goes along helps two. If a signal is carefully filtered before passing to a distortion will help avoid he distortion injecting aliased frequenceis.

Journey uses 3 for the first time in a long time. It was created at 192 000 samples per second rather than the default 96000. This means that many less frequencies from the audio spectrum end up 'escaping' up above the Nyquist limit because that limit is up at 96KHz.

This has been a challenge until now because 192000 takes twice as much storage as 96000. Storage has been the major performance limit for Sonic Field. I originally designed SF to run in the Cloud. I had this idea that memory would be no limit. However, I soon realised I was unlikely to actually cluster SF in the cloud any time soon. I run it on a 16Gig Mac Book Pro myself. The runs out of memory very easily doing a complex patch. 

But no longer - audio signals can not be backed by Memory Mapped Files. Thus I can run patches which required 30,40,50 or even 100 Gigs quite effectively. The performance drop is too bad if the patch is written to access small blocks of audio at a time. 

Until the latest release of SF the previous data files system and the memory mapped one suffered from no having aggressive garbage collection. I tried to couple them to the Java garbage collector. That did not work well. It resulted in data which was going to be garbage collected still being written to disk. To get the system to work well, I needed to collect file backed garbage straight away at the point it was not longer wanted. 

Now SF has reference counted garbage collection
The implementation under the covers will be published on Nerds Central.
From a patch writing point of view is it simple:
  1. All new signals are generated with a count of 1.
  2. Every time a signal is passed to a processor its count is reduced.
  3. When a count hits 0 the signal is garbage collected.
  4. The + operators (as in +mySignal) will increase the count of a signal.
  5. If the same signal is to be passed to more than one processor the + operator can be used to keep the correct count.
  6. In some situations, this can lead to the count being too high and the signal not being collected.
  7. The - operator reduces the count and so allows collection under these situations.

Here is an example of using the + and - operators:

        convol_=sf.FrequencyDomain(sf.Concatenate(convol,sf.Silence(grainLength)))
        signal_=sf.Concatenate(signal,sf.Silence(grainLength))
        out=[]
        for grain in sf.Granulate(signal_,grainLength):
            (signal_i,at)=grain
            out.append((reverbInner(signal_i,+convol_,grainLength),at))
        -convol_

This new system makes SF run much faster due to not wasting resource writing dead data out to disk.

Here is the patch which created Journey:

import math
import random
sf.SetSampleRate(192000)

# Single threaded for debug
#def sf_do(toDo):
#   return toDo()

def ring(pitch,length):
    print "Ring: " + str(pitch) + "/" + str(length)
    sig1 = sf.SineWave(length,pitch*1.2)
    sig2 = sf.SineWave(length,pitch*1.2 + 1)
    env  = sf.SimpleShape((0,-60),(125,0),(length,-30))
    
    sig1 = sf.Multiply(+env,sig1)
    sig1 = sf.Pcnt90(sf.DirectMix(1,sig1))
    sig3 = sf.PhaseModulatedSineWave(pitch,sig1)
    sig3 = sf.Multiply(+env,sig3)

    sig2 = sf.Multiply(+env,sig2)
    sig2 = sf.Pcnt90(sf.DirectMix(1,sig2))
    sig4 = sf.PhaseModulatedSineWave(pitch,sig2)
    sig4 = sf.Multiply(env,sig4)
    
    sig5 = sf.Volume(sf.Mix(sig3,sig4),6)
    sig=sf.Saturate(sig5)
    sig=sf.ResonantFilter(sig,0.99,0.05,1000.0/pitch)
    return sf.Realise(sf.Normalise(sig))

def doFormant(sig,f1,f2,f3):
    #sig=sf.BesselLowPass(sig,f3,1)
    def doFormantInner(a,b,c,d):
        def doFII():
            return sf.RBJPeaking(a,b,c,d)
        return sf_do(doFII)
    sig1=doFormantInner(+sig,f1,1,40)
    sig2=doFormantInner(+sig,f2,2,20)
    sig3=doFormantInner( sig,f3,1,40)
    x=sf.Mix(sig1,sig2,sig3)
    x=sf.Normalise(x)
    return sf.Realise(x)

def makeSingBase(pitch,length):
    pitch=float(pitch)
    length=float(length)
    drop=1.0
    notes=[]
    for i in range(1,100):
        thisPitch=pitch*i
        if(thisPitch>10000):
            continue
        print thisPitch
        notes.append(sf.NumericVolume(sf.PhasedSineWave(length,thisPitch,random.random()),drop))
        drop=drop*0.6
    sig=sf.Normalise(sf.Mix(notes))
    return sig

def doSingEnv(sig):
    length=sf.Length(+sig)
    a=0
    d=0
    s=0
    r=length
    k1=50.0
    k2=length-50.0
    if(length<1000):
        a=100.0
        d=250.0
        s=(length-d)/2.0+d
    else:
        a=length*0.1
        d=length*0.25
        s=length*0.5
        
    env=sf.SimpleShape((0,-90),(k1,-30),(a,0),(d,-6),(s,-12),(k2,-30),(r,-90))
    sig=sf.Multiply(sig,env)
    sig=sf.Normalise(sig)
    return sig

#beat
def doFormant1(sig):
    return doFormant(sig,300,2800,3300)

#bit
def doFormant2(sig):
    return doFormant(sig,430,2500,3100)

#bet
def doFormant3(sig):
    return doFormant(sig,600,2350,3000)

#bat
def doFormant4(sig):
    return doFormant(sig,860,2050,2850)

#part
def doFormant5(sig):
    return doFormant(sig,850,1200,2800)

#pot 
def doFormant6(sig):
    return doFormant(sig,590,900,2700)

#boat
def doFormant7(sig):
    return doFormant(sig,470,1150,2700)

#boat
def doFormant8(sig):
    return doFormant(sig,470,1150,2700)

#book
def doFormant9(sig):
    return doFormant(sig,370,950,2650)
#but
def doFormant10(sig):
    return doFormant(sig,760,1400,2800)

#pert
def doFormant11(sig):
    return doFormant(sig,500,1650,1950)

formants=[
    doFormant1,
    doFormant2,
    doFormant3,
    doFormant4,
    doFormant5,
    doFormant6,
    doFormant7,
    doFormant8,
    doFormant9,
    doFormant10,
    doFormant11
]

def doNote(pitch,length,formant):
    def doNoteInner():
        sig=ring(pitch,length)
        sig=formants[int(formant)](sig)
        length_=sf.Length(+sig)
        env=sf.NumericShape((0,0),(length_/2.0,1),(length_,0))
        y=sf.Multiply(sig,env)
        x=sf.Realise(y)
        return x
    return sf_do(doNoteInner)


root    = 32
initial = sf.Silence(2000)
nNotes  = 64
length  = 16384
#length  = 1024

def makeTrack():
    notesL=[]
    notesR=[]
    at=1000
    for x in range(0,nNotes):
        print "Performing note: " + str(x)
        a  = 1+x%7
        b  = 1+x%11
        c  = 2*(1+x%3)
        d  = ((x+1)%3)*2
        e  = math.floor(x%22/2)
        f  = math.floor((11+x)%22/2)
        g  = 1.0+(x%8.0)/3.0
        h  = 1.0+(x%16.0)/6.0
        i  = x%5
        print (a,b,c,d,e,f,g,h,i)
        fa = root*a
        fb = root*b
        na1 = doNote(fa,length*g,e)
        nb1 = doNote(fb,length*h,f)
        a = 8  - (x%7)
        b = 12 - (x%11)
        fa = root*a
        fb = root*b
        na2 = doNote(fa,length*g,e)
        nb2 = doNote(fb,length*h,f)
        signal=sf.Volume(na1,c)
        signal=sf.Concatenate(signal,nb1)
        signal=sf.Concatenate(signal,sf.Volume(na2,d))
        signal=sf.Concatenate(signal,nb2)
        leftBias  = i/4.0
        rightBias = 1.0-leftBias
        leftT     = 30*leftBias
        rightT    = 30*rightBias
        signal=sf.Normalise(signal)
        sl=sf.NumericVolume(+signal,leftBias)
        sr=sf.NumericVolume( signal,rightBias)
        notesL.append((sl,at+leftT))
        notesR.append((sr,at+rightT))
        at=at+length/4
        
    def mixL():
        return sf.FixSize(sf.WaveShaper(-0.03,0.2,0,-1,0.2,2,sf.Normalise(sf.MixAt(notesL))))  
    def mixR():
        return sf.FixSize(sf.WaveShaper(-0.03,0.2,0,-1,0.2,2,sf.Normalise(sf.MixAt(notesR))))  
    ret=(sf_do(mixL),sf_do(mixR))
    return ret

(left,right)=makeTrack()
left=left.get()
right=right.get()

lr=sf.Length(+right)
ll=sf.Length(+left)
if(lr>ll):
    left=sf.Concatenate(left,sf.Silence(lr-ll))
elif(ll>lr):
    right=sf.Concatenate(right,sf.Silence(ll-lr))

def OnTop(signal,root):
    inp=sf.Saturate(sf.FixSize(+signal))
    x=sf.RBJPeaking(+inp,root*20,0.25,24)
    y=sf.RBJPeaking(+inp,root*25,0.25,24)
    z=sf.RBJPeaking( inp,root*30,0.25,24)
    x=sf.Saturate(sf.FixSize(x))
    y=sf.Saturate(sf.FixSize(y))
    z=sf.Saturate(sf.FixSize(z))
    return sf.FixSize(sf.Mix(signal,sf.Pcnt15(x),sf.Pcnt_10(y),sf.Pcnt10(z)))
            
left=sf.Realise(OnTop(left,root))
right=sf.Realise(OnTop(right,root))
sf.WriteFile32((+left,+right),"temp/temp.wav")

def reverbInner(signal,convol,grainLength):
    def reverbInnerDo():
        mag=sf.Magnitude(+signal)
        if mag>0:
            signal_=sf.Concatenate(signal,sf.Silence(grainLength))
            signal_=sf.FrequencyDomain(signal_)
            signal_=sf.CrossMultiply(convol,signal_)
            signal_=sf.TimeDomain(signal_)
            newMag=sf.Magnitude(+signal_)
            signal_=sf.NumericVolume(signal_,mag/newMag)        
            # tail out clicks due to amplitude at end of signal 
            return signal_
        else:
            -convol
            return signal
            
    return sf_do(reverbInnerDo)

def reverberate(signal,convol):
    def reverberateDo():
        grainLength = sf.Length(+convol)
        convol_=sf.FrequencyDomain(sf.Concatenate(convol,sf.Silence(grainLength)))
        signal_=sf.Concatenate(signal,sf.Silence(grainLength))
        out=[]
        for grain in sf.Granulate(signal_,grainLength):
            (signal_i,at)=grain
            out.append((reverbInner(signal_i,+convol_,grainLength),at))
        -convol_
        return sf.Realise(sf.Normalise(sf.MixAt(out)))
    return sf_do(reverberateDo)

 
(convoll,convolr)=sf.ReadFile("temp/revb.wav")

wleft =reverberate(+left,convoll)
wright=reverberate(+right,convolr)
wleft=wleft.get()
wright=wright.get()

left_out=sf.Normalise(sf.MixAt(
    (sf.Pcnt60(+wleft),10),
    (sf.Pcnt10(+wright),40),
    (sf.Pcnt10(+wleft),120),
    (sf.Pcnt15(+left),0),
    (sf.Pcnt5(+right),110)
))

right_out=sf.Normalise(sf.MixAt(
    (sf.Pcnt70(+wright),10),
    (sf.Pcnt10(wleft),40),
    (sf.Pcnt10(wright),130),
    (sf.Pcnt20(right),0),
    (sf.Pcnt5(left),105)
))
#left  = sf.Realise(left)
#right = sf.Realise(right)
sf.WriteFile32((left_out,right_out),"temp/temp_post.wav")

No comments:

Post a Comment