Provides data for a samples available event.
Inheritance: System.EventArgs
Exemplo n.º 1
0
        private void OnSamplesAvailable(object sender, SamplesAvailableEventArgs e)
        {
            lock (captures)
            {
                AudioSource source;
                AudioCaptureEntity entity;
                if (!captureToSourceLookup.TryGetValue (e.Provider, out source) || !captures.TryGetValue (source, out entity))
                    return;

                bool muted = (entity.Muted || this.captureMuted);

                if ((!entity.Talking && muted) || (!entity.AudioCapture.IsCapturing && entity.Options.Mode == AudioEngineCaptureMode.Explicit))
                    return;

                if (entity.Talking && muted)
                {
                    entity.Talking = false;
                    AudioSender.EndSending (source);
                    return;
                }

                if (e.Available < source.CodecSettings.FrameSize)
                    return;

                bool talking = entity.Talking;
                if (entity.CurrentTargets == null || entity.CurrentTargets.Length == 0)
                {
                    entity.TargetType = TargetType.Channel;
                    var currentChannel = Context.GetCurrentChannel();
                    if (currentChannel == null)
                        return;

                    entity.CurrentTargets = new[] { currentChannel.ChannelId };
                }

                AudioEngineCaptureMode mode = entity.Options.Mode;

                int framesAvailable = e.Available / source.CodecSettings.FrameSize;
                int talkingFrames = (mode == AudioEngineCaptureMode.Explicit) ? framesAvailable : 0;
                int talkingIndex = -1;

                byte[][] frames = new byte[framesAvailable][];
                for (int i = 0; i < frames.Length; ++i)
                {
                    byte[] samples = entity.AudioCapture.ReadSamples (source.CodecSettings.FrameSize);
                    if (mode == AudioEngineCaptureMode.Activated)
                    {
                        talking = entity.VoiceActivation.IsTalking (samples);
                        if (talking)
                        {
                            talkingFrames++;
                            if (talkingIndex == -1)
                                talkingIndex = i;
                        }

                        if (talking && !entity.Talking)
                            AudioSender.BeginSending (source);
                    }

                    frames[i] = samples;
                }

                if (talkingFrames > 0 && talkingFrames != frames.Length)
                {
                    byte[][] actualFrames = new byte[talkingFrames][];
                    Array.Copy (frames, talkingIndex, actualFrames, 0, talkingFrames);
                    frames = actualFrames;
                }

                if (talking)
                    AudioSender.SendAudioDataAsync (source, entity.TargetType, entity.CurrentTargets, frames);
                else if (entity.Talking && entity.Options.Mode == AudioEngineCaptureMode.Activated)
                    AudioSender.EndSending (source);

                entity.Talking = talking;
            }
        }
        private void OnSamplesAvailable(object sender, SamplesAvailableEventArgs e)
        {
            if (this.activation == null)
                return;
            if (e.Available < FrameSize)
                return;

            byte[] samples = e.Provider.ReadSamples (FrameSize);
            ActivationLevel = this.activation.GetLevel (samples);
            IsActivating = this.activation.IsTalking (samples);
        }
Exemplo n.º 3
0
        private void OnSamplesAvailable(object sender, SamplesAvailableEventArgs e)
        {
            lock (captures)
            {
                AudioSource        source;
                AudioCaptureEntity entity;
                if (!captureToSourceLookup.TryGetValue(e.Provider, out source) || !captures.TryGetValue(source, out entity))
                {
                    return;
                }

                bool muted = (entity.Muted || this.captureMuted);

                if ((!entity.Talking && muted) || (!entity.AudioCapture.IsCapturing && entity.Options.Mode == AudioEngineCaptureMode.Explicit))
                {
                    return;
                }

                if (entity.Talking && muted)
                {
                    entity.Talking = false;
                    AudioSender.EndSending(source);
                    return;
                }

                if (e.Available < source.CodecSettings.FrameSize)
                {
                    return;
                }

                bool talking = entity.Talking;
                if (entity.CurrentTargets == null || entity.CurrentTargets.Length == 0)
                {
                    entity.TargetType = TargetType.Channel;
                    var currentChannel = Context.GetCurrentChannel();
                    if (currentChannel == null)
                    {
                        return;
                    }

                    entity.CurrentTargets = new[] { currentChannel.ChannelId };
                }

                AudioEngineCaptureMode mode = entity.Options.Mode;

                int framesAvailable = e.Available / source.CodecSettings.FrameSize;
                int talkingFrames   = (mode == AudioEngineCaptureMode.Explicit) ? framesAvailable : 0;
                int talkingIndex    = -1;

                byte[][] frames = new byte[framesAvailable][];
                for (int i = 0; i < frames.Length; ++i)
                {
                    byte[] samples = entity.AudioCapture.ReadSamples(source.CodecSettings.FrameSize);
                    if (mode == AudioEngineCaptureMode.Activated)
                    {
                        talking = entity.VoiceActivation.IsTalking(samples);
                        if (talking)
                        {
                            talkingFrames++;
                            if (talkingIndex == -1)
                            {
                                talkingIndex = i;
                            }
                        }

                        if (talking && !entity.Talking)
                        {
                            AudioSender.BeginSending(source);
                        }
                    }

                    frames[i] = samples;
                }

                if (talkingFrames > 0 && talkingFrames != frames.Length)
                {
                    byte[][] actualFrames = new byte[talkingFrames][];
                    Array.Copy(frames, talkingIndex, actualFrames, 0, talkingFrames);
                    frames = actualFrames;
                }

                if (talking)
                {
                    AudioSender.SendAudioDataAsync(source, entity.TargetType, entity.CurrentTargets, frames);
                }
                else if (entity.Talking && entity.Options.Mode == AudioEngineCaptureMode.Activated)
                {
                    AudioSender.EndSending(source);
                }

                entity.Talking = talking;
            }
        }