Mark Heath avatar Mark Heath committed 46ab975

now drawn from ReaPeaks mipmap

Comments (0)

Files changed (3)

WpfWaveform/MainWindow.xaml.cs

         void MainWindow_Loaded(object sender, RoutedEventArgs e)
         {
             var generator = new WaveFormPointsGenerator();
-            var peaks = generator.GetPeaks(@"E:\Audio\Music\Coldplay\X&Y\04-Fix You.mp3", 100);
+            var mipMap = generator.GetPeaks(@"E:\Audio\Music\Coldplay\X&Y\04-Fix You.mp3", 4100);
             var strokeBrush = new SolidColorBrush(Color.FromRgb(0xC1, 0xC1, 0x93));
-            canvas.Children.Add(generator.GetBezierPath(peaks.Select(p => p.LeftMax), 0, 2, 110, -100, strokeBrush, Brushes.Beige));
-            canvas.Children.Add(generator.GetBezierPath(peaks.Select(p => p.LeftMin), 0, 2, 110, -100, strokeBrush, Brushes.Beige));
+            canvas.Children.Add(generator.GetBezierPath(mipMap.Peaks.Select(p => p.Channels[0].Max / 32768.0), 0, 2, 110, -100, strokeBrush, Brushes.Beige));
+            canvas.Children.Add(generator.GetBezierPath(mipMap.Peaks.Select(p => p.Channels[0].Min / 32768.0), 0, 2, 110, -100, strokeBrush, Brushes.Beige));
         }
 
     }

WpfWaveform/WaveFormPointsGenerator.cs

 
 namespace WpfWaveform
 {
-    class Peak
-    {
-        public double LeftMin  { get; private set; }
-        public double LeftMax  { get; private set; }
-        public double RightMin { get; private set; }
-        public double RightMax { get; private set; }
-
-        public Peak(double leftMin, double leftMax, double rightMin, double rightMax)
-        {
-            this.LeftMin  = leftMin;
-            this.LeftMax  = leftMax;
-            this.RightMin = rightMin;
-            this.RightMax = rightMax;
-        }
-    }
-
     class WaveFormPointsGenerator
     {
-        public IEnumerable<Peak> GetPeaks(string fileName, int millisecondsPerUpdate)
+        public MipMap GetPeaks(string fileName, int samplesPerPeak)
         {
-            List<Peak> peaks = new List<Peak>();
-            peaks.Add(new Peak(0,0,0,0));
+            MipMap m = new MipMap();
+            m.DivisionFactor = samplesPerPeak;
+            
+            List<PeakValues> peaks = new List<PeakValues>();
             using (var reader = new Mp3FileReader(fileName))
             {
-                int stepSize = (reader.WaveFormat.AverageBytesPerSecond / 1000) * millisecondsPerUpdate;
+                int channels = reader.WaveFormat.Channels;
+                int stepSize = samplesPerPeak * channels * (reader.WaveFormat.BitsPerSample / 8);
                 WaveBuffer buffer = new WaveBuffer(stepSize);
                 int read;
                 while ((read = reader.Read(buffer.ByteBuffer, 0, stepSize)) > 0)
                 {
+                    PeakValues peakValues = new PeakValues(reader.WaveFormat.Channels);
                     int samples = read / 2; // assume 16 bit
-                    double maxLeft = 0;
-                    double minLeft = 0;
-                    double maxRight = 0;
-                    double minRight = 0;
-                    for (int sample = 0; sample < samples; sample += 2)
+                    
+                    for (int index = 0; index < samples; index++)
                     {
-                        double sampleLeft = buffer.ShortBuffer[sample] / 32768.0;
-                        maxLeft = Math.Max(maxLeft, sampleLeft);
-                        minLeft = Math.Min(minLeft, sampleLeft);
-                        double sampleRight = buffer.ShortBuffer[sample + 1] / 32768.0;
-                        maxRight = Math.Max(maxRight, sampleLeft);
-                        minRight = Math.Min(minRight, sampleLeft);
+                        int ch = index % channels;
+                        peakValues.Channels[ch].Max = Math.Max(peakValues.Channels[ch].Max, buffer.ShortBuffer[index]);
+                        peakValues.Channels[ch].Min = Math.Min(peakValues.Channels[ch].Min, buffer.ShortBuffer[index]);
                     }
-                    peaks.Add(new Peak(minLeft, maxLeft, minRight, maxRight));
+                    peaks.Add(peakValues);
                 }
             }
-            peaks.Add(new Peak(0,0,0,0));
-            return peaks;
+            m.Peaks = peaks.ToArray();
+            return m;
         }
 
         public Path GetBezierPath(IEnumerable<double> magnitude, double xOffset, double xStep, double yOffset, double yMult, Brush stroke, Brush fill)
         private IEnumerable<Point> GetPoints(IEnumerable<double> magnitude, double xOffset, double xStep, double yOffset, double yMult)
         {
             List<Point> points = new List<Point>();
+            points.Add(new Point(xOffset, yOffset)); xOffset += xStep; // extra zero point at beginning
             foreach (var m in magnitude)
             {
                 points.Add(new Point(xOffset, yOffset + m * yMult));
                 xOffset += xStep;
             }
+            points.Add(new Point(xOffset, yOffset)); xOffset += xStep; // extra zero point at end
             return points;
         }
 

WpfWaveform/WpfWaveform.csproj

       <Generator>MSBuild:Compile</Generator>
       <SubType>Designer</SubType>
     </ApplicationDefinition>
+    <Compile Include="ReaPeaksFileReader.cs" />
     <Compile Include="WaveFormPointsGenerator.cs" />
     <Page Include="MainWindow.xaml">
       <Generator>MSBuild:Compile</Generator>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.