Crossfire JXClient, Trunk  R20561
ClipCache.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 
25 import java.io.IOException;
26 import javax.sound.sampled.AudioInputStream;
27 import javax.sound.sampled.AudioSystem;
28 import javax.sound.sampled.Clip;
29 import javax.sound.sampled.DataLine;
30 import javax.sound.sampled.Line;
31 import javax.sound.sampled.LineUnavailableException;
32 import javax.sound.sampled.UnsupportedAudioFileException;
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
35 
40 public class ClipCache {
41 
45  @NotNull
47 
52  @Nullable
53  private final DebugWriter debugSound;
54 
61  public ClipCache(@NotNull final AudioFileLoader audioFileLoader, @Nullable final DebugWriter debugSound) {
62  this.audioFileLoader = audioFileLoader;
63  this.debugSound = debugSound;
64  }
65 
72  @Nullable
73  public DataLine allocateClip(@Nullable final String name, @NotNull final String action) {
74  return newClip(name, action);
75  }
76 
81  public void freeClip(@NotNull final Line clip) {
82  if (debugSound != null) {
83  debugSound.debugProtocolWrite("freeClip: "+System.identityHashCode(clip));
84  }
85  clip.close();
86  }
87 
94  @Nullable
95  private DataLine newClip(@Nullable final String name, @NotNull final String action) {
96  try {
97  try (final AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(audioFileLoader.getInputStream(name, action))) {
98  final Clip clip;
99  try {
100  clip = AudioSystem.getClip();
101  clip.open(audioInputStream);
102  } catch (final IllegalArgumentException ex) {
103  final UnsupportedAudioFileException ex2 = new UnsupportedAudioFileException(ex.getMessage());
104  ex2.initCause(ex);
105  throw ex2;
106  }
107  if (debugSound != null) {
108  debugSound.debugProtocolWrite("newClip: "+System.identityHashCode(clip)+" "+name+"/"+action);
109  }
110  return clip;
111  }
112  } catch (final UnsupportedAudioFileException ex) {
113  if (debugSound != null) {
114  debugSound.debugProtocolWrite("newClip["+name+"/"+action+"]: "+ex.getMessage());
115  }
116  return null;
117  } catch (final LineUnavailableException ex) {
118  if (debugSound != null) {
119  debugSound.debugProtocolWrite("newClip["+name+"/"+action+"]: "+ex.getMessage());
120  }
121  return null;
122  } catch (final IOException ex) {
123  if (debugSound != null) {
124  debugSound.debugProtocolWrite("newClip["+name+"/"+action+"]: "+ex.getMessage());
125  }
126  return null;
127  }
128  }
129 
130 }
DataLine newClip(@Nullable final String name, @NotNull final String action)
Allocates a new clip.
Definition: ClipCache.java:95
DataLine allocateClip(@Nullable final String name, @NotNull final String action)
Allocates a new clip.
Definition: ClipCache.java:73
final DebugWriter debugSound
The writer for logging sound related information or.
Definition: ClipCache.java:53
void debugProtocolWrite(@NotNull final CharSequence str)
Writes a message to the debug protocol.
Writer debug information to a log file.
InputStream getInputStream(@Nullable final String name, @NotNull final String action)
Returns an input stream for an audio file.
final AudioFileLoader audioFileLoader
The AudioFileLoader for loading audio files.
Definition: ClipCache.java:46
ClipCache(@NotNull final AudioFileLoader audioFileLoader, @Nullable final DebugWriter debugSound)
Creates a new instance.
Definition: ClipCache.java:61
void freeClip(@NotNull final Line clip)
Deallocates a clip.
Definition: ClipCache.java:81