Cabbage Logo
Back to Cabbage Site

Csound wrapper/plugin for Unreal Engine

I couldn’t see it or really find any info on examples (which is SO frustrating @Juce game engine when), which bit is the bad-bad perf thread there, like the processNextBlock() function?

Can you try again :laughing: What do you mean exactly?

1 Like

hahahah sorry i’m losing it over here, which part of the code I sent was using my own performance thread?

Well not yours, but when you compile Csound you start a CsoundPerformanceThread object. I have some meeting tomorrow morning, but will try to create a simple example for you. Btw, you’re on Windows right?

No rush at all thanks so much for taking the time to help out! God I thought CsoundPerformanceThread was the norm ahhaha I’ve used them every time I’ve used the API you have me worried now!
I’m on Ubuntu at the moment for Unreal

The Csound thread object is only needed when you’re not working with a framework that can’t handle its own audio. If a framework can handle audio, then it’s much simpler to call performKsmps() directly in its own processing methods.

Good to know! I’ll try this on Windows, but the code should be the same for any platform. It will probably take me longer to set up a simple scene using blueprints than it will to code the Csound stuff :see_no_evil:

1 Like

Just downloading and installing now. This could take a while, I’m currently about 100 metres from my wifi router :see_no_evil:

11GBs! It will be lunch time before I’m ready to go with this :frowning:

Hi @Syl_Morrison, it’s finally installed! Now how do I link to Csound in my new class? It seems that I have to edit the build.cs file, can I see how you did this? It might save me some time…

Ok, I think I’m set up now. I have it linking to Csound. But I have to leave it now, I won’t be able to return to it till after 15.00. On the other hand, I can see now how this should be done and it shouldn’t take more than a few moments to get it going, so long as it’s actually linking correctly to Csound…

Sorry, just awake, late one last night! yeah it’s insane, takes up 100gb on my ubuntu machine lol, I assume you figured it out but the linking is
PublicAdditionalLibraries.Add("/usr/local/lib/libcsound64.so");
or whatever your csound dylib is called and wherever it is!

You’re still on student time I see :laughing:

So it turns out that Unreal also uses an UNLIKELY macro. This could get messy. It’s all over the Csound code base…

Did you not hit any problems with that?

I can add a VS ignore for those issues, but for some reason it’s still not finding the csound header files. Hmmm…

Here’s my horrific workaround:

#pragma once

#ifdef LIKELY
#undef LIKELY
#undef UNLIKELY
#endif
THIRD_PARTY_INCLUDES_START
#include “/usr/local/include/csound/csound.hpp”
#include “/usr/local/include/csound/csPerfThread.hpp”
#include “/usr/local/include/csound/csdl.h”
THIRD_PARTY_INCLUDES_END
#include
#ifdef LIKELY
#undef LIKELY
#undef UNLIKELY
#endif
#ifndef LIKELY /* Hints compiler that expression is likely to be true, much softer than UE_ASSUME - allows (penalized by worse performance) expression to be false */
#if ( defined(clang) || defined(GNUC) ) && (PLATFORM_UNIX) // effect of these on non-Linux platform has not been analyzed as of 2016-03-21
#define LIKELY(x) __builtin_expect(!!(x), 1)
#else
#define LIKELY(x) (x)
#endif
#endif

#ifndef UNLIKELY /* Hints compiler that expression is unlikely to be true, allows (penalized by worse performance) expression to be true */
#if ( defined(clang) || defined(GNUC) ) && (PLATFORM_UNIX) // effect of these on non-Linux platform has not been analyzed as of 2016-03-21
#define UNLIKELY(x) __builtin_expect(!!(x), 0)
#else
#define UNLIKELY(x) (x)
#endif
#endif
#include “CoreMinimal.h”

Not very elegant but it works for me hahahah, literally just allows csound to use its definition then redefines it with Unreal’s definition

I think ignoring the warnings should be Ok. I’m up and running now. Just need to run this in Unreal and see that the linking is Ok. Then I’ll add some Csound magic…

That’s wonderful guys! Keep on!
Waiting for some magic :wink:

Ok. This was pretty trivial in the end. Here’s what I did:

  1. Create a new FPS project in Unreal.
  2. Create a New C++ class SynthComponent.
  3. Edit the newly generated class so that it adds the Csound headers.
  4. Then apply a little Csound magic:
#include "SimpleCsoundTest.h"

bool USimpleCsoundTest::Init(int32& SampleRate)
{
	NumChannels = 1;

	csound.reset(new Csound());
	csCompileResult = csound->Compile("C:\\Users\\rory\\sourcecode\\UE4Projects\\CsoundTest\\test.csd");
	if (csCompileResult == 0) // compiled OK...
	{
		CSspout = csound->GetSpout();
		CSspin = csound->GetSpin();
	}


	return true;
}

int32 USimpleCsoundTest::OnGenerateAudio(float* OutAudio, int32 NumSamples)
{
	for (int32 Sample = 0; Sample < NumSamples; ++Sample, ksmpsIndex++)
	{
		if (ksmpsIndex == csound->GetKsmps())
		{
			csCompileResult = csound->PerformKsmps();
			if(csCompileResult == 0)
				ksmpsIndex = 0;
		}

		OutAudio[Sample] = CSspout[ksmpsIndex];
	}
	return NumSamples;
}

Here are the source files. Source.zip (4.1 KB)
This is the simple test csd I used. test.csd (357 Bytes) .

Note it only works with in mono for now. If you want to support stereo you will need to update the OnGenerateAudio() method. Csound use interleaved audio, so what out for that. I’m sure Unreal does too. That means if things are in stereo, each pair of samples represent left and right channels.

This is not a plugin, but should be enough to get you started in the right way. It really is very simple to use the Csound API. If you find yourself struggling with it, you’re probably doing something wrong :laughing: Ask here before it gets too late!!

btw, is this part of your final year work for college? This is your last year right?

@giovannibedetti I haven’t got around to our CsoundUnity issue yet, but will do so shortly…

1 Like

nevermind, I found the problem about tables :wink:
BTW Nice to see this is working!! and pretty similar to Unity!

Great. I was just about to jump on that.

You’re an actual saint thank you so so much will try this now!
It’s not, I ended up not finishing my 4th year (still got the level 7 from 3rd year) because I got kicked out of my house in Dublin, so decided to go into dev full time, I’m actually trying to learn unreal for a job I’m doing to port our codebase in and have interactive UI elements sending stuff to our backend which eventually gets sent to a mega CSD!

I’m sorry guys, I’m just realizing how far this thread has moved and that I was mentioned in it. I could have helped since I’ve been using Csound with UE4 for a while. I do have a very messy implementation (not intended for release but for personal use) that works also on Android (tested with Oculus Quest) and recently I added a way for routing multichannel audio from a Csound USynthComponent to receiver actors in the scene. So far I’ve tested it with 8 virtual speakers placed in a scene and works great (PC). Let me know if I can help with anything but it might be better to email me directly info@hcenteno.net (It’s hard to track the large amount of notifications from different forums/lists/FaceBook groups etc. that I get).

1 Like