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;
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)