public static SampleSource[] CreateFromWaveFile(string fileName) { using (var reader = new WaveFileReader(fileName)) { ISampleProvider sp; int sourceSamples; if (reader.WaveFormat.Encoding == WaveFormatEncoding.Pcm) { if (reader.WaveFormat.BitsPerSample == 16) { sp = new Pcm16BitToSampleProvider(reader); sourceSamples = (int)(reader.Length / 2); } else if (reader.WaveFormat.BitsPerSample == 24) { sp = new Pcm24BitToSampleProvider(reader); sourceSamples = (int)(reader.Length / 3); } else { throw new ArgumentException("Currently only 16 or 24 bit PCM samples are supported"); } } else if (reader.WaveFormat.Encoding == WaveFormatEncoding.IeeeFloat) { sp = new WaveToSampleProvider(reader); sourceSamples = (int)(reader.Length / 4); } else { throw new ArgumentException("Must be PCM or IEEE float"); } float[] sampleData = new float[sourceSamples]; int n = sp.Read(sampleData, 0, sourceSamples); if (n != sourceSamples) { throw new InvalidOperationException(String.Format("Couldn't read the whole sample, expected {0} samples, got {1}", n, sourceSamples)); } SampleSource[] result = new SampleSource[9]; // Normal pitch int normal = 4; result[normal] = new SampleSource(sampleData, sp.WaveFormat); int pitch = 5; for (int currentPitch = 0; currentPitch < normal; currentPitch++, pitch--) { float[] changedPitchData = new float[sampleData.Length/pitch]; for (int j = 0, i = 0; i < sampleData.Length && j < changedPitchData.Length; i++) { if (i % pitch == 0) { changedPitchData[j] = sampleData[i]; j++; } } result[currentPitch] = new SampleSource(changedPitchData, sp.WaveFormat); } pitch = 2; for (int currentPitch = normal+1; currentPitch < result.Length; currentPitch++, pitch++) { float[] changedPitchData = new float[sampleData.Length * pitch]; for (int j = 0, i = 0; i < sampleData.Length && j < changedPitchData.Length; j++) { changedPitchData[j] = sampleData[i]; if (j % pitch == 0) { i++; } } result[currentPitch] = new SampleSource(changedPitchData, sp.WaveFormat); } return result; } }
public MusicSampleProvider(SampleSource sampleSource) { this.sampleSource = sampleSource; }