Skip to content

Instantly share code, notes, and snippets.

@troy-lamerton
Last active March 24, 2019 20:54
Show Gist options
  • Save troy-lamerton/e7be4438c5a2fca948a2795ac5093714 to your computer and use it in GitHub Desktop.
Save troy-lamerton/e7be4438c5a2fca948a2795ac5093714 to your computer and use it in GitHub Desktop.
public unsafe void OnAudioDataCallback(IntPtr _, char id1, char id2, short* pcmFrames, int frameCount, int frameRateHz, int channels, int isSilence) {
// ignore silent samples
if (isSilence == 1) return;
if (channels != 2) {
Debug.LogWarning("unexpected number of channels " + channels);
return;
}
sampleBytes = new byte[frameCount * 2 * channels]; // each channel has a frame of 2 bytes
/*
Starting from pcmFrames pointer, iterate over the array in memory
expecting vivox frames to be in format LRLRLR...
which results in byte array: LLRRLLRRLLRR...
*/
for (int i = 0; i < frameCount * channels; i++) {
// expand the frame into two bytes
short frame = pcmFrames[i];
BitHelpers.ToBytes(frame, out sampleBytes[i * 2], out sampleBytes[i * 2 + 1]);
}
udpClient.SendAsync(sampleBytes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment