Friday, January 27, 2012

Blender and Csound

Allow me to show you a video I made a few days ago:
What you probably saw was a nine second video of a 3d computer animation of a purple cube striking a gold platform of sorts. If you had your speakers on, you would have also noticed a ringing sound happening every time the cube touched the platform.
The video itself is pretty lame and boring, but the idea behind is actually something I've been thinking about doing for a while.
What is happening
The video above has two separately created components: a video track and an audio track. The audio track was rendered using Csound, reading instructions on what notes to play from a something called a score file. This same set of instructions was read and interpreted by another piece of software called Blender, which then produced the visual part you saw above.

The steps
While there is probably a much more efficient way of going about this, this is the process I used to produce this animation. My computer programming knowledge is pretty sparse, so I go with what I know will work.
1. Write/Generate a Csound Score. (In this case, the score was generated with information regarding instrument number, start time, duration, pitch, and amplitude)
2. Render an audio file with the created score file in Csound. Make sure the samplerate is set to 48khz
3. Format the score file so that there are only i-statements. In other words, make the score file as simple as possible. (This was done using a short python script)
4. Parse the reformatted score file and generate a new file readable by Blender. In this case, it is a file with a bunch of lines saying dobounce(x,y) where x is the time of the strike, and y is the release. The parser was written in C to take advantage of the fscanf command.
5. Paste these new instructions in a Python script, which will be able to read them and directly work with Blender.
6. Run the Python script + generated score instructions inside of Blender. This will automatically create the necessary keyframes for the animation.
7. Render the animation to a video file.
8. Take the video file and the audio file and sync them together using mencoder. Since the audio is at 48khz, the audio and video should (hopefully) sync up perfectly.

And that is all there is to it!

Why this is so cool


This is a really cool breakthrough for me for a number of reasons. The biggest reason is because I finally figured out a way to incorporate visuals into my music in a simple, but complex way. Unlike most visualizers which take simply amplitude information from an audio file, mine took individual note information and parameters to render visuals. I just used start time and duration for animation parameters, but the possibilities are virtually endless! I believe visuals are especially important for 21st century music. Many times, the music is so out of touch with an average listener that it can be helpful to have a video to explain what the music is doing in order to gain a further appreciation of what is happening.

The second reason why I like this project so much is that it uses entirely open source software. Csound, Python, Blender 3d, and C are all under some sort of GPL or Creative Commons liscense. If there is anything that I want to stress in this blog it is this: the open source platform is the future of digital audio technology. I will repeat myself: the open source platform is the future of digital audio technology.

Thursday, January 19, 2012

Drummachine Tweaks and Github

This one will be short. Much shorter than it should be.

First off, I added a few more additions to my drum machine:

1. Added humanization. Velocities and timing will be randomly tweaked by a given amount.
2. Drums now have the ability to randomly "glitch." All this is is a really fast retrigger.  The percentage of the drum sounds that glitch is determined by a single value between 0 and 1.
3. function has an added "start time" parameter, which specifies what time the program should write the drum pattern. Useful for more complicated arrangements.

I made an 8-bar demo showing what my drum machine can do:





Csound Glitch Drums Test by The Zebra Project

Everything you hear was rendered from a Csound file. No post-processing or nothin'. Score generation for this was done using a small script in python I made in the summer called xm2sco. You can find a version of it here: xm2sco.sourceforge.net. It is still in early development, and will be going through some major changes. More to come on that. 

Would you like the code for this project? I made a github account and uploaded the code here

Monday, January 16, 2012

The Algorithmic Drum Machine, part 2

I'm slowly adding more and more to my algorithmic drum machine. Today I added the ability for my drum machine to reverse samples:


Algorithmic Drum Machine 2 by The Zebra Project

The first clip plays the drum pattern for four bars straight. The second clip plays the drum pattern for four bars with random variation and the new reverse effect.

