@matfah/

Sound FR 2011

Java

No description

fork
loading
Files
  • Main.java
  • Sound.java

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.4293489049379824","path":"Main.java","file":{"path":"Main.java","content":{"asEncoding":{"base64":"aW1wb3J0IGphdmEubGFuZy5yZWZsZWN0Lio7CgpwdWJsaWMgY2xhc3MgTWFpbiB7CiAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogIAogICAgdGVzdExpbWl0QW1wbGl0dWRlKCk7CiAgICB0ZXN0VHJpbVNpbGVuY2VGcm9tQmVnaW5uaW5nKCk7CiAgICBTeXN0ZW0ub3V0LnByaW50bG4oIkFsbCBUZXN0cyBQYXNzZWQhIik7CiAgICAKICB9CiAgCiAgcHVibGljIHN0YXRpYyB2b2lkIHRlc3RMaW1pdEFtcGxpdHVkZSgpIHsKICAgIAogICAgRmllbGQgc2FtcGxlc0ZpZWxkID0gbnVsbDsKICAgIAogICAgdHJ5IHsKICAgICAgc2FtcGxlc0ZpZWxkID0gU291bmQuY2xhc3MuZ2V0RGVjbGFyZWRGaWVsZCgic2FtcGxlcyIpOwogICAgfQogICAgY2F0Y2goRXhjZXB0aW9uIGUpIHsKICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJFcnJvciBhY2Nlc3NpbmcgdGhlIHNhbXBsZXMgaW5zdGFuY2UgdmFyaWFibGUiKTsKICAgICAgU3lzdGVtLmV4aXQoMCk7CiAgICB9CiAgICAKICAgIHNhbXBsZXNGaWVsZC5zZXRBY2Nlc3NpYmxlKHRydWUpOwogICAgCiAgICBmb3IoaW50IGxlbmd0aCA9IDA7IGxlbmd0aCA8PSAyMDsgbGVuZ3RoKyspIHsKICAgICAgCiAgICAgIGludFtdIHNhbXBsZXMgPSBuZXcgaW50W2xlbmd0aF07CiAgICAgIGludFtdIGZpeGVkID0gbmV3IGludFtsZW5ndGhdOwogICAgICAKICAgICAgaW50IGxpbWl0ID0gKGludCkoTWF0aC5yYW5kb20oKSAqIDEwKTsKICAgICAgaW50IGNvdW50ID0gMDsKICAgICAgCiAgICAgIGZvcihpbnQgaSA9IDA7IGkgPCBsZW5ndGg7IGkrKykgewogICAgICAgIHNhbXBsZXNbaV0gPSAoaW50KShNYXRoLnJhbmRvbSgpKjQwKSAtIDIwOwogICAgICAgIGZpeGVkW2ldID0gTWF0aC5taW4oTWF0aC5hYnMoc2FtcGxlc1tpXSksIGxpbWl0KSAqIChpbnQpTWF0aC5zaWdudW0oc2FtcGxlc1tpXSk7CiAgICAgICAgaWYoZml4ZWRbaV0gIT0gc2FtcGxlc1tpXSkKICAgICAgICAgIGNvdW50Kys7CiAgICAgIH0KICAgICAgCiAgICAgIFNvdW5kIHNvdW5kID0gbmV3IFNvdW5kKCk7CiAgICAgIAogICAgICB0cnkgewogICAgICAgIHNhbXBsZXNGaWVsZC5zZXQoc291bmQsIHNhbXBsZXMpOwogICAgICB9CiAgICAgIGNhdGNoKEV4Y2VwdGlvbiBlKSB7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJFcnJvciBzZXR0aW5nIHRoZSBzYW1wbGVzIGluc3RhbmNlIHZhcmlhYmxlIik7CiAgICAgICAgU3lzdGVtLmV4aXQoMCk7CiAgICAgIH0KICAgICAgCiAgICAgIGludCB2YWx1ZSA9IDA7CiAgICAgIAogICAgICB0cnkgewogICAgICAgIHZhbHVlID0gc291bmQubGltaXRBbXBsaXR1ZGUobGltaXQpOwogICAgICB9CiAgICAgIGNhdGNoKEV4Y2VwdGlvbiBlKSB7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJFcnJvciBjYWxsaW5nIGxpbWl0QW1wbGl0dWRlKCkiKTsKICAgICAgICBTeXN0ZW0uZXhpdCgwKTsKICAgICAgfQogICAgICAKICAgICAgaWYodmFsdWUgIT0gY291bnQpIHsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIlZhbHVlIHJldHVybmVkIGJ5IGxpbWl0QW1wbGl0dWRlK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wcmludGxuKCJFcnJvciBhY2Nlc3NpbmcgdGhlIHNhbXBsZXMgaW5zdGFuY2UgdmFyaWFibGUiKTsKICAgICAgICBTeXN0ZW0uZXhpdCgwKTsKICAgICAgfQogICAgICAKICAgICAgaWYoIWphdmEudXRpbC5BcnJheXMuZXF1YWxzKHNhbXBsZXMsIGZpeGVkKSkgewogICAgICAgIAogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigidHJpbVNpbGVuY2VGcm9tQmVnaW5uaW5nKCkgZGlkIG5vdCBjaGFuZ2UgYXJyYXkgdmFsdWVzIGNvcnJlY3RseSIpOwogICAgICAgIFN5c3RlbS5leGl0KDApOwogICAgICB9CiAgICB9CiAgfQp9"},"asBuffer":null},"loaded":true}}
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
import java.lang.reflect.*;

