<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Kinect;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace kinectApp.Entities
{
    public class KinectAdapter : IDisposable
    {
        private KinectSensor iSensor;
        private MultiSourceFrameReader iFrameReader;
        private Texture2D iRGBVideo;
        private GraphicsDevice iGraphicsDevice;

        private byte[] iColourImageBuffer;
        private Body[] _bodies;
        private Joint[] _joints;
        private readonly static object iVideoLock = new object();
        private readonly static object iJointLock = new object();

        private const int kWidth = 1920;
        private const int kHeight = 1080;

        private List<Task> iProcessingTasks;

        public KinectAdapter(GraphicsDevice aGraphicsDevice)
        {
            iSensor = KinectSensor.GetDefault();
            iGraphicsDevice = aGraphicsDevice;

            iProcessingTasks = new List<Task>(500);
            iColourImageBuffer = new byte[kWidth * kHeight * 4];
        }

        /// <summary>
        /// Gets if the Kinect Sensor is currently open
        /// </summary>
        public bool IsOpen
        {
            get; private set;
        }

        /// <summary>
        /// Gets if a Kinect is available
        /// </summary>
        public bool IsAvailable
        {
            get; private set;
        }

        /// <summary>
        /// Gets a string representing the connection status of the Kinect
        /// </summary>
        public string ConnectedStatus
        {
            get
            {
                return string.Format("Source {0}AVAILABLE", (IsAvailable) ? string.Empty : "NOT ");
            }
        }

        /// <summary>
        /// Gets the current RGBVideo stream from the Kinect sensor.
        /// </summary>
        public Texture2D KinectRGBVideo
        {
            get
            {
                lock (iVideoLock)
                {
                    return iRGBVideo;
                }
            }
        }

        /// <summary>
        /// Gets the current found joints from the last frame.
        /// </summary>
        public Joint[] KinectJoints
        {
            get
            {
                lock(iJointLock)
                {
                    return _joints;
                }               
            }
        }

        /// <summary>
        /// Opens up a Kinect sensor attaching to the right events
        /// </summary>
        /// <param name="aHandler"></param>
        public void OpenSensor()
        {
            if (!IsOpen)
            {
                //Open Sensor so we can use it!
                iSensor.Open();
                IsOpen = true;

                iSensor.IsAvailableChanged += KSensor_IsAvailableChanged;
            
                //Setup the video feed from the Kinect Camera!
                iFrameReader = iSensor.OpenMultiSourceFrameReader(FrameSourceTypes.Color | FrameSourceTypes.Body);

                iFrameReader.MultiSourceFrameArrived += KFrameReader_MultiSourceFrameArrived;
            }
        }

        private void KFrameReader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
        {
            var T = Task.Factory.StartNew(() =>
            {
                // Retrieve multisource frame reference
                MultiSourceFrameReference multiRef = e.FrameReference;

                MultiSourceFrame multiFrame = multiRef.AcquireFrame();
                if (multiFrame == null) return;

                // Retrieve data stream frame references
                ColorFrameReference colorRef = multiFrame.ColorFrameReference;
                BodyFrameReference bodyRef = multiFrame.BodyFrameReference;

                ProcessRGBVideo(colorRef);
                ProcessJoints(bodyRef);

            }).ContinueWith((aTask) => iProcessingTasks.Remove(aTask));

            iProcessingTasks.Add(T);
        }

        /// <summary>
        /// Releases resources used by the Kinect objects
        /// </summary>
        public void CloseSensor()
        {
            if (IsOpen)
            {
                iSensor.IsAvailableChanged -= KSensor_IsAvailableChanged;
                iFrameReader.Dispose();
                iFrameReader = null;<commit_msg>Proper multithreading of processing Kinect<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Kinect;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace kinectApp.Entities
{
    public class KinectAdapter : IDisposable
    {
        private KinectSensor iSensor;
        private MultiSourceFrameReader iFrameReader;
        private Texture2D iRGBVideo;
        private GraphicsDevice iGraphicsDevice;

        private byte[] iColourImageBuffer;
        private Body[] _bodies;
        private Joint[] _joints;
        private readonly static object iVideoLock = new object();
        private readonly static object iJointLock = new object();

        private const int kWidth = 1920;
        private const int kHeight = 1080;

        private List<Task> iProcessingTasks;

        public KinectAdapter(GraphicsDevice aGraphicsDevice)
        {
            iSensor = KinectSensor.GetDefault();
            iGraphicsDevice = aGraphicsDevice;

            iProcessingTasks = new List<Task>(500);
            iColourImageBuffer = new byte[kWidth * kHeight * 4];
        }

        /// <summary>
        /// Gets if the Kinect Sensor is currently open
        /// </summary>
        public bool IsOpen
        {
            get; private set;
        }

        /// <summary>
        /// Gets if a Kinect is available
        /// </summary>
        public bool IsAvailable
        {
            get; private set;
        }

        /// <summary>
        /// Gets a string representing the connection status of the Kinect
        /// </summary>
        public string ConnectedStatus
        {
            get
            {
                return string.Format("Source {0}AVAILABLE", (IsAvailable) ? string.Empty : "NOT ");
            }
        }

        /// <summary>
        /// Gets the current RGBVideo stream from the Kinect sensor.
        /// </summary>
        public Texture2D KinectRGBVideo
        {
            get
            {
                lock (iVideoLock)
                {
                    return iRGBVideo;
                }
            }
        }

        /// <summary>
        /// Gets the current found joints from the last frame.
        /// </summary>
        public Joint[] KinectJoints
        {
            get
            {
                lock(iJointLock)
                {
                    return _joints;
                }               
            }
        }

        /// <summary>
        /// Opens up a Kinect sensor attaching to the right events
        /// </summary>
        /// <param name="aHandler"></param>
        public void OpenSensor()
        {
            if (!IsOpen)
            {
                //Open Sensor so we can use it!
                iSensor.Open();
                IsOpen = true;

                iSensor.IsAvailableChanged += KSensor_IsAvailableChanged;
            
                //Setup the video feed from the Kinect Camera!
                iFrameReader = iSensor.OpenMultiSourceFrameReader(FrameSourceTypes.Color | FrameSourceTypes.Body);

                iFrameReader.MultiSourceFrameArrived += KFrameReader_MultiSourceFrameArrived;
            }
        }

        private void KFrameReader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
        {
            var T = Task.Factory.StartNew(() =>
            {
                // Retrieve multisource frame reference
                MultiSourceFrameReference multiRef = e.FrameReference;

                MultiSourceFrame multiFrame = multiRef.AcquireFrame();
                if (multiFrame == null) return;

                // Retrieve data stream frame references
                ColorFrameReference colorRef = multiFrame.ColorFrameReference;
                BodyFrameReference bodyRef = multiFrame.BodyFrameReference;

                Task.Factory.StartNew(() => ProcessRGBVideo(colorRef));
                Task.Factory.StartNew(() => ProcessJoints(bodyRef));

            }).ContinueWith((aTask) => iProcessingTasks.Remove(aTask));

            iProcessingTasks.Add(T);
        }

        /// <summary>
        /// Releases resources used by the Kinect objects
        /// </summary>
        public void CloseSensor()
        {
            if (IsOpen)
            {
                iSensor.IsAvailableChanged -= KSensor_IsAvailableChanged;
                iFrameReader.Dispose();
                iFrameReader = null;
{'fraction_non_alphanumeric': 0.08055649051946595, 'fraction_numerical': 0.003365870077415012, 'mean_word_length': 1.5202148713599095, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7317152', 'n_tokens_mistral': 2261, 'n_tokens_neox': 2136, 'n_words': 557}