-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditribe.java
1980 lines (1875 loc) · 54.2 KB
/
Editribe.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package editribe;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.dnd.*;
import java.awt.event.*;
import java.io.*;
import java.nio.ByteBuffer;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import javax.sound.sampled.*;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.event.*;
import javax.swing.table.*;
//*******************************************************************
//==================== EDITRIBE =====================================
//******************************************************************
public class Editribe extends JFrame
{
private JButton convertButton = new JButton("Convert...");
private JButton deleteAllButton = new JButton("Del All From");
private JButton deleteButton = new JButton("Delete");
private JButton exportAllButton = new JButton("Exp All");
private JButton exportButton = new JButton("Export");
private JButton loopButton = new JButton("Loop");
private JButton mergeButton = new JButton("Merge...");
private JButton playButton = new JButton("Play");
private JButton quitButton = new JButton("Quit");
private JButton saveButton = new JButton("Save");
private JButton squinchButton = new JButton("Squinch");
private JFrame frame = new JFrame();
private JLabel findLabel = new JLabel(" Find: ");
private JLabel freeSpaceLabel = new JLabel(" Seconds left: ");
private JLabel freeSpaceLabel2 = new JLabel("---");
private JLabel memoryLabel = new JLabel(" Memory left: ");
private JLabel memoryLabel2 = new JLabel("---");
private JLabel sampleLabel = new JLabel(" Total samples: ");
private JLabel sampleLabel2 = new JLabel("---");
private JScrollPane scrollPane;
private JTextField findField = new JTextField();
private Merge mergeWindow = null;
private Convert convertWindow = null;
private AllFile af = new AllFile();
private Object currValue = "";
final private static int MAX_COLUMNS = 12;
final private static int MAX_ROWS = 902;
final private static int sampleNumber = 0;
final private static int sampleName = 1;
final private static int category = 2;
final private static int sampleLength = 3;
final private static int sampleStart = 4;
final private static int loopStart = 5;
final private static int loopEnd = 6;
final private static int looping = 7;
final private static int stereo = 8;
final private static int loudness = 9;
final private static int sampleRate = 10;
final private static int tuning = 11;
private String[] categories = { "[Analog]", "[Audio In]", "Kick", "Snare", "Clap", "HiHat",
"Cymbal", "Hits", "Shots", "Voice", "SE", "FX", "Tom", "Perc", "Phrase", "Loop",
"PCM", "User"
};
private String[] columnNames = { "#", "Sample Name", "Category", "Size", "Start", "Loop Start",
"Loop End", "Lp", "St", "Ld", "Bitrate", "Tun"
};
private JComboBox<String> dropCatCombo = new JComboBox<String>(categories);
private TableModel tm = new DefaultTableModel()
{
public boolean isCellEditable(int row, int column)
{
if ((af.sampleBank[row].kChunkStart != -1) && ((column == sampleName)
|| (column > sampleLength))) return true;
return false;
}
public String getColumnName(int column)
{
return columnNames[column];
}
public Object getValueAt(int row, int column)
{
if ((af.sampleBank[row].kChunkStart == -1) && (column != sampleNumber)) return null;
switch (column)
{
case sampleNumber:
return row2osc(row);
case sampleName:
return af.sampleBank[row].kSampleName;
case category:
return categories[af.sampleBank[row].kCategory];
case sampleLength:
return af.sampleBank[row].kSampleSize;
case sampleStart:
return af.sampleBank[row].kSampleStart;
case loopStart:
return af.sampleBank[row].kLoopStart;
case loopEnd:
return af.sampleBank[row].kLoopEnd;
case looping:
return af.sampleBank[row].kLooping;
case stereo:
return af.sampleBank[row].numChannels;
case loudness:
return af.sampleBank[row].kLoudness;
case sampleRate:
return af.sampleBank[row].sampleRate;
case tuning:
return af.sampleBank[row].kTuning;
}
return null;
}
public void setValueAt(Object value, int row, int column)
{
switch (column)
{
case sampleNumber:
// do nothing
break;
case sampleName:
af.sampleBank[row].kSampleName = (String)value;
break;
case category:
af.sampleBank[row].kCategory = Arrays.asList(categories).indexOf((String)value);
break;
case sampleLength:
af.sampleBank[row].kSampleSize = (Integer)value;
break;
case sampleStart:
af.sampleBank[row].kSampleStart = (Integer)value;
break;
case loopStart:
af.sampleBank[row].kLoopStart = (Integer)value;
break;
case loopEnd:
af.sampleBank[row].kLoopEnd = (Integer)value;
break;
case looping:
af.sampleBank[row].kLooping = (Integer)value;
break;
case stereo:
af.sampleBank[row].numChannels = (Integer)value;
break;
case loudness:
af.sampleBank[row].kLoudness = (Integer)value;
break;
case sampleRate:
af.sampleBank[row].sampleRate = (Integer)value;
break;
case tuning:
af.sampleBank[row].kTuning = (Integer)value;
break;
}
fireTableCellUpdated(row, column);
}
public int getColumnCount()
{
return MAX_COLUMNS;
}
public int getRowCount()
{
return MAX_ROWS;
}
public Class getColumnClass(int column)
{
if ((column == sampleName) || (column == category)) return String.class;
return Integer.class;
}
};
private JTable table1 = new JTable()
{
public String getToolTipText(MouseEvent me)
{
String reply = null;
switch(table1.columnAtPoint(me.getPoint()))
{
case tuning:
reply = "<html>+/-14: 1 step<br>+/-17: 2 steps<br>+/-19: 3 steps(20#)<br>" +
"+/-20: 4 steps(20b)<br>+/-22: 5 steps<br>+/-24: 6 steps<br>+/-28: " +
"7 steps<br>+/-32: 8 steps<br>+/-36: 9 steps<br>+/-40: 10 steps<br>" +
"+/-44: 11 steps<br>+/-48: 12 steps<br>+/-63: 24 steps</html>";
break;
case stereo:
reply = "<html>0 = Mono<br>1 = Stereo</html>";
break;
case looping:
reply = "<html>0 = Looping<br>1 = One-Shot</html>";
break;
case loudness:
reply = "<html>0 = +0dB<br>1 = +12dB</html>";
break;
default:
break;
}
return reply;
}
};
public Editribe()
{
// TABLE
table1.setModel(tm);
table1.setPreferredScrollableViewportSize(new Dimension(705, 615));
((DefaultTableCellRenderer)table1.getTableHeader().getDefaultRenderer())
.setHorizontalAlignment(JLabel.CENTER);
setWidth(sampleNumber, 35);
setWidth(sampleName, 155);
setWidth(category, 65);
setWidth(sampleStart, 70);
setWidth(sampleLength, 70);
setWidth(loopStart, 70);
setWidth(loopEnd, 70);
setWidth(looping, 25);
setWidth(stereo, 25);
setWidth(loudness, 25);
setWidth(sampleRate, 60);
setWidth(tuning, 35);
table1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table1.getTableHeader().setReorderingAllowed(false);
table1.setShowGrid(true);
table1.setGridColor(new Color(235, 235, 235));
table1.setDragEnabled(true);
table1.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
int row2 = table1.rowAtPoint(me.getPoint());
int col = table1.columnAtPoint(me.getPoint());
if ((me.getClickCount() > 1) && (col == category)
&& (af.sampleBank[row2].kChunkStart != -1))
{
af.sampleBank[row2].kCategory = dropCatCombo.getSelectedIndex();
updateSelection();
}
}
});
ListSelectionListener lst = new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
updateSelection();
}
};
table1.getSelectionModel().addListSelectionListener(lst);
table1.getColumnModel().getSelectionModel().addListSelectionListener(lst);
table1.getModel().addTableModelListener(new TableModelListener()
{
public void tableChanged(TableModelEvent e)
{
int row2 = e.getFirstRow();
int col = e.getColumn();
Object cv = table1.getValueAt(row2, col);
switch (col)
{
case sampleName:
{
if (((String) cv).trim().length() == 0)
{
af.sampleBank[row2].kSampleName = (String)currValue;
JOptionPane.showMessageDialog(null, "Name cannot be blank.");
}
else
{
int namelen = ((String) cv).length();
if (namelen > 16)
{
cv = ((String) cv).substring(0, 16);
JOptionPane.showMessageDialog(null, "Name has been trimmed.");
}
af.sampleBank[row2].kSampleName = (String) cv;
}
break;
}
case sampleStart:
{
if (((Integer)cv < 0) && ((Integer)cv > af.sampleBank[row2].kSampleSize))
{
af.sampleBank[row2].kSampleStart = (Integer)currValue;
JOptionPane.showMessageDialog(null, "Sample Start must be in bounds.");
}
else
{
af.sampleBank[row2].kSampleStart = (Integer)cv;
}
break;
}
case loopStart:
{
if (((Integer)cv < 0) && ((Integer)cv > af.sampleBank[row2].kSampleSize))
{
af.sampleBank[row2].kLoopStart = (Integer)currValue;
JOptionPane.showMessageDialog(null, "Loop Start must be in bounds.");
}
else
{
af.sampleBank[row2].kLoopStart = (Integer)cv;
}
break;
}
case loopEnd:
{
if (((Integer)cv < 0) && ((Integer)cv > af.sampleBank[row2].kLoopEnd))
{
af.sampleBank[row2].kLoopEnd = (Integer)currValue;
JOptionPane.showMessageDialog(null, "Loop End must be in bounds.");
}
else
{
af.sampleBank[row2].kLoopEnd = (Integer)cv;
}
break;
}
case loudness:
{
if (((Integer)cv == 0) || ((Integer)cv == 1))
{
af.sampleBank[row2].kLoudness = (Integer)cv;
}
else
{
af.sampleBank[row2].kLoudness = (Integer)currValue;
JOptionPane.showMessageDialog(null, "Loudness must be a 1 or 0.");
}
break;
}
case sampleRate:
{
if (((Integer)cv > 192000) || ((Integer)cv <= 0))
{
af.sampleBank[row2].sampleRate = (Integer)currValue;
JOptionPane.showMessageDialog(null,
"Sample rate must be from 1 to 1962000.");
}
else
{
af.sampleBank[row2].sampleRate = (Integer)cv;
}
break;
}
case tuning:
{
if ((Integer)cv > -64 && (Integer)cv < 64)
{
af.sampleBank[row2].kTuning = (Integer)cv;
}
else
{
af.sampleBank[row2].kTuning = (Integer)currValue;
JOptionPane.showMessageDialog(null, "Tuning must be -63 to 63.");
}
break;
}
default:
break;
}
}
});
table1.setDropTarget(new DropTarget()
{
public synchronized void drop(DropTargetDropEvent dtde)
{
int row2 = table1.rowAtPoint(dtde.getLocation());
frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
try
{
dtde.acceptDrop(DnDConstants.ACTION_MOVE);
if (dtde.getTransferable().isDataFlavorSupported(DataFlavor.javaFileListFlavor))
{
final List fileList = (List)dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
Collections.sort(fileList);
for (int i = 0; i < fileList.size(); i++)
{
File f = (File) fileList.get(i);
String sfn = f.getName();
if (sfn.toLowerCase().endsWith(".all"))
{
af.allFile = new File(f.getAbsolutePath());
af.readFile();
dtde.dropComplete(true);
table1.setRowSelectionInterval(0, 0);
deleteButton.setEnabled(true);
deleteAllButton.setEnabled(true);
squinchButton.setEnabled(true);
mergeButton.setEnabled(true);
saveButton.setEnabled(true);
table1.requestFocusInWindow();
frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
updateSelection();
return;
}
else if (!sfn.toLowerCase().endsWith(".wav") || f.isDirectory())
{
JOptionPane.showMessageDialog(null, sfn + " failed to load.");
}
else
{
if (!deleteAllButton.isEnabled()) return;
Sample s = new Sample();
if (s.loadSample(f, 0) < 0)
{
JOptionPane.showMessageDialog(null, sfn + " is not a valid WAV.");
}
else
{
s.kSampleName = sfn.substring(0, sfn.indexOf('.'));
if (s.kSampleName.length() > 16)
{
s.kSampleName = s.kSampleName.substring(0, 16);
}
s.kChunkStart = 0;
s.kCategory = dropCatCombo.getSelectedIndex();
af.sampleBank[row2] = s;
updateChunkStarts();
row2++;
if (row2 >= MAX_ROWS)
{
row2 = MAX_ROWS - 1;
break;
}
table1.setRowSelectionInterval(row2, row2);
}
}
}
}
else if (dtde.getTransferable().isDataFlavorSupported(DataFlavor.stringFlavor))
{
String drop = (String) dtde.getTransferable().getTransferData(DataFlavor.stringFlavor);
String[] r = drop.split("\\t");
int oldRow = osc2row(Integer.parseInt(r[0]));
Sample s = af.sampleBank[oldRow];
af.sampleBank[oldRow] = af.sampleBank[row2];
af.sampleBank[row2] = s;
}
}
catch (Exception ex) {
throwError(ex.toString());
}
dtde.dropComplete(true);
table1.setRowSelectionInterval(row2, row2);
updateSelection();
frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
});
scrollPane = new JScrollPane(table1);
dropCatCombo.setSelectedIndex(17); // user cat
// BUTTON ACTIONS
playButton.setFocusable(false);
playButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
int row2 = getRow();
if (af.sampleBank[row2].kChunkStart > 0)
{
int length = af.sampleBank[row2].kSampleSize;
int st = af.sampleBank[row2].numChannels + 1;
int srate = af.sampleBank[row2].sampleRate;
int bps = af.sampleBank[row2].bitsPerSample;
if (srate > 48000) srate = 48000; /// Clip max
AudioFormat auf = new AudioFormat(srate, bps, st, true, false);
try
{
Clip clip = AudioSystem.getClip();
clip.open(auf, af.sampleBank[row2].kSampleChunk, 0, length);
clip.start();
}
catch (Exception ex) {
throwError(ex.toString());
}
}
}
});
mergeButton.setFocusable(false);
mergeButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (mergeWindow == null) mergeWindow = new Merge();
mergeWindow.mShow();
}
});
convertButton.setFocusable(false);
convertButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (convertWindow == null) convertWindow = new Convert();
convertWindow.cShow();
};
});
deleteButton.setFocusable(false);
deleteButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
int row2 = getRow();
af.sampleBank[row2] = new Sample();
incRow(row2);
}
});
deleteAllButton.setFocusable(false);
deleteAllButton.setBackground(new Color(255, 128, 128));
deleteAllButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null, "Are you sure?"))
{
int row2 = getRow();
for (int i = row2; i < MAX_ROWS; i++) af.sampleBank[i] = new Sample();
updateSelection();
}
}
});
squinchButton.setFocusable(false);
squinchButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
for (int i = 0; i < MAX_ROWS - 1; i++)
{
if (af.sampleBank[i].kSampleName.equals(""))
{
for (int j = i + 1; j < MAX_ROWS; j++)
{
if (!af.sampleBank[j].kSampleName.equals(""))
{
af.sampleBank[i] = af.sampleBank[j];
af.sampleBank[j] = new Sample();
break;
}
}
}
}
updateSelection();
}
});
saveButton.setFocusable(false);
saveButton.setBackground(new Color(128, 255, 128));
saveButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
af.writeFile();
frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
});
quitButton.setFocusable(false);
quitButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (saveButton.isEnabled())
{
int reply = JOptionPane.showConfirmDialog(null, "Save before Quit?");
if (reply == JOptionPane.YES_OPTION) af.writeFile();
else if (reply == JOptionPane.CANCEL_OPTION) return;
}
System.exit(0);
}
});
loopButton.setFocusable(false);
loopButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
int row2 = getRow();
if ((Integer) af.sampleBank[row2].kLooping == 0)
{
af.sampleBank[row2].kLoopStart = af.sampleBank[row2].kSampleSize -
(2 * (af.sampleBank[row2].numChannels + 1));
af.sampleBank[row2].kLoopEnd = af.sampleBank[row2].kLoopStart;
af.sampleBank[row2].kLooping = 1;
loopButton.setText("Make Loop");
}
else
{
af.sampleBank[row2].kLoopStart = 0;
af.sampleBank[row2].kLooping = 0;
loopButton.setText("Unloop");
}
incRow(row2);
}
});
exportButton.setFocusable(false);
exportButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
int row2 = getRow();
exportWave(row2);
incRow(row2);
}
});
exportAllButton.setFocusable(false);
exportAllButton.setBackground(new Color(128, 128, 255));
exportAllButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null, "Are you sure?"))
{
for (int i = 0; i < MAX_ROWS; i++)
{
exportWave(i);
}
}
}
});
findField.setPreferredSize(new Dimension(50, 27));
findField.addKeyListener(new KeyAdapter()
{
public void keyPressed(final KeyEvent ke)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
String f = findField.getText().toUpperCase();
if (f.length() > 0)
{
int startHere = 0;
int row2 = getRow();
if (ke.getKeyChar() == KeyEvent.VK_ENTER) startHere = row2 + 1;
if (startHere < MAX_ROWS)
{
for (int i = startHere; i < MAX_ROWS; i++)
{
if (af.sampleBank[i].kSampleName.toUpperCase().contains(f))
{
table1.setRowSelectionInterval(i, i);
break;
}
}
}
}
else
{
table1.setRowSelectionInterval(0, 0);
}
table1.scrollRectToVisible(table1.getCellRect(table1.getSelectedRow(), 0, true));
}
});
}
});
// FRAME
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("EdiTribe");
frame.setLayout(new FlowLayout());
frame.add(sampleLabel);
frame.add(sampleLabel2);
frame.add(freeSpaceLabel);
frame.add(freeSpaceLabel2);
frame.add(memoryLabel);
frame.add(memoryLabel2);
frame.add(findLabel);
frame.add(findField);
frame.add(convertButton);
frame.add(saveButton);
frame.add(quitButton);
frame.add(scrollPane);
frame.add(playButton);
frame.add(dropCatCombo);
frame.add(deleteAllButton);
frame.add(deleteButton);
frame.add(squinchButton);
frame.add(exportButton);
frame.add(exportAllButton);
frame.add(loopButton);
frame.add(mergeButton);
frame.pack();
frame.setSize(740, 740);
frame.setResizable(false);
frame.setVisible(true);
updateSelection();
deleteButton.setEnabled(false);
deleteAllButton.setEnabled(false);
squinchButton.setEnabled(false);
mergeButton.setEnabled(false);
saveButton.setEnabled(false);
}
private void updateSelection()
{
int row2 = getRow();
int col = getCol();
currValue = table1.getValueAt(row2, col);
if (af.sampleBank[row2].kChunkStart == -1)
{
loopButton.setEnabled(false);
exportButton.setEnabled(false);
exportAllButton.setEnabled(false);
playButton.setEnabled(false);
}
else
{
loopButton.setEnabled(true);
exportButton.setEnabled(true);
exportAllButton.setEnabled(true);
playButton.setEnabled(true);
if (af.sampleBank[row2].kLooping == 0) loopButton.setText("Unloop");
else loopButton.setText(" Loop ");
}
int tr = 4096;
int totalSamples = 0;
for (int i = 0; i < MAX_ROWS; i++)
{
if (af.sampleBank[i].kChunkStart > 0)
{
totalSamples++;
tr += 1232 + af.sampleBank[i].kSampleSize;
}
}
int spaceRemaining = 26214396 - tr;
if (spaceRemaining < 0)
{
freeSpaceLabel2.setForeground(Color.red);
memoryLabel2.setForeground(Color.red);
}
else
{
freeSpaceLabel2.setForeground(Color.black);
memoryLabel2.setForeground(Color.black);
}
DecimalFormat twoPlaces = new DecimalFormat("##0.00");
freeSpaceLabel2.setText(twoPlaces.format(((float) spaceRemaining - 1232.f) / 96000.0f));
memoryLabel2.setText(Integer.toString(spaceRemaining));
sampleLabel2.setText(totalSamples + " ");
table1.repaint();
}
private void exportWave(int row2)
{
if (!af.sampleBank[row2].kSampleName.equals(""))
{
String expName = af.sampleBank[row2].kSampleName.replaceAll("\\W+", "_");
expName = row2osc(row2) + " " + expName.trim() + ".wav";
File file = new File(expName);
if (file.isFile()) file.delete();
try
{
af.sampleBank[row2].saveSample(file, row2);
}
catch (Exception ex) {
throwError(ex.toString());
}
}
}
private void setWidth(int column, int width)
{
table1.getColumnModel().getColumn(column).setMinWidth(width);
table1.getColumnModel().getColumn(column).setPreferredWidth(width);
table1.getColumnModel().getColumn(column).setMaxWidth(width);
}
private void updateChunkStarts()
{
int nextStart = 4096;
for (int i = 0; i < MAX_ROWS; i++)
{
if (af.sampleBank[i].kChunkStart != -1)
{
af.sampleBank[i].kChunkStart = nextStart;
nextStart += af.sampleBank[i].kSampleSize + 1232;
}
}
}
private int getCol()
{
int col = table1.getSelectedColumn();
if (col < 0) col = 0;
return col;
}
private int getRow()
{
int row2 = table1.getSelectedRow();
if (row2 < 0) row2 = 0;
return row2;
}
private void incRow(int row2)
{
row2++;
if (row2 >= MAX_ROWS) row2 = MAX_ROWS - 1;
table1.setRowSelectionInterval(row2, row2);
table1.scrollRectToVisible(new Rectangle(table1.getCellRect(table1.getSelectedRow(), 0, true)));
updateSelection();
}
private Integer row2osc(int r)
{
if (r < 403) return r + 19;
else return r + 98;
}
private Integer osc2row(int o)
{
if (o < 422) return o - 19;
else return o - 98;
}
private short osc2import(int r)
{
short bump = 0;
if (r < 55) bump = 31;
else if (r < 81) bump = 32;
else if (r < 93) bump = 33;
else if (r < 102) bump = 34;
else if (r < 147) bump = 35;
else if (r < 148) bump = 36;
else if (r < 149) bump = 37;
else if (r < 151) bump = 38;
else if (r < 422) bump = 39;
else bump = 49;
return (short) (r + bump);
}
/********************************************************************
*==================== CONVERT ======================================*
********************************************************************/
private class Convert
{
private String[] sampleRates = { "11025", "16000", "22050", "32000", "44100", "48000"};
private String[] trimLevels = { "-20dB", "-30dB", "-40dB", "-50dB", "-60dB", "-70dB", "-80dB", "-90dB", "-oodB"};
JFrame cFrame = new JFrame("Sample Converter");
JButton sourceFolderButton = new JButton("Set Source Folder");
JLabel sampleRateLabel = new JLabel("Sample Rate:");
JComboBox sampleRateCombo = new JComboBox(sampleRates);
JCheckBox averageChannelsCheckBox = new JCheckBox("Average L & R Channels?", false);
JLabel trimLevelBeginLabel = new JLabel("Trim Silence from Beginning:");
JComboBox trimLevelBeginCombo = new JComboBox(trimLevels);
JCheckBox zeroBeginCheckBox = new JCheckBox("Begin With Zero Crossing?", false);
JLabel trimLevelEndLabel = new JLabel("Trim Silence from End:");
JComboBox trimLevelEndCombo = new JComboBox(trimLevels);
JCheckBox zeroEndCheckBox = new JCheckBox("End With Zero Crossing?", false);
JButton cancelButton = new JButton("Cancel");
JButton convertButton = new JButton("Convert");
JFileChooser fcSource = new JFileChooser();
private Convert()
{
cancelButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
cFrame.setVisible(false);
}
});
convertButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
cFrame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
throwError(dateFormat.format(new Date()) + ": Conversion begin...");
String nd = fcSource.getSelectedFile().toString() + "-converted";
File newDir = new File(nd);
cConvert(fcSource.getSelectedFile(), newDir);
throwError(dateFormat.format(new Date()) + ": Conversion complete!");
cFrame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
});
fcSource.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
sourceFolderButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (fcSource.showOpenDialog(cFrame) == JFileChooser.APPROVE_OPTION)
{
sourceFolderButton.setText(fcSource.getSelectedFile().getName());
convertButton.setEnabled(true);
}
}
});
sampleRateCombo.setSelectedItem("44100");
trimLevelBeginCombo.setSelectedItem("-oodB");
trimLevelEndCombo.setSelectedItem("-oodB");
convertButton.setEnabled(false);
cFrame.setLayout(new GridLayout(0, 1));
cFrame.add(sourceFolderButton);
cFrame.add(sampleRateLabel);
cFrame.add(sampleRateCombo);
cFrame.add(averageChannelsCheckBox);
cFrame.add(trimLevelBeginLabel);
cFrame.add(trimLevelBeginCombo);
cFrame.add(zeroBeginCheckBox);
cFrame.add(trimLevelEndLabel);
cFrame.add(trimLevelEndCombo);
cFrame.add(zeroEndCheckBox);
cFrame.add(cancelButton);
cFrame.add(convertButton);
cFrame.setSize(210, 300);
cFrame.setLocation(200, 200);
cFrame.setResizable(false);
}
private void cConvert(File ff, File df)
{
if (!df.exists())
{
df.mkdir();
}
File[] subs = ff.listFiles();
for (File f : subs)
{
if (f.isDirectory())
{
String nd = df.toString() + System.getProperty("file.separator") + f.getName();
File newDir = new File(nd);
cConvert(f, newDir);
}
else if (f.getName().toLowerCase().endsWith(".wav"))
{
Sample s = new Sample();
s.kSaveKorg = false;
try
{
if (s.loadSample(f, 0) != -1)
{
if (mangle(s))
{
File newF = new File(df.toString() + System.getProperty("file.separator") + f.getName());
s.saveSample(newF, 0);
}
else
{
throwError(f.toString() + " failed conversion.");
}
}
}
catch (Exception ex) {
throwError(ex.toString());
}
}
}
System.gc();
}
private boolean mangle(Sample s)
{
int numSamples = (s.kSampleSize / (s.numChannels + 1)) / ((s.bitsPerSample + 7) / 8);
if (numSamples < 1) return false;
double samples[] = new double[numSamples];
int ctr = 0;
// Collect samples and flatten to mono, throw away right channel
if (s.audioFormat == 1)
{
if (s.bitsPerSample == 16)
{
byte[] bb = new byte[2];