Ejemplo n.º 1
0
        /// <summary>
        /// Releases the unmanaged resources used by the <see cref="WavInputAdapter"/> object and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                try
                {
                    if (disposing)
                    {
                        if ((object)m_dataReader != null)
                        {
                            m_dataReader.Close();
                            m_dataReader.Dispose();
                            m_dataReader = null;
                        }

                        if ((object)m_dataCache != null)
                        {
                            m_dataCache.Dispose();
                        }
                    }
                }
                finally
                {
                    m_disposed = true;          // Prevent duplicate dispose.
                    base.Dispose(disposing);    // Call base class Dispose().
                }
            }
        }
Ejemplo n.º 2
0
        public void TestFFTConvolutionWavStream()
        {
            WaveDataReader wavReader = WaveDataReader.FromFile("ToneWithHighFrequency.wav");

            while (wavReader.WaveStream.Position < wavReader.WaveStream.Length)
            {
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Attempts to close the file and release resources held by the adapter.
 /// </summary>
 protected override void AttemptDisconnection()
 {
     if ((object)m_dataReader != null)
     {
         m_dataReader.Close();
         m_dataReader.Dispose();
         m_dataReader = null;
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Attempts to close the file and release resources held by the adapter.
 /// </summary>
 protected override void AttemptDisconnection()
 {
     if (m_data != null)
     {
         m_data.Close();
         m_data.Dispose();
     }
     m_data = null;
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Attempts to open the file to start getting wave data.
        /// </summary>
        protected override void AttemptConnection()
        {
            m_dataReader = OpenWaveDataReader();
            m_dataIndex  = 0;

            m_startTime = DateTime.UtcNow.Ticks;

            new Thread(ProcessMeasurements)
            {
                IsBackground = true
            }.Start();
        }
Ejemplo n.º 6
0
        // Open wave reader - either from memory loaded WAV or directly from disk
        private WaveDataReader OpenWaveDataReader()
        {
            if (MemoryCache)
            {
                if ((object)m_dataCache == null)
                {
                    m_dataCache = new BlockAllocatedMemoryStream();

                    using (FileStream stream = File.OpenRead(WavFileName))
                        stream.CopyTo(m_dataCache);
                }

                m_dataCache.Position = 0;

                return(WaveDataReader.FromStream(m_dataCache));
            }

            return(WaveDataReader.FromFile(WavFileName));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Attempts to open the file to start getting wave data.
        /// </summary>
        protected override void AttemptConnection()
        {
            WaveFile fileInfo = WaveFile.Load(WavFileName, false);

            m_channels    = fileInfo.Channels;
            m_sampleRate  = fileInfo.SampleRate;
            m_numSamples  = fileInfo.DataChunk.ChunkSize / fileInfo.BlockAlignment;
            m_audioLength = fileInfo.AudioLength;

            m_data      = WaveDataReader.FromFile(WavFileName);
            m_dataIndex = 0;

            //if (file.Channels != OutputMeasurements.Length)
            //    throw new ArgumentException(string.Format("The number of channels in the WAV file must match the number of output measurements. Channels: {0}, Measurements: {1}", file.Channels, OutputMeasurements.Length));

            m_startTime = DateTime.UtcNow.Ticks;

            Thread t = new Thread(ProcessMeasurements);

            t.IsBackground = true;
            t.Start();
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Releases the unmanaged resources used by the <see cref="WavInputAdapter"/> object and optionally releases the managed resources.
 /// </summary>
 /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
 protected override void Dispose(bool disposing)
 {
     if (!m_disposed)
     {
         try
         {
             if (disposing)
             {
                 if (m_data != null)
                 {
                     m_data.Close();
                     m_data.Dispose();
                 }
                 m_data = null;
             }
         }
         finally
         {
             m_disposed = true;          // Prevent duplicate dispose.
             base.Dispose(disposing);    // Call base class Dispose().
         }
     }
 }
Ejemplo n.º 9
0
        private void ProcessMeasurements()
        {
            List <IMeasurement> measurements = new List <IMeasurement>((int)(Ticks.ToSeconds(GapThreshold) * m_sampleRate * m_channels * 1.1D));

            LittleBinaryValue[] sample;

            while (Enabled)
            {
                try
                {
                    SpinWait spinner = new SpinWait();

                    // Determine what time it is now
                    long now = DateTime.UtcNow.Ticks;

                    // Assign a timestamp to the next sample based on its location
                    // in the file relative to the other samples in the file
                    long timestamp = m_startTime + (m_dataIndex * Ticks.PerSecond / m_sampleRate);

                    if (now - timestamp > GapThreshold)
                    {
                        // Reset the start time and delay next transmission in an attempt to catch up
                        m_startTime = now - (m_dataIndex * Ticks.PerSecond / m_sampleRate) + Ticks.FromSeconds(RecoveryDelay);
                        timestamp   = now;
                        OnStatusMessage(MessageLevel.Info, "Start time reset.");
                    }

                    // Keep generating measurements until
                    // we catch up to the current time.
                    while (timestamp < now)
                    {
                        sample = m_dataReader.GetNextSample();

                        // If the sample is null, we've reached the end of the file - close and reopen,
                        // resetting the data index and start time
                        if (sample == null)
                        {
                            m_dataReader.Close();
                            m_dataReader.Dispose();

                            m_dataReader = OpenWaveDataReader();
                            m_dataIndex  = 0;

                            m_startTime = timestamp;
                            sample      = m_dataReader.GetNextSample();
                        }

                        // Create new measurements, one for each channel, and add them to the measurements list
                        for (int i = 0; i < m_channels; i++)
                        {
                            measurements.Add(Measurement.Clone(OutputMeasurements[i], sample[i].ConvertToType(TypeCode.Double), timestamp));
                        }

                        // Update the data index and recalculate the assigned timestamp for the next sample
                        m_dataIndex++;
                        timestamp = m_startTime + (m_dataIndex * Ticks.PerSecond / m_sampleRate);
                    }

                    OnNewMeasurements(measurements);
                    measurements.Clear();

                    while (DateTime.UtcNow.Ticks - timestamp <= GapThreshold / 100)
                    {
                        // Ahead of schedule -- pause for a moment
                        spinner.SpinOnce();
                    }
                }
                catch (Exception ex)
                {
                    OnProcessException(MessageLevel.Warning, ex);
                }
            }
        }