-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainDisplayWindow.java
More file actions
606 lines (465 loc) · 18.5 KB
/
Copy pathMainDisplayWindow.java
File metadata and controls
606 lines (465 loc) · 18.5 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
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
package daw;
import java.awt.*;
import java.util.*;
import javax.sound.sampled.AudioInputStream;
import javax.swing.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
/**
*
* @author opdada01 Daniel Opdahl
* @author mantno01 Noah Manternach
* @author nteste01 Teboho Nteso
*
*/
//The MainDisplayWindow class is where our audio display swing components and is where our audio
//information is held (e.g., tracks_list). MainDisplayWindow acts as the hub for all file editing,
//as well as the container for our audio display objects.
public class MainDisplayWindow extends JPanel {
/* FIELDS */
public ProgramFrame program_frame;
public ArrayList<File> tracks_list;
public File track;
//track_index and current_track differ in function: track_index is for creating the audio files
//in AudioDisplayContainer,AudioFileInfo, and AudioFileVisualDisplay. current_track is for
//playback purposes ONLY.
public int track_index;
public int current_track;
private AudioInputStream our_audio_stream;
/* CONSTRUCTOR */
public MainDisplayWindow(ProgramFrame program_frame, ArrayList<File> tracks_list, int current_track) {
//Here we set relevant playback variables and set our layout.
setTracksList(tracks_list);
setProgramFrame(program_frame);
setCurrentTrack(current_track);
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
setBorder(BorderFactory.createLineBorder(Color.black));
}
//addAudioFile adds an audio file to the MainDisplayWindow using track_index, as any audio file
//a user wishes to add will already be in tracks_list when this method is called. We create an
//AudioDisplayContainer for the track, add it to MainDisplayWindow and then repaint. We do not
//reset current_track when we add and audio file.
void addAudioFile(int track_index) {
AudioDisplayContainer new_container = new AudioDisplayContainer(this, getTracksList().get(track_index));
new_container.setMaximumSize(new Dimension(Integer.MAX_VALUE, 250));
add(new_container);
//audio_display_container_list.add(new_container);
revalidate();
repaint();
}
//removeAudioFile first removes the AudioDisplayContainer object from our MainDisplayWindow. Next,
//it repaints. Then, it checks to see if the track the was inside of the removed AudioDisplayContainer
//object was the track at index 0 in tracks_list. If it was, it resets new_idex to zero so that
//new_idex can set our_current_track_display to a valid track. If it wasn't, it drops new_index to
//the next track so that our_current_track_display can be set to that track. Finally, we remove the
//track from tracks_list, set the current track in ProgramFrame, repaint both the ProgramFrame and
//our_current_track_display and then set current_track.
void removeAudioFile(AudioDisplayContainer audio_display_container) {
remove(audio_display_container);
revalidate();
repaint();
int new_index;
if (getTracksList().indexOf(audio_display_container.getTrack()) - 1 >= 0) {
new_index = (getTracksList().indexOf(audio_display_container.getTrack()) - 1);
}
else {
new_index = 0;
}
getTracksList().remove(audio_display_container.getTrack());
getProgramFrame().setCurrentTrack(new_index);
getProgramFrame().our_current_track_display.revalidate();
getProgramFrame().our_current_track_display.repaint();
getProgramFrame().revalidate();
getProgramFrame().repaint();
setCurrentTrack(new_index);
}
//selectTrack sets current_track to a different track in tracks_list in both here and ProgramFrame,
//then it repaints both the ProgramFrame and our_current_track_display.
void selectTrack(File selected_track) {
setCurrentTrack(getTracksList().indexOf(selected_track));
getProgramFrame().setCurrentTrack(getTracksList().indexOf(selected_track));
getProgramFrame().our_current_track_display.revalidate();
getProgramFrame().our_current_track_display.repaint();
getProgramFrame().revalidate();
getProgramFrame().repaint();
}
//adjustAmplitudeClip creates a write_file to write the edited track to. It also creates a FileInputStream
//out of current_file and a FileOutputStream out of write_file. Then, it copies the file header of the .wav
//file from the FileOutputStream to the FileInputStream. Then, it reads in a pair of bytes, smashes the two
//bytes into a short, multiplies the short by scaling_ratio, then extracts the two bytes from the short. If
//either of the two bytes are greater than their max value of 127, they are reset to 127. Finally the two
//modified bytes are written out to the FileOutputStream. Finally we automatically add write_file to
//tracks_list and this using addAudioFile.
void adjustAmplitudeClip(double scaling_ratio) {
File current_file = getTracksList().get(getCurrentTrack());
StringBuilder sb_current = new StringBuilder(current_file.getPath());
sb_current.delete(sb_current.indexOf(".wav"), (sb_current.indexOf(".wav")+4));
File write_file = new File(sb_current + "_amplitude_clipped.wav");
FileOutputStream out = null;
FileInputStream in = null;
try {
in = new FileInputStream(current_file);
out = new FileOutputStream(write_file);
}
catch (Exception file_not_found) {}
try {
for (int i = 0; i<(current_file.length()/2); i++) {
while (i < 44) {
byte b1 = (byte) (in.read() & 0xff);
byte b2 = (byte) (in.read() & 0xff);
out.write(b1);
out.write(b2);
i++;
}
byte b1 = (byte) (in.read() & 0xff);
byte b2 = (byte) (in.read() & 0xff);
short s = ((short) (b2 << 8 | (((int) b1) & 0xff) & 0xffff));
s = (short) (s * scaling_ratio);
byte b3 = ((byte) (s & 0xff));
byte b4 = ((byte) (s >> 8 & 0xff));
if (b1 * scaling_ratio >= 127 || b1 * scaling_ratio <= -128) {
if (b1 > 0) {
b3 = 127;
}
if (b1 < 0) {
b3 = -128;
}
}
if (b2 * scaling_ratio >= 127 || b2 * scaling_ratio <= -128) {
if (b2 > 0) {
b4 = 127;
}
if (b2 < 0) {
b4 = -128;
}
}
out.write(b3);
out.write(b4);
}
getTracksList().add(write_file);
addAudioFile(getTracksList().indexOf(write_file));
}
catch (Exception could_not_read_write_to_iostreams) {}
}
//Unfinished
void resample(int resample_rate) {
File current_file = getTracksList().get(getCurrentTrack());
File write_file = new File("C:\\Users\\dopda\\Desktop\\DAW WAV Files\\" + "_resampled_" + getTracksList().get(getCurrentTrack()).getName());
FileOutputStream out = null;
FileInputStream in = null;
try {
in = new FileInputStream(current_file);
out = new FileOutputStream(write_file);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
int i=0;
while(i < 11 ) {
byte b1 = (byte) (in.read() & 0xff);
byte b2 = (byte) (in.read() & 0xff);
byte b3 = (byte) (in.read() & 0xff);
byte b4 = (byte) (in.read() & 0xff);
i++;
int val = b4 << 24 | b3 << 16 & 0xff0000 | b2 << 8 & 0xff00 | ((int) b1) & 0xff;
if (i == 7) {
byte leastSignificantByte = (byte) (val & 0xff);
byte nextToLeastSignificantByte = (byte) (val >> 8 & 0xff);
byte nextToMostSignificantByte = (byte) (val >> 16 & 0xff);
byte mostSignificantByte = (byte) (val >> 32 & 0xff);
out.write(leastSignificantByte);
out.write(nextToLeastSignificantByte);
out.write(nextToMostSignificantByte);
out.write(mostSignificantByte);
System.out.println("VAL: " + val);
}
if (i == 8) {
val = val *2;
byte leastSignificantByte = (byte) (val & 0xff);
byte nextToLeastSignificantByte = (byte) (val >> 8 & 0xff);
byte nextToMostSignificantByte = (byte) (val >> 16 & 0xff);
byte mostSignificantByte = (byte) (val >> 32 & 0xff);
out.write(leastSignificantByte);
out.write(nextToLeastSignificantByte);
out.write(nextToMostSignificantByte);
out.write(mostSignificantByte);
}
if (i != 7 && i != 8) {
out.write(b1);
out.write(b2);
out.write(b3);
out.write(b4);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//adjustAmplitudeNormalize creates a write_file to write the edited track to. It also creates a FileInputStream
//out of current_file and a FileOutputStream out of write_file. Then, it copies the file header of the .wav
//file from the FileOutputStream to the FileInputStream. Then, it reads through all of the audio data and finds
//the largest byte, stored as max_byte. It then modifies scaling_ratio so that no byte multiplied by scaling_ratio
//is greater than 127. Then, we close the FileInputStream and reopen it to read through it again. Finally, we go
//through FileInputStream, reading in a pair of bytes, smashing the two bytes into a short, multiplying the short
//by adjusted_scaling_ratio, then extracting the two bytes from the short. Finally the two modified bytes are
//written out to the FileOutputStream. Finally we automatically add write_file to tracks_list and this using
//addAudioFile.
void adjustAmplitudeNormalize(double scaling_ratio) {
File current_file = getTracksList().get(getCurrentTrack());
StringBuilder sb_current = new StringBuilder(current_file.getPath());
sb_current.delete(sb_current.indexOf(".wav"), (sb_current.indexOf(".wav")+4));
File write_file = new File(sb_current + "_amplitude_normalized.wav");
FileOutputStream out = null;
FileInputStream in = null;
try {
in = new FileInputStream(current_file);
out = new FileOutputStream(write_file);
}
catch (Exception file_not_found) {}
double adjusted_scaling_ratio;
byte max_byte = 0;
try {
for (int i = 0; i < current_file.length(); i++) {
if (i > 44) {
byte current_byte = (byte) (in.read() & 0xff);
if (current_byte > max_byte) {
max_byte = current_byte;
}
if (current_byte == 127) {
}
}
}
}
catch (Exception could_not_read_write_to_iostreams) {}
if (scaling_ratio > 1) {
adjusted_scaling_ratio = ((double) 126 / (double) max_byte);
}
else {
adjusted_scaling_ratio = scaling_ratio;
}
try {
in.close();
in = new FileInputStream(current_file);
}
catch (Exception file_not_found) {}
try {
for (int i = 0; i < current_file.length()/2; i++) {
while (i < 44) {
byte b1 = (byte) (in.read() & 0xff);
byte b2 = (byte) (in.read() & 0xff);
out.write(b1);
out.write(b2);
i++;
}
byte b1 = (byte) (in.read() & 0xff);
byte b2 = (byte) (in.read() & 0xff);
short s = ((short) (b2 << 8 | (((int) b1) & 0xff) & 0xffff));
s = (short) (s * adjusted_scaling_ratio);
byte b3 = ((byte) (s & 0xff));
byte b4 = ((byte) (s >> 8 & 0xff));
out.write(b3);
out.write(b4);
}
getTracksList().add(write_file);
addAudioFile(getTracksList().indexOf(write_file));
}
catch (Exception could_not_read_write_to_iostreams) {}
}
void erase() {
File current_file = getTracksList().get(getCurrentTrack());
StringBuilder sb_current = new StringBuilder(current_file.getPath());
sb_current.delete(sb_current.indexOf(".wav"), (sb_current.indexOf(".wav")+4));
File write_file = new File(sb_current + "_erased.wav");
FileOutputStream out = null;
FileInputStream in = null;
try {
in = new FileInputStream(current_file);
out = new FileOutputStream(write_file);
}
catch (Exception file_not_found) {}
try {
for (int i = 0; i<(current_file.length()/2); i++) {
while (i < 44) {
byte b1 = (byte) (in.read() & 0xff);
byte b2 = (byte) (in.read() & 0xff);
System.out.println(b1 + " " + b2 + " ");
out.write(b1);
out.write(b2);
System.out.println(b1 + " " + b2 + " ");
System.out.println(i);
i++;
}
byte b1 = (byte) (in.read() & 0xff);
byte b2 = (byte) (in.read() & 0xff);
short s = ((short) (b2 << 8 | (((int) b1) & 0xff) & 0xffff));
s = (short) 0;
byte b3 = ((byte) (s & 0xff));
byte b4 = ((byte) (s >> 8 & 0xff));
out.write(b3);
out.write(b4);
}
getTracksList().add(write_file);
addAudioFile(getTracksList().indexOf(write_file));
}
catch (Exception could_not_read_write_to_iostreams) {}
}
void merge(File merge_selected_file) {
StringBuilder sb_current = new StringBuilder(merge_selected_file.getPath());
sb_current.delete(sb_current.indexOf(".wav"), (sb_current.indexOf(".wav")+4));
File write_file = new File(sb_current + "_merged.wav");
short[] short_array1 = new short[(int) (merge_selected_file.length() - 44)/2];
short[] short_array2 = new short[(int) (getTracksList().get(getCurrentTrack()).length() - 44)/2];
FileOutputStream out = null;
FileInputStream in1 = null;
FileInputStream in2 = null;
try {
in1 = new FileInputStream(merge_selected_file);
in2 = new FileInputStream(getTracksList().get(getCurrentTrack()));
out = new FileOutputStream(write_file);
}
catch (Exception file_not_found) {}
try {
for (int i1 = 0; i1<merge_selected_file.length()/2; i1++) {
while (i1 < 44) {
byte b1 = (byte) (in1.read() & 0xff);
byte b2 = (byte) (in1.read() & 0xff);
i1++;
}
byte b1 = (byte) (in1.read() & 0xff);
byte b2 = (byte) (in1.read() & 0xff);
short s = ((short) (b2 << 8 | (((int) b1) & 0xff) & 0xffff));
short_array1[i1-44] = s;
}
}
catch (Exception could_not_read_write_to_iostreams) {}
try {
for (int i1 = 0; i1<getTracksList().get(getCurrentTrack()).length()/2; i1++) {
while (i1 < 44) {
byte b1 = (byte) (in2.read() & 0xff);
byte b2 = (byte) (in2.read() & 0xff);
out.write(b1);
out.write(b2);
i1++;
}
byte b1 = (byte) (in2.read() & 0xff);
byte b2 = (byte) (in2.read() & 0xff);
short s = ((short) (b2 << 8 | (((int) b1) & 0xff) & 0xffff));
short_array2[i1-44] = s;
}
}
catch (Exception could_not_read_write_to_iostreams) {}
if (short_array2.length > short_array1.length) {
for (int i1 = 0; i1 < short_array2.length; i1++) {
while (i1 < short_array1.length) {
short read_short = (short) (short_array1[i1] + short_array2[i1]);
byte b3 = ((byte) (read_short & 0xff));
byte b4 = ((byte) (read_short >> 8 & 0xff));
try {
out.write(b3);
out.write(b4);
}
catch (Exception could_not_read_write_to_iostreams) {}
i1++;
}
}
}
if (short_array1.length > short_array2.length) {
for (int i1 = 0; i1 < short_array1.length; i1++) {
while (i1 < short_array2.length) {
short read_short = (short) (short_array1[i1] + short_array2[i1]);
byte b3 = ((byte) (read_short & 0xff));
byte b4 = ((byte) (read_short >> 8 & 0xff));
try {
out.write(b3);
out.write(b4);
}
catch (Exception could_not_read_write_to_iostreams) {}
i1++;
}
short read_short = (short) (short_array1[i1]);
byte b3 = ((byte) (read_short & 0xff));
byte b4 = ((byte) (read_short >> 8 & 0xff));
try {
out.write(b3);
out.write(b4);
}
catch (Exception could_not_read_write_to_iostreams) {}
}
}
getTracksList().add(write_file);
addAudioFile(getTracksList().indexOf(write_file));
}
void reverse() {
File current_file = getTracksList().get(getCurrentTrack());
StringBuilder sb_current = new StringBuilder(current_file.getPath());
sb_current.delete(sb_current.indexOf(".wav"), (sb_current.indexOf(".wav")+4));
File write_file = new File(sb_current + "_Reverse.wav");
short[] short_array = new short[(int) (getTracksList().get(getCurrentTrack()).length() - 44)/2];
FileOutputStream out = null;
FileInputStream in = null;
try {
in = new FileInputStream(current_file);
out = new FileOutputStream(write_file);
}
catch (Exception file_not_found) {}
try {
for (int i = 0; i<current_file.length()/2; i++) {
while (i < 44) {
byte b1 = (byte) (in.read() & 0xff);
byte b2 = (byte) (in.read() & 0xff);
out.write(b1);
out.write(b2);
i++;
}
byte b1 = (byte) (in.read() & 0xff);
byte b2 = (byte) (in.read() & 0xff);
short s = ((short) (b2 << 8 | (((int) b1) & 0xff) & 0xffff));
short_array[i-44] = s;
}
for(int i = 0; i < short_array.length / 2; i++) {
short temp = short_array[i];
short_array[i] = short_array[short_array.length - i - 1];
short_array[short_array.length - i - 1] = temp;
}
for(int i = 0; i < short_array.length; i++) {
short read_short = short_array[i];
byte b3 = ((byte) (read_short & 0xff));
byte b4 = ((byte) (read_short >> 8 & 0xff));
out.write(b3);
out.write(b4);
}
getTracksList().add(write_file);
addAudioFile(getTracksList().indexOf(write_file));
}
catch (Exception could_not_read_write_to_iostreams) {}
getTracksList().add(write_file);
addAudioFile(getTracksList().indexOf(write_file));
}
/* ACCESSORS */
ProgramFrame getProgramFrame() {
return program_frame;
}
ArrayList<File> getTracksList() {
return tracks_list;
}
int getCurrentTrack() {
return current_track;
}
private AudioInputStream getAudioInputStream() {
return our_audio_stream;
}
/* MUTATORS */
void setCurrentTrack(int other) {
current_track = other;
}
void setTracksList(ArrayList<File> other) {
tracks_list = other;
}
void setProgramFrame(ProgramFrame other) {
program_frame = other;
}
}