Crossfire JXClient, Trunk  R20561
SoundManager.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.sound;
23 
28 import java.util.Collection;
29 import java.util.EnumSet;
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
32 
39 public class SoundManager {
40 
44  @NotNull
45  private final ClipManager clipManager;
46 
51  @Nullable
52  private final DebugWriter debugSound;
53 
57  @NotNull
58  private final MusicManager musicManager;
59 
63  private boolean enabled;
64 
68  @NotNull
69  private final Collection<Sounds> mutedSounds = EnumSet.allOf(Sounds.class);
70 
75  @NotNull
76  @SuppressWarnings("FieldCanBeLocal")
78 
79  @Override
80  public void start() {
81  muteMusic(true);
82  mute(Sounds.CHARACTER, true);
83  }
84 
85  @Override
86  public void metaserver() {
87  muteMusic(true);
88  mute(Sounds.CHARACTER, true);
89  }
90 
91  @Override
92  public void preConnecting(@NotNull final String serverInfo) {
93  // ignore
94  }
95 
96  @Override
97  public void connecting(@NotNull final String serverInfo) {
98  muteMusic(true);
99  mute(Sounds.CHARACTER, true);
100  }
101 
102  @Override
103  public void connecting(@NotNull final ClientSocketState clientSocketState) {
104  // ignore
105  }
106 
107  @Override
108  public void connected() {
109  muteMusic(false);
110  mute(Sounds.CHARACTER, false);
111  }
112 
113  @Override
114  public void connectFailed(@NotNull final String reason) {
115  // ignore
116  }
117 
118  };
119 
126  public SoundManager(@NotNull final GuiStateManager guiStateManager, @Nullable final DebugWriter debugSound) {
127  final AudioFileLoader audioFileLoader = new AudioFileLoader(debugSound);
128  clipManager = new ClipManager(audioFileLoader, debugSound);
129  musicManager = new MusicManager(audioFileLoader, debugSound);
130  this.debugSound = debugSound;
131  guiStateManager.addGuiStateListener(guiStateListener);
132  }
133 
138  public void setEnabled(final boolean enabled) {
139  if (this.enabled == enabled) {
140  return;
141  }
142 
143  this.enabled = enabled;
144  musicManager.setEnabled(enabled);
145  }
146 
153  public void playClip(@NotNull final Sounds type, @Nullable final String name, @NotNull final String action) {
154  if (!enabled) {
155  if (debugSound != null) {
156  debugSound.debugProtocolWrite("playClip(type="+type+", name="+name+", action="+action+"): sound is muted");
157  }
158  return;
159  }
160 
161  if (mutedSounds.contains(type)) {
162  if (debugSound != null) {
163  debugSound.debugProtocolWrite("playClip(type="+type+", name="+name+", action="+action+"): sound type is muted");
164  }
165  return;
166  }
167 
168  if (debugSound != null) {
169  debugSound.debugProtocolWrite("playClip(type="+type+", name="+name+", action="+action+")");
170  }
171  clipManager.play(name, action);
172  }
173 
179  private void mute(@NotNull final Sounds type, final boolean mute) {
180  if (mute) {
181  mutedSounds.add(type);
182  } else {
183  mutedSounds.remove(type);
184  // XXX: stop running sounds of type
185  }
186  }
187 
193  public void playMusic(@Nullable final String name) {
194  musicManager.play(name);
195  }
196 
201  private void muteMusic(final boolean muted) {
202  musicManager.setMuted(muted);
203  }
204 
208  public void shutdown() {
209  if (debugSound != null) {
210  debugSound.debugProtocolWrite("shutdown");
211  }
212  musicManager.shutdown();
213  clipManager.shutdown();
214  }
215 
216 }
void shutdown()
Terminates all sounds and free resources.
Interface for listeners interested gui state changes.
void setMuted(final boolean muted)
Sets whether background music is muted.
final MusicManager musicManager
The music manager for playing background music.
void shutdown()
Terminates all running clips and free resources.
void debugProtocolWrite(@NotNull final CharSequence str)
Writes a message to the debug protocol.
final GuiStateListener guiStateListener
The GuiStateListener for detecting established or dropped connections.
void muteMusic(final boolean muted)
Mutes or unmutes background music.
Writer debug information to a log file.
void shutdown()
Terminates a playing background music and free resources.
void play(@Nullable final String name, @NotNull final String action)
Plays the given sound effect.
final ClipManager clipManager
The clip manager for playing sound effects.
void mute(@NotNull final Sounds type, final boolean mute)
Mutes or unmutes sound effects.
void playClip(@NotNull final Sounds type, @Nullable final String name, @NotNull final String action)
Plays a sound clip.
void play(@Nullable final String name)
Plays the given music.
final DebugWriter debugSound
The writer for logging sound related information or.
SoundManager(@NotNull final GuiStateManager guiStateManager, @Nullable final DebugWriter debugSound)
Creates a new instance.
void setEnabled(final boolean enabled)
Sets whether the sound system is enabled.
Manages a set of sound clips (short sound effects).
void setEnabled(final boolean enabled)
Sets whether background music is enabled.
final Collection< Sounds > mutedSounds
The muted sounds.
void playMusic(@Nullable final String name)
Plays a background music.
boolean enabled
Whether sound is enabled.
Connection progress states of the Crossfire server connection.