1
1
using System ;
2
- using System . Collections . Generic ;
3
2
using System . Linq ;
4
- using System . Text ;
5
3
using System . Windows . Input ;
6
4
using NAudio . Wave ;
7
- using NAudio . Wave . SampleProviders ;
8
- using System . ComponentModel ;
9
5
using NAudioWpfDemo . ViewModel ;
10
6
11
7
namespace NAudioWpfDemo . DrumMachineDemo
12
8
{
13
9
class DrumMachineDemoViewModel : ViewModelBase , IDisposable
14
10
{
15
11
private IWavePlayer waveOut ;
16
- private DrumPattern pattern ;
12
+ private readonly DrumPattern pattern ;
17
13
private DrumPatternSampleProvider patternSequencer ;
18
14
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 ; }
21
17
22
18
public DrumMachineDemoViewModel ( DrumPattern pattern )
23
19
{
24
20
this . pattern = pattern ;
25
- this . tempo = 100 ;
21
+ tempo = 100 ;
26
22
PlayCommand = new DelegateCommand ( Play ) ;
27
23
StopCommand = new DelegateCommand ( Stop ) ;
28
24
}
@@ -34,8 +30,8 @@ private void Play()
34
30
Stop ( ) ;
35
31
}
36
32
waveOut = new WaveOut ( ) ;
37
- this . patternSequencer = new DrumPatternSampleProvider ( pattern ) ;
38
- this . patternSequencer . Tempo = tempo ;
33
+ patternSequencer = new DrumPatternSampleProvider ( pattern ) ;
34
+ patternSequencer . Tempo = tempo ;
39
35
waveOut . Init ( patternSequencer ) ;
40
36
waveOut . Play ( ) ;
41
37
}
@@ -44,7 +40,7 @@ private void Stop()
44
40
{
45
41
if ( waveOut != null )
46
42
{
47
- this . patternSequencer = null ;
43
+ patternSequencer = null ;
48
44
waveOut . Dispose ( ) ;
49
45
waveOut = null ;
50
46
}
@@ -57,21 +53,16 @@ public void Dispose()
57
53
58
54
public int Tempo
59
55
{
60
- get
61
- {
62
- return tempo ;
63
- }
56
+ get => tempo ;
64
57
set
65
58
{
66
- if ( tempo != value )
59
+ if ( tempo == value ) return ;
60
+ tempo = value ;
61
+ if ( patternSequencer != null )
67
62
{
68
- this . tempo = value ;
69
- if ( this . patternSequencer != null )
70
- {
71
- this . patternSequencer . Tempo = value ;
72
- }
73
- OnPropertyChanged ( "Tempo" ) ;
63
+ patternSequencer . Tempo = value ;
74
64
}
65
+ OnPropertyChanged ( "Tempo" ) ;
75
66
}
76
67
}
77
68
0 commit comments