grame-cncm / faust

Functional programming language for signal processing and sound synthesis
http://faust.grame.fr
Other
2.53k stars 319 forks source link

faust2unity no sound in editor #637

Open marvpaul opened 3 years ago

marvpaul commented 3 years ago

I tried to include a Faust example into Unity (2020.3.0f1). I used this example: https://github.com/grame-cncm/faust/blob/master/examples/bela/FMSynth2.dsp

First I used the command ./faust2unity -ios -osx -nvoices 6 synth3.dsp I imported the resulting FaustPlugin_synth3.unitypackage into my Unity project, attached the FaustPlugin_synth3.cs to a GameObject, clicked on the "Initialization" button in the Unity Inspector and have written a simple script to trigger the plugin. The script I used is the following:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class testFaust : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButtonDown(0)){
            Debug.Log("Key on");
            GameObject.Find("Faust").GetComponent<FaustPlugin_synth3>().context.keyOn(0, 440, 127);
        } else if(Input.GetMouseButtonUp(0)){
            GameObject.Find("Faust").GetComponent<FaustPlugin_synth3>().context.keyOff(0, 440, 127);
        }
    }
}

I was not able to hear any sound. Is this a bug in faust2unity or did I missed something here?

marvpaul commented 3 years ago

I have done some further investigations. What I found out so far: Faust is using Unity's OnAudioFilterRead method to change / add the audio data which is generated by the faust plugin. In the Unity documentation (https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnAudioFilterRead.html) it says that each entry in the audio data array should be in the range from -1 to 1. When I log an entry of this audio data after the faust plugin has filled it, it turns out that the values are way higher than 1 (up to 1.062668E+10).

private void OnAudioFilterRead(float[] buffer, int numchannels) {
        ctx.process(buffer, buffer.Length / numchannels, numchannels);
        Debug.Log(buffer[10]);
    }

This just seems to be unexpected. Perhaps thats part of the problem?

sletz commented 3 years ago

It seems like the FMSynth2.dsp run in monophonic mode (in the Faust IDE) does not go outside the [-1,1] range. So can you first test the FMSynth2.dsp code in monophonic mode?

marvpaul commented 3 years ago

@sletz I tried to compile the faust code using the command ./faust2unity -ios -osx -nvoices 1 synth3.dsp but the problem still persists. I can't hear any sound when using the plugin in Unity.

sletz commented 3 years ago

no -nvoices for now

marvpaul commented 3 years ago

@sletz I now used the command ./faust2unity -ios -osx synth3.dsp for conversion. I imported the .unitypackage, assigned the generated FaustPlugin_synth3.cs to a GameObject, clicked "Initialization" button of the script, clicked "Play" and then clicked the "Gate" button of the script. unfortunately there's still no sound.

sletz commented 3 years ago

I suggest trying simpler DSP examples, even one generating constant signals, and debugging with Debug.Log(buffer[10]); kind of code.