Skip to content

Commit 1a99d54

Browse files
committed
general coding style cleanup in WPF demo app
1 parent 04171e8 commit 1a99d54

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+245
-467
lines changed

NAudioWpfDemo/AudioPlaybackDemo/AudioPlayback.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Windows;
33
using NAudio.Wave;
44

5-
namespace NAudioWpfDemo
5+
namespace NAudioWpfDemo.AudioPlaybackDemo
66
{
77
class AudioPlayback : IDisposable
88
{
@@ -87,7 +87,7 @@ public void Dispose()
8787
Stop();
8888
CloseFile();
8989
playbackDevice?.Dispose();
90-
playbackDevice = null;
90+
playbackDevice = null;
9191
}
9292
}
9393
}

NAudioWpfDemo/AudioPlaybackDemo/AudioPlaybackDemoPlugin.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Windows.Controls;
5-
using NAudioWpfDemo.AudioPlaybackDemo;
65
using NAudioWpfDemo.Utils;
76

8-
namespace NAudioWpfDemo
7+
namespace NAudioWpfDemo.AudioPlaybackDemo
98
{
109
class AudioPlaybackDemoPlugin : IModule
1110
{

NAudioWpfDemo/AudioPlaybackDemo/AudioPlaybackDemoView.xaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
<UserControl x:Class="NAudioWpfDemo.AudioPlaybackDemoView"
1+
<UserControl x:Class="NAudioWpfDemo.AudioPlaybackDemo.AudioPlaybackDemoView"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6-
xmlns:local="clr-namespace:NAudioWpfDemo"
76
mc:Ignorable="d"
87
d:DesignHeight="300" d:DesignWidth="300">
98
<Grid>

NAudioWpfDemo/AudioPlaybackDemo/AudioPlaybackDemoView.xaml.cs

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Linq;
4-
using System.Text;
5-
using System.Windows;
63
using System.Windows.Controls;
7-
using System.Windows.Data;
8-
using System.Windows.Documents;
9-
using System.Windows.Input;
10-
using System.Windows.Media;
11-
using System.Windows.Media.Imaging;
12-
using System.Windows.Navigation;
13-
using System.Windows.Shapes;
144

15-
namespace NAudioWpfDemo
5+
namespace NAudioWpfDemo.AudioPlaybackDemo
166
{
177
/// <summary>
188
/// Interaction logic for AudioPlaybackDemo.xaml

NAudioWpfDemo/AudioPlaybackDemo/AudioPlaybackViewModel.cs

+7-12
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Text;
5-
using System.ComponentModel;
64
using System.Windows.Input;
75
using Microsoft.Win32;
8-
using System.Windows;
9-
using NAudio.Wave;
10-
using NAudioWpfDemo.AudioPlaybackDemo;
116
using NAudioWpfDemo.ViewModel;
127

13-
namespace NAudioWpfDemo
8+
namespace NAudioWpfDemo.AudioPlaybackDemo
149
{
1510
class AudioPlaybackViewModel : ViewModelBase, IDisposable
1611
{
17-
private AudioPlayback audioPlayback;
18-
private List<IVisualizationPlugin> visualizations;
12+
private readonly AudioPlayback audioPlayback;
13+
private readonly List<IVisualizationPlugin> visualizations;
1914
private IVisualizationPlugin selectedVisualization;
2015
private string selectedFile;
2116

22-
public ICommand OpenFileCommand { get; private set; }
23-
public ICommand PlayCommand { get; private set; }
24-
public ICommand PauseCommand { get; private set; }
25-
public ICommand StopCommand { get; private set; }
17+
public ICommand OpenFileCommand { get; }
18+
public ICommand PlayCommand { get; }
19+
public ICommand PauseCommand { get; }
20+
public ICommand StopCommand { get; }
2621

2722
public AudioPlaybackViewModel(IEnumerable<IVisualizationPlugin> visualizations)
2823
{

NAudioWpfDemo/AudioPlaybackDemo/IVisualizationPlugin.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Linq;
4-
using System.Text;
53
using NAudio.Dsp;
64

75
namespace NAudioWpfDemo.AudioPlaybackDemo

NAudioWpfDemo/AudioPlaybackDemo/PolygonWaveFormVisualization.cs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ class PolygonWaveFormVisualization : IVisualizationPlugin
88

99
public object Content => polygonWaveFormControl;
1010

11-
1211
public void OnMaxCalculated(float min, float max)
1312
{
1413
polygonWaveFormControl.AddValue(max, min);

NAudioWpfDemo/AudioPlaybackDemo/SampleAggregator.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using NAudio.Dsp;
44
using NAudio.Wave;
55

6-
namespace NAudioWpfDemo
6+
namespace NAudioWpfDemo.AudioPlaybackDemo
77
{
88
public class SampleAggregator : ISampleProvider
99
{
@@ -33,10 +33,10 @@ public SampleAggregator(ISampleProvider source, int fftLength = 1024)
3333
{
3434
throw new ArgumentException("FFT Length must be a power of two");
3535
}
36-
this.m = (int)Math.Log(fftLength, 2.0);
36+
m = (int)Math.Log(fftLength, 2.0);
3737
this.fftLength = fftLength;
38-
this.fftBuffer = new Complex[fftLength];
39-
this.fftArgs = new FftEventArgs(fftBuffer);
38+
fftBuffer = new Complex[fftLength];
39+
fftArgs = new FftEventArgs(fftBuffer);
4040
this.source = source;
4141
}
4242

NAudioWpfDemo/AudioPlaybackDemo/SpectrumAnalyzerVisualization.cs

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class SpectrumAnalyzerVisualization : IVisualizationPlugin
1111

1212
public object Content => spectrumAnalyser;
1313

14-
1514
public void OnMaxCalculated(float min, float max)
1615
{
1716
// nothing to do

NAudioWpfDemo/AudioPlaybackDemo/WaveformVisual.cs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Windows.Media;
53
using System.Windows;
4+
using System.Windows.Media;
65

7-
namespace NAudioWpfDemo
6+
namespace NAudioWpfDemo.AudioPlaybackDemo
87
{
98
class WaveFormVisual : FrameworkElement, IWaveFormRenderer
109
{
@@ -13,14 +12,14 @@ class WaveFormVisual : FrameworkElement, IWaveFormRenderer
1312
private readonly List<Point> maxPoints;
1413
private readonly List<Point> minPoints;
1514
double yTranslate = 40;
16-
double yScale = 40;
15+
double yScale = 40;
1716

1817
public WaveFormVisual()
1918
{
2019
maxPoints = new List<Point>();
2120
minPoints = new List<Point>();
2221
children = new VisualCollection(this);
23-
children.Add(CreateWaveFormVisual());
22+
children.Add(CreateWaveFormVisual());
2423
}
2524

2625
private DrawingVisual CreateWaveFormVisual()
+10-22
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,28 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
1+
using System.Collections.Generic;
52
using NAudio.Wave;
6-
using NAudio.Wave.SampleProviders;
73

84
namespace NAudioWpfDemo.DrumMachineDemo
95
{
106
class DrumKit
117
{
12-
private List<SampleSource> sampleSources;
13-
private WaveFormat waveFormat;
8+
private readonly List<SampleSource> sampleSources;
149

1510
public DrumKit()
1611
{
17-
SampleSource kickSample = SampleSource.CreateFromWaveFile("Samples\\kick-trimmed.wav");
18-
SampleSource snareSample = SampleSource.CreateFromWaveFile("Samples\\snare-trimmed.wav");
19-
SampleSource closedHatsSample = SampleSource.CreateFromWaveFile("Samples\\closed-hat-trimmed.wav");
20-
SampleSource openHatsSample = SampleSource.CreateFromWaveFile("Samples\\open-hat-trimmed.wav");
21-
sampleSources = new List<SampleSource>();
12+
var kickSample = SampleSource.CreateFromWaveFile("Samples\\kick-trimmed.wav");
13+
var snareSample = SampleSource.CreateFromWaveFile("Samples\\snare-trimmed.wav");
14+
var closedHatsSample = SampleSource.CreateFromWaveFile("Samples\\closed-hat-trimmed.wav");
15+
var openHatsSample = SampleSource.CreateFromWaveFile("Samples\\open-hat-trimmed.wav");
16+
sampleSources = new List<SampleSource> {kickSample, snareSample, closedHatsSample, openHatsSample};
2217

23-
sampleSources.Add(kickSample);
24-
sampleSources.Add(snareSample);
25-
sampleSources.Add(closedHatsSample);
26-
sampleSources.Add(openHatsSample);
27-
this.waveFormat = WaveFormat.CreateIeeeFloatWaveFormat(openHatsSample.SampleWaveFormat.SampleRate, openHatsSample.SampleWaveFormat.Channels);
18+
WaveFormat = WaveFormat.CreateIeeeFloatWaveFormat(openHatsSample.SampleWaveFormat.SampleRate, openHatsSample.SampleWaveFormat.Channels);
2819
}
2920

30-
public virtual WaveFormat WaveFormat
31-
{
32-
get { return waveFormat; }
33-
}
21+
public virtual WaveFormat WaveFormat { get; }
3422

3523
public MusicSampleProvider GetSampleProvider(int note)
3624
{
37-
return new MusicSampleProvider(this.sampleSources[note]);
25+
return new MusicSampleProvider(sampleSources[note]);
3826
}
3927
}
4028
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
using System;
2-
using System.Linq;
3-
4-
namespace NAudioWpfDemo.DrumMachineDemo
1+
namespace NAudioWpfDemo.DrumMachineDemo
52
{
63
class DrumMachineDemoPlugin : IModule
74
{
85
private DrumMachineDemoView view;
96
private DrumMachineDemoViewModel viewModel;
107

11-
public string Name
12-
{
13-
get { return "Drum Machine"; }
14-
}
8+
public string Name => "Drum Machine";
159

1610
public System.Windows.Controls.UserControl UserInterface
1711
{
@@ -29,12 +23,9 @@ public System.Windows.Controls.UserControl UserInterface
2923

3024
public void Deactivate()
3125
{
32-
if (view != null)
33-
{
34-
viewModel.Dispose();
35-
view = null;
36-
viewModel = null;
37-
}
26+
viewModel?.Dispose();
27+
view = null;
28+
viewModel = null;
3829
}
3930
}
4031
}

NAudioWpfDemo/DrumMachineDemo/DrumMachineDemoViewModel.cs

+13-22
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Linq;
4-
using System.Text;
53
using System.Windows.Input;
64
using NAudio.Wave;
7-
using NAudio.Wave.SampleProviders;
8-
using System.ComponentModel;
95
using NAudioWpfDemo.ViewModel;
106

117
namespace NAudioWpfDemo.DrumMachineDemo
128
{
139
class DrumMachineDemoViewModel : ViewModelBase, IDisposable
1410
{
1511
private IWavePlayer waveOut;
16-
private DrumPattern pattern;
12+
private readonly DrumPattern pattern;
1713
private DrumPatternSampleProvider patternSequencer;
1814
private int tempo;
19-
public ICommand PlayCommand { get; private set; }
20-
public ICommand StopCommand { get; private set; }
15+
public ICommand PlayCommand { get; }
16+
public ICommand StopCommand { get; }
2117

2218
public DrumMachineDemoViewModel(DrumPattern pattern)
2319
{
2420
this.pattern = pattern;
25-
this.tempo = 100;
21+
tempo = 100;
2622
PlayCommand = new DelegateCommand(Play);
2723
StopCommand = new DelegateCommand(Stop);
2824
}
@@ -34,8 +30,8 @@ private void Play()
3430
Stop();
3531
}
3632
waveOut = new WaveOut();
37-
this.patternSequencer = new DrumPatternSampleProvider(pattern);
38-
this.patternSequencer.Tempo = tempo;
33+
patternSequencer = new DrumPatternSampleProvider(pattern);
34+
patternSequencer.Tempo = tempo;
3935
waveOut.Init(patternSequencer);
4036
waveOut.Play();
4137
}
@@ -44,7 +40,7 @@ private void Stop()
4440
{
4541
if (waveOut != null)
4642
{
47-
this.patternSequencer = null;
43+
patternSequencer = null;
4844
waveOut.Dispose();
4945
waveOut = null;
5046
}
@@ -57,21 +53,16 @@ public void Dispose()
5753

5854
public int Tempo
5955
{
60-
get
61-
{
62-
return tempo;
63-
}
56+
get => tempo;
6457
set
6558
{
66-
if (tempo != value)
59+
if (tempo == value) return;
60+
tempo = value;
61+
if (patternSequencer != null)
6762
{
68-
this.tempo = value;
69-
if (this.patternSequencer != null)
70-
{
71-
this.patternSequencer.Tempo = value;
72-
}
73-
OnPropertyChanged("Tempo");
63+
patternSequencer.Tempo = value;
7464
}
65+
OnPropertyChanged("Tempo");
7566
}
7667
}
7768

+9-17
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,40 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Text;
5-
using System.ComponentModel;
64

75
namespace NAudioWpfDemo.DrumMachineDemo
86
{
97
public class DrumPattern
108
{
11-
private byte[,] hits;
12-
private List<string> noteNames;
9+
private readonly byte[,] hits;
10+
private readonly List<string> noteNames;
1311

1412
public DrumPattern(IEnumerable<string> notes, int steps)
1513
{
16-
this.noteNames = new List<string>(notes);
17-
this.Steps = steps;
14+
noteNames = new List<string>(notes);
15+
Steps = steps;
1816
hits = new byte[Notes, steps];
1917
}
2018

21-
public int Steps { get; private set; }
19+
public int Steps { get; }
2220

23-
public int Notes
24-
{
25-
get { return noteNames.Count; }
26-
}
21+
public int Notes => noteNames.Count;
2722

2823
public byte this[int note, int step]
2924
{
30-
get { return hits[note, step]; }
25+
get => hits[note, step];
3126
set
3227
{
3328
if (hits[note, step] != value)
3429
{
3530
hits[note, step] = value;
36-
if (PatternChanged != null)
37-
{
38-
PatternChanged(this, EventArgs.Empty);
39-
}
31+
PatternChanged?.Invoke(this, EventArgs.Empty);
4032
}
4133
}
4234
}
4335

4436
public event EventHandler PatternChanged;
4537

46-
public IList<string> NoteNames { get { return this.noteNames.AsReadOnly(); } }
38+
public IList<string> NoteNames => noteNames.AsReadOnly();
4739
}
4840
}

0 commit comments

Comments
 (0)