immersivecognition / unity-experiment-framework

UXF - Framework for creating human behaviour experiments in Unity
https://immersivecognition.github.io/unity-experiment-framework/
MIT License
215 stars 41 forks source link

Audio Saver as WAV file #74

Closed fbencive closed 3 years ago

fbencive commented 3 years ago

Dear UXF users, I am troubling with saving an audioclip as WAV file. I am following the lines you posted in the "Programming ideas" section. I write below my code: I want that the method RecordAudio starts when the trial begin, and StopAudio starts when the trial ends. My issue is that fileIOManager.ManageInWorker Does not exists. I am new in programming with Unity and in using UXF so I am sorry if my issue is silly! But please can you help me to understand how to fix it? Best, Federica

using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using UXF;

public class AudioRecording : MonoBehaviour { private AudioClip audioClip; public int recordTime = 10; // no. of seconds can be set in Unity Editor inspector private const int sampleRate = 16000; // sample rate for recording speech public string mic;

public void RecordAudio()     
{
    if (Microphone.devices.Length == 0)
    {
        Debug.LogWarning("No microphone found to record audio clip sample with.");
        return;
    }
    mic = Microphone.devices[0];
    audioClip = Microphone.Start(mic, false, recordTime, sampleRate);

}

public void StopAudio()
{
    Microphone.End(mic);

    // method that converts audioclip to wav bytes
    byte[] data = WavUtility.FromAudioClip(audioClip);
    string fname = string.Format("mic_T{0:000}.wav", Session.instance.currentTrialNum);
    string path = Application.dataPath;

    string outputLocation = Path.Combine(path, fname); // 

    //store in results, easier to access later
    Session.instance.CurrentTrial.result["mic_filename"] = fname;

    fileIOManager.ManageInWorker(() => **//that's where I got an error**
     {
         File.WriteAllBytes(outputLocation, data);
     });
}

}

jackbrookes commented 3 years ago

Hi, the code snippet is out of date (refers to an old version of UXF that uses a system called FileIOManager), I have now corrected it.

Instead, we should use the trial.SaveBytes(...) method (documentation).

This code should work better:

public class AudioRecording : MonoBehaviour
{
    private AudioClip audioClip;
    public int recordTime = 10; // no. of seconds can be set in Unity Editor inspector
    private const int sampleRate = 16000; // sample rate for recording speech
    public string mic;

    public void RecordAudio()     
    {
        if (Microphone.devices.Length == 0)
        {
            Debug.LogWarning("No microphone found to record audio clip sample with.");
            return;
        }
        mic = Microphone.devices[0];
        audioClip = Microphone.Start(mic, false, recordTime, sampleRate);

    }

    public void StopAudio()
    {
        Microphone.End(mic);

        // method that converts audioclip to wav bytes
        byte[] data = WavUtility.FromAudioClip(audioClip);
        string fname = string.Format("mic_T{0:000}.wav", Session.instance.currentTrialNum);

        // save data, automatically puts it in the session folder.
        // it will also store the name in the trial_results file for easier referencing during analysis.
        Session.instance.currentTrial.SaveBytes(data, fname);
    }
}
fbencive commented 3 years ago

Thank you so much! And thanks also for the great job you did in implementing UXF, it is a very useful tool. Best, Federica