Skip to content

Instantly share code, notes, and snippets.

@cgsdev0
Last active September 11, 2026 23:54
Show Gist options
  • Select an option

  • Save cgsdev0/5fe89c713160a2da4b8656455f7afae0 to your computer and use it in GitHub Desktop.

Select an option

Save cgsdev0/5fe89c713160a2da4b8656455f7afae0 to your computer and use it in GitHub Desktop.
int latency = AudioSettings.outputSampleRate / 10;
AudioClip clip = Microphone.Start(device, true, 1, AudioSettings.outputSampleRate);
int last = 0;
int dist(int samples, int start, int end) {
if (end >= start) return end - start;
return (end + samples) - start; // handle wrapping
}
while(true) {
int current = Microphone.GetPosition(device);
int delta = dist(clip.samples, last, current);
if (delta > latency) {
// get a sample
clip.GetData(data, last);
last = current;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment