A simple example of triggering a sound in SuperCollider from blender using 'open sound control'

Introduction

This tutorial will explain the very basics of triggering a Synth in SuperCollider from Blenders game engine.

Before continuing you will need the following ready;

  • Blender installed
  • SuperCollider


Installing Python - SimpleOSC : v0.2.5

To send OSC messages from Blender to SuperCollider we are going take advantage of Blenders ability to incorporate Python scripting. The task of sending OSC messages has been made relatively easy for us from the hard work Daniel Holth & Clinton McChesney who produced 'simpleosc' and then built upon by IXI software to make things even simpler.

In this example We will use this set of Python files.

http://www.ixi-software.net/content/body_backyard_osc.html

Inside there are some set up instructions, but I think you just need to do this… (check)

Once you have a folder called pyserial look inside and there should be a folder just called 'osc'.
You need put this folder in with the other Python libraries Blender uses. For some reason these files are hidden away inside of the Blender application. You can get to them like this.

Turn on hidden files by opening a terminal and running the following two lines.

username$ defaults write com.apple.Finder AppleShowAllFiles yes
username$ Killall Finder

Killall Finder sounds a bit drastic but it just relaunches finder

To turn off the hidden files just do the same again but use 'no' instead of yes


Setting up the Blender Example

Open Blender from a Terminal. This is important as it allow you see what Blender is outputting and debug any problems. Here is the line I use, but you may need to change the address of you application and the window size.

username$ /Applications/blender/Blender.app/Contents/MacOS/blender -p 240 0 1200 900

I won't go into the details of creating shapes in Blender, but basically for this example we just need to make a sphere and place above a flat example, a plane or cube should do the trick.

Now we need to give the sphere some physical properties.

Select the sphere and then move into 'logic' section of the 'buttons window'.

Now turn the sphere into a 'dynamic' 'actor' by clicking the 'actor' button and then give the sphere properties like this;

www.simonblackmore.net_notes_media_blender_to_sc_1_ball_bounce_ball.jpg

To see if this has worked we need to run the game engine.

Click in the '3D Panel' and just press 'P'. The '3D Panel' should now change into the game view.

With a bit of luck the ball should fall and land on the object below it.

To make the ball bounce you need to give the object, and the object below a 'restitut' value….

Writing the Python script

In Blender you need to open up a text window and put in the following python script.

import osc
import socket
import GameLogic
import Rasterizer
 
try : # only does the init once
	osc.state
except AttributeError:
	osc.init()
	osc.state = 1
 
c = GameLogic.getCurrentController()
scene = GameLogic.getCurrentScene()
 
touch = c.getSensor("Collision")
 
if	touch.isPositive():
	bundle = osc.createBundle()
	osc.appendToBundle(bundle, "/s_new", ["sinewave", 1000])
	osc.sendBundle(bundle, "127.0.0.1", 57110)

Now we need to attach an object sensor to the script so that it is run when the sphere hits the other object.

You need to go into the logic panel and set up the sphere or the base as a collision sensor and link that to the name of the python script

www.simonblackmore.net_notes_media_blender_to_sc_1_ball_bounce_panel_image.jpg

That should be pretty much everything in Blender now lets move onto SuperCollider

SuperCollider


// Create a sine synth and store it
(
SynthDef("sinewave",
{arg midinote = 80, amp = 0.9, dur = 1, bus=0, attack = 0,length = 0.01, decay = 0.01,volu = 1;
	var x;
	x = SinOsc.ar(midinote.midicps, mul: amp)
			*
			EnvGen.kr(Env.linen(attack, length, decay, 
volu), doneAction: 2);
Out.ar(bus,x);
}).store;
)


// This is really usefull for trouble shooting.....
s.dumpOSC(1) ; // This command lets you view the messages that are being sent to the server.
s.dumpOSC(0);  // turns off osc printing

// Triger it
s.sendMsg("/s_new", "sinewave", 1000, 1, 0);

// To send messages to another computer use something like this

n = NetAddr("localhost",12000);

// 57110 is the default port for the local server

n.sendMsg("/n_set",x,"freq",500);


n.sendMsg("/s_new","sinewave",1000,1,0);

If everything is working in SuperCollider when you return to blender and run the game engine each time the sphere hits the base SuperCollider should produce a sound.

All the files can be downloaded here, please mess with them and do something a bit more interesting.

http://www.simonblackmore.net/notes_media/blender_to_sc/1_ball_bounce/

I have a few more examples that I have used in workshops. I have not had the chance to write them up but here they are are anyway.

This example detects the mouse movement and moves a ball along a table in blender and which moves through a audio buffer in SuperCollider.

http://www.simonblackmore.net/notes_media/blender_to_sc/2_grain_table/

This example uses some maths to detect the distance between two objects and sends the result as an OSC message to control the volume of a synth.

http://www.simonblackmore.net/notes_media/blender_to_sc/3_square_fade/

 
blender_to_supercollider_1.txt · Last modified: 2008/06/22 13:04 by simon
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki