Cabbage Logo
Back to Cabbage Site

CSoundUnity Beginner Questions

Hi there,

I’ve been looking around for the ideal method of bringing one of my projects to life. Essentially I want to build a library of instruments and effects that can be triggered and modulated in realtime through VR interactions. Here’s a little demo of what I’m working on: vimeo.com/386364861.

CSoundUnity looks like the way to go but I’m a total beginner when it comes to CSound and DSP programming as a whole really. So I have a couple of questions before I dive in.

  1. So far I’ve been working with a basic MIDI out bridge API that I can use to trigger the notes of a vsti and then send Control Changes to tweak the assigned parameters. From what I understand, the latter is doable as I can see the sliders when importing my csd instrument, but I can’t figure out (through my own testing or looking through docs/ forums) if and how I can trigger notes on the instrument. What’s the best approach for me to do this?

  2. Once I have the first bit set up, will it be possible to have these plug-ins be aware of each other. For example side-chaining compressors to dynamically alter volumes and creating effects racks and busses?

Ideally I would love to have the freedom and flexibility with the audio system back-end to replicate how I work with a DAW, but obviously that won’t be an easy feat. Assuming the vision can in theory be achieved using Cabbage/ CSoundUnity, I’ll be looking for participants on the project, so please do get in touch!

1 Like

You project looks great. So you are sending MIDI data from Unity to a host? And you want to drop the use of a separate host and do all your audio work from within Unity? If so I think CsoudUnity can be of some use here.

Instruments in Csound can be triggered via MIDI, or some simple score events. For instance, if you wanted to play MIDI note 60 from a collision event you could do something like this:

void OnCollisionEnter(Collision collision)
{
    csoundUnity.sendScoreEvent("i\"MySynth\" 0 1 60");
}

Where “MySynth” is the synth you want to play. Start time is 0, duration is 1 and we pass 60 as the MIDI note number. Of course each synth will accept unique parameters depending on how it was written.

You can update a synth while is it playing by sending channel data:

void Update()
{
    csoundUnity.setChannel("filterCutOff", transform.position.x);
}

Channels are like MIDI control changes, while score event are like MIDI notes.

Certainly. But it will take some thought in terms of design. I’m working on a VR project at the moment and I’ve putt all my instruments into a single .csd file. Each instrument is ware of each other, and each game object can speak directly to the corresponding instruments. Master effects racks and a piece of cake in this context. Side-chaining is pretty trivial too. True modularity will take some thought, but it is also possible.

Note that when using instruments in this way, there are not really plugins at all. They are merely Csound orchestras that contain a number of instrument definitions. So they are pretty light weight in comparison to a proper VST plugin.

This is a pretty good introduction to Csound:
https://csound-floss.firebaseapp.com/
And I have some video on YouTube about using Cabbage to create some simple plugins.

Finally, we are working on better packing of CsoundUnity at the moment. What OS are you on? I can try to get you a working package of the new beta version rather than the current version.

Btw, reimplementing all those effects and synths you demo in you vimeo clip will be quite a lot of work. You might start by looking through the Cabbage examples to see which instruments you can use straight out of the box.

1 Like

It seems a wonderful project, the visuals are stunning!
I’m very curious to understand how you created those effects: did you use the Shader Graph or write your own shaders?
I also had in mind to create something like this for VR.
So happy to help!

1 Like

Hi Rory,

That’s fantastic news! The fact that it can all run directly from unity like that AND be light-weight is a huge relief.

Thank you for the tips and the Csound introduction link, I’ll be reading and testing all this out over the next week using the Cabbage examples.

Yep sign me up for the latest CsoundUnity package. I’m on Windows10.

Hi Giovanni,
Thanks! A lot of the visuals make extensive use of the new VFX graph on Unity which conveniently was released at the same time I started this project. GPU particles + tangible sound is a match made in heaven.
I will certainly be in need of help with the scope of where I’d like to take this.

Sounds good!
You did a great work with the VFX graph, all the effects are impressive! :clap:

1 Like

