Exemplo n.º 1
0
        private void SendPulse()
        {
            var shortSamples = new short[audioFormat.BytesPerFrame / 2];

            for (int i = 0; i < shortSamples.Length; i++)
            {
                shortSamples[i] = oscillator.GetNextSample();
            }
            var byteSamples = new byte[audioFormat.BytesPerFrame];

            Buffer.BlockCopy(shortSamples, 0, byteSamples, 0, byteSamples.Length);
            lastPulseSentAt = DateTime.Now;
            lastFrame       = byteSamples;
        }
        protected override void OnSamples(long sampleTime, long sampleDuration, byte[] sampleData)
        {
            try
            {
                // Raise an event if we managed to successfully capture data.
                if (!dataReceived)
                {
                    dataReceived = true;
                    if (CaptureSuccessful != null)
                    {
                        CaptureSuccessful(this, new EventArgs());
                    }
                }

                RootAudioContext.Resampler.Write(sampleData);

                bool moreFrames;
                do
                {
                    if (RootAudioContext.Resampler.Read(RootAudioContext.ResampleBuffer, out moreFrames))
                    {
                        // Hack to create single-tone signal - I'm sure there are better places to do this. So sue me.
                        if (UseGeneratedTone)
                        {
                            for (int i = 0; i < RootAudioContext.ResampleBuffer.Length; i++)
                            {
                                short nextSample = oscillator.GetNextSample();
                                RootAudioContext.ResampleBuffer[i]   = (byte)nextSample;
                                RootAudioContext.ResampleBuffer[++i] = (byte)(nextSample >> 8);
                            }
                        }

                        var resetEvents = new AutoResetEvent[AudioControllers.Count];
                        for (int i = 0; i < AudioControllers.Count; i++)
                        {
                            // Throw the actual encoding and sending out onto a separate thread, so we can take advantage of multiple processors.
                            resetEvents[i] = new AutoResetEvent(false);
                            ThreadPool.QueueUserWorkItem(obj =>
                            {
                                var index = (int)obj;
                                try
                                {
                                    var copy = new byte[RootAudioContext.ResampleBuffer.Length];
                                    Buffer.BlockCopy(RootAudioContext.ResampleBuffer, 0, copy, 0, RootAudioContext.ResampleBuffer.Length);
                                    var submitCtx = AudioContexts[index];
                                    AudioControllers[index].SubmitRecordedFrame(submitCtx, copy);
                                }
                                catch (Exception ex)
                                {
                                    ClientLogger.Debug(ex.ToString);
                                }
                                finally
                                {
                                    resetEvents[index].Set();
                                }
                            }, i);
                        }
                        WaitHandle.WaitAll(resetEvents);
                    }
                } while (moreFrames);
            }
            catch (Exception ex)
            {
                ClientLogger.Debug(ex.Message);
            }
        }