Cabbage Logo
Back to Cabbage Site

CsoundUnity for beat matching

Hi Rory,

Thank you so much! This now works perfectly when I use the default my Macbook build-in speaker as output device, but there are couple other questions or problems might need your assistance.

  1. I have a Focusrite Scarlett 2i2 audio interface (Connect to my monitor speaker), I have to use this audio interface in order to output audio with my wonderful monitor speaker. But when I switch the output device to Scarlett 2i2 instead of Macbook build-in speaker, this tempo/pitch goes faster and higher than the original, I have to set the pitch of CsoundUnity audio source to around ‘0.945’ in order to play with the original speed and pitch of the track.

  2. .wav file is too big for building a game, is that possible to use temposcal opcode in CsoundUnity to process .ogg audio file? Which is way more smaller than the original .wav files.

Thanks!

I’m not sure what could be causing this. Can you try a plain old AudioSource and clip and see if there are any pitch differences when you change sound card? [quote=“Tao_Yao, post:8, topic:553”]
.wav file is too big for building a game, is that possible to use temposcal opcode in CsoundUnity to process .ogg audio file? Which is way more smaller than the original .wav files.
[/quote]

I think it should be possible. Csound uses libsndfile to load sound files from disk, if it’s supported by libsndfile, then it’s supported in Csound.

[edit] just checked, .ogg files should be fine.

Hi Rory,

Thanks, I tried .ogg file and it works. But after I put the first .ogg file in the folder and play it, the pitches and speed of the AudioSource again goes higher even I switched to .wav file later (I’m using the Macbook speaker output now and before I put .ogg file in my project and script the pitch to play the normal speed and pitch was ‘1’). So I believe it’s the same bug occurs and haven’t been fixed completely yet.

You can also briefly tell me a little bit about the bug so that I can also try to modify CsoundUnity.cs and fix it? If I fix it I’ll send you the fixed version.

But if you can help me try it out with .ogg file in the same folder with .wav it would great.

The issue was that I was incrementing the ksmps index by the number of channels, rather than one. It’s fixed now:


I can’t see why using a .ogg would be any difference to a wave file. I’ll take a look.

I’m not seeing any problems here, .ogg and .wav files play back at the correct speed for me. I’m testing with this little instrument:

<Cabbage>
form caption("Test"), size(300, 200)
</Cabbage>
<CsoundSynthesizer>
<CsOptions>
-n -d -m0d
</CsOptions>
<CsInstruments>
sr 	= 	48000 
ksmps 	= 	32
nchnls 	= 	2
0dbfs	=	1 

instr TEMPOSCALE
SPath chnget "AudioPath"
SFilename sprintf "%s/pianoSample.wav", SPath
a1, a2 diskin2 SFilename, 1, 0, 1
outs a1, a2*0
endin  

</CsInstruments>
<CsScore>
i"TEMPOSCALE" 0 [3600*12]
</CsScore>
</CsoundSynthesizer>

I can swap the .wav file for an .ogg file and hear no difference in playback pitch.

Hi Rory,

Thanks for telling me the issue, I will do some research on that.

I can swap between .wav and .ogg file and they are in the same speed and pitch too, but the problem now is that both .wav and .ogg files have to be adjusted the pitch of AudioSource component to ‘0.92’ in order to playback the original pitch and speed (like play directly through the quicktime player or a plain AudioSource with the default pitch ‘1.0’ ). Is that also happens to you? Or you mean you can play the original pitch and speed with CsoundUnity AudioSource pitch ‘1.0’?

Good news is my Scarlett Audio Interface have no problem anymore!

Thanks again for your time and patience:)

Csound_Time_Stretch.csd (1.8 KB)
Here’s the updated version of my .csd file used for my Unity project, I update it a little bit, if you have no problem with it, probably it’s because there’s something wrong with my Csound file?


You can change the path and put an audio file in your computer here, I used SetChannel() function to trigger the Csound from Untiy script instead of using area.

I’m afraid I won’t have time to look into this right now, but I can confirm that a CsoundUnity streaming sound file plays back the same pitch for me as an AudioSource/Clip combination reading the same file. I might get a chance to look into this again tomorrow. FWIW, try the simple example I posted and see if things work as you would expect when you are not using the temposcal opcode. If they do, then we can assume that there is a) an issue with your code, or b) an issue with temposcal.

One interesting happens, I believe the main reason is sample rate in Csound.

When I use the example you gave me with ‘diskin2’ opcode, your original sample rate was set to 48000 and it works perfect with AudioSource pitch to 1, but when I set that to 44100, it behave just like my .csd file that you have to adjust that pitch to ‘0.92’ in Unity to play the normal pitch.

