-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.java
More file actions
222 lines (202 loc) · 5.24 KB
/
Controller.java
File metadata and controls
222 lines (202 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package aoop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFormattedTextField;
import javax.swing.JSlider;
/**
* Controller of MVC pattern
* manages user input
* @author Daniel and Erik
*/
public class Controller {
/**
* Constructor. Initializes filechooser
*/
public Controller()
{
/* filechooser */
fc = new JFileChooser();
fc.setFileFilter(new WavFilter());
}
/**
* Add view part of MVC pattern
* @param v view
*/
public void addView(View v)
{
this.v = v;
}
/**
* Add model part of MVC pattern
* @param m model
*/
public void addModel(Model m)
{
this.m = m;
}
/**
* ActionListener for play button
* @return PlayHandler
*/
public ActionListener getPlayListener()
{
return (ActionEvent e) -> {
m.getSound().play();
};
}
/**
* ActionListener for scalable filters
* @param f scalable filter
* @return ScalableFilterHandler
*/
public ActionListener getScalableFilterMenuListener(Filter f)
{
return (ActionEvent e) -> {
m.setCurrentFilter(f);
v.showSlider();
};
}
/**
* Apply scalable filter
* @param scale JSlider scale
* @return ApplyScalableHandler
*/
public ActionListener getApplyScalableFilterListener(JSlider scale)
{
return (ActionEvent e) -> {
ScalableFilter tmp = (ScalableFilter)m.getCurrentFilter();
Sound s = m.getSound();
tmp.setScale(((double)scale.getValue()));
s.addFilter(tmp);
};
}
/**
* Apply Karplus Strong filter
* @param lSlider Length of filter
* @param dampSlider Damping
* @return Karplus Filter Handler
*/
public ActionListener getApplyKarplusFilterListener(JSlider lSlider,
JSlider dampSlider)
{
return (ActionEvent e) ->
{
KarplusStrongFilter tmp = (KarplusStrongFilter)m.getCurrentFilter();
Sound s = m.getSound();
tmp.setScale(lSlider.getValue(), dampSlider.getValue());
s.addFilter(tmp);
};
}
/**
* Menu exit handler
* @return
*/
public ActionListener menuExitListener()
{
return (ActionEvent e) ->
{
System.exit(0);
};
}
/**
* Shows Dialoge new Tone
* @return new Tone Handler
*/
public ActionListener getNewToneListener()
{
return (ActionEvent e) ->
{
v.showNewTone();
};
}
/**
* Shows Dialoge new Tone
* @return new Tone Handler
*/
public ActionListener getNewNoiseListener()
{
return (ActionEvent e) ->
{
v.showWhiteNoiseSlider();
};
}
/**
* Shows browse for file dialogue
* @return browse handler
*/
public ActionListener getBrowseDiskListener()
{
return (ActionEvent e) ->
{
int ret = fc.showOpenDialog(v);
if(ret == JFileChooser.APPROVE_OPTION)
{
//fc.setDialogTitle("Select file to load");
File file = fc.getSelectedFile();
Sample s = new Sample(StdAudio.read(file.toString()));
m.setSound(new WavSound(s));
}
};
}
/**
* Shows save file dialogue
* @return save file handler
*/
public ActionListener getSaveFileListener()
{
return (ActionEvent e) ->
{
int ret = fc.showSaveDialog(v);
if (ret == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
StdAudio.save(file.getAbsolutePath(), m.getSample());
}
};
}
/**
* Handles create button
* Strings in freq and dur must be double
* @param freq textfield containing frequency
* @param dur textfield containing duration
* @return
*/
public ActionListener getCreateNewToneListener(JFormattedTextField freq, JFormattedTextField dur)
{
return (ActionEvent e) ->
{
String tmp = freq.getText().replace(",",".");
tmp = tmp.replaceAll("\\s+", "");
double f = Double.parseDouble(tmp);
double d = Double.parseDouble(dur.getText().replace(",",".").replace(" ", ""));
m.setSound( new Tone(f,d));
};
}
public ActionListener getCreateWhiteNoise(JSlider dur)
{
return (ActionEvent e) ->
{
m.setSound(new WhiteNoise(dur.getValue()/10));
};
}
/**
* Handles Karplus filter menu item
* @param f karplus filter
* @return Karplus filter handler
*/
public ActionListener getKarplusStrongfilterListener(Filter f)
{
return (ActionEvent e) ->
{
m.setCurrentFilter(f);
v.showKarplus();
};
}
/**
* field variables
*/
private final JFileChooser fc;
private Model m;
private View v;
}