AudioUnitStatus AudioInputCallBack(AudioUnitRenderActionFlags actionFlags, AudioTimeStamp timeStamp, uint busNumber, uint numberFrames, AudioUnit audioUnit) { MemoryStream ms = new MemoryStream(); String s = "a000"; byte[] bufWriter = Encoding.ASCII.GetBytes(s.ToCharArray(), 0, 4); ms.Write(bufWriter, 0, 4); bufWriter = BitConverter.GetBytes(AudioSessionId); if (BitConverter.IsLittleEndian) Array.Reverse(bufWriter); ms.Write(bufWriter, 0, 4); long time = (long) (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds; //Console.WriteLine ((time - lasttime) + " ms delay"); lasttime = time; bufWriter = BitConverter.GetBytes(time); if (BitConverter.IsLittleEndian) Array.Reverse(bufWriter); ms.Write(bufWriter, 0, 8); var buffer = new AudioBuffer() { NumberChannels = 1, DataByteSize = (int)numberFrames * 2, Data = System.Runtime.InteropServices.Marshal.AllocHGlobal((int)numberFrames * 2) }; var bufferList = new AudioBuffers(1); bufferList[0] = buffer; var status = audioUnit.Render(ref actionFlags, timeStamp, busNumber, numberFrames, bufferList); var send = new byte[buffer.DataByteSize]; System.Runtime.InteropServices.Marshal.Copy(buffer.Data, send, 0, send.Length); ms.Write (send, 0, send.Length); Console.Write("\n Buffer: "); foreach (byte b in send) Console.Write("\\x" + b); Console.Write("\n"); System.Runtime.InteropServices.Marshal.FreeHGlobal(buffer.Data); byte[] sendbuf = ms.ToArray(); if (sendbuf.Length > 4096) throw new Exception("Packet size too large!"); Task tk = Task.Factory.StartNew(() => { try { var aSender = audioCaller.BeginSend(sendbuf, sendbuf.Length, null, null); aSender.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(3)); if (aSender.IsCompleted) audioCaller.EndSend(aSender); } catch { } }); return AudioUnitStatus.OK; }
AudioUnitStatus AudioInputCallBack(AudioUnitRenderActionFlags actionFlags, AudioTimeStamp timeStamp, uint busNumber, uint numberFrames, AudioUnit audioUnit) { var buffer = new AudioBuffer() { NumberChannels = 1, DataByteSize = (int)numberFrames * 2, Data = System.Runtime.InteropServices.Marshal.AllocHGlobal((int)numberFrames * 2) }; var bufferList = new AudioBuffers(1); bufferList[0] = buffer; var status = audioUnit.Render(ref actionFlags, timeStamp, busNumber, numberFrames, bufferList); var send = new byte[buffer.DataByteSize]; System.Runtime.InteropServices.Marshal.Copy(buffer.Data, send, 0, send.Length); var handler = DataAvailable; if (handler != null) handler(this, send); Console.Write("\n Buffer: "); foreach (byte b in send) Console.Write("\\x" + b); Console.Write("\n"); System.Runtime.InteropServices.Marshal.FreeHGlobal(buffer.Data); return AudioUnitStatus.OK; }