forked from grame-cncm/faust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjava-swing.java
452 lines (384 loc) · 15.8 KB
/
java-swing.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/************************************************************************
FAUST Architecture File
Copyright (C) 2011 Kjetil Matheussen
Copyright (C) 2013 Grame
---------------------------------------------------------------------
This Architecture section is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; If not, see <http://www.gnu.org/licenses/>.
EXCEPTION : As a special exception, you may create a larger work
that contains this FAUST architecture section and distribute
that work under terms of your choice, so long as this FAUST
architecture section is not modified.
************************************************************************
************************************************************************/
/***
Duplex is supported.
Example on how to use it:
faust -lang java -a java-swing.java noise.dsp > mydsp.java && javac mydsp.java && java mydsp
Audio playback info gathered from this thread:
http://www.java-forums.org/awt-swing/2087-make-sound-play-java-application.html
***/
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.border.*;
import javax.swing.colorchooser.*;
import javax.swing.filechooser.*;
import javax.accessibility.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.util.*;
import java.io.*;
import java.applet.*;
import java.net.*;
import java.nio.Buffer.*;
import java.nio.FloatBuffer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import java.lang.Math;
import sun.audio.*;
import java.io.*;
import javax.sound.sampled.*;
class Sound {
mydsp my_mydsp;
final int nFrames = 1024;
SourceDataLine sourceDataLine;
TargetDataLine targetDataLine;
AudioFormat getOutputAudioFormat()
{
float sampleRate = my_mydsp.fSamplingFreq;
int sampleSizeInBits = 16;
int channels = my_mydsp.getNumOutputs();
boolean signed = true;
boolean bigEndian = true; // Using 'false' here is falling on OSX...
return new AudioFormat(sampleRate,
sampleSizeInBits,
channels,
signed,
bigEndian);
}
AudioFormat getInputAudioFormat()
{
float sampleRate = my_mydsp.fSamplingFreq;
int sampleSizeInBits = 16;
int channels = my_mydsp.getNumInputs();
boolean signed = true;
boolean bigEndian = true; // Using 'false' here is falling on OSX...
return new AudioFormat(sampleRate,
sampleSizeInBits,
channels,
signed,
bigEndian);
}
class PlayThread extends Thread {
float inverse_gain_float = 1.f / 32767.f;
float inverse_gain_double = 1 / 32767;
float[][] output_float_buffer = new float[my_mydsp.getNumOutputs()][nFrames];
double[][] output_double_buffer = new double[my_mydsp.getNumOutputs()][nFrames];
byte[] output_interleaved = new byte[my_mydsp.getNumOutputs() * nFrames * 2];
java.nio.ShortBuffer output_wrapped = java.nio.ByteBuffer.wrap(output_interleaved).asShortBuffer();
float[][] input_float_buffer = new float[my_mydsp.getNumInputs()][nFrames];
double[][] input_double_buffer = new double[my_mydsp.getNumInputs()][nFrames];
byte[] input_interleaved = new byte[my_mydsp.getNumInputs() * nFrames * 2];
java.nio.ShortBuffer input_wrapped = java.nio.ByteBuffer.wrap(input_interleaved).asShortBuffer();
public void process_float()
{
// Deinterleave and convert inputs
int ipos = 0;
for (int i = 0; i < nFrames; i++) {
for(int ch = 0; ch < my_mydsp.getNumInputs(); ch++) {
input_float_buffer[ch][i] = (float)input_wrapped.get(ipos++) * inverse_gain_float;
}
}
// Compute Faust effect
my_mydsp.compute(nFrames, input_float_buffer, output_float_buffer);
// Convert and interleave outputs
ipos = 0;
for (int i = 0; i < nFrames; i++){
for(int ch = 0; ch < my_mydsp.getNumOutputs(); ch++) {
output_wrapped.put(ipos++, (short)(output_float_buffer[ch][i] * 32767.f));
}
}
}
/*
public void process_double()
{
// Deinterleave and convert inputs
int ipos = 0;
for (int i = 0; i < nFrames; i++) {
for(int ch = 0; ch < my_mydsp.getNumInputs(); ch++) {
input_double_buffer[ch][i] = (double)input_wrapped.get(ipos++) * inverse_gain_double;
}
}
// Compute Faust effect
my_mydsp.compute(nFrames, input_double_buffer, output_double_buffer);
// Convert and interleave outputs
ipos = 0;
for (int i = 0; i < nFrames; i++){
for(int ch = 0; ch < my_mydsp.getNumOutputs(); ch++) {
output_wrapped.put(ipos++, (short)(output_double_buffer[ch][i] * 32767));
}
}
}
*/
public void run()
{
try {
while (true) {
if (my_mydsp.getNumInputs() > 0) {
targetDataLine.read(input_interleaved, 0, nFrames * 2 * my_mydsp.getNumInputs());
}
process_float();
if (my_mydsp.getNumOutputs() > 0) {
sourceDataLine.write(output_interleaved, 0, nFrames * 2 * my_mydsp.getNumOutputs());
}
}
} catch (Exception e) {
System.out.println(e);
System.exit(0);
}
}
}
Sound(mydsp my_mydsp) {
this.my_mydsp = my_mydsp;
try {
if (my_mydsp.getNumInputs() > 0) {
AudioFormat audioInputFormat = getInputAudioFormat();
DataLine.Info input_dataLineInfo = new DataLine.Info(TargetDataLine.class, audioInputFormat);
targetDataLine = (TargetDataLine)AudioSystem.getLine(input_dataLineInfo);
targetDataLine.open(audioInputFormat);
targetDataLine.start();
}
if (my_mydsp.getNumOutputs() > 0) {
AudioFormat audioOutputFormat = getOutputAudioFormat();
DataLine.Info output_dataLineInfo = new DataLine.Info(SourceDataLine.class, audioOutputFormat);
sourceDataLine = (SourceDataLine)AudioSystem.getLine(output_dataLineInfo);
sourceDataLine.open(audioOutputFormat);
sourceDataLine.start();
}
Thread playThread = new Thread(new PlayThread());
playThread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
<<includeIntrinsic>>
class Meta {
void declare(String name, String value) {}
}
interface FaustVarAccess {
public String getId();
public void set(float val);
public float get();
}
class UI {
JFrame jframe;
JPanel jpanel;
public static Dimension HGAP2 = new Dimension(2, 1);
public static Dimension HGAP10 = new Dimension(10, 1);
public static Dimension VGAP5 = new Dimension(1, 5);
public static Dimension VGAP10 = new Dimension(1, 10);
UI() {
jframe = new JFrame("Test Frame");
jpanel = new JPanel();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.pack();
jframe.setVisible(true);
jframe.getContentPane().add(jpanel);
//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
public void declare(String id, String key, String value) {}
// -- layout groups
public void openTabBox(String label) {}
public void openHorizontalBox(String label) {}
public void openVerticalBox(String label) {}
public void closeBox() {}
// -- active widgets
public void addButton(String label, final FaustVarAccess varAccess) {
final JButton b = new JButton(label);
b.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
varAccess.set(1);
}
public void mouseReleased(MouseEvent e) {
varAccess.set(0);
}
});
jpanel.add(b);
jpanel.add(Box.createRigidArea(HGAP10));
jframe.pack();
}
public void addCheckButton(String label, FaustVarAccess varAccess)
{
System.out.println("TODO");
addButton(label,varAccess);
}
public void addVerticalSlider(String label, final FaustVarAccess varAccess, float init, float min, float max, float step)
{
JLabel tf = new JLabel(label);
//ChangeListener listener = new SliderListener(tf,label);
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
final JSlider s;
int factor = 1;
if (java.lang.Math.abs(max - min) <= 0.1) {
p.setBorder(new TitledBorder(label+"*1000"));
factor = 1000;
} else if (java.lang.Math.abs(max - min) <= 1) {
p.setBorder(new TitledBorder(label+"*100"));
factor = 100;
} else if (java.lang.Math.abs(max - min) <= 10) {
p.setBorder(new TitledBorder(label+"*10"));
factor = 10;
} else {
p.setBorder(new TitledBorder(label));
}
s = new JSlider(JSlider.VERTICAL, (int)(min * factor), (int)(max * factor), (int)(init * factor));
s.setMajorTickSpacing((int)((max - min) * factor / 4));
s.setMinorTickSpacing((int)((max - min) * factor / 8));
s.setPaintTicks(true);
s.setPaintLabels(true);
s.getAccessibleContext().setAccessibleName(label);
s.getAccessibleContext().setAccessibleDescription(label);
if (factor == 1000) {
s.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e) {
varAccess.set((float)s.getValue()/1000.f);
}
});
} else if (factor == 100) {
s.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e) {
varAccess.set((float)s.getValue()/100.f);
}
});
} else if (factor == 10) {
s.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e) {
varAccess.set((float)s.getValue()/10.f);
}
});
} else {
s.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
varAccess.set((float)s.getValue());
}
});
}
p.add(Box.createRigidArea(HGAP10));
p.add(s);
p.add(Box.createRigidArea(HGAP10));
jpanel.add(p);
jframe.pack();
}
public void addHorizontalSlider(String label, final FaustVarAccess varAccess, float init, float min, float max, float step)
{
JLabel tf = new JLabel(label);
//ChangeListener listener = new SliderListener(tf,label);
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
p.setBorder(new TitledBorder(label));
final JSlider s;
int factor = 1;
if (java.lang.Math.abs(max - min) <= 0.1) {
p.setBorder(new TitledBorder(label+"*1000"));
factor = 1000;
} else if (java.lang.Math.abs(max - min) <= 1) {
p.setBorder(new TitledBorder(label+"*100"));
factor = 100;
} else if (java.lang.Math.abs(max - min) <= 10) {
p.setBorder(new TitledBorder(label+"*10"));
factor = 10;
} else {
p.setBorder(new TitledBorder(label));
}
s = new JSlider(JSlider.HORIZONTAL, (int)(min * factor), (int)(max * factor), (int)(init * factor));
s.setMajorTickSpacing((int)((max - min) * factor / 4));
s.setMinorTickSpacing((int)((max - min) * factor / 8));
s.setPaintTicks(true);
s.setPaintLabels(true);
s.getAccessibleContext().setAccessibleName(label);
s.getAccessibleContext().setAccessibleDescription(label);
if (factor == 1000) {
s.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e) {
varAccess.set((float)s.getValue()/1000.f);
}
});
} else if (factor == 100) {
s.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e) {
varAccess.set((float)s.getValue()/100.f);
}
});
} else if (factor == 10) {
s.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e) {
varAccess.set((float)s.getValue()/10.f);
}
});
} else {
s.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
varAccess.set((float)s.getValue());
}
});
}
p.add(Box.createRigidArea(VGAP5));
p.add(s);
p.add(Box.createRigidArea(VGAP5));
jpanel.setLayout(new BoxLayout(jpanel, BoxLayout.Y_AXIS));
jpanel.add(p);
jpanel.add(Box.createRigidArea(VGAP10));
jframe.pack();
}
public void addNumEntry(String label, FaustVarAccess varAccess, float init, float min, float max, float step)
{
System.out.println("TODO");
addHorizontalSlider(label, varAccess, init, min, max, step);
}
// -- passive display widgets
public void addHorizontalBargraph(String label, FaustVarAccess varAccess, float min, float max)
{
System.out.println("TODO");
}
public void addVerticalBargraph(String label, FaustVarAccess varAccess, float min, float max)
{
System.out.println("TODO");
}
}
class dsp {
public int fSamplingFreq;
public static void main(String... aArgs)
{
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
} catch (Exception e) {
System.out.println("Couldn't set lookandfeel");
}
UI ui = new UI();
mydsp my_mydsp = new mydsp();
my_mydsp.init(44100);
my_mydsp.buildUserInterface(ui);
Sound sound = new Sound(my_mydsp);
}
}
<<includeclass>>