I need to process some audio recorded from a microphone, but he is being reproduced by my application. How can I avoid that? This is my code for recording and processing the audio:
AudioSource audioSrc;
SoundTrigger trigger;
float[] spectrum = new float[1024];
AudioSource[] sources;
int srcIndex;
void Start()
{
trigger = new SoundTrigger(4000, 620);
GameObject a = new GameObject("AudioSource");
audioSrc = a.AddComponent<AudioSource>();
Instantiate(a);
audioSrc.clip = Microphone.Start(null, true, 1000, 44100);
while (!(Microphone.GetPosition(null) > 0)) { }
sources = new AudioSource[1]{ audioSrc };
srcIndex = 0;
sources[srcIndex].Play();
}
void Update()
{
sources[srcIndex].GetSpectrumData(spectrum, 0, FFTWindow.BlackmanHarris);
}
I tried to mute and lower the volume of the audio listener, but it doesn’t work, because then I dont have audio to process. I also tried to use 3d sound, and put the audio listener far away, but I didnt manage to do it. Is there a good solution for this?
I’m not the best at this sort of thing but may I suggest something.
I think the problem is you’re not calling Microphone.End(null). Another thing you can try is to move your .Play code to another function which you can trigger at a push of a button or something. One last thing is I added .playOnAwake = false, in case it is initialized with a true from Unity
Hope this helps
AudioSource audioSrc;
SoundTrigger trigger;
float[] spectrum = new float[1024];
AudioSource[] sources;
int srcIndex;
void Start()
{
trigger = new SoundTrigger(4000, 620);
GameObject a = new GameObject("AudioSource");
audioSrc = a.AddComponent<AudioSource>();
Instantiate(a);
audioSrc.playOnAwake = false;
audioSrc.clip = Microphone.Start(null, true, 1000, 44100);
while (!(Microphone.GetPosition(null) > 0)) { }
// stop the recording!
Microphone.End(null);
// consider moving this to another function (maybe one that is triggered at a push of a button or something)
sources = new AudioSource[1]{ audioSrc };
srcIndex = 0;
sources[srcIndex].Play();
}
void Update()
{
sources[srcIndex].GetSpectrumData(spectrum, 0, FFTWindow.BlackmanHarris);
}