﻿using System.Collections;
using UnityEngine;

public class TableLoader : MonoBehaviour
{
    [Tooltip("The audio clip from which the samples will be loaded. It must reside inside a Resources folder")]
    public AudioClip source;
    [Tooltip("The Csound table number that will be created and filled with the loaded samples")]
    public int tableNumber = 100;

    CsoundUnity csound;

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

        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");
            var res = csound.CreateTable(tableNumber, samples);
            if (res == 0) Debug.Log("Created table 100!");
            else Debug.LogError("Cannot create table");
        });
    }
}
