Just a quick reply with a sample for file loading:
csd:
<Cabbage>
form caption("Read soundfile"), size(300, 200)
</Cabbage>
<CsoundSynthesizer>
<CsOptions>
-n -d -m0d
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 32
nchnls = 2
0dbfs = 1.0
instr 1
prints p4
;if you have mp3s use mp3in
;ibufsize = 64
;ar1, ar2 mp3in p4, 0, 0, 0, ibufsize
;for pcm files use diskin2
a1, a2 diskin2 p4, 1, 0, 1
outs a1, a2
endin
</CsInstruments>
<CsScore>
</CsScore>
</CsoundSynthesizer>
C#:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class ReadFileController : MonoBehaviour
{
public string[] AudioFilesNames;
void Awake()
{
//assign member variable
csoundUnity = GetComponent<CsoundUnity>();
}
private CsoundUnity csoundUnity;
private Dictionary<int, string> _audioFilePaths;
// Use this for initialization
void Start()
{
_audioFilePaths = new Dictionary<int, string>();
var count = 0;
foreach (var fileName in AudioFilesNames)
{
var path = Path.Combine(Application.streamingAssetsPath, fileName);
Debug.Log($"Checking path: {path} \nFile Exists? " + (File.Exists(path) ? "true" : "false"));
#if UNITY_EDITOR_OSX
// path = "file://" + path;
#endif
_audioFilePaths.Add(count, path);
count++;
}
//comment this if you don't want autoplay of the first sample
StartPlaying(0);
}
public void StartPlaying(int fileIndex)
{
SendScoreEvent(1, fileIndex, true);
}
public void StopPlaying(int fileIndex)
{
SendScoreEvent(1, fileIndex, false);
}
//send score works also with instruments with string names,
//but sending stop with i-instrName doesn't work
//so use ints for now
//also, to stop the instrument, this should be started with infinite duration
void SendScoreEvent(int instrNum, int fileIndex, bool isStart)
{
var score = "i ";
if (!isStart)
{
score += "-" + instrNum + " 0 0";
}
else
{
var path = _audioFilePaths[fileIndex];
Debug.Log($"Checking path: {path} \nFile Exists? " + (File.Exists(path) ? "true" : "false"));
//sending score with full path enclosed in escape chars
score += (instrNum + " 0 -1" + " \"" + path + "\"");
}
Debug.Log("sending score: " + score);
csoundUnity.SendScoreEvent(score);
}
}
Later I’ll check the csd you posted to see if I can make it work on Unity.
Cheers!
EDIT: (the dictionary seems pretty useless you could pass directly the AudioFileNames by index)
Here I’m passing the file path as p4, ie the fourth element of the score, so the score becomes something like this:
i 1 0 -1 "/pathToProject/Assets/StreamingAssets/piano2.wav"
instrument 1 (p1, instr number) 0 (p2, start time, immediate) -1 (p3, duration of instr performance, negative if you want it to last forever until you stop it) “path” (p4)