Hi
I have a sound with markers and would like to implement standard marker looping functionality.
Looks that should be easy with:
AudioClip myClip = AudioClip.Create(“MySinoid”,44100, 2, 44100, false, true, OnAudioRead, OnAudioSetPosition);
In function OnAudioRead I would like to provide my samples according to markers information,
but I have a problem even with playing simple clip properly from the begining to the end.
Can you please tell me why I can’t hear begining of my clip or where to set real_pos=0;
using UnityEngine;
using System.Collections;
public class clb_test : MonoBehaviour
{
int real_pos=0;
AudioSource looping_src=null;
public AudioClip source_clip;
float [] samples = null;
public bool started=false;
void Start() {
AudioSettings.outputSampleRate=44100;
looping_src=(AudioSource)gameObject.AddComponent("AudioSource");
looping_src.ignoreListenerVolume = true;
looping_src.playOnAwake=false;
looping_src.loop=true;
samples = new float[source_clip.samples * source_clip.channels];
source_clip.GetData(samples,0);
AudioClip myClip = AudioClip.Create("MySinoid",source_clip.frequency, source_clip.channels, source_clip.frequency, false, true, OnAudioRead, OnAudioSetPosition);
looping_src.clip = myClip;
}
void OnAudioRead(float[] data)
{
int count = 0;
//string s="T:"+System.DateTime.Now.Second.ToString()+","+System.DateTime.Now.Millisecond.ToString();
//Debug.Log(s+" real_pos:" +real_pos.ToString());
while (count < data.Length)
{
//data[count] = Mathf.Sign(Mathf.Sin(2 * Mathf.PI * 440 * position / sampleRate));
int len=samples.Length;
if (real_pos>=len)
{
data[count]=0;
}
else
{
data[count]=samples[real_pos];
}
count++;
real_pos++;
}
}
void OnAudioSetPosition(int newPosition)
{
//string s="T:"+System.DateTime.Now.Second.ToString()+","+System.DateTime.Now.Millisecond.ToString();
//Debug.Log("OnAudioSetPosition:"+ s + "new pos "+newPosition.ToString());
if (!started) //it not helps at all
{
real_pos=newPosition; //If it is here play constantly 1st second of clip which is normal
started=true;
}
}
void OnGUI ()
{
if(GUILayout.Button("play"))
{
started=false;
//real_pos=0; //if it is here can't hear begining of my clip (check with voice - then easy to detect bug)
looping_src.Play();
}
if(GUILayout.Button("stop"))
{
looping_src.Stop();
started=false;
}
}
}