-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioDicerExampleGUI.java
346 lines (302 loc) · 10.1 KB
/
AudioDicerExampleGUI.java
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* This file is part of AudioDicer,
* Copyright 2020 Philip Freihofner.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the
* following conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of
* its contributors may be used to endorse or promote products
* derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package example;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioFormat.Encoding;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.Line.Info;
import javax.sound.sampled.LineUnavailableException;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import com.adonax.audiodicer.AudioDicer;
import com.adonax.audiodicer.AudioDicer.CrossFadeMode;
public class AudioDicerExampleGUI {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
public void run(){
JFrame frame = new AudioDicerGUI_wSliders();
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
final class AudioDicerGUI_wSliders extends JFrame {
private static final long serialVersionUID = 1L;
private DemoPlayer_adv demoPlayer_adv;
private AudioDicer audioDicer;
private int initialSliceSize = 44100;
private int initialOverlapSize = 2000;
private double initialVolume = 0.5;
private double initialSpeed = 1;
private CrossFadeMode crossFadeMode = CrossFadeMode.NONE;
AudioDicerGUI_wSliders() {
// Streamer setup
URL url;
url = AudioDicerExampleGUI.class.getResource("brook.wav");
// url = AudioDicerExampleGUI.class.getResource("Brownian_6_200.wav");
// url = AudioDicerExampleGUI.class.getResource("chordgliss.wav");
audioDicer = new AudioDicer();
try {
audioDicer.loadAudioDataFromWAV(url);
audioDicer.setSliceSize(initialSliceSize);
audioDicer.setOverlap(initialOverlapSize);
audioDicer.setCrossFadeMode(crossFadeMode);
audioDicer.setVolume(initialVolume);
audioDicer.setSpeed(initialSpeed);
} catch (UnsupportedAudioFileException | IOException e1) {
e1.printStackTrace();
}
// Player setup
demoPlayer_adv = new DemoPlayer_adv(audioDicer);
Thread thread = new Thread(demoPlayer_adv);
thread.start();
// GUI setup
setTitle("Usage: SlicedAudioStreamer_with Sliders");
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(400, 200));
JButton button = new JButton("Press to Start Streamer");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (demoPlayer_adv.getPlaying()) {
demoPlayer_adv.setPlaying(false);
button.setText("Press to Start Streamer");
} else {
demoPlayer_adv.setPlaying(true);
button.setText("Press to Stop Streamer");
}
}
});
JLabel volumeSliderLabel = new JLabel("Volume");
JSlider volumeSlider = new JSlider();
volumeSlider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
demoPlayer_adv.slicedAudioStreamer.setVolume(
volumeSlider.getValue()/100.0);
}});
JLabel speedSliderLabel = new JLabel("Speed");
JSlider speedSlider = new JSlider();
speedSlider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
double val = speedSlider.getValue();
val = val / 25; // 0:4
val = Math.pow(2, val); // 1:16
val = val / 4; // 0.25:4
demoPlayer_adv.slicedAudioStreamer.setSpeed(val);
}});
JLabel sliceSizeSliderLabel = new JLabel("Slice Size");
JTextField sliceSizeField = new JTextField();
sliceSizeField.setText(String.valueOf(initialSliceSize));
sliceSizeField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int sliceSizeFrames = Integer.valueOf(sliceSizeField.getText());
try {
audioDicer.setSliceSize(sliceSizeFrames);
} catch (IllegalArgumentException arg0) {
sliceSizeField.setText(String.valueOf(
audioDicer.getSliceSize()));
}
}});
JLabel overlapSizeSliderLabel = new JLabel("Overlap Size");
JTextField overlapSizeField = new JTextField();
overlapSizeField.setText(String.valueOf(initialOverlapSize));
overlapSizeField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int overlapFrames = Integer.valueOf(overlapSizeField.getText());
try {
audioDicer.setOverlap(overlapFrames);
} catch (IllegalArgumentException arg0) {
overlapSizeField.setText(
String.valueOf(audioDicer.getOverlap()));
arg0.printStackTrace();
}
}});
JRadioButton rbCrossFadeNone = new JRadioButton(CrossFadeMode.NONE.name());
rbCrossFadeNone.setSelected(true);
rbCrossFadeNone.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
audioDicer.setCrossFadeMode(CrossFadeMode.NONE);
}});
JRadioButton rbCrossFadeLinear = new JRadioButton(CrossFadeMode.LINEAR.name());
rbCrossFadeLinear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
audioDicer.setCrossFadeMode(CrossFadeMode.LINEAR);
}});
JRadioButton rbCrossFadeSine = new JRadioButton(CrossFadeMode.SINE.name());
rbCrossFadeSine.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
audioDicer.setCrossFadeMode(CrossFadeMode.SINE);
}});
ButtonGroup bgCrossFade = new ButtonGroup();
bgCrossFade.add(rbCrossFadeNone);
bgCrossFade.add(rbCrossFadeLinear);
bgCrossFade.add(rbCrossFadeSine);
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.PAGE_START;
c.fill = GridBagConstraints.BOTH;
c.ipady = 10;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 3;
panel.add(button, c);
c.gridy = 1;
c.gridwidth = 1;
panel.add(volumeSliderLabel, c);
c.gridx = 0;
c.gridx = 1;
c.gridwidth = 2;
panel.add(volumeSlider, c);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
panel.add(speedSliderLabel, c);
c.gridx = 1;
c.gridwidth = 2;
panel.add(speedSlider, c);
c.gridx = 0;
c.gridy = 3;
c.gridwidth = 1;
panel.add(sliceSizeSliderLabel, c);
c.gridx = 1;
c.gridwidth = 1;
panel.add(sliceSizeField, c);
c.gridx = 0;
c.gridy = 4;
c.gridwidth = 1;
panel.add(overlapSizeSliderLabel, c);
c.gridx = 1;
c.gridwidth = 1;
panel.add(overlapSizeField, c);
c.gridx = 0;
c.gridy = 5;
c.gridwidth = 1;
panel.add(rbCrossFadeNone, c);
c.gridx = 1;
panel.add(rbCrossFadeLinear, c);
c.gridx = 2;
panel.add(rbCrossFadeSine, c);
add(panel);
pack();
setLocationRelativeTo(null);
}
}
class DemoPlayer_adv implements Runnable {
SourceDataLine sdl;
AudioDicer slicedAudioStreamer;
float[] pcmBuffer;
byte[] byteBuffer, silence;
final int PCM_BUFFER_SIZE = 1024 * 4;
final int BYTE_BUFFER_SIZE = PCM_BUFFER_SIZE * 4; // 4 bytes per frame
final int SDL_BUFFER_SIZE = 1024 * 8;
private volatile boolean playing;
public void setPlaying(boolean playCommand) {
if (playCommand) {
if (! slicedAudioStreamer.getRunning()) {
slicedAudioStreamer.start();
}
}
playing = playCommand;
}
public boolean getPlaying() { return playing; }
public DemoPlayer_adv(AudioDicer slicedAudioStreamer) {
this.slicedAudioStreamer = slicedAudioStreamer;
try {
sdl = getSourceDataLine();
sdl.start();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
pcmBuffer = new float[PCM_BUFFER_SIZE];
byteBuffer = new byte[BYTE_BUFFER_SIZE];
silence = new byte[BYTE_BUFFER_SIZE];
}
@Override
public void run()
{
while(true)
{
if (playing)
{
int n = slicedAudioStreamer.read(byteBuffer);
sdl.write(byteBuffer, 0, n);
}
else
{
sdl.write(silence, 0, BYTE_BUFFER_SIZE);
}
}
}
//////////// UTILITIES ////////////
private SourceDataLine getSourceDataLine() throws LineUnavailableException
{
AudioFormat audioFmt = new AudioFormat(
Encoding.PCM_SIGNED, 44100, 16, 2, 4, 44100, false);
Info info = new DataLine.Info(SourceDataLine.class, audioFmt);
SourceDataLine sdl = (SourceDataLine)AudioSystem.getLine(info);
sdl.open(audioFmt, SDL_BUFFER_SIZE);
return sdl;
}
}