Integrating the Switchboard SDK Into a Unity Project
Switchboard SDK is implemented in C++. To be able to use it from Unity, which is written in C#, you have to use Native Plugins. It allows for calling into C++ code from C#, through a C interface.
- Write your Switchboard audio engine in C++.
- Create a C interface for it.
- Compile it as a library. Add the library to the
Assets/Plugins
directory of your Unity project. - Add a C# script that imports the library and calls the C interface.
- Integrate the C# script into your Unity app.
This way the audio processing is completely detached from the Unity audio system, making it possible to create a low-latency, real-time audio pipeline for your purposes.
Write the Audio Engine in C++ with a C Interface
Download the Switchboard SDK for your required platform. Create a new C++ project and implement your audio engine using the Switchboard SDK. Here is an example of how to initialize the SDK and create a simple audio graph with Switchboard SDK in C++ with a C interface on macOS:
- C++
#include "AudioEngine.hpp"
#include "AudioGraph.hpp"
#include "AudioGraphFactory.hpp"
const std::string json = R"(
{
"title": "Audio Engine",
"nodes": [
{
"id": "gain",
"type": "Gain"
}
],
"connections": [
{
"sourceNode": "inputNode",
"destinationNode": "gain"
},
{
"sourceNode": "gain",
"destinationNode": "outputNode"
}
]
})";
extern "C" {
static AudioGraph* audioGraph { AudioGraphFactory::parseJSON(json) }
static AudioEngine audioEngine { AudioIO::Core };
void initializeSwitchboardSDK() {
SwitchboardSDK::initialize("Your app ID", "Your app secret");
}
void start() {
audioEngine.start(&audioGraph);
}
void stop() {
audioEngine.stop();
}
}
Compile the C++ Code as a Dynamic Library
Compile your C++ code into a static or dynamic library (e.g., .dll
for Windows, .dylib
for macOS, .so
for Linux). Make sure to export the C interface functions.
Use the Native Plugin in Unity
- Place the compiled dynamic library and the C interface header in the
Assets/Plugins
directory of your Unity project. - Create a C# script to call the native functions. Here is an example with SwitchboardAudioEngine as the library name:
- C#
using System.Runtime.InteropServices;
using UnityEngine;
public class SwitchboardAudioEnginePlugin : MonoBehaviour
{
[DllImport("SwitchboardAudioEngine")]
private static extern void initializeSwitchboardSDK();
[DllImport("SwitchboardAudioEngine")]
private static extern void start();
[DllImport("SwitchboardAudioEngine")]
private static extern void stop();
void Initialize()
{
initializeSwitchboardSDK();
}
void Start()
{
start();
}
void Stop()
{
stop();
}
// ...other Unity code...
}
- Attach the C# script to a GameObject in your Unity scene.
By following these steps, you can integrate the Switchboard SDK into your Unity project using a C++ audio engine and a Native Plugin.