But when I changed the sample rate in my Csound file (with ‘tempscal’ opcode) to 48000 (originally was 44100), nothing happens and you still need to set pitch to ‘0.92’, and I even tried to change the my Csound sample rate to ‘32Hz’ and it’s still the same, when I set sample rate below 32 Hz in my Csound file it stopped playing. Which is really weird.

I believe either tempscal, ftgentmp or GEN01 (coz I used GEN01 to pass my audio file to tempscal) ignore or have an error with the sample rate we set in the header of Csound file.

But anyway, I would really appreciate if you can help me try this out tomorrow. I’ll also keep trying this until the problem has been resolved.

I think there’s nothing to do with your CsoundUnity API but Csound itself. Maybe I should try some other opcode and stop using GEN01 and see if that will work.

Hi Rory,

Don’t worry I’ve fixed the issue, there’s a severe problem with Unity compatibility in Unix operating system, I think that’s one of the reason why so many people hate Mac, haha

I think apple still haven’t realize this bug yet and I hope they can fix it in the next update, I’ll report this bug to one of my friend who works in apple as an engineer.

Let’s keep in touch and I’ll let you know if I found some good ideas or bugs about CsoundUnity!

-Tao

I’m glad you got it sorted, but I’m none the wiser as to what was causing the issue? Can you give any more details in case it comes up again for others?

I was wrong, it’s caused by the sample rate, if the sample rate of the sound file is 44.1kHz, then the sample rate setting in csound must be 48kHz, and the output device (of computer or audio interface) have to be 44.1kHz in order to play the original pitch.

btw, I found another really interesting thing, you can try to use my Csound file with that tempscal opcode in Unity.

If you use purely an Audio Source in Unity without Csound processing, the negative frequency will play the audio clip backwards as we all know, and pitch higher than +1 will play the audio faster.

But when I use CsoundUntiy Audio Source, the negative frequency is no longer playing the audio back wards, instead it will just pause the audio just like you set the pitch to ‘0’, but pitch higher than +1 will still make it play faster, can you try that sometime when you are free? I’m curious what is the main reason.

I won’t be able to try it until next week, but I’ll certainly let you know what I find out.

No problem, thanks! Have a great weekend.

Hi Rory,

Another problem I got and haven’t told you yet is since I’ve imported CsoundUnity to Unity and using my .csd file to process the audio in Unity, every time I modify and compile my C# script to Unity will cause Unity to crash and auto quit.

Also I can’t click the “PLay” button on my CsoundQT IDE to compile my csd file anymore since it will crash even Unity is not opening. If I use terminal on my Mac to compile Csound file it still works (csound -odac my_csound_file_path).

I also tried some of my other Unity projects that didn’t import CsoundUnity, and compile a script won’t cause the Unity program to crash.

Did anybody have these problems before?

Are you editing the .csd file while Unity is running the game? What is your C# script? Perhaps it’s causing something to go astray? [quote=“Tao_Yao, post:23, topic:553”]
Also I can’t click the “PLay” button on my CsoundQT IDE to compile my csd file anymore since it will crash even Unity is not opening. If I use terminal on my Mac to compile Csound file it still works (csound -odac my_csound_file_path).
[/quote]

I’m not sure what’s going on here. Both CsoundUnity and CsoundQT should be using the same version of Csound. One thing I have noted about Unity in the past, is that you must close it and reopen it whenever you use any other audio software. it seems to like to grab audio focus away from other apps. It catches me out on Windows all the time.[quote=“Tao_Yao, post:23, topic:553”]
I also tried some of my other Unity projects that didn’t import CsoundUnity, and compile a script won’t cause the Unity program to crash.

Did anybody have these problems before?
[/quote]

Oh I’ve written quite a few scripts in my time that could take down Unity. You’re not alone there :wink: You should check the Unity editor log. That should may give you more details to work with.

I don’t have any such issues. I’ve attached the file I tested with. It loads a sound file to a table, which the next instrument reads using a temposcal opcode. When I move my Pitch slider to the negative in my AudioSource inspector I get the expected change in playback direction. TemposcaleTest.csd (514 Bytes)

Hi Rory,

I think I found another bug of CsoundUnity.

Did you remember the relative path you told me before?, the “AudioPath” channel in Csonund stand for Assets/Audio and use that channel to store in a String variable so that you don’t have to type the directory from the root in your computer? Although it works in Unity, but when you build the project if you use “AudioPath” %s, the audio processed by CsoundUnity won’t be played anymore. The I switched back to using the directory from the root “/user/…/UnityProject/Assets…”, and build the project, the audio will play.

Is that also happens to you? Maybe you can try it out sometime.

Sorry for so many questions, haha, but I really think CsoundUnity will help a lot of game audio developers.

Do you mean it won’t play when you build your game?