Пример #1
0
        private void Audio_FrameArrived(object sender, AudioBeamFrameArrivedEventArgs e)
        {
            AudioBeamFrameReference frameReference = e.FrameReference;

            using (var frameList = frameReference.AcquireBeamFrames()) {
                if (frameList == null)
                {
                    return;
                }

                IReadOnlyList <AudioBeamSubFrame> subFrameList = frameList[0].SubFrames;
                List <float> beamAngles = new List <float> {
                };
                foreach (var frame in subFrameList)
                {
                    if (frame.BeamAngleConfidence > 0.3)
                    {
                        beamAngles.Add(frame.BeamAngle);
                    }
                }

                byte[] bytes = new byte[beamAngles.Count * 4 + 1];
                Buffer.BlockCopy(BitConverter.GetBytes(beamAngles.Count), 0, bytes, 0, 1); // first 1 byte number of beams
                for (int i = 0; i < beamAngles.Count; ++i)
                {
                    Buffer.BlockCopy(BitConverter.GetBytes(beamAngles[i]), 0, bytes, i * 4 + 1, 4);
                }

                this.client.Publish("/kinect/detected/audio", bytes);
            }

            ++this.kinectFrameCount;
        }
Пример #2
0
        private void Reader_FrameArrived(object sender, AudioBeamFrameArrivedEventArgs e)
        {
            AudioBeamFrameReference frameReference = e.FrameReference;
            AudioBeamFrameList      frameList      = frameReference.AcquireBeamFrames();

            if (frameList != null)
            {
                using (frameList)
                {
                    byte[] audioBuffer = new byte[_kinect.AudioSource.SubFrameLengthInBytes];
                    IReadOnlyList <AudioBeamSubFrame> subFrameList = frameList[0].SubFrames;
                    foreach (AudioBeamSubFrame subFrame in subFrameList)
                    {
                        int bytesRecorded = (int)subFrame.FrameLengthInBytes;
                        subFrame.CopyFrameDataToArray(audioBuffer);


                        if (_waveBufferPos + bytesRecorded < _waveBuffer.Length)
                        {
                            Array.Copy(audioBuffer, 0, _waveBuffer, _waveBufferPos, bytesRecorded);
                            _waveBufferPos += bytesRecorded;
                        }
                        else
                        {
                            PublishBuffer();

                            Array.Copy(audioBuffer, _waveBuffer, bytesRecorded);
                            _waveBufferPos = bytesRecorded;
                        }
                    }
                }
            }
        }
Пример #3
0
        private void Audio_FrameArrived(object sender, AudioBeamFrameArrivedEventArgs e)
        {
            AudioBeamFrameReference frameReference = e.FrameReference;

            using (var frameList = frameReference.AcquireBeamFrames()) {
                if (frameList == null)
                {
                    return;
                }

                IReadOnlyList <AudioBeamSubFrame> subFrameList = frameList[0].SubFrames;

                foreach (var frame in subFrameList)
                {
                    if (frame.BeamAngleConfidence > 0.3)
                    {
                        byte[] bytes = new byte[4];
                        Buffer.BlockCopy(BitConverter.GetBytes(frame.BeamAngle), 0, bytes, 0, 4);
                        this.client.Publish("/kinect/detected/audio", bytes);
                    }

#if PUBLISH_RAW_AUDIO
                    byte[] audioBuffer = new byte[this.kinectSensor.AudioSource.SubFrameLengthInBytes];
                    frame.CopyFrameDataToArray(audioBuffer);
                    byte[] convertedBuffer = new byte[audioBuffer.Length >> 1];
                    ConvertKinectAudioStream(audioBuffer, convertedBuffer);
                    this.client.Publish("/kinect/stream/rawaudio", convertedBuffer);
#endif
                    ++this.kinectFrameCount;
                }
            }

            this.client.Publish("/kinect/audio/alive", new byte[1]);
        }
