Cabbage Logo
Back to Cabbage Site

Timing and MIDI in CsoundUnity

I’m trying out CsoundUnity using a csd that reads a midi file. I had to put a complete pathname after the -F flag as it didn’t find the midi file in the Assets/Csound files, but that’s not my question.

When I try running the scene, the sound seems weirdly stretched out and distorted. I printed some time information to the console using the times opcode, and between the time it printed out 0 seconds and it printed out 11 seconds, 36 seconds had actually elapsed according to my stopwatch. Currently the csd has no sr or kr specified, but I tried specifying sr = 44100 and sr = 48000 and the sound changed but was still incorrect.

I’m wondering if I don’t understand something about how Unity works with Csound/Audio Source component or if reading a midi file is not recommended. I’ve been trying this out on a couple of Macs (10.13.4) running Unity 5.4.0f3. Thanks for any help!

Bill Alves

Hi Bill. I’ve never used midi files with csound in unity. I’m not sure what might be causing this issue. Have you set unity’s audio setting to the highest priority, iirc, there are some settings that you can tweak to get better performance. Perhaps this helps?

Thanks for getting back to me, Rory! Here’s an update: I converted the MIDI file to Csound sco and confirmed that it plays back correctly outside of unity. However, the behavior remains, so now I know that the MIDI is not the problem. I tried setting the audio source to highest priority and attaching it to different game objects with no effect. I have commented out references to it in my object script, so unity should not be communicating with Csound channels.

Another update: I discovered that the playback speed depends on kr. The sound is still distorted, but if ksmps is 25, I’m getting close to the correct playback timing. If ksmps is 100, it plays back 4 times faster. In the score I have t 0 108, which plays back correctly outside of unity. Thanks again!

Do you mean to say that general score events are not firing at the correct time? I’m surprised I haven’t ever noticed this, but I have always sent real time events rather than use a score. But if the score is not working correctly then I need to dig into this, as that’s not a great scenario for users!

Hi Rory, yes, it seems like the timing of the score events depends on kr. I have settled on ksmps=32 to get the timing right (and I saw you have a kmps variable set to that in CsoundUnity.cs), but the output is still distorted. I have tried playing with Csound -b and -B, but I don’t know how they relate to the Unity audio buffers. I can send you the csd, but it’s nothing special.

Please send on the .csd file. I’m still away for the next few days, but will try it out when I get back to a PC with Unity on it.

I’ve emailed it to the ***** address. Thanks again!

Thanks Bill. I’ll check it out when I get a chance.

Hi Bill. I managed to install Unity on a PC here and did some tests. I think the distortion is being caused by the fact that Csound seems to set nchnls to 1 as the default setting. This was leading to garbage in the right speaker. I think the Csound audio interface wrappers are clever enough to discard this, but mine are not :joy: The simple fix here is to set nchnls = 2 in your orc header.

I also had a look into the score issue, but couldn’t find anything wrong with it. It must have something to do with the t statement in the score? When I create a simple score without any t statement it seems to work fine. For example, the following .csd plays back 4 hits of your sound file every second. The timing seems fine to me.

PlayAudio.csd (464 Bytes)

You’ll see this .csd accesses the sound file from a directory in your Assets folder. You can then access the audio doing something like this:

SAudio chnget "AudioPath"
SFilename sprintf "%s/glock1401.wav", SAudio 

I’m not sure where you were putting your audio files, but putting them in a directory like this lets you quickly play them back using Unity’s audio file player. This can be handy for quickly auditioning a sound file.

Let me know how you get on.