using NAudio.Wave.SampleProviders; namespace QuikDawEditor.SampleProviders; public class ClipSampleProvider : ISampleProvider { public string RelRes() { ReleaseResources(); return "Resources released"; } private WaveFormat m_waveFormat; internal ISampleProvider sourceProvider; internal ClipStream myClipStream; public WaveStream useWaveStream; public ClipSampleProvider() { //m_waveFormat = baseStream.WaveFormat; m_waveFormat = WaveFormat.CreateIeeeFloatWaveFormat(44100, 2); } public void AssignSampleProvider(bool reversed) { if (myClipStream.clipWaveStreamReversed == null) myClipStream.CreateReversedWaveStream(); switch (reversed) { case true: useWaveStream = myClipStream.clipWaveStreamReversed; break; case false: useWaveStream = myClipStream.clipWaveStreamNormal; break; } //Force resample to 44100 switch (useWaveStream.WaveFormat.Channels) { case 1: sourceProvider = new WdlResamplingSampleProvider(useWaveStream.ToSampleProvider().ToStereo(), 44100); break; case 2: sourceProvider = new WdlResamplingSampleProvider(useWaveStream.ToSampleProvider(), 44100); break; } } public WaveFormat WaveFormat { get { return m_waveFormat; } } public void Dispose() //make another method to dispose everything but the usewavestream!!!!!!!!!!!! { if (myClipStream != null) { myClipStream.clipWaveStreamNormal?.Close(); myClipStream.clipWaveStreamReversed?.Close(); } useWaveStream?.Dispose(); myClipStream = null; sourceProvider = null; } private int sampsRead; public int Read(float[] buffer, int offset, int count) { int totalSampsRead = 0; if (!projPlayer.IsProjectPlaying) { Array.Clear(buffer, 0, count); return count; } try { while (totalSampsRead < count) { sampsRead = sourceProvider.Read(buffer, offset + totalSampsRead, count - totalSampsRead); if (sampsRead == 0) { if (useWaveStream.Position == 0) break; // something wrong with the source stream //else loop useWaveStream.Position = 0; } totalSampsRead += sampsRead; } } catch (Exception bufEx) { Console.WriteLine("error with sourceProvider read: " + bufEx.Message + " ::::::(" + m_waveFormat.Channels.ToString() + " channels)"); } //catch (Exception ex) { projPlayer.StopPlayTimer(); ReleaseResources(); MessageBox.Show("Error in clip sampleprovider\n" + ex.Message); } totalSampsRead = (int)(totalSampsRead / (m_waveFormat.Channels / (double)2)); //if (totalSampsRead == 0) // To return silence //{ // totalSampsRead = count; // Array.Clear(buffer, 0, count); //} return totalSampsRead; } }