Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private unsafe void ProcessFrameOutput(AudioFrame frame)
- {
- using (var buffer = frame.LockBuffer(AudioBufferAccessMode.Read))
- using (var reference = buffer.CreateReference())
- {
- byte* dataInBytes;
- uint capacityInBytes;
- ((IMemoryBufferByteAccess) reference).GetBuffer(out dataInBytes, out capacityInBytes);
- if (capacityInBytes == 0) // Don't send empty frames.
- return;
- var capacityInFloats = capacityInBytes/4;
- float* dataInFloat = (float*)dataInBytes;
- var floats = new float[capacityInFloats];
- Marshal.Copy((IntPtr)dataInFloat, floats, 0, (int)capacityInFloats);
- var shorts = Float2Short(floats);
- ToxAvModel.Instance.SendAudioFrame(_friendNumber, new ToxAvAudioFrame(shorts, _samplingRate, 1));
- Debug.WriteLine(shorts.Length);
- }
- }
- private short[] Float2Short(float[] inSamples)
- {
- short[] outSamples = new short[inSamples.Length];
- for (int i = 0; i < inSamples.Length; i++)
- {
- float dtmp;
- if (inSamples[i] >= 0)
- {
- dtmp = inSamples[i] + 0.5f;
- if (dtmp > short.MaxValue)
- {
- dtmp = short.MaxValue;
- }
- }
- else
- {
- dtmp = inSamples[i] - 0.5f;
- if (dtmp < short.MinValue)
- {
- dtmp = short.MinValue;
- }
- }
- outSamples[i] = (short)(dtmp);
- }
- return outSamples;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement