Dear experienced developers,
I've spent many months with trying Resonance Audio and it almost drove me crazy. Please help me,
I need to finally solve this problem and use Resonance Audio in a school project.
Problem description
When I try to change 3d attributes of Resonance Audio Source DSP, the sound doesn’t seem to be afected at all. It seems that just
Used technologies
- CSharp
- Official FMOD wrapper
- FMOD version 1.10.16
- Resonance Audio plugin
Code
using System;
using System.Runtime.InteropServices;
using FMOD;
namespace FmodResonanceTest
{
internal class Program
{
private static FMOD.System _fmod;
private static Channel _channel;
private static DSP _listenerDSP;
private static uint _listenerHandle;
private static ChannelGroup _masterGroup;
private static uint _raHandle;
private static Sound _sound;
private static DSP _soundFieldDSP;
private static uint _soundFieldHandle;
private static DSP _sourceDSP;
private static uint _sourceHandle;
private static ChannelGroup _worldGroup;
private static byte[] GetBytes(IntPtr ptr, int length)
{
if (ptr != IntPtr.Zero)
{
byte[] byteArray = new byte[length];
Marshal.Copy(ptr, byteArray, 0, length);
return byteArray;
}
// Return an empty array if the pointer is null.
return new byte[1];
}
private static void Check(RESULT r, string activity)
{
if (r != RESULT.OK)
throw new InvalidOperationException(activity);
}
private static void Say(string s, bool wait = true)
{
Console.WriteLine(s);
if (wait)
Wait();
}
private static void Set3dParameters(DSP sourceDSP, DSP_PARAMETER_3DATTRIBUTES param3d)
{
int propertiesSize = Marshal.SizeOf(typeof(DSP_PARAMETER_3DATTRIBUTES));
IntPtr propertiesPtr = Marshal.AllocHGlobal(propertiesSize);
Marshal.StructureToPtr(param3d, propertiesPtr, false);
Check(sourceDSP.setParameterData(8, GetBytes(propertiesPtr,
propertiesSize)), "Sending 3d properties to source DSP");
Marshal.FreeHGlobal(propertiesPtr);
}
private static void TestSet3dParameters()
{
_sourceDSP.setParameterFloat(0, 24f);
DSP_PARAMETER_3DATTRIBUTES param3d = new DSP_PARAMETER_3DATTRIBUTES()
{
relative = new _3D_ATTRIBUTES()
{
position = new VECTOR()
{
x = 0f,
y = 0f,
z = 0f
},
velocity = new VECTOR()
{
x = 0f,
y = 0f,
z = 0f
},
forward = new VECTOR()
{
x = 0f,
y = 0f,
z = 0f
},
up = new VECTOR()
{
x = 0f,
y = 0f,
z = 0f
}
},
absolute = new _3D_ATTRIBUTES()
{
position = new VECTOR()
{
x = -5f,
y = -5f,
z = 0f
},
velocity = new VECTOR()
{
x = 0f,
y = 0f,
z = 0f
},
forward = new VECTOR()
{
x = 0f,
y = 0f,
z = 0f
},
up = new VECTOR()
{
x = 0f,
y = 0f,
z = 0f
}
}
};
Set3dParameters(_sourceDSP, param3d);
}
private static void TestSetRoomProperties()
{
// set the room properties
RoomProperties roomProperties = new RoomProperties()
{
// listener position
positionX = 0f,
positionY = 0f,
positionZ = 0f,
// room dimensions
dimensionsX = 4f,
dimensionsY = 5f,
dimensionsZ = 4f,
// listener orientation
rotationX = 0f,
rotationY = 0f,
rotationZ = 0f,
rotationW = 0f,
// surface materials of all sides of the room
materialLeft = SurfaceMaterial.ConcreteBlockPainted,
materialRight = SurfaceMaterial.ConcreteBlockPainted,
materialBottom = SurfaceMaterial.LinoleumOnConcrete,
materialTop = SurfaceMaterial.ConcreteBlockPainted,
materialFront = SurfaceMaterial.WoodPanel,
materialBack = SurfaceMaterial.GlassThin,
// reverb parameters
reflectivity = 0f,
reverbGainDb = 1f,
reverbBrightness = -1f,
reverbTime = .1f
};
UpdateAudioRoom(_listenerDSP, roomProperties);
}
private static void UpdateAudioRoom(FMOD.DSP listenerPlugin, RoomProperties roomProperties)
{
int roomPropertiesSize = Marshal.SizeOf(typeof(RoomProperties));
IntPtr roomPropertiesPtr = Marshal.AllocHGlobal(roomPropertiesSize);
Marshal.StructureToPtr(roomProperties, roomPropertiesPtr, false);
listenerPlugin.setParameterData(1, GetBytes(roomPropertiesPtr,
roomPropertiesSize));
Marshal.FreeHGlobal(roomPropertiesPtr);
}
private static void Wait()
{
Console.WriteLine("Press a key");
Console.ReadLine();
}
[STAThread]
private static void Main(string[] args)
{
// initialization
Factory.System_Create(out _fmod);
Check(_fmod.init(32, INITFLAGS.NORMAL, IntPtr.Zero), "FMOD initialization");
Check(_fmod.loadPlugin("resonanceaudio.dll", out _raHandle), "Loading Resonance Audio plugin");
Check(_fmod.getNestedPlugin(_raHandle, 0, out _listenerHandle), "Getting handle of Listener nested plugin");
Check(_fmod.getNestedPlugin(_raHandle, 1, out _soundFieldHandle), "Getting handle of Near Field nested plugin");
Check(_fmod.getNestedPlugin(_raHandle, 2, out _sourceHandle), "Getting handle of Source nested plugin");
Check(_fmod.createDSPByPlugin(_listenerHandle, out _listenerDSP), "Listener DSP creation");
Check(_fmod.createDSPByPlugin(_soundFieldHandle, out _soundFieldDSP), "Near Field DSP creation");
Check(_fmod.createDSPByPlugin(_sourceHandle, out _sourceDSP), "Source DSP creation");
Check(_fmod.createChannelGroup("world", out _worldGroup), "Creating channel group");
_fmod.getMasterChannelGroup(out _masterGroup);
Check(_masterGroup.addGroup(_worldGroup, true, out DSPConnection dc), "Adding new channel group to master group");
Check(_worldGroup.addDSP(CHANNELCONTROL_DSP_INDEX.TAIL, _listenerDSP), "Applying Listener DSP on channel group");
Check(_fmod.createSound("sound.wav", MODE.DEFAULT, out _sound), "Loading sound");
Check(_fmod.playSound(_sound, _worldGroup, true, out _channel), "Sound playback");
Check(_channel.addDSP(FMOD.CHANNELCONTROL_DSP_INDEX.TAIL, _soundFieldDSP), "Applying Near Field DSP on channel");
Check(_channel.addDSP(FMOD.CHANNELCONTROL_DSP_INDEX.TAIL, _sourceDSP), "Applying Source DSP on channel");
TestSetRoomProperties();
TestSet3dParameters();
// start playback
_channel.setPaused(false);
Wait();
_fmod.release();
}
}
}