- Home /
Syncing Audio with GetOutputData
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
public class example : MonoBehaviour {
AudioClip recordedAudioClip;
float[] blockBuffer;
List<float> clipBuffer = new List<float>();
bool isRecording = false;
int size = 680;
void Start() {
GameObject.Find("audiosource").audio.GetOutputData(blockBuffer,0);
}
void Update() {
if((Input.GetKeyDown("r")) && (isRecording == false)) {
Debug.Log ("Start Recording");
isRecording = true;
clipBuffer.Clear();
blockBuffer = new float[size];
recordedAudioClip = AudioClip.Create("output",441000,1,44100,false,false);
}
else if((Input.GetKeyDown("r")) && (isRecording == true)) {
Debug.Log ("Stop Recording");
isRecording = false;
recordedAudioClip.SetData(clipBuffer.ToArray(),0);
}
// Play Audio
if(Input.GetKeyDown("p")) {
Debug.Log ("Play Recording");
AudioSource.PlayClipAtPoint(recordedAudioClip, Camera.main.transform.position);
}
// Increase size of sample buffer
if(Input.GetKeyDown("]")) {
size=size+10;
Debug.Log (size);
}
if(Input.GetKeyDown("[")) {
size=size-10;
Debug.Log (size);
}
if(Input.GetKeyDown(".")) {
size=size+1;
Debug.Log (size);
}
if(Input.GetKeyDown(",")) {
size=size-1;
Debug.Log (size);
}
if(Input.GetKeyDown("=")) {
size=size+100;
Debug.Log (size);
}
if(Input.GetKeyDown("-")) {
size=size-100;
Debug.Log (size);
}
if (isRecording) {
GameObject.Find("audiosource").audio.GetOutputData(blockBuffer,0);
clipBuffer.AddRange(blockBuffer);
}
}
}
I don't have any error checking on this code, it's only for testing my concept. Please take a look.
The code takes short samples of a particular audio clip and is suppose to stream it to a buffer until a person stops it, and then play that audio clip. I know nothing about audio processing, but I can't figure how to adjust the buffer sample to pull the right amount of audio. Right not it's static-laced. Too big of an array and it's slow. To small of an array it's fast. And based on the fact that this is called every frame, it's also dependent on how fast or slow someone's computer is. I also assume it's based on the outputSampleRate
Go ahead and try it out. Create a scene with an audio source called 'audiosource' and add some random clip to it. Attach this script to the camera for example. I would like the audio that is recorded to sound EXACTLY like the audio from the audio source, but I don't know how much access I'm granted to the sound buffer.
I can't use OnAudioFilterRead() because this isn't performed on audio that can be heard, but audio that can not be heard. (muted audio source)
Answer by Skeletim · Jul 24, 2013 at 02:52 AM
I'm documenting this for others.
Based on the code above GetOutputData executed every frame. The issue was that it didn't need to. It only needed to execute after each sample. So I had to get the time of the sample, which is the sample size / frequency of audio clip.
That gives you the length of the audio gathered from the sample. So a sample size of 8192 (32k) from an audio clip that is at 44100hz, is 8192 / 44100 = 0.185759637 or ~18ms
So you'll want to get the next sample after 18ms has passed. Since this is done in my code every frame, I just had to use Time.deltaTime and added up Time.deltaTime every frame, and check that against the time it takes to play the clip. If it exceeds that, then I grab another another sample, if not, then I pass it up.
Granted the math isn't perfect, and I don't know if it ever will be, so you'll still get small pops and cracks, but at least for my purpose, this is perfect, and shouldn't be noticeable.