Cabbage Logo
Back to Cabbage Site

CsoundUnity Package (UPM) development

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

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.

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 (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);
    });
}

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…

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? :thinking:

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

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

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.

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?

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 :thinking:

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?

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

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

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

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

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

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…

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… :thinking:

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

The file needs to be in the Resources folder and with these settings
Schermata 2020-06-11 alle 11.46.54

edit: preload audio data probably useless

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 :laughing:

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?