Пример #4
0
        private void Reader_AudioBeamFrameArrived(object sender, AudioBeamFrameArrivedEventArgs e)
        {
            AudioBeamFrameReference frameReference = e.FrameReference;
            AudioBeamFrameList      frameList      = frameReference.AcquireBeamFrames();

            if (frameList != null)
            {
                using (frameList)
                {
                    // Only one audio beam is supported. Get the sub frame list for this beam
                    IReadOnlyList <AudioBeamSubFrame> subFrameList = frameList[0].SubFrames;

                    this.fullAudio = new Byte[audioBuffer.Length * subFrameList.Count];
                    int start = 0;
                    foreach (AudioBeamSubFrame subFrame in subFrameList)
                    {
                        subFrame.CopyFrameDataToArray(this.audioBuffer);
                        this.audioBuffer.CopyTo(this.fullAudio, start);
                        start += this.audioBuffer.Length;
                    }

                    this.publisher.SendByteArray(this.fullAudio);
                    this.fullAudio = null;
                }
            }
        }
        private void onAudioFrameArrived(object sender, AudioBeamFrameArrivedEventArgs e)
        {
            AudioBeamFrameReference audioFrameRefrence = e.FrameReference;

            try
            {
                AudioBeamFrameList frameList = audioFrameRefrence.AcquireBeamFrames();
                if (frameList != null)
                {
                    using (frameList)
                    {
                        IReadOnlyList <AudioBeamSubFrame> subFrameList = frameList[0].SubFrames;

                        foreach (AudioBeamSubFrame subFrame in subFrameList)
                        {
                            this.audioContainer.utcTime             = (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
                            this.audioContainer.beamAngle           = subFrame.BeamAngle;
                            this.audioContainer.beamAngleConfidence = subFrame.BeamAngleConfidence;
                            byte[] array = new byte[this.audioSource.SubFrameLengthInBytes];
                            subFrame.CopyFrameDataToArray(array);
                            for (int i = 0; i < array.Length; i += sizeof(float))
                            {
                                audioContainer.audioStream[(int)(i / sizeof(float))] = BitConverter.ToSingle(array, i);
                            }
                            string jsonString = JsonConvert.SerializeObject(this.audioContainer);
                            int    diff       = 4100 - jsonString.Length;
                            for (int i = 0; i < diff; i++)
                            {
                                jsonString += " ";
                            }
                            byte[] transmittedData = new byte[jsonString.Length * sizeof(char)];
                            System.Buffer.BlockCopy(jsonString.ToCharArray(), 0, transmittedData, 0, transmittedData.Length);
                            this.audioConnector.Broadcast(transmittedData);
                            subFrame.Dispose();
                        }
                    }
                    frameList.Dispose();
                }
            }
            catch
            {
            }
        }
Пример #6
0
        private void beamFrameArrived(object sender, AudioBeamFrameArrivedEventArgs e)
        {
            AudioBeamFrameReference frameReference = e.FrameReference;

            AudioBeamFrameList frameList = frameReference.AcquireBeamFrames();

            if (frameList != null)
            {
                // AudioBeamFrameList is IDisposable
                using (frameList)
                {
                    float readAngle = frameList[0].AudioBeam.BeamAngle;
                    if (readAngle != this.beamAngle)
                    {
                        //Console.WriteLine(subFrame.BeamAngle);
                        this.beamAngle = readAngle;
                        foreach (BeamAngleListener listener in beamAngleListeners)
                        {
                            listener.onBeamAngleChanged(beamAngle);
                        }
                    }

                    /*
                     * // Only one audio beam is supported. Get the sub frame list for this beam
                     * IReadOnlyList<AudioBeamSubFrame> subFrameList = frameList[0].SubFrames;
                     *
                     * // Loop over all sub frames, extract audio buffer and beam information
                     * foreach (AudioBeamSubFrame subFrame in subFrameList)
                     * {
                     *  if (subFrame.BeamAngle != this.beamAngle)
                     *  {
                     *      //Console.WriteLine(subFrame.BeamAngle);
                     *      this.beamAngle = subFrame.BeamAngle;
                     *      foreach (BeamAngleListener listener in beamAngleListeners)
                     *      {
                     *          listener.onBeamAngleChanged(subFrame.BeamAngle);
                     *      }
                     *  }
                     * }
                     * */
                }
            }
        }
Пример #7
0
        private void Reader_FrameArrived(object sender, AudioBeamFrameArrivedEventArgs e)
        {
            AudioBeamFrameReference frameReference = e.FrameReference;
            AudioBeamFrameList      frameList      = frameReference.AcquireBeamFrames();

            if (frameList != null)
            {
                using (frameList)
                {
                    IReadOnlyList <AudioBeamSubFrame> subFrameList = frameList[0].SubFrames;

                    // Loop over all sub frames, extract audio buffer and beam information
                    foreach (AudioBeamSubFrame subFrame in subFrameList)
                    {
                        subFrame.CopyFrameDataToArray(this.audioBuffer);
                        if (fileStream.CanWrite == true)
                        {
                            fileStream.Write(audioBuffer, 0, audioBuffer.Length);
                            size += audioBuffer.Length;
                        }
                    }
                }
            }
        }
Пример #8
0
/**
 *      void _selectedController_StateChanged(object sender, XboxControllerStateChangedEventArgs e)
 *      {
 *          OnPropertyChanged("SelectedController");
 *
 *          // Where the action happens with the controller.
 *          if (SelectedController.IsAPressed)
 *          {
 *              Console.WriteLine("A is pressed");
 *          }
 *          else if (SelectedController.IsBPressed)
 *          {
 *              Console.WriteLine("B is pressed");
 *          }
 *      }
 *
 *      public XboxController SelectedController
 *      {
 *          get { return _selectedController; }
 *      }
 *
 *      volatile bool _keepRunning;
 *      XboxController _selectedController;
 *
 *      public void OnPropertyChanged(string name)
 *      {
 *          if (PropertyChanged != null)
 *          {
 *              Action a = () => { PropertyChanged(this, new PropertyChangedEventArgs(name)); };
 *              Dispatcher.BeginInvoke(a, null);
 *          }
 *      }
 *
 *
 *
 *      private void SelectedControllerChanged(object sender, RoutedEventArgs e)
 *      {
 *          _selectedController = XboxController.RetrieveController(((ComboBox)sender).SelectedIndex);
 *          OnPropertyChanged("SelectedController");
 *      }
 **/
        void audioReader_FrameArrived(object sender, AudioBeamFrameArrivedEventArgs e)
        {
            //Only interperet Audio if in Audible Tracking state
            if (this.trackingMode == TrackingMode.AUDIBLE)
            {
                AudioBeamFrameReference frameReference = e.FrameReference;

                try
                {
                    AudioBeamFrameList frameList = frameReference.AcquireBeamFrames();

                    if (frameList != null)
                    {
                        // AudioBeamFrameList is IDisposable
                        using (frameList)
                        {
                            // Only one audio beam is supported. Get the sub frame list for this beam
                            IReadOnlyList <AudioBeamSubFrame> subFrameList = frameList[0].SubFrames;

                            // Loop over all sub frames, extract audio buffer and beam information
                            foreach (AudioBeamSubFrame subFrame in subFrameList)
                            {
                                // Check if beam angle and/or confidence have changed
                                bool updateBeam = false;

                                if (subFrame.BeamAngle != this.beamAngle)
                                {
                                    this.beamAngle = subFrame.BeamAngle;
                                    updateBeam     = true;
                                }

                                if (subFrame.BeamAngleConfidence != this.beamAngleConfidence)
                                {
                                    this.beamAngleConfidence = subFrame.BeamAngleConfidence;
                                    updateBeam = true;
                                }
                                if (updateBeam)
                                {
                                    // Refresh display of audio beam
                                    this.AudioBeamChanged();
                                }
                                else
                                {
                                    // If there are has been no update in the audio beam then return the X servo velocity to 0
                                    if (panTilt.IsReady)
                                    {
                                        this.cannonXVelocity = 5;
                                        panTilt.PanX(this.CannonXVelocity);
                                    }
                                }
                            }
                        }
                    }
                }

                catch (Exception)
                {
                    // Ignore if the frame is no longer available
                }
            }
        }
Пример #9
0
        /// <summary>
        /// Handles the audio frame data arriving from the sensor
        /// </summary>
        /// <param name="sender">object sending the event</param>
        /// <param name="e">event arguments</param>
        private void Reader_FrameArrived(object sender, AudioBeamFrameArrivedEventArgs e)
        {
            AudioBeamFrameReference frameReference = e.FrameReference;
            AudioBeamFrameList      frameList      = frameReference.AcquireBeamFrames();

            if (frameList != null)
            {
                // AudioBeamFrameList is IDisposable
                using (frameList)
                {
                    // Only one audio beam is supported. Get the sub frame list for this beam
                    IReadOnlyList <AudioBeamSubFrame> subFrameList = frameList[0].SubFrames;

                    // Loop over all sub frames, extract audio buffer and beam information
                    foreach (AudioBeamSubFrame subFrame in subFrameList)
                    {
                        // Check if beam angle and/or confidence have changed
                        bool updateBeam = false;

                        if (subFrame.BeamAngle != this.beamAngle)
                        {
                            this.beamAngle = subFrame.BeamAngle;
                            updateBeam     = true;
                        }

                        if (subFrame.BeamAngleConfidence != this.beamAngleConfidence)
                        {
                            this.beamAngleConfidence = subFrame.BeamAngleConfidence;
                            updateBeam = true;
                        }

                        if (updateBeam)
                        {
                            // Refresh display of audio beam
                            this.AudioBeamChanged();
                        }

                        // Process audio buffer
                        subFrame.CopyFrameDataToArray(this.audioBuffer);

                        for (int i = 0; i < this.audioBuffer.Length; i += BytesPerSample)
                        {
                            // Extract the 32-bit IEEE float sample from the byte array
                            float audioSample = BitConverter.ToSingle(this.audioBuffer, i);

                            this.accumulatedSquareSum += audioSample * audioSample;
                            ++this.accumulatedSampleCount;

                            if (this.accumulatedSampleCount < SamplesPerColumn)
                            {
                                continue;
                            }

                            float meanSquare = this.accumulatedSquareSum / SamplesPerColumn;

                            if (meanSquare > 1.0f)
                            {
                                // A loud audio source right next to the sensor may result in mean square values
                                // greater than 1.0. Cap it at 1.0f for display purposes.
                                meanSquare = 1.0f;
                            }

                            // Calculate energy in dB, in the range [MinEnergy, 0], where MinEnergy < 0
                            float energy = MinEnergy;

                            if (meanSquare > 0)
                            {
                                energy = (float)(10.0 * Math.Log10(meanSquare));
                            }

                            lock (this.energyLock)
                            {
                                // Normalize values to the range [0, 1] for display
                                this.energy[this.energyIndex] = (MinEnergy - energy) / MinEnergy;
                                this.energyIndex = (this.energyIndex + 1) % this.energy.Length;
                                ++this.newEnergyAvailable;
                            }

                            this.accumulatedSquareSum   = 0;
                            this.accumulatedSampleCount = 0;
                        }
                    }
                }
            }
        }
Пример #10
0
        /// <summary>
        /// Handles the audio frame data arriving from the sensor
        /// </summary>
        /// <param name="sender">object sending the event</param>
        /// <param name="e">event arguments</param>
        public void Reader_FrameArrived(object sender, AudioBeamFrameArrivedEventArgs e)
        {
            AudioBeamFrameReference frameReference = e.FrameReference;

            try
            {
                AudioBeamFrameList frameList = frameReference.AcquireBeamFrames();

                if (frameList != null)
                {
                    // AudioBeamFrameList is IDisposable
                    using (frameList)
                    {
                        // Only one audio beam is supported. Get the sub frame list for this beam
                        IReadOnlyList <AudioBeamSubFrame> subFrameList = frameList[0].SubFrames;

                        // Loop over all sub frames, extract audio buffer and beam information
                        foreach (AudioBeamSubFrame subFrame in subFrameList)
                        {
                            subFrame.CopyFrameDataToArray(this.audioBuffer);


                            for (int i = 0; i < this.audioBuffer.Length; i += BytesPerSample)
                            {
                                // Extract the 32-bit IEEE float sample from the byte array
                                float audioSample = BitConverter.ToSingle(this.audioBuffer, i);

                                this.accumulatedSquareSum += audioSample * audioSample;
                                ++this.accumulatedSampleCount;

                                if (this.accumulatedSampleCount < SamplesPerColumn)
                                {
                                    continue;
                                }

                                float meanSquare = this.accumulatedSquareSum / SamplesPerColumn;

                                if (meanSquare > 1.0f)
                                {
                                    //    // A loud audio source right next to the sensor may result in mean square values
                                    //    // greater than 1.0. Cap it at 1.0f for display purposes.
                                    meanSquare = 1.0f;
                                }

                                // Calculate energy in dB, in the range [MinEnergy, 0], where MinEnergy < 0
                                float energy = MinEnergy;

                                if (meanSquare > 0)
                                {
                                    energy = (float)(10.0 * Math.Log10(meanSquare));
                                }

                                lock (this.energyLock)
                                {
                                    // Normalize values to the range [0, 1] for display
                                    this.energy[this.energyIndex] = (MinEnergy - energy) / MinEnergy;
                                    //  this.energy[this.energyIndex] = energy;
                                    this.energyIndex = (this.energyIndex + 1) % this.energy.Length;
                                    ++this.newEnergyAvailable;
                                }

                                this.accumulatedSquareSum   = 0;
                                this.accumulatedSampleCount = 0;
                            }
                            audioPreAnalysis.analyzeAudio(this.audioBuffer, this.energy);
                        }
                    }
                }
            }
            catch (Exception)
            {
                // Ignore if the frame is no longer available
            }
        }