It's pretty amazing to me how much life the reverse effect adds to the drums. I limited this example to only 4 bars, but I could make it any amount I want and it would all still be interesting due to the randomness. I used to make this sort of drum texture by hand using Renoise. This takes a split second to generate the score file.

Samples used are from the Vengeance Drum Sample pack. They have some pretty drum hits there, so I highly suggest you check it out.

My python script isn't all that different from the original drum machine. The only difference is an added reverse variable, set by a number between 0-1. A value of 1 will make 100% of the samples of that instrument reversed:

import random

#declare the base drum pattern in a typical 16th note cell fashion

no_weight = []

for i in range (16): no_weight.append(1)

snare_note =     (0,0,0,0,1,1,0,0,0,0,0,0,1,1,1,1)
snare_vel =     (0,0,0,0,1,.25,0,0,0,0,0,0,1,.1,.5,.5)
snare_weight =     (0,0,0,0,.9,.5,0,0,0,0,0,0,.9,.2,.1,.2)

kick_note =     (1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0)
kick_vel =     (1,0,0,0,0,0,0,0,0,0,1,0,0,0,.5,0)
kick_weight =     (1,0,0,0,0,0,0,0,0,0,1,0,0,0,.5,0)

hihat_note =     (1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
hihat_vel =     (1,.1,.4,.25,1,.1,.4,.25,1,.1,.4,.25,1,.1,.4,.25,)
hihat_weight =     (.5,.6,.8,.8,.9,.8,.5,.6,.5,.6,.8,.8,.9,.8,.5,.6)

#open score file
score = open("drumscore.sco", "w")
tempo = 200

#write tempo
score.write("t 0 " + str(tempo) + "\n")

def drumseq(name,instr,note,vel,bar_num,weight, rand):
    score.write(";" + str(name) + "\n")
    time = 0
    for bar in range(bar_num):
        for i in range(16):
            rand_val = random.random()
            if note[i] == 1 and rand_val <= weight[i]:
                time = i * .25 + 4*bar
                rand_val = random.random()
                reverse = 1
                if rand_val < rand: reverse = -1
                score.write("i" + str(instr) + " " + str(time) +" .25 "+ str(vel[i]) + " " + str(reverse) + "\n")


#write the parts
drumseq("snare", 1, snare_note, snare_vel, 4, no_weight, 0)
drumseq("kick", 2, kick_note, kick_vel, 4, no_weight, 0)
drumseq("hihat", 3, hihat_note, hihat_vel, 4, no_weight, 0)
score.close()

I had to do some modifications to the Csound orchestra file in order to give the reverse option. Instead of using the soundin opcode, I used the diskin2 opcode, which will reverse the sound file if the frequency is a negative number. In my score, a p5 value of 1 would play a sound file normally, and a p5 value of -1 would reverse it. I also made some modifications so that the duration of the instrument would not exceed the length of the file. Without this, the sample would loop.

The orchestra code here took a little bit of creative thinking and research to figure out, so I'll post an example of one instrument:

instr 1
    iamp = .3
    ivel = p4
    ilen filelen "snare9.wav"
    p3 = ilen
    a1,a1 diskin2 "snare9.wav", p5, 0, 1
    outs a1*iamp*ivel, a1*iamp*ivel
endin


The next step in this project is going to be humanization, followed by possibly glitch and stutter. Here's to hoping I'll still have the interest by then!

Saturday, January 14, 2012

The Algorithmic Drum Machine

I kind of pulled a Mark Zuckerberg on this little project I made today, so I'll try to right my wrongs here:

Dan, this is your idea which I stole and implemented using Python and Csound. Please know that I did this out of love, and I am looking forward how you will implement your version of this drum machine in Supercollider.

Here is a little sample of what my drum machine sounds like:

Randomized Drum Machine Experiment by The Zebra Project

The drum sounds are just one-shot samples being triggered by the soundin opcode in Csound. No synthesis happening here. Actually, the code is so boring that I'm not going to even bother posting it. I won't be posting the samples either, but know that they are from the "Acetone Rhythm Ace" folder in this big file of drum samples I downloaded once.

The drum algorithm starts off with a base drum pattern. Inputting the base drum pattern is very similar to how you would program a traditional 16th note step sequencer. Every 16th note has a specific velocity and weight. Velocity speaks for itself. Weight is the hip algorithmic variable. Weight is a number between 0 and 1. The closer the number reaches 1, the more likely the number will be triggered. A note with a weight of .9 is going to get triggered 90% of the time with a specified velocity.

Below is the python code I used for the score generator. This code will render 4 bars of my example drum pattern to a Csound Score file. There, it can be rendered. Because this algorithm uses stochastic (random) elements, no two renders are alike.

import random

#declare the base drum pattern in a typical 16th note cell fashion

snare_note = (0,0,0,0,1,1,0,0,0,0,0,0,1,1,1,1)
snare_vel = (0,0,0,0,1,.25,0,0,0,0,0,0,1,.5,.5,.5)
snare_weight = (0,0,0,0,.9,.5,0,0,0,0,0,0,.9,.2,.1,.2)

kick_note = (1,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0)
kick_vel = (1,0,0,0,0,0,0,0,1,0,0,0,0,0,.5,0)
kick_weight = (1,0,0,0,0,0,0,0,.9,0,0,0,0,0,.5,0)

hihat_note = (1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
hihat_vel = (1,.1,.4,.25,1,.1,.4,.25,1,.1,.4,.25,1,.1,.4,.25,)
hihat_weight = (.5,.6,.8,.8,.9,.8,.5,.6,.5,.6,.8,.8,.9,.8,.5,.6)

#open score file
score = open("drumscore.sco", "w")
tempo = 120

#write tempo
score.write("t 0 " + str(tempo) + "\n")

def drumseq(name,instr,note,vel,bar_num,weight):
score.write(";" + str(name) + "\n")
time = 0
for bar in range(bar_num):
for i in range(16):
rand_val = random.random()
if note[i] == 1 and rand_val <= weight[i]:
time = i * .25 + 4*bar
score.write("i" + str(instr) + " " + str(time) +" .25 "+ str(vel[i]) + "\n")


#write the parts
drumseq("snare", 1, snare_note, snare_vel, 4, snare_weight)
drumseq("kick", 2, kick_note, kick_vel, 4, kick_weight)
drumseq("hihat", 3, hihat_note, hihat_vel, 4, hihat_weight)
score.close()



This is a rather basic implementation of this drum algorithm and has much room for improvements. In the future, I hope to incorporate more elements of processing like reverse and pitch shift in addition to humanization to strive to generate an organic sounding drum pattern.

Thursday, January 12, 2012

Synth Pads: the hardest sounds to do right

Today, I decided to try to design a synth "pad" sound. The thought of designing a pad really frightens me. You are essentially designing a patch where notes are meant to be held indefinitely. In a pad patch, a minute-long note should keep the audience interested right up to the last millisecond. This is a scary concept for me. How does one make an instrument where a single long note can be the opposite of boring?

So, I woke up this morning, fired up my laptop, took a deep breath, and came up with this humble example in Csound:
FM Pad by The Zebra Project

Here are some general concepts I thought about while making this sound:

1. Swell and Articulation:

When I designed pad sounds in the past, this one always got me. Every patch I made was just not articulate enough. I could only play really long notes, and this was a huge limiting factor for what the instrument could play. When designing this patch, I realized that pad swells are about growth in timbre, not volume. Articulation is extremely important for a versatile pad synth. Without it, your pad will always sound behind the beat. My patch has a volume envelope with an attack of 10ms and a separate "filter"(it's not really a filter, but we will call it that for now) envelope with an attack of 5 seconds.

2. Timbre and Change

A pad sound can't just be sawtooth wave. Listeners would be bored of it after the first few seconds. How the sound timbre changes over time determines the quality of Pad. Usually, I would have gone with a low-pass filter, but today I chose to use FM synthesis because you can do drastic changes to the waveform with minimal effort (it was also more interesting than a low-pass filter.) I was able to get a really interesting sound by slowly change the modulator number in the C:M ratio with a gentle LFO. I also put in a random number generator which adds very subtle deviation to pitch.

3.Wideness and Size

Creating width and size is the trick to giving your sound that overwhelming feeling. "Width" refers to the difference in sound between the left and right speakers. Slight modifications to each side gives the sound variety and realism. In my patch, I slightly detuned the left and right signals. "Size" refers to how large a sound is. The patch I have has 3 slightly detuned oscillators to give the sound of a large ensemble. To have made this a bigger sound, I could have added a large digital reverb. I didn't because it would have made the patch more complicated than I wanted it to be. Plus, I like to be careful with my reverb. It makes things messy very quickly and I almost consider it a cop-out when making good pad sounds. Reverb is like salt. It's good with the meal, but it isn't the meal.




Alrighty. That's all I have to say about that for now. Here is the Csound Code. Enjoy!


<CsoundSynthesizer>
<CsOptions>
-W
-o fmpad.wav
</CsOptions>
<CsInstruments>
sr = 96000
ksmps = 10
nchnls = 2
0dbfs = 1
instr 1
ipch = cpspch(p4)
iamp = p5*.15

;amplitude envelope with a fast attack
aenv linsegr 0, .0001, 1, 8, .9, 1, 0
;timbre modulation envelope with a slow attack
;this one changes the modulation index of the FM synth
kenv2 linseg 0, 5, 2
klfo lfo .01, .5

;random number generator. adds variety to the pitch.
krand randi .0005, 3

;3 detuned FM oscillators, sent to the right speaker
a1 foscil iamp*aenv, ipch*(1+krand)*.999, 1, 3+klfo, kenv2, 1
a2 foscil iamp*aenv, ipch*(1+krand)*1.002, 1, 3+klfo, kenv2, 1
a3 foscil iamp*aenv, ipch*(1+krand)*.996, 1, 3+klfo, kenv2, 1

;3 detuned FM oscillators, sent to the left speaker
a4 foscil iamp*aenv, ipch*(1+krand)*1.001, 1, 3+klfo, kenv2, 1
a5 foscil iamp*aenv, ipch*(1+krand)*1.004, 1, 3+klfo, kenv2, 1
a6 foscil iamp*aenv, ipch*(1+krand)*.998, 1, 3+klfo, kenv2, 1

aout = (a1+a2+a3)/3 ;combine the oscillators
aout2 = (a4+a5+a6)/3

outs aout2, aout
endin
</CsInstruments>
<CsScore>
f1 0 4096 10 1
t 0 70
;Score Generated using python and Milkytracker
i 1 0 2.0 9.02 1
i 1 2.0 2.0 9.01 1
i 1 0 4.0 7.02 1
i 1 0 4.0 6.02 1
i 1 0 8.0 8.02 1
i 1 0 8.0 8.05 1
i 1 0 8.0 8.09 1
i 1 4.0 4.0 9.0 1
i 1 4.0 4.0 6.1 1
i 1 4.0 4.0 5.1 1
i 1 8.0 2.0 9.02 1
i 1 8.0 4.0 8.04 1
i 1 8.0 4.0 8.07 1
i 1 8.0 4.0 8.09 1
i 1 10.0 2.0 9.01 1
i 1 8.0 4.0 6.09 1
i 1 8.0 4.0 5.09 1
i 1 12.0 2.0 8.07 1
i 1 14.0 2.0 8.04 1
i 1 12.0 7.0 8.02 1
i 1 16.0 3.0 8.06 1
i 1 12.0 7.0 8.09 1
i 1 12.0 7.0 9.02 1
i 1 12.0 7.0 6.02 1
i 1 12.0 7.0 5.02 1

</CsScore>
</CsoundSynthesizer>

Tuesday, January 10, 2012

Galbanum Waveforms

About a week ago, I purchased the Galbanum Architecture waveform pack on a really insistent impulse. For forty bucks, I downloaded 25,000 waveforms in wav32 format which are 2048 samples each. Besides offering many variations on classic waveforms like square, saws, and triangle waves, they also have a set of unusual waves. Many hours of experimentation lie ahead of me.

The problem is, how do I test each one? Waveforms are organized into different folders. Originally, I would go into a folder, load up a sample one by one in Renoise, and loop them. This was a time consuming effort.

Today, I figured out a way to do that in Csound.
<CsoundSynthesizer>
<CsOptions>
-odac
-M1
--midi-key-pch=4
</CsOptions>
<CsInstruments>
sr = 96000
ksmps = 10
nchnls = 1
0dbfs = 1
alwayson 2
instr 2

gktbl init 1
imax = 40
kkey sensekey

if kkey == 106 then ;go down if 'j' is pressed
gktbl = gktbl - 1
elseif kkey == 107 then ;go down if 'k' is pressed
gktbl = gktbl +1
endif

;cycle through the waveforms. if it reaches the end function tables, go to the beginning
if gktbl < 1 then
gktbl = imax
elseif gktbl > imax then
gktbl = 1
endif
if kkey == 32 then
printk2 gktbl
endif
endin

instr 1
;de-click the sounds
kpad linsegr 0, .01, 1, .01, 0
a1 oscil3 .3*kpad, cpspch(p4), i(gktbl)
out a1
endin

</CsInstruments>
<CsScore>
#include "tables" ;python generated file which contains 40 function tables with the waveforms
f 0 3600 ;stay open for an hour
</CsScore>
</CsoundSynthesizer>


The first thing I did was automatically generate all the wavetables to load up in Csound. With some google-fu and python, I created a file called "tables" which generated 40 function tables that could be loaded up into Csound. Each function table is a different waveform in the folder. To give you a sense of what these function tables look like, here is an example of one:

f1 0 2048 1 "Cos Saw.wav" 0 0 0

The synth itself is just an oscillator with some volume padding to prevent clicks. Just enough to get an idea of what the waveform sounds like. It takes realtime midi input from my USB keyboard, and I made it so the J and K keys would cycle through the waveforms. Pressing the space bar returns the function table number in the terminal window.

Hopefully this will be a useful tool for browsing through these waveforms!

Monday, January 9, 2012

Flux

In some ways, I have found that making music on a computer to be the exact opposite of making music on a traditional instrument. With a traditional instrument, one is always striving for perfection. With a computer, everything can be done "perfectly" right at the beginning. In fact, it is so perfect that doesn't sound musical at all. So while a traditional musician would spend his time striving for perfection, a computer musician would find himself chipping away at it.

Today I decided to play around a little with the idea of fluctuations ("flux," for the hip folks out there.)

Take a listen to these two Csound instruments I made, playing identical pieces of music:


Flux by The Zebra Project

Instrument A is nothing more than a simple triangle-wave oscillator with a volume envelope. Instrument B sounds very similar to instrument A, except with a few modifications. This was accomplished using two simple techniques:

1. Doubling and Wideness

A common producers technique is to duplicate an audio signal, add subtle variation to one of them, and then hard-pan each one left and right so that the two signals come out of separate speakers. This creates a very "wide" effect which can be especially heard in headphones.

To make this "wide" effect in instrument B, I made two oscillators with slight variations in tuning and in their volume envelopes, and sent one to the left speaker, and other to the right speaker.

2. Sample and Hold

Real instruments don't have perfect intonation, and neither should instrument B. Nuance is the key to creating expressive musical sounds. Pitch nuances can be made using a what is called a sample and hold generator. What exactly sample and hold generators do is beyond the scope of this post, so I highly recommend you read the synth secret article on it.

Instrument B has a sample and hold generator which generates a random number between 0 and 1 8 times a second. A value of 1 indicates that the pitch will get shifted by the maximum amount, and a value of 0 indicates no change at all. The depth of this change is determined by the variable idepth Instrument B.


For those interested, here is the code.


<CsoundSynthesizer>
<CsOptions>
;-odac ;realtime output
-o flux.aiff ;render to audio file

</CsOptions>
<CsInstruments>
sr = 96000
ksmps = 32
nchnls = 2
0dbfs = 1
instr 1
iamp = .2
ipch = cpspch(p4)

;one volume envelope
kamp1 linsegr 0, .0003, 1, .5, .05, .2, 0

;basic oscilator with no modulation
a1 oscil iamp*kamp1, ipch, 1

outs a1, a1
endin

instr 2
iamp = .2
ipch = cpspch(p4)
;sample and hold generator
krand randi 1, 8
;depth of modulation, where modulation is a number between 0-1 (best results < .05)
idepth = ipch* .005

;two volume envelopes, with subtle differences
kamp1 linsegr 0, .0003, 1, .5, .05, .2, 0
kamp2 linsegr 0, .0005, 1, .51, .05, .2, 0

;two oscillators with slight variations
a1 oscil iamp*kamp1, ipch*.998 + idepth*krand, 1
a2 oscil iamp*kamp2, ipch*1.002 + idepth*krand, 1

outs a2, a1 ;each oscillator sent to a different speaker
endin



</CsInstruments>
<CsScore>
f1 0 2048 7 1 1024 -1 1024 1 ;Triangle Wave
;Score Generated from a MIDI file made in Renoise and converted using another Csound instrument
i1 0.000000 0.913379 8.020000
i1 0.260771 1.304762 8.090000
i1 0.391383 1.434921 9.040000
i1 0.652154 1.434921 9.070000
i1 0.913152 1.173923 8.050000
i1 1.565306 0.521769 8.040000
i1 1.826077 0.260998 8.090000
i1 2.086848 0.913379 8.020000
i1 2.347846 1.304535 8.090000
i1 2.478231 1.435147 9.040000
i1 3.000000 1.174150 8.050000
i1 3.652154 0.782766 8.040000
i1 3.913152 0.652381 8.090000
i1 2.739229 2.087075 9.070000
i1 4.173923 0.913152 8.020000
i1 4.434694 0.913379 8.090000
i1 4.565306 0.913152 9.060000
i1 4.826077 0.913379 9.040000
i1 5.086848 0.913379 8.000000
i1 5.347846 0.652381 8.070000
i1 5.478231 0.521995 9.040000
i1 5.739229 0.260998 9.020000
i1 6.000000 1.304535 9.010000
i1 6.000000 1.304535 8.020000
i1 6.000000 1.304535 8.059999

i2 8.347846 0.913152 8.020000
i2 8.608617 1.304762 8.090000
i2 8.739229 1.434921 9.040000
i2 9.000000 1.434921 9.070000
i2 9.260771 1.174150 8.050000
i2 9.913152 0.521769 8.040000
i2 10.173923 0.260998 8.090000
i2 10.434694 0.913379 8.020000
i2 10.695692 1.304535 8.090000
i2 10.826077 1.434921 9.040000
i2 11.347846 1.174150 8.050000
i2 12.000000 0.782766 8.040000
i2 12.260771 0.652608 8.090000
i2 11.086847 2.087302 9.070000
i2 12.521770 0.913152 8.020000
i2 12.782539 0.913379 8.090000
i2 12.913153 0.913152 9.060000
i2 13.173923 0.913152 9.040000
i2 13.434694 0.913379 8.000000
i2 13.695692 0.652381 8.070000
i2 13.826077 0.521995 9.040000
i2 14.086848 0.261225 9.020000
i2 14.347846 1.304535 8.059999
i2 14.347846 1.304535 8.020000
i2 14.347846 1.304535 9.010000
</CsScore>
</CsoundSynthesizer>