Commits

Mark Heath committed ee041c8

Can now show WAV, MP3 and ReaPeaks

Comments (0)

Files changed (2)

WpfWaveform/ReaPeaksFileReader.cs

                 int sourceFilesize = reader.ReadInt32();
                 for (int mipMap = 0; mipMap < mipMapCount; mipMap++)
                 {
-                    mipMaps.Add(ReadMipMap(reader));
+                    mipMaps.Add(ReadMipMapHeader(reader));
+                }
+                for (int mipMap = 0; mipMap < mipMapCount; mipMap++)
+                {
+                    ReadMipMapData(reader, mipMaps[mipMap]);
                 }
             }
             this.MipMaps = mipMaps.ToArray();
         }
 
-        private MipMap ReadMipMap(BinaryReader reader)
+        private MipMap ReadMipMapHeader(BinaryReader reader)
         {
             var mipMap = new MipMap();
             mipMap.DivisionFactor = reader.ReadInt32(); // number of samples per peak
             int numberOfPeakSamples = reader.ReadInt32();
             mipMap.Peaks = new PeakValues[numberOfPeakSamples];
-            for (int n = 0; n < numberOfPeakSamples; n++)
+            return mipMap;
+        }
+
+        private void ReadMipMapData(BinaryReader reader, MipMap mipMap)
+        {
+            for (int n = 0; n < mipMap.Peaks.Length; n++)
             {
                 mipMap.Peaks[n] = new PeakValues(channels);
                 for (int ch = 0; ch < channels; ch++)
                     }
                     else // 1.0
                     {
-                        mipMap.Peaks[n].Channels[ch].Min = mipMap.Peaks[n].Channels[ch].Max;
+                        mipMap.Peaks[n].Channels[ch].Min = (short)(0 - mipMap.Peaks[n].Channels[ch].Max);
                     }
                 }
             }
-            return mipMap;
         }
     }
 }

WpfWaveform/WaveFormPointsGenerator.cs

     {
         public MipMap GetPeaks(string fileName, int samplesPerPeak)
         {
+            if (fileName.EndsWith(".ReaPeaks", StringComparison.CurrentCultureIgnoreCase))
+            {
+                var reaPeaks = new ReaPeaksFileReader(fileName);
+                return reaPeaks.MipMaps[0];
+            }
+
             MipMap m = new MipMap();
             m.DivisionFactor = samplesPerPeak;
-            
+
             List<PeakValues> peaks = new List<PeakValues>();
-            using (var reader = new Mp3FileReader(fileName))
+            using (var reader = GetReader(fileName))
             {
                 int channels = reader.WaveFormat.Channels;
                 int stepSize = samplesPerPeak * channels * (reader.WaveFormat.BitsPerSample / 8);
             return m;
         }
 
+        private WaveStream GetReader(string fileName)
+        {
+            if (fileName.EndsWith(".mp3", StringComparison.CurrentCultureIgnoreCase))
+            {
+                return new Mp3FileReader(fileName);
+            }
+            else if (fileName.EndsWith(".wav", StringComparison.CurrentCultureIgnoreCase))
+            {
+                return new WaveFileReader(fileName);
+            }
+            else
+            {
+                throw new ArgumentException("Unsupported file type");
+            }
+        }
+
         public Path GetBezierPath(IEnumerable<double> magnitude, double xOffset, double xStep, double yOffset, double yMult, Brush stroke, Brush fill)
         {
             var points = GetPoints(magnitude, 0, 2, 110, -100).ToArray();