Ejemplo n.º 1
0
        void QueueInputCompleted(object sender, InputCompletedEventArgs e)
        {
            // return if we aren't actively monitoring audio packets
            if (!IsActive)
            {
                return;
            }

            var buffer = (AudioQueueBuffer)Marshal.PtrToStructure(e.IntPtrBuffer, typeof(AudioQueueBuffer));

            if (DataBufferReached != null)
            {
                var data = new byte[buffer.AudioDataByteSize];
                Marshal.Copy(buffer.AudioData, data, 0, (int)buffer.AudioDataByteSize);

                DataBufferReached(this, new DataBufferEventArgs(data));
            }

            var status = _audioQueue.EnqueueBuffer(e.IntPtrBuffer, _bufferSize, e.PacketDescriptions);

            if (status != AudioQueueStatus.Ok)
            {
                // todo:
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Handles iOS audio buffer queue completed message.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Input completed parameters.</param>
        private void QueueInputCompleted(object sender, InputCompletedEventArgs e)
        {
            // return if we aren't actively monitoring audio packets
            if (!Active)
            {
                return;
            }

            var buffer = (AudioQueueBuffer)Marshal.PtrToStructure(e.IntPtrBuffer, typeof(AudioQueueBuffer));

            if (OnBroadcast != null)
            {
                var send = new byte[buffer.AudioDataByteSize];
                Marshal.Copy(buffer.AudioData, send, 0, (int)buffer.AudioDataByteSize);

                OnBroadcast(this, new EventArgs <byte[]>(send));
            }

            var status = _audioQueue.EnqueueBuffer(e.IntPtrBuffer, _bufferSize, e.PacketDescriptions);

            if (status != AudioQueueStatus.Ok)
            {
                // todo:
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Handles iOS audio buffer queue completed message.
        /// </summary>
        /// <param name='sender'>Sender object</param>
        /// <param name='e'> Input completed parameters.</param>
        void QueueInputCompleted(object sender, InputCompletedEventArgs e)
        {
            try
            {
                // we'll only broadcast if we're actively monitoring audio packets
                if (!Active)
                {
                    return;
                }

                //copy data from the audio queue to a byte buffer
                var buffer     = (AudioQueueBuffer)System.Runtime.InteropServices.Marshal.PtrToStructure(e.IntPtrBuffer, typeof(AudioQueueBuffer));
                var audioBytes = new byte [buffer.AudioDataByteSize];
                System.Runtime.InteropServices.Marshal.Copy(buffer.AudioData, audioBytes, 0, (int)buffer.AudioDataByteSize);

                //broadcast the audio data to any listeners
                OnBroadcast?.Invoke(this, audioBytes);

                //check for null/active again, because the auto stop logic may stop the audio queue from within this handler!
                if (Active)
                {
                    var status = audioQueue.EnqueueBuffer(e.IntPtrBuffer, bufferSize, e.PacketDescriptions);

                    if (status != AudioQueueStatus.Ok)
                    {
                        System.Diagnostics.Debug.WriteLine("AudioStream.QueueInputCompleted() :: audioQueue.EnqueueBuffer returned non-Ok status :: {0}", status);
                        OnException?.Invoke(this, new Exception($"audioQueue.EnqueueBuffer returned non-Ok status :: {status}"));
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("AudioStream.QueueInputCompleted() :: Error: {0}", ex);
            }
        }
 static void Queue_InputCompleted(object sender, InputCompletedEventArgs e)
 {
     DELETEME++;
     if (DELETEME <= 1000)
     {
         Console.WriteLine("Input Completed!");
     }
 }
Ejemplo n.º 5
0
 private void RaiseCompleted(I input, R response)
 {
     Task.Run(() =>
     {
         InputCompletedEventArgs args = new InputCompletedEventArgs(input, response);
         InputCompleted?.Invoke(this, args);
     });
 }
Ejemplo n.º 6
0
        private void AudioQueueOnInputCompleted(object sender, InputCompletedEventArgs args)
        {
            var buffer = (AudioQueueBuffer)Marshal.PtrToStructure(args.IntPtrBuffer, typeof(AudioQueueBuffer));
            var send   = new byte[buffer.AudioDataByteSize];

            Marshal.Copy(buffer.AudioData, send, 0, (int)buffer.AudioDataByteSize);
            _subject.OnNext(send);

            var status = audioQueue.EnqueueBuffer(args.IntPtrBuffer, this.pushsize, args.PacketDescriptions);
        }
        void _queue_InputCompleted(object sender, InputCompletedEventArgs e)
        {
            if (!_isRecording)
                return;

            var buffer = (AudioQueueBuffer)System.Runtime.InteropServices.Marshal.PtrToStructure(e.IntPtrBuffer, typeof(AudioQueueBuffer));
            _audioFile.WritePackets(false, _startingPacketCount, buffer.PacketDescriptions, buffer.AudioData, (int)buffer.AudioDataByteSize);
            _startingPacketCount += e.PacketDescriptions.Length;
            // adding a queue                    
            _queue.EnqueueBuffer(e.IntPtrBuffer, _bufferByteSize, e.PacketDescriptions);            
        }       
Ejemplo n.º 8
0
        void InputQueueInputCompleted(object sender, InputCompletedEventArgs e)
        {
            var buffer = (AudioQueueBuffer)Marshal.PtrToStructure(e.IntPtrBuffer, typeof(AudioQueueBuffer));

            var status = inputQueue.EnqueueBuffer(e.IntPtrBuffer, e.PacketDescriptions);

            if (status == AudioQueueStatus.Ok)
            {
                samplesUpdated(this, new SamplesUpdatedEventArgs(AudioQueueBufferToDoubleArray(buffer)));
            }
        }
Ejemplo n.º 9
0
        void _queue_InputCompleted(object sender, InputCompletedEventArgs e)
        {
            if (!_isRecording)
            {
                return;
            }

            var buffer = (AudioQueueBuffer)System.Runtime.InteropServices.Marshal.PtrToStructure(e.IntPtrBuffer, typeof(AudioQueueBuffer));

            _audioFile.WritePackets(false, _startingPacketCount, buffer.PacketDescriptions, buffer.AudioData, (int)buffer.AudioDataByteSize);
            _startingPacketCount += e.PacketDescriptions.Length;
            // adding a queue
            _queue.EnqueueBuffer(e.IntPtrBuffer, _bufferByteSize, e.PacketDescriptions);
        }
Ejemplo n.º 10
0
        private void HandleInputCompleted(object sender, InputCompletedEventArgs e)
        {
            Log.Debug(TAG, "HandleInputCompleted");

            if (!Active)
            {
                return;
            }

            var buffer = (AudioQueueBuffer)System.Runtime.InteropServices.Marshal.PtrToStructure(e.IntPtrBuffer, typeof(AudioQueueBuffer));

            ProcessBuffer(buffer);

            var status = inputQueue.EnqueueBuffer(e.IntPtrBuffer, AudioBufferLength, e.PacketDescriptions);
        }
Ejemplo n.º 11
0
        private void AudioQueueOnInputCompleted(object sender, InputCompletedEventArgs args)
        {
            var buffer    = (AudioQueueBuffer)Marshal.PtrToStructure(args.IntPtrBuffer, typeof(AudioQueueBuffer));
            var tmpBuffer = new byte[buffer.AudioDataByteSize];
            var result    = new float[buffer.AudioDataByteSize / 2];

            Marshal.Copy(buffer.AudioData, tmpBuffer, 0, (int)buffer.AudioDataByteSize);

            for (int index = 0, i = 0; i < tmpBuffer.Length; index++, i += 2)
            {
                var x = BitConverter.ToInt16(tmpBuffer, i);
                result[index] = Math.Abs(x) > 100 ? x / 32767.0f : 0f;
            }
            _callback?.Invoke(result);

            _audioQueue.EnqueueBuffer(args.IntPtrBuffer, _bufferSize, args.PacketDescriptions);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Handles iOS audio buffer queue completed message.
        /// </summary>
        /// <param name='sender'>Sender object</param>
        /// <param name='e'> Input completed parameters.</param>
        void QueueInputCompleted(object sender, InputCompletedEventArgs e)
        {
            // return if we aren't actively monitoring audio packets
            if (!this.Active)
            {
                return;
            }

            var buffer = (AudioQueueBuffer)System.Runtime.InteropServices.Marshal.PtrToStructure(e.IntPtrBuffer, typeof(AudioQueueBuffer));

            var send = new byte [buffer.AudioDataByteSize];

            System.Runtime.InteropServices.Marshal.Copy(buffer.AudioData, send, 0, (int)buffer.AudioDataByteSize);

            this.OnBroadcast?.Invoke(this, send);

            var status = audioQueue.EnqueueBuffer(e.IntPtrBuffer, this.bufferSize, e.PacketDescriptions);

            if (status != AudioQueueStatus.Ok)
            {
                OnException?.Invoke(this, new Exception($"audioQueue.EnqueueBuffer returned non-Ok status :: {status}"));
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Handles iOS audio buffer queue completed message.
        /// </summary>
        /// <param name='sender'>Sender object</param>
        /// <param name='e'> Input completed parameters.</param>
        void QueueInputCompleted(object sender, InputCompletedEventArgs e)
        {
            try
            {
                // we'll only broadcast if we're actively monitoring audio packets
                if (!Active)
                {
                    return;
                }

                if (e.Buffer.AudioDataByteSize > 0)
                {
                    var audioBytes = new byte [e.Buffer.AudioDataByteSize];
                    Marshal.Copy(e.Buffer.AudioData, audioBytes, 0, (int)e.Buffer.AudioDataByteSize);

                    // broadcast the audio data to any listeners
                    OnBroadcast?.Invoke(this, audioBytes);

                    // check if active again, because the auto stop logic may stop the audio queue from within this handler!
                    if (Active)
                    {
                        BufferOperation(() => audioQueue.EnqueueBuffer(e.IntPtrBuffer, null), null, status =>
                        {
                            Debug.WriteLine("AudioStream.QueueInputCompleted() :: audioQueue.EnqueueBuffer returned non-Ok status :: {0}", status);
                            OnException?.Invoke(this, new Exception($"audioQueue.EnqueueBuffer returned non-Ok status :: {status}"));
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("AudioStream.QueueInputCompleted() :: Error: {0}", ex.Message);

                OnException?.Invoke(this, new Exception($"AudioStream.QueueInputCompleted() :: Error: {ex.Message}"));
            }
        }
Ejemplo n.º 14
0
        public void SendAudio(object sender, InputCompletedEventArgs e)
        {
//            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);

            //bufWriter = BitConverter.GetBytes(0);
            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);

            /*Console.WriteLine ("MS Length before: " + ms.Length);
             *
             * unsafe {
             *      byte* bufptr = (byte*) e.IntPtrBuffer;
             *
             *      for (int i = 0; i < 1280; i++) {
             *              byte pt = (*(bufptr + i));
             *              //if (BitConverter.IsLittleEndian) Array.Reverse(pt);
             *              ms.Write (new byte[] { pt }, 0, 1);
             *              Console.Write ("\\x" + pt);
             *      }
             * }*/

//			var buffer = (AudioQueueBuffer)System.Runtime.InteropServices.Marshal.PtrToStructure(e.IntPtrBuffer, typeof(AudioQueueBuffer));
//
//			var send = new byte[buffer.AudioDataByteSize];
//			System.Runtime.InteropServices.Marshal.Copy(buffer.AudioData, send, 0, (int)buffer.AudioDataByteSize);
//			ms.Write (send, 0, send.Length);
//			Console.WriteLine ("\nMS Length after: " + ms.Length);
            //short sample16Bit = BitConverter.ToInt16(payload, 0);
            //InputLoudness = Math.Abs(sample16Bit / 32000.00);

            //if (isTalking) recorder.EnqueueBuffer (e.IntPtrBuffer, byteSize, null);


//            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
//                {
//
//                }
//            });
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Handles iOS audio buffer queue completed message.
        /// </summary>
        /// <param name='sender'>Sender object</param>
        /// <param name='e'> Input completed parameters.</param>
        private void QueueInputCompleted(object sender, InputCompletedEventArgs e)
        {
            // return if we aren't actively monitoring audio packets
            if (!this.Active)
            {
                return;
            }

            var buffer = (AudioQueueBuffer)System.Runtime.InteropServices.Marshal.PtrToStructure(e.IntPtrBuffer, typeof(AudioQueueBuffer));
            if (this.OnBroadcast != null)
            {
                var send = new byte[buffer.AudioDataByteSize];
                System.Runtime.InteropServices.Marshal.Copy(buffer.AudioData, send, 0, (int)buffer.AudioDataByteSize);

                this.OnBroadcast(this, new EventArgs<byte[]>(send));
            }
                               
            var status = audioQueue.EnqueueBuffer(e.IntPtrBuffer, this.bufferSize, e.PacketDescriptions);  

            if (status != AudioQueueStatus.Ok)
            {
                // todo: 
            }
        }       
Ejemplo n.º 16
0
        public void SendAudio(object sender, InputCompletedEventArgs e)
        {
            //            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);

            //bufWriter = BitConverter.GetBytes(0);
            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);

            /*Console.WriteLine ("MS Length before: " + ms.Length);

            unsafe {
                byte* bufptr = (byte*) e.IntPtrBuffer;

                for (int i = 0; i < 1280; i++) {
                    byte pt = (*(bufptr + i));
                    //if (BitConverter.IsLittleEndian) Array.Reverse(pt);
                    ms.Write (new byte[] { pt }, 0, 1);
                    Console.Write ("\\x" + pt);
                }
            }*/

            //			var buffer = (AudioQueueBuffer)System.Runtime.InteropServices.Marshal.PtrToStructure(e.IntPtrBuffer, typeof(AudioQueueBuffer));
            //
            //			var send = new byte[buffer.AudioDataByteSize];
            //			System.Runtime.InteropServices.Marshal.Copy(buffer.AudioData, send, 0, (int)buffer.AudioDataByteSize);
            //			ms.Write (send, 0, send.Length);
            //			Console.WriteLine ("\nMS Length after: " + ms.Length);
            //short sample16Bit = BitConverter.ToInt16(payload, 0);
            //InputLoudness = Math.Abs(sample16Bit / 32000.00);

            //if (isTalking) recorder.EnqueueBuffer (e.IntPtrBuffer, byteSize, null);

            //            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
            //                {
            //
            //                }
            //            });
        }