-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sound.java
114 lines (97 loc) · 2.94 KB
/
Sound.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
import java.io.*;
import javax.sound.sampled.*;
import legacy.Debug;
public class Sound {
static Clip levelMusic, gameMusic;
static int volume = 50;
public Sound() {}
/**
* initializes the sound player
*
* @throws Exception
*/
public static void init() throws Exception {
AudioInputStream level = AudioSystem.getAudioInputStream(new File("Sound\\kahoot.wav")),
game = AudioSystem.getAudioInputStream(new File("Sound\\misstherage.wav"));
levelMusic = AudioSystem.getClip();
levelMusic.open(level);
gameMusic = AudioSystem.getClip();
gameMusic.open(game);
playMusic(0);
}
/**
* Play's the background music
*
* @param music provides an id for which music needs to be played
*/
public static void playMusic(int music) {
switch (music) {
case 1:
levelMusic.stop();
levelMusic.setMicrosecondPosition(0);
gameMusic.loop(Clip.LOOP_CONTINUOUSLY);
break;
default:
gameMusic.stop();
gameMusic.setMicrosecondPosition(0);
levelMusic.loop(Clip.LOOP_CONTINUOUSLY);
break;
}
}
/**
* plays a sound effect
*
* @param sfx sends query to which sound effect needs to be played
*/
public static void playSfx(int sfx) {
try {
sfx(sfx);
} catch (Exception e) {
System.err.println("Sound effect could not play");
e.printStackTrace();
}
}
/**
* determines and plays the correct sound effect
*
* @param sfx provides a key for which sound effect needs to be played
* @throws Exception
*/
public static void sfx(int sfx) throws Exception {
AudioInputStream stream = null;
switch (sfx) {
case 1:
break;
default:
stream = AudioSystem.getAudioInputStream(new File("Sound\\click.wav"));
break;
}
Clip clip = AudioSystem.getClip();
clip.open(stream);
FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
gainControl.setValue(20f * (float) Math.log10(Sound.volume/100.0));
clip.start();
}
/**
* Sets the music volume
*
* @param volume provides the volume in %
*/
public static void setMusicVolume(int volume) {
// Debug.print(volume);
FloatControl gainControl = (FloatControl) levelMusic.getControl(FloatControl.Type.MASTER_GAIN);
gainControl.setValue(20f * (float) Math.log10(volume/100.0));
gainControl = (FloatControl) gameMusic.getControl(FloatControl.Type.MASTER_GAIN);
gainControl.setValue(20f * (float) Math.log10(volume/100.0));
// Bson.updateMusic(volume);
}
/**
* Sets the sound effect's volume
*
* @param volume provides the volume in %
*/
public static void setSfxVolume(int volume) {
Sound.volume = volume;
// Bson.updateSFX(volume);
}
}