public void TestFFTConvolutionWavStream() { WaveDataReader wavReader = WaveDataReader.FromFile("ToneWithHighFrequency.wav"); while (wavReader.WaveStream.Position < wavReader.WaveStream.Length) { } }
// 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)); }
/// <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(); }
// Generates new measurements since the last time this was called. private void ProcessMeasurements() { // Declare the variables use in this method. 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("Start time reset."); } // Keep generating measurements until // we catch up to the current time. while (timestamp < now) { sample = m_data.GetNextSample(); // If the sample is null, we've reached the end of the file. // Close and reopen it, resetting the data index and start time. if (sample == null) { m_data.Close(); m_data.Dispose(); m_data = WaveDataReader.FromFile(WavFileName); m_dataIndex = 0; m_startTime = timestamp; sample = m_data.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(ex); } } }