# CsoundUnity: Where is the read output from csound actually passed to Unity's AudioSource?

**URL:** <https://forum.csound.com/t/csoundunity-where-is-the-read-output-from-csound-actually-passed-to-unitys-audiosource/725>\
**Category:** General\
**Created:** [March 26, 2022, 9:15pm UTC](https://forum.csound.com/t/csoundunity-where-is-the-read-output-from-csound-actually-passed-to-unitys-audiosource/725 "2022-03-26T21:15:49Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![DespairBear](https://forum.csound.com/user_avatar/forum.csound.com/despairbear/32/88_2.png) [@DespairBear](https://forum.csound.com/u/DespairBear)\
**Post date:** [March 26, 2022, 9:15pm UTC](https://forum.csound.com/t/csoundunity-where-is-the-read-output-from-csound-actually-passed-to-unitys-audiosource/725/1 "2022-03-26T21:15:49Z")

</div>

Hey all,

Doing some mucking around with optimization in Unity. Trying to figure out where the output from csound is actually passed to Unity’s audio engine. Was trying to figure out if there’s a way to apply Unity’s “Preserve Sample Rate” setting that they have for audio sample files to csound, so it doesn’t automatically upsample in the case of mismatch. Still delving into Unity and how it works, so this may not be possible, but looking into it

---

<div class="post-metadata">

**Author:** ![g\_b](https://forum.csound.com/user_avatar/forum.csound.com/g_b/32/208_2.png) [@g\_b](https://forum.csound.com/u/g_b)\
**Post date:** [March 26, 2022, 10:59pm UTC](https://forum.csound.com/t/csoundunity-where-is-the-read-output-from-csound-actually-passed-to-unitys-audiosource/725/2 "2022-03-26T22:59:38Z")

</div>

Hi @DespairBear,

Welcome back!  
This is the line where we grab the Csound output.

> <https://github.com/rorywalsh/CsoundUnity/blob/e1748a6aee94e7aad57397ee77e93e33c4f5e079/Runtime/CsoundUnity.cs#L1459>

---

<div class="post-metadata">

**Author:** ![DespairBear](https://forum.csound.com/user_avatar/forum.csound.com/despairbear/32/88_2.png) [@DespairBear](https://forum.csound.com/u/DespairBear)\
**Post date:** [March 26, 2022, 11:06pm UTC](https://forum.csound.com/t/csoundunity-where-is-the-read-output-from-csound-actually-passed-to-unitys-audiosource/725/3 "2022-03-26T23:06:33Z")

</div>

Thanks, good to be back 😛

Ah okay, so is the samples array somehow linked in to the AudioSource? So setting the samples array will then be fetched/outputted by the AudioSource?

---

<div class="post-metadata">

**Author:** ![g\_b](https://forum.csound.com/user_avatar/forum.csound.com/g_b/32/208_2.png) [@g\_b](https://forum.csound.com/u/g_b)\
**Post date:** [March 26, 2022, 11:56pm UTC](https://forum.csound.com/t/csoundunity-where-is-the-read-output-from-csound-actually-passed-to-unitys-audiosource/725/4 "2022-03-26T23:56:51Z")

</div>

Yes everything works thanks to the OnAudioFilterRead callback, that is used to read the AudioSource samples and pass them to Csound (if an AudioClip is provided, and processAudioClip is true) and then fill the AudioSource samples with the output from Csound.

---

<div class="post-metadata">

**Author:** ![DespairBear](https://forum.csound.com/user_avatar/forum.csound.com/despairbear/32/88_2.png) [@DespairBear](https://forum.csound.com/u/DespairBear)\
**Post date:** [March 27, 2022, 1:18am UTC](https://forum.csound.com/t/csoundunity-where-is-the-read-output-from-csound-actually-passed-to-unitys-audiosource/725/5 "2022-03-27T01:18:45Z")

</div>

Ahh okay, that makes sense. I wonder if there is a way to prevent Unity’s default upsampling. OnAudioFilterRead is called based on Unity’s AudioSettings sample rate correct? I wonder if there’s a way to force synchronization with csounds sample rate on a single object or if that is currently impossible. My initial thought is to have some sort of check on each OnAudioFilterRead where frames not within the csounds instance sample rate are essentially ignored. I don’t know if OnAudioFilterRead would be stable enough for an operation like this though, if it’s linked to some constant in a manner similar to FixedUpdate or if there’s fluctuations that might make such an approach unreliable. The documentation on how it actually functions seems sparse

Having a generic low, medium and high setting for csounds with predetermined ksmps and sr values to save DSP CPU for sounds that might be more in the background, where it wouldn’t matter as much if they have a very sample rate would be extremely handy.

---

<div class="post-metadata">

**Author:** ![DespairBear](https://forum.csound.com/user_avatar/forum.csound.com/despairbear/32/88_2.png) [@DespairBear](https://forum.csound.com/u/DespairBear)\
**Post date:** [March 27, 2022, 4:51am UTC](https://forum.csound.com/t/csoundunity-where-is-the-read-output-from-csound-actually-passed-to-unitys-audiosource/725/6 "2022-03-27T04:51:20Z")

</div>

Realized kind of a janky solution that seems to do the trick

If your csound instance is set to run at a sample rate lower than that of your Unity project, you can just have a quick check of the csound instance’s sample rate and the projects and then set the pitch of the AudioSource object. So 10K sample rate csound instance, 44.1K sample rate Unity project, AudioSource pitch set to 0.2268

The instance I was running, differences in performance:  
10K sr Unity Project, 10K sr csound, Audio Source Pitch 1.0: peak~2.4% consumption  
44.1K sr Unity Project, 10K sr csound, Audio Source Pitch 1.0: peak~9.1% consumption  
44.1K sr Unity Project, 10K sr csound, AudioSource Pitch 0.2: Peak~2.4% consumption

So this seems to solve the issue without any complicated solutions. Obviously if there was a way to forcibly set OnAudioFilterRead’s sample rate per AudioSource instance, that would be ideal for long term stability purposes, but this seems to do the trick well enough with current versions of Unity

---

<div class="post-metadata">

**Author:** ![g\_b](https://forum.csound.com/user_avatar/forum.csound.com/g_b/32/208_2.png) [@g\_b](https://forum.csound.com/u/g_b)\
**Post date:** [March 27, 2022, 11:02am UTC](https://forum.csound.com/t/csoundunity-where-is-the-read-output-from-csound-actually-passed-to-unitys-audiosource/725/7 "2022-03-27T11:02:29Z")

</div>

This is very interesting, thanks for reporting.  
I never tried messing with the pitch of the CsoundUnity’s AudioSource, this is really smart!  
Yes the ideal solution would be having more control on how the OnAudioFilterRead callback works (and a more detailed documentation).  
I will add this trick to the CsoundUnity docs (and add you to the contributors if you want), many thanks!!

---

<div class="post-metadata">

**Author:** ![DespairBear](https://forum.csound.com/user_avatar/forum.csound.com/despairbear/32/88_2.png) [@DespairBear](https://forum.csound.com/u/DespairBear)\
**Post date:** [March 28, 2022, 6:06am UTC](https://forum.csound.com/t/csoundunity-where-is-the-read-output-from-csound-actually-passed-to-unitys-audiosource/725/8 "2022-03-28T06:06:55Z")

</div>

Oh awesome, yeah absolutely, glad to be of service 😃

I modified the code so I can easily set the sample rate in inspector right on the Csound Unity script. I could push up a PR if you wanted to scope if it’s something you may want to include in the package.

---

<div class="post-metadata">

**Author:** ![g\_b](https://forum.csound.com/user_avatar/forum.csound.com/g_b/32/208_2.png) [@g\_b](https://forum.csound.com/u/g_b)\
**Post date:** [March 28, 2022, 8:44am UTC](https://forum.csound.com/t/csoundunity-where-is-the-read-output-from-csound-actually-passed-to-unitys-audiosource/725/9 "2022-03-28T08:44:19Z")

</div>

Yes please!  
I tried messing with the AudioSource’s pitch and I didn’t hear / see any change, so yes a pull req would be great 😉  
Be aware that I pushed a new version yesterday!

---

<div class="post-metadata">

**Author:** ![DespairBear](https://forum.csound.com/user_avatar/forum.csound.com/despairbear/32/88_2.png) [@DespairBear](https://forum.csound.com/u/DespairBear)\
**Post date:** [March 29, 2022, 1:09am UTC](https://forum.csound.com/t/csoundunity-where-is-the-read-output-from-csound-actually-passed-to-unitys-audiosource/725/10 "2022-03-29T01:09:47Z")

</div>

Sweet, I’ll get that in this evening 👍

---

<div class="post-metadata">

**Author:** ![DespairBear](https://forum.csound.com/user_avatar/forum.csound.com/despairbear/32/88_2.png) [@DespairBear](https://forum.csound.com/u/DespairBear)\
**Post date:** [March 29, 2022, 3:33am UTC](https://forum.csound.com/t/csoundunity-where-is-the-read-output-from-csound-actually-passed-to-unitys-audiosource/725/11 "2022-03-29T03:33:57Z")

</div>

Actually make it tomorrow, realized I need to update and test CsoundUnityChild to work with this as well 😛

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [March 30, 2022, 8:24am UTC](https://forum.csound.com/t/csoundunity-where-is-the-read-output-from-csound-actually-passed-to-unitys-audiosource/725/13 "2022-03-30T08:24:46Z")

</div>

You need only fork the current project. Clone the fork to your PC, and checkout the branch you wish to make the pull request to. Make your changes. Push your changes to your fork. Then from your fork’s github page, you can make a pull request. You don’t need any permissions to do this.

---

<div class="post-metadata">

**Author:** ![DespairBear](https://forum.csound.com/user_avatar/forum.csound.com/despairbear/32/88_2.png) [@DespairBear](https://forum.csound.com/u/DespairBear)\
**Post date:** [March 30, 2022, 10:02am UTC](https://forum.csound.com/t/csoundunity-where-is-the-read-output-from-csound-actually-passed-to-unitys-audiosource/725/14 "2022-03-30T10:02:41Z")

</div>

Sweet thanks, put in a PR for an initial review 👍

---

<div class="post-metadata">

**Author:** ![g\_b](https://forum.csound.com/user_avatar/forum.csound.com/g_b/32/208_2.png) [@g\_b](https://forum.csound.com/u/g_b)\
**Post date:** [March 30, 2022, 10:49am UTC](https://forum.csound.com/t/csoundunity-where-is-the-read-output-from-csound-actually-passed-to-unitys-audiosource/725/15 "2022-03-30T10:49:35Z")

</div>

Thanks!  
Will review later this evening, but it looks good!

---

<div class="post-metadata">

**Author:** ![DespairBear](https://forum.csound.com/user_avatar/forum.csound.com/despairbear/32/88_2.png) [@DespairBear](https://forum.csound.com/u/DespairBear)\
**Post date:** [March 31, 2022, 4:50am UTC](https://forum.csound.com/t/csoundunity-where-is-the-read-output-from-csound-actually-passed-to-unitys-audiosource/725/16 "2022-03-31T04:50:26Z")

</div>

So got another question. Where is the AudioSource actually linked to csound’s `out` opcode?  
Does this happen somewhere in CsoundUnityBridge? Or is this somewhere in the precompiled DLL?

---

<div class="post-metadata">

**Author:** ![g\_b](https://forum.csound.com/user_avatar/forum.csound.com/g_b/32/208_2.png) [@g\_b](https://forum.csound.com/u/g_b)\
**Post date:** [March 31, 2022, 8:31am UTC](https://forum.csound.com/t/csoundunity-where-is-the-read-output-from-csound-actually-passed-to-unitys-audiosource/725/17 "2022-03-31T08:31:05Z")

</div>

There is no link, the destination of the out opcode depends on the Csound options.  
In this case we grab that output with the API (GetOutputSample).  
Sorry yesterday I didn’t have time to look at the pull request!

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [March 31, 2022, 8:37am UTC](https://forum.csound.com/t/csoundunity-where-is-the-read-output-from-csound-actually-passed-to-unitys-audiosource/725/18 "2022-03-31T08:37:00Z")

</div>

[https://github.com/rorywalsh/CsoundUnity/blob/master/Runtime/CsoundUnity.cs#L866](https://github.com/rorywalsh/CsoundUnity/blob/master/Runtime/CsoundUnity.cs#L866)

There are also other methods for grabbing a single sample.

---

<div class="post-metadata">

**Author:** ![DespairBear](https://forum.csound.com/user_avatar/forum.csound.com/despairbear/32/88_2.png) [@DespairBear](https://forum.csound.com/u/DespairBear)\
**Post date:** [March 31, 2022, 9:44am UTC](https://forum.csound.com/t/csoundunity-where-is-the-read-output-from-csound-actually-passed-to-unitys-audiosource/725/19 "2022-03-31T09:44:35Z")

</div>

Ah okay, thanks for clarifying, finally clicked. One final question, is there a maximum number of output channels that csounds supports in a single instance? I see up to 8 in the documentation, but I’m assuming there’s a way it could write to more than that

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [March 31, 2022, 10:00am UTC](https://forum.csound.com/t/csoundunity-where-is-the-read-output-from-csound-actually-passed-to-unitys-audiosource/725/20 "2022-03-31T10:00:49Z")

</div>

I don’t think there is a maximum. Just set nchnls to whatever you like.

---

<div class="post-metadata">

**Author:** ![g\_b](https://forum.csound.com/user_avatar/forum.csound.com/g_b/32/208_2.png) [@g\_b](https://forum.csound.com/u/g_b)\
**Post date:** [March 31, 2022, 10:06am UTC](https://forum.csound.com/t/csoundunity-where-is-the-read-output-from-csound-actually-passed-to-unitys-audiosource/725/21 "2022-03-31T10:06:24Z")

</div>

I’m not sure, maybe @rory has more info on this.  
For sure you can use chnset to output as many aux channels you need.  
In CsoundUnity we’re limited by Unity of course, in the ProjectSettings/Audio you can go up to Surround 7.1. But the availability of this channel setup depends on the target platform.

[Next page](https://forum.csound.com/t/csoundunity-where-is-the-read-output-from-csound-actually-passed-to-unitys-audiosource/725.md?page=2)
