1. Mark Heath
  2. WpfWaveform

Commits

Mark Heath  committed 806702a

Ability to select file

  • Participants
  • Parent commits 46ab975
  • Branches default

Comments (0)

Files changed (3)

File WpfWaveform/MainWindow.xaml

View file
         xmlns:local="clr-namespace:WpfWaveform"
         Title="MainWindow" Height="350" Width="525">
     <Grid>
-        <Border BorderBrush="LightBlue" BorderThickness="5" Margin="0,5,0,0">
+        <Grid.RowDefinitions>
+            <RowDefinition Height="Auto" />
+            <RowDefinition Height="282*" />
+        </Grid.RowDefinitions>
+        <StackPanel Orientation="Horizontal" Margin="5">
+            <Button Content="Load" Width="80" Click="Button_Click" />
+        </StackPanel>
+        <Border BorderBrush="LightBlue" BorderThickness="5" Grid.Row="1">
             <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                 <local:AutoSizeCanvas x:Name="canvas" Background="Transparent"/>
             </ScrollViewer>

File WpfWaveform/MainWindow.xaml.cs

View file
 using System.Windows.Navigation;
 using System.Windows.Shapes;
 using NAudio.Wave;
+using Microsoft.Win32;
 
 namespace WpfWaveform
 {
 
         void MainWindow_Loaded(object sender, RoutedEventArgs e)
         {
+
+        }
+
+        private void Button_Click(object sender, RoutedEventArgs e)
+        {
+            OpenFileDialog ofd = new OpenFileDialog();
+            ofd.Filter = "All supported files|*.wav;*.mp3;*.ReaPeaks";
+            bool? result = ofd.ShowDialog();
+            if (result.HasValue && result.Value)
+            {
+                GenerateWaveform(ofd.FileName);
+            }
+        }
+
+        private void GenerateWaveform(string fileName)
+        {
             var generator = new WaveFormPointsGenerator();
-            var mipMap = generator.GetPeaks(@"E:\Audio\Music\Coldplay\X&Y\04-Fix You.mp3", 4100);
+            var mipMap = generator.GetPeaks(fileName, 4100);
             var strokeBrush = new SolidColorBrush(Color.FromRgb(0xC1, 0xC1, 0x93));
+            canvas.Children.Clear();
             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));
         }
 
+
     }
 }

File WpfWaveform/ReaPeaksFileReader.cs

View file
         private string magicHeader;
         private int channels;
         private int sampleRate;
-        private List<MipMap> mipMaps;
+
+        public MipMap[] MipMaps { get; private set; }
 
         // http://www.reaper.fm/sdk/reapeaks.txt
         public ReaPeaksFileReader(string filename)
         {
+            var mipMaps = new List<MipMap>();
             using (BinaryReader reader = new BinaryReader(File.OpenRead(filename)))
             {
                 this.magicHeader = Encoding.ASCII.GetString(reader.ReadBytes(4));
                 int sourceFilesize = reader.ReadInt32();
                 for (int mipMap = 0; mipMap < mipMapCount; mipMap++)
                 {
-                    this.mipMaps.Add(ReadMipMap(reader));
+                    mipMaps.Add(ReadMipMap(reader));
                 }
             }
+            this.MipMaps = mipMaps.ToArray();
         }
 
         private MipMap ReadMipMap(BinaryReader reader)