# CsoundUnity Package (UPM) development

**URL:** <https://forum.cabbageaudio.com/t/csoundunity-package-upm-development/1916>\
**Category:** Csound for Games\
**Created:** [6 May 2020 14:57 UTC](https://forum.cabbageaudio.com/t/csoundunity-package-upm-development/1916 "2020-05-06T14:57:30Z")\
**Posts on this page:** 20\
**Page:** 23

<div class="post-metadata">

**Author:** ![giovannibedetti](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/giovannibedetti/32/4146_2.png) [@giovannibedetti](https://forum.cabbageaudio.com/u/giovannibedetti)\
**Post date:** [10 June 2020 14:15 UTC](https://forum.cabbageaudio.com/t/csoundunity-package-upm-development/1916/441 "2020-06-10T14:15:59Z")

</div>

Hope it’s not too much work! 😟  
Great you’re starting a new project with this version! Let me know how it goes!

---

<div class="post-metadata">

**Author:** ![rorywalsh](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/rorywalsh/32/165_2.png) [@rorywalsh](https://forum.cabbageaudio.com/u/rorywalsh)\
**Post date:** [10 June 2020 14:38 UTC](https://forum.cabbageaudio.com/t/csoundunity-package-upm-development/1916/442 "2020-06-10T14:38:29Z")

</div>

I’t snot much work at all considering I still have the Csound code I wrote. I changed the function signature for the GetSamples() mthod so it looks like this:

`public static MYFLT[] GetSamples(string source, SamplesOrigin origin, bool writeChannelData = false)`

Because it turns out that in a lot of cases you won’t want to write the number of channels to the start of the table. For example, in this new project I have to add some granular processing, which reads from a mono table. No need for any additional info to be written.

---

<div class="post-metadata">

**Author:** ![giovannibedetti](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/giovannibedetti/32/4146_2.png) [@giovannibedetti](https://forum.cabbageaudio.com/u/giovannibedetti)\
**Post date:** [10 June 2020 15:11 UTC](https://forum.cabbageaudio.com/t/csoundunity-package-upm-development/1916/443 "2020-06-10T15:11:33Z")

</div>

To make an example: I am using the opcode partikkel with the table loaded with CreateTable.  
If I set 2 as speed the playback is normal, 1 is at half speed, now.  
Would that correct this issue?  
EDIT:  
Try this with a stereo file:

[partikkel-2.csd](https://forum.cabbageaudio.com/uploads/short-url/2QtC55xDokVqdUDACTwr4Yuhs9N.csd) (7.0 KB)

```
public AudioClip source;

CsoundUnity csound;

// Start is called before the first frame update
IEnumerator Start()
{
    csound = GetComponent<CsoundUnity>();
    if (!csound)
        Debug.LogWarning("Csound not found?");

    while (!csound.IsInitialized)
    {
        yield return null; //waiting for initialization
    }

    yield return CsoundUnity.GetSamples(source.name, CsoundUnity.SamplesOrigin.Resources, (samples) =>
    {
        Debug.Log("samples loaded: "+samples.Length+" creating table");
        csound.CreateTable(100, samples);
    });
}

```

---

<div class="post-metadata">

**Author:** ![rorywalsh](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/rorywalsh/32/165_2.png) [@rorywalsh](https://forum.cabbageaudio.com/u/rorywalsh)\
**Post date:** [10 June 2020 15:20 UTC](https://forum.cabbageaudio.com/t/csoundunity-package-upm-development/1916/444 "2020-06-10T15:20:12Z")

</div>

> [@giovannibedetti](#):
>
> Would that correct this issue?

I assume there are no issues if you use mono sound files? Should we not be taking only a single channel from the sound file and passing it to a table, unless we want to use a custom UDO that can read interleaved data? There is little point writing a stereo file to a mono table, when only GEN01s can be stereo, and we can’t dynamically generate them without a reference to a file on disk?

On another note, do you think it would be possible to have the editor sliders update when a channel updates in the game? For example, I have a set of control channels I can manipulate in the editor. But when I call `SetChannel()` in code they don’t update. This would be nice to have, but I don’t know if it’s a little too much to implement?

[edit] I only saw your edit now, yes this is what I was talking about, we shouldn’t try to load a stereo file into a mono table…

---

<div class="post-metadata">

**Author:** ![giovannibedetti](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/giovannibedetti/32/4146_2.png) [@giovannibedetti](https://forum.cabbageaudio.com/u/giovannibedetti)\
**Post date:** [10 June 2020 15:36 UTC](https://forum.cabbageaudio.com/t/csoundunity-package-upm-development/1916/445 "2020-06-10T15:36:30Z")

</div>

Yes, the mono file plays at the right speed!  
Indeed the fact that we cannot deal directly with the audio files is annoying, since lots of Csound code uses those. So a smart solution is definitely needed.  
Could it be a script that lives on its own to be added to the CsoundUnity gameobject?  
This script loads the AudioClips and organizes them in tables, maybe with a clear editor inspector.  
Then on start creates the table on the referenced CsoundUnity instance, that can use those, maybe through a custom UDO.  
Do we need some data structure to hold the settings? 🤔

> [@rorywalsh](#):
>
> On another note, do you think it would be possible to have the editor sliders update when a channel updates in the game?

Yes it should be a matter of calling Editor.SetDirty or something like this!

---

<div class="post-metadata">

**Author:** ![rorywalsh](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/rorywalsh/32/165_2.png) [@rorywalsh](https://forum.cabbageaudio.com/u/rorywalsh)\
**Post date:** [10 June 2020 16:04 UTC](https://forum.cabbageaudio.com/t/csoundunity-package-upm-development/1916/446 "2020-06-10T16:04:05Z")

</div>

> [@giovannibedetti](#):
>
> Indeed the fact that we cannot deal directly with the audio files is annoying, since lots of Csound code uses those. So a smart solution is definitely needed.

I think we are pretty close to a workable solution. Here’s the run down as far as i am aware:

1. Users can read samples from disk IF they place them somewhere in the standalone data/StreamingAssets folder
2. Users can use inch/ins to access samples from an audio clip in real time
3. If users want to work with samples contained in the Resources folder, they will need to stop using the `diskin` opcodes, and use table opcodes instead. If they wish, they can use one one of the CsoundUnity UDOs that replicated diskin2, but using tables instead. For example, see the basic code below that will play back either a mono or stereo file from a single table.

I can’t think of any other situations off the top of my head that can’t be addressed using any of the above approaches?

**Sample Csound code**  
Will read mono or stereo files - uses the new` GetSamples()` method that writes the number of channels to the first position in the table.

```
opcode UnityTableRead, a[],i
    setksmps 1
    iTable xin
    kCount init 0
    iNumChannels tab_i 0, iTable
    print iNumChannels
    aOutArr[] init iNumChannels
    iNumSamples = ftlen(iTable)
    if iNumChannels == 1 then
        aOutArr[0] tab 1+kCount, iTable
        kCount = (kCount<iNumSamples ? kCount+1 : 0)
    elseif iNumChannels == 2 then
        aOutArr[0] tab 1+kCount, iTable
        aOutArr[1] tab 1+kCount+1, iTable
        kCount = (kCount<iNumSamples/2 ? kCount+2 : 0)
    endif
    xout aOutArr 
endop

instr 1
    print ftlen(9000)
    print tab_i(0, 9000)
    aSig[] UnityTableRead 9000
    print lenarray:i(aSig)
    k1 downsamp aSig[0]

    if lenarray:i(aSig) == 1 then
        outs aSig[0], aSig[0] 
    else
        outs aSig[0], aSig[1]
    endif
endin

```

---

<div class="post-metadata">

**Author:** ![giovannibedetti](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/giovannibedetti/32/4146_2.png) [@giovannibedetti](https://forum.cabbageaudio.com/u/giovannibedetti)\
**Post date:** [10 June 2020 16:39 UTC](https://forum.cabbageaudio.com/t/csoundunity-package-upm-development/1916/447 "2020-06-10T16:39:27Z")

</div>

Perfect!  
Yes, we are covered.  
Great!

With opcodes like partikkel that read tables the only thing to do is to scale the speed based on the num of channels.

---

<div class="post-metadata">

**Author:** ![rorywalsh](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/rorywalsh/32/165_2.png) [@rorywalsh](https://forum.cabbageaudio.com/u/rorywalsh)\
**Post date:** [10 June 2020 18:09 UTC](https://forum.cabbageaudio.com/t/csoundunity-package-upm-development/1916/448 "2020-06-10T18:09:02Z")

</div>

No, I think in these cases we only write one chanel to the table. So if the audio clip is stereo we let the user decide if they want to write the left or the right channel. Or?

---

<div class="post-metadata">

**Author:** ![rorywalsh](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/rorywalsh/32/165_2.png) [@rorywalsh](https://forum.cabbageaudio.com/u/rorywalsh)\
**Post date:** [11 June 2020 08:33 UTC](https://forum.cabbageaudio.com/t/csoundunity-package-upm-development/1916/449 "2020-06-11T08:33:19Z")

</div>

Btw @giovannibedetti, as soon as I enable `FILEWATCHER_ON` Unity just hangs indefinitely. The problem is I can’t get an error log, because it just hangs 🤔

---

<div class="post-metadata">

**Author:** ![giovannibedetti](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/giovannibedetti/32/4146_2.png) [@giovannibedetti](https://forum.cabbageaudio.com/u/giovannibedetti)\
**Post date:** [11 June 2020 09:16 UTC](https://forum.cabbageaudio.com/t/csoundunity-package-upm-development/1916/450 "2020-06-11T09:16:45Z")

</div>

I should have a look at what is happening on Windows.  
Can you try first to disable the test csound for errors?  
line 126

```
var result = TestCsoundForErrors(fileChanged);

```

into

```
var result = 0;

```

and comment lines from 214 to 223

and see what happens?

---

<div class="post-metadata">

**Author:** ![rorywalsh](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/rorywalsh/32/165_2.png) [@rorywalsh](https://forum.cabbageaudio.com/u/rorywalsh)\
**Post date:** [11 June 2020 09:35 UTC](https://forum.cabbageaudio.com/t/csoundunity-package-upm-development/1916/451 "2020-06-11T09:35:44Z")

</div>

So it was my code 😆 Ouch! I’ll look into it.

---

<div class="post-metadata">

**Author:** ![giovannibedetti](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/giovannibedetti/32/4146_2.png) [@giovannibedetti](https://forum.cabbageaudio.com/u/giovannibedetti)\
**Post date:** [11 June 2020 09:36 UTC](https://forum.cabbageaudio.com/t/csoundunity-package-upm-development/1916/452 "2020-06-11T09:36:47Z")

</div>

I don’t know, it’s the only thing I cannot test right now!  
😃

---

<div class="post-metadata">

**Author:** ![rorywalsh](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/rorywalsh/32/165_2.png) [@rorywalsh](https://forum.cabbageaudio.com/u/rorywalsh)\
**Post date:** [11 June 2020 09:39 UTC](https://forum.cabbageaudio.com/t/csoundunity-package-upm-development/1916/453 "2020-06-11T09:39:04Z")

</div>

That fixes the hang, but my .csd file still doesn’t update? Hmmm…

---

<div class="post-metadata">

**Author:** ![giovannibedetti](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/giovannibedetti/32/4146_2.png) [@giovannibedetti](https://forum.cabbageaudio.com/u/giovannibedetti)\
**Post date:** [11 June 2020 09:40 UTC](https://forum.cabbageaudio.com/t/csoundunity-package-upm-development/1916/454 "2020-06-11T09:40:38Z")

</div>

Do you see the green logs when you save the csd file externally?

 ![Schermata 2020-06-11 alle 11.41.01](https://forum.cabbageaudio.com/uploads/default/original/2X/4/4535a32d9325b5592f552bf52247ce924f55579d.png)

(and this is a bug actually, should have done it once!)

---

<div class="post-metadata">

**Author:** ![rorywalsh](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/rorywalsh/32/165_2.png) [@rorywalsh](https://forum.cabbageaudio.com/u/rorywalsh)\
**Post date:** [11 June 2020 09:41 UTC](https://forum.cabbageaudio.com/t/csoundunity-package-upm-development/1916/455 "2020-06-11T09:41:40Z")

</div>

No, but wait, I think there is something else I need to do each time i re-clone the repo? I asked about it before…let me check this thread…

---

<div class="post-metadata">

**Author:** ![rorywalsh](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/rorywalsh/32/165_2.png) [@rorywalsh](https://forum.cabbageaudio.com/u/rorywalsh)\
**Post date:** [11 June 2020 09:46 UTC](https://forum.cabbageaudio.com/t/csoundunity-package-upm-development/1916/456 "2020-06-11T09:46:02Z")

</div>

There’s something else strange going on too. The tables don’t seem to be getting all the samples. They are half filled with 0s… 🤔

---

<div class="post-metadata">

**Author:** ![rorywalsh](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/rorywalsh/32/165_2.png) [@rorywalsh](https://forum.cabbageaudio.com/u/rorywalsh)\
**Post date:** [11 June 2020 09:46 UTC](https://forum.cabbageaudio.com/t/csoundunity-package-upm-development/1916/457 "2020-06-11T09:46:43Z")

</div>

My bad. Wow, i’m on a roll this morning 😆

---

<div class="post-metadata">

**Author:** ![giovannibedetti](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/giovannibedetti/32/4146_2.png) [@giovannibedetti](https://forum.cabbageaudio.com/u/giovannibedetti)\
**Post date:** [11 June 2020 09:47 UTC](https://forum.cabbageaudio.com/t/csoundunity-package-upm-development/1916/458 "2020-06-11T09:47:46Z")

</div>

The file needs to be in the Resources folder and with these settings

 ![Schermata 2020-06-11 alle 11.46.54](https://forum.cabbageaudio.com/uploads/default/original/2X/8/87d9b30563017ff6d6bb1a23f870309960743712.png)

edit: preload audio data probably useless

---

<div class="post-metadata">

**Author:** ![rorywalsh](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/rorywalsh/32/165_2.png) [@rorywalsh](https://forum.cabbageaudio.com/u/rorywalsh)\
**Post date:** [11 June 2020 09:51 UTC](https://forum.cabbageaudio.com/t/csoundunity-package-upm-development/1916/459 "2020-06-11T09:51:50Z")

</div>

It wasn’t that, I had hacked the GetSamples() method and made a shit of it. Still not getting any green logs when I change a file. Is there something else I need to do, my efforts at reading all 400+ messages in this thread failed 😆

---

<div class="post-metadata">

**Author:** ![giovannibedetti](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/giovannibedetti/32/4146_2.png) [@giovannibedetti](https://forum.cabbageaudio.com/u/giovannibedetti)\
**Post date:** [11 June 2020 10:05 UTC](https://forum.cabbageaudio.com/t/csoundunity-package-upm-development/1916/460 "2020-06-11T10:05:28Z")

</div>

it should be it, or uncomment first line of filewatcher

```
//#define FILEWATCHER_ON

```

Then try to uncomment also the Debug.Logs, to understand if it starts!  
Maybe the filepath is still wrong?

[Previous page](https://forum.cabbageaudio.com/t/csoundunity-package-upm-development/1916.md?page=22)

[Next page](https://forum.cabbageaudio.com/t/csoundunity-package-upm-development/1916.md?page=24)