public class Main {
  public static void main(String[] args) {
  
    testLimitAmplitude();
    testTrimSilenceFromBeginning();
    System.out.println("All Tests Passed!");
    
  }
  
  public static void testLimitAmplitude() {
    
    Field samplesField = null;
    
    try {
      samplesField = Sound.class.getDeclaredField("samples");
    }
    catch(Exception e) {
      System.out.println("Error accessing the samples instance variable");
      System.exit(0);
    }
    
    samplesField.setAccessible(true);
    
    for(int length = 0; length <= 20; length++) {
      
      int[] samples = new int[length];
      int[] fixed = new int[length];
      
      int limit = (int)(Math.random() * 10);
      int count = 0;
      
      for(int i = 0; i < length; i++) {
        samples[i] = (int)(Math.random()*40) - 20;
        fixed[i] = Math.min(Math.abs(samples[i]), limit) * (int)Math.signum(samples[i]);
        if(fixed[i] != samples[i])
          count++;
      }
      
      Sound sound = new Sound();
      
      try {
        samplesField.set(sound, samples);
      }
      catch(Exception e) {
        System.out.println("Error setting the samples instance variable");
        System.exit(0);
      }
      
      int value = 0;
      
      try {
        value = sound.limitAmplitude(limit);
      }
      catch(Exception e) {
        System.out.println("Error calling limitAmplitude()");
        System.exit(0);
      }
      
      if(value != count) {
        System.out.println("Value returned by limitAmplitude() is not correct");
        System.exit(0);
      }
      
      if(!java.util.Arrays.equals(samples, fixed)) {
        System.out.println("limitAmplitude() did not change array values correctly");
        System.exit(0);
      }
    }
    
  }
  
  public static void testTrimSilenceFromBeginning() {
    Field samplesField = null;
    
    try {
      samplesField = Sound.class.getDeclaredField("samples");
    }
    catch(Exception e) {
      System.out.println("Error accessing the samples instance variable");
      System.exit(0);
    }
    
    samplesField.setAccessible(true);
    
    for(int length = 1; length <= 20; length++) {
      
      int[] samples = new int[length];
      samples[length-1] = (int)(Math.random()*7)+1;
      
      int leadingZeros = (int)(Math.random()*(length-1));
      int[] fixed = new int[length - leadingZeros];
      fixed[fixed.length-1] = samples[length - 1];

      for(int i = leadingZeros; i < length-1; i++) {
        samples[i] = ((int)(Math.random()*3)+1) * (int)Math.pow(-1, (int)(Math.random()*2));
        fixed[i - leadingZeros] = samples[i];
      }

      Sound sound = new Sound();
      
      try {
        samplesField.set(sound, samples);
      }
      catch(Exception e) {
        System.out.println("Error setting the samples instance variable");
        System.exit(0);
      }
      
      try {
        sound.trimSilenceFromBeginning();
      }
      catch(Exception e) {
        System.out.println("Error calling trimSilenceFromBeginning()");
        System.exit(0);
      }
      
      try {
        samples = (int[])samplesField.get(sound);
      }
      catch(Exception e) {
        System.out.println("Error accessing the samples instance variable");
        System.exit(0);
      }
      
      if(!java.util.Arrays.equals(samples, fixed)) {
        
        System.out.println("trimSilenceFromBeginning() did not change array values correctly");
        System.exit(0);
      }
    }
  }
}