Loading/Playing an audio file in Unity
I'm currently having trouble loading/playing back an audio file in Unity.
I have a simple patch (link below) based on the js "loading samples" example. Originally I compiled and tested it in Wwise and everything worked as expected. I've now tried to integrate essentially the same patch in Unity and I'm not getting any audio when playing the scene.
https://enzienaudio.com/h/cdraudio/UnitySampTest_03/
- The AudioLib.dll settings should be correct
- The .cs file is attached to a cube
- 'Play on awake' is selected
- Added a 'Load Sample' script (HvLoadSample.cs) containing the following:
`using System.Collections; using System.Collections.Generic; using UnityEngine;
public class HvLoadSample : MonoBehaviour {
public AudioClip clip;
private Hv_UnitySampTest_03_AudioLib _audio;
void Start ()
{
_audio = GetComponent<Hv_UnitySampTest_03_AudioLib>();
_audio.FillTableWithMonoAudioClip("myTable", clip);
}
} `
I would be expecting the sample to play once the scene launches but I'm getting nothing. Not sure if I've missed a step somewhere or if there's a different method of setting the table size that should be used in Unity instead of the [r setTableSize-myTable]?
@cdraudio sorry yeah that javascript example is slightly out of date, the [r setTableSize-myTable] is being handled manually in the js part of the example.
Try using this looper patch here: https://enzienaudio.com/h/enzienaudio/looper/
It's very similar to what you have, though the sample loader script looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LooperController : MonoBehaviour {
public AudioClip clip;
private Hv_looper_AudioLib looper;
void Start () {
looper = GetComponent<Hv_looper_AudioLib>();
// expecting a stereo file
float[] buffer = new float[clip.samples];
// fill the left channel
clip.GetData(buffer, 0);
looper.FillTableWithFloatBuffer("channelL", buffer);
// fill the right channel
clip.GetData(buffer, 1);
looper.FillTableWithFloatBuffer("channelR", buffer);
// update the table size parameter
looper.SetFloatParameter(Hv_looper_AudioLib.Parameter.Tablesize, clip.samples);
}
}
Note the looper.SetFloatParameter at the end is notifying the patch of the buffer length. I don't think was happening in your code so you'll never hear anything.
I've actually been working on a nice feature to auto expose an AudioClip interface in the Unity inspector for tables within the patch. Stay tuned!
I’ve pushed an update to the unity target on the dev channel of heavy to support @hv_table. If you have a table object declared like: [table dogwoof 0 @hv_table], in the unity inspector it’ll auto generate an AudioClip interface to drag and drop samples that will be loaded when the game starts
Thanks for the reply. I'll try that out now.
hey @cdraudio did you managed to get it working?
Sorry for the late reply @diplojocus. Yeah, got the script and patch working fine though I've ran into a couple other issues. I can open a separate thread for them, though one that relates to this was an issue with 3D positioning of the audio source. Volume attenuation works fine but there's no panning. Noticed this was brought up in #112. Was this ever resolved or should I look into the work arounds mentioned in the thread?
What about stereo file? I've tried load stereo but channels left and right was same. When I've separated it in DAW and made left and right channel, then load them, it play correctly. I wonder is there a way to load stereo file, divided it to left and right channel and then load them to 2 tables?
Currently it will load the first channel (left) of a stereo file into the table. Your method of separating the files into individual channels sounds correct. You could probably create a batch script to automate splitting the bounces from your DAW.
Pd doesn't really have a concept of stereo when patching as you can have as many channels as you want.
@diplojocus ok, thanks.