I think it might be best to go through the Cabbage examples and see if you can get familiar with the code. If you find examples of things that will work in your project we can look at how best to implement them. :wink:

1 Like

My thoughts exactly. It looks, and sounds, really impressive.

1 Like

Hi again,

I’ve set up a new test project to try and get the SendScoreEvent working, but I’m already having complications and there aren’t a whole lot of examples using this particular method.

So, I’ve got the CSoundUnity script set up in my scene with Iain McCurdy’s example “oscbnk_synth.csd” linked to it. I then have a control script to trigger a SendScoreEvent to the relevant GameObject component. I’m using similar to the above:

public CsoundUnity csu;
void OnMouseDown()
{
csu.SendScoreEvent("i\ “oscbnk_synth\ " 0 1 60”);
}

I’m not sure whether the Oscillator Bank Synth doesn’t take midi note numbers and instead wants frequency or (more likely) that it’s something wrong with the way I’m calling it. I’ve never seen string quotations within a string before.

Iain doesn’t name his instruments and some of his older instruments aren’t exactly set up for score events. Leave it with me, it’s lunch time here but I can get a working example back to you in a few hours.

if @giovannibedetti doesn’t beat me too it…

I tried this csd and parsing fails on Unity at line 1015. So the orchestra is not compiling! Let me check if I can solve this. That’s the first thing. Your code should use instrument names or numbers, not the file name. At a quick look at the csd code you should call the instr 1 so something like this: csu.SendScoreEvent(“i1 0 1 60”); to have a note of instrument 1 that starts immediately, lasts 1 second, and has a p4 with value 60 (your midi note). I think it should be sufficient to change line 722 of the csd with:
inum = p4
instead of:
inum notnum
that reads a midi event note and stores in var inum
But first let’s check why it’s not compiling!
EDIT:
the problem seems to be the opcode ‘exciter’ if I comment lines from 1019 to 1024 the csd compiles.

aha! okay am I looking at line 144: ‘massign 0,1’ to figure out the instrument number?

Okay I’ll try your tips on replacing line 722 and commenting the exciter. Will get back to you.

Whoops, on Windows you should copy the Exciter dll from the system Csound to the unity one…

Ok I’ll add the dll in the package. Btw midi should work in Unity so there’s no need to change instr 1.
Am I wrong? I never worried about this since I have no midi keyboard with me :sweat_smile:

The massign makes no sense in this context. Nor do any of the midi specific opcodes…

yes this assigns the midi channel 0 to instr 1
so instr 1 is receiving midi from channel 0!

Because SendScoreEvent doesn’t use midi at all? I was trying to find where in the code this instrument gets assigned to: 1 so I know in future where to look for naming.

I’ve done the edits and it does now compile (although the line 722 edit has made the synth sound like thunder, no matter i’m just looking for a trigger at this point).

When I check the log after playing I’m getting an: “Invalid ftable no. 0.000000”

No SendScoreEvent is meant to act like the csound score, so to activate/deactivate instruments with a specific syntax: this doc is an interesting read.

Yes I also get the invalid ftable message, later I’ll check what can be done to make it work.
I suggest you to start with some simpler csd that doesn’t require midi for now.

Sorry, my lunch was epic! Just looking into this now. Give me a sec…

1 Like

So a few things here. As you know, the exciter.dll lib was not in the Unity runtime folder along with csound64.dll. You might also have postted that putting it there didn’t fix the error. I think this is because we are not setting the plugins dir properly. I’ll take a look.

I’ve attached an updated .csd file that should work.

oscbnk_synth.csd (214.9 KB)

I’m triggering a note in a mouseDown event:

private void OnMouseDown()
{
    csoundUnity.SendScoreEvent("i2 0 3 60 80");
}

Note that the invalid ftable was because Iain uses a combobox to let the user select waveform. CsoundUnity editor doesn’t support comboboxes so this value wasn’t being sent to Csound, hence the init time error. I swapped it out for a slider. Also not that Iain was using some funky mechanism to achieve legato play, we don’t have that here. Just straight up note on for a fixed duration. Of course this can be changed in time.