-
Notifications
You must be signed in to change notification settings - Fork 1
/
SoundManager.pde
121 lines (98 loc) · 2.41 KB
/
SoundManager.pde
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
/*
see processing.js wrapper
*/
public class SoundManager{
boolean muted = false;
Minim minim;
PlayerQueue matchPlayer;
PlayerQueue successSwapPlayer;
PlayerQueue failSwapPlayer;
/*
Handles the issue where we want to play multiple audio streams from the same clip.
*/
private class PlayerQueue{
private ArrayList <AudioPlayer> players;
private String path;
public PlayerQueue(String audioPath){
path = audioPath;
players = new ArrayList<AudioPlayer>();
appendPlayer();
}
public void close(){
for(int i = 0; i < players.size(); i++){
players.get(i).close();
}
}
public void play(){
int freePlayerIndex = -1;
for(int i = 0; i < players.size(); i++){
if(players.get(i).isPlaying() == false){
freePlayerIndex = i;
break;
}
}
if(freePlayerIndex == -1){
appendPlayer();
freePlayerIndex = players.size()-1;
}
players.get(freePlayerIndex).play();
players.get(freePlayerIndex).rewind();
}
private void appendPlayer(){
AudioPlayer player = minim.loadFile(path);
players.add(player);
}
public void setMute(boolean m){
for(int i = 0; i < players.size(); i++){
if(m){
players.get(i).mute();
}
else{
players.get(i).unmute();
}
}
}
}
public void init(){
}
public SoundManager(PApplet applet){
minim = new Minim(applet);
successSwapPlayer = new PlayerQueue("audio/success_swap.wav");
failSwapPlayer = new PlayerQueue("audio/fail_swap.wav");
matchPlayer = new PlayerQueue("audio/success_swap.wav");
}
public void setMute(boolean isMuted){
muted = isMuted;
successSwapPlayer.setMute(muted);
failSwapPlayer.setMute(muted);
matchPlayer.setMute(muted);
}
public boolean isMuted(){
return muted;
}
/*
*/
private void play(AudioPlayer player){
if(muted || player.isPlaying()){
return;
}
player.play();
player.rewind();
}
public void playSuccessSwapSound(){
successSwapPlayer.play();
}
public void playMatchSound(){
matchPlayer.play();
}
public void playFailSwapSound(){
failSwapPlayer.play();
}
public void stop(){
failSwapPlayer.close();
successSwapPlayer.close();
matchPlayer.close();
minim.stop();
// super.stop();
}
}