-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sound Player
140 lines (90 loc) · 3.04 KB
/
Sound Player
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
//Create string to get user input for notes.
String wav = SimpleInput.getString("Enter a sequence of notes to play: ");
System.out.println(wav);
//Creates integer to store lenght of string
//Creates final sound to hold all notes of the string
int songL = wav.length();
Sound finalSound = new Sound (songL);
//Converts sting to sound wave
for(int i = 0; i < wav.length(); i++)
{
double freq;
char input = wav.charAt(i);
char[] sound = new char[wav.charAt(i)];
char[] notes = {"78!9@a#bc$d%ef^g&A*BC(D)EF_G+H"};
//If '.' character represents 0 frequency
if(notes != input)
{
freq = 0.0;
} else {
freq = calcFreq(input);
}
Sound Note = sineWave(freq, 20000);
finalSound = mergeSounds(finalSound, Note);
}
//Plays sound
finalSound.explore();
finalSound.play();
String output;
output = FileChooser.pickAFile();
finalSound.write(output);
}
//End Main Method
//Method calculates Frequency and notes based on user input.
public static double calcFreq ( char note )
{
String noteRepresentation = "78!9@a#bc$d%ef^g&A*BC(D)EF_G+H";
double freq;
int x = noteRepresentation.indexOf(note);
int y = noteRepresentation.indexOf('H');
double exponent = (x - y)/12.0;
freq = 440.0 * Math.pow (2.0, exponent); // freq becomes 196.0 (if rounded)
return freq;
}
//Method creates Sine wave for different notes
public static Sound sineWave (double freq, int amplitude)
{
int maxLength = 22050/8;
Sound s = new Sound (maxLength);
double sampleRate = s.getSamplingRate();
double rawV;
int indexV;
int newV;
double interval = 1.0/freq;
double samplesPerCycle = interval * sampleRate;
double maxV = 2 * Math.PI;
SoundSample sound[] = s.getSamples();
for ( int i = 0; i < s.getLength(); i++ )
{
rawV = Math.sin((i/samplesPerCycle) * maxV);
newV = (int) (amplitude * rawV);
sound[i].setValue ( newV );
}
return s;
}
//Method creates song from joined notes.
public static Sound mergeSounds ( Sound s1 , Sound s2)
{
int length1 = s1.getLength();
int length2 = s2.getLength();
int finalLength = length1 + length2;
Sound wave = new Sound(finalLength);
SoundSample sound1[] = s1.getSamples();
SoundSample sound2[] = s2.getSamples();
SoundSample finalSound[] = wave.getSamples();
for (int i = 0 ; i < length1 ; i++ )
{
int amp1 = sound1[i].getValue();
int total = i;
finalSound[total].setValue ( amp1 );
}
for (int i = 0 ; i < length2; i++ )
{
int amp2 = sound2[i].getValue();
int total = i + length1;
finalSound[total].setValue ( amp2 );
}
return wave;
}
}
// end of program