Crossfire JXClient, Trunk
ClipLoader.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-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.sound;
24 
26 import java.io.IOException;
27 import javax.sound.sampled.AudioFormat;
28 import javax.sound.sampled.AudioFormat.Encoding;
29 import javax.sound.sampled.AudioInputStream;
30 import javax.sound.sampled.AudioSystem;
31 import javax.sound.sampled.Clip;
32 import javax.sound.sampled.DataLine;
33 import javax.sound.sampled.Line;
34 import javax.sound.sampled.LineUnavailableException;
35 import javax.sound.sampled.UnsupportedAudioFileException;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.annotations.Nullable;
38 
43 public class ClipLoader {
44 
48  @NotNull
50 
55  @Nullable
56  private final DebugWriter debugSound;
57 
64  public ClipLoader(@NotNull final AudioFileLoader audioFileLoader, @Nullable final DebugWriter debugSound) {
65  this.audioFileLoader = audioFileLoader;
66  this.debugSound = debugSound;
67  }
68 
73  public void freeClip(@NotNull final Line clip) {
74  if (debugSound != null) {
75  debugSound.debugProtocolWrite("freeClip: "+System.identityHashCode(clip));
76  }
77  clip.close();
78  }
79 
85  @Nullable
86  public DataLine allocateClip(@NotNull final CharSequence action) {
87  try {
88  try (AudioInputStream rawInputStream = AudioSystem.getAudioInputStream(audioFileLoader.getInputStream(action))) {
89  final Clip clip;
90  try {
91  final AudioFormat baseFormat = rawInputStream.getFormat();
92  final AudioFormat decodedFormat = new AudioFormat(Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, baseFormat.getChannels(), baseFormat.getChannels()*2, baseFormat.getSampleRate(), false);
93  try (AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(decodedFormat, rawInputStream)) {
94  clip = AudioSystem.getClip();
95  clip.open(audioInputStream);
96  }
97  } catch (final IllegalArgumentException|NegativeArraySizeException ex) {
98  final UnsupportedAudioFileException ex2 = new UnsupportedAudioFileException(ex.getMessage());
99  ex2.initCause(ex);
100  throw ex2;
101  }
102  if (debugSound != null) {
103  debugSound.debugProtocolWrite("newClip: "+System.identityHashCode(clip)+" "+action);
104  }
105  return clip;
106  }
107  } catch (final IOException|LineUnavailableException|UnsupportedAudioFileException ex) {
108  if (debugSound != null) {
109  debugSound.debugProtocolWrite("newClip["+action+"]: "+ex.getMessage());
110  }
111  return null;
112  }
113  }
114 
115 }
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.sound.ClipLoader.ClipLoader
ClipLoader(@NotNull final AudioFileLoader audioFileLoader, @Nullable final DebugWriter debugSound)
Definition: ClipLoader.java:64
com.realtime.crossfire.jxclient.sound.ClipLoader
Definition: ClipLoader.java:43
com.realtime.crossfire.jxclient.sound.ClipLoader.debugSound
final DebugWriter debugSound
Definition: ClipLoader.java:56
com.realtime.crossfire.jxclient.sound.AudioFileLoader
Definition: AudioFileLoader.java:41
com.realtime.crossfire.jxclient.sound.ClipLoader.freeClip
void freeClip(@NotNull final Line clip)
Definition: ClipLoader.java:73
com.realtime.crossfire.jxclient.sound.ClipLoader.audioFileLoader
final AudioFileLoader audioFileLoader
Definition: ClipLoader.java:49
com.realtime.crossfire.jxclient.util
Definition: Codec.java:23
com.realtime.crossfire
com.realtime.crossfire.jxclient.sound.ClipLoader.allocateClip
DataLine allocateClip(@NotNull final CharSequence action)
Definition: ClipLoader.java:86
com.realtime
com
com.realtime.crossfire.jxclient.util.DebugWriter
Definition: DebugWriter.java:36
com.realtime.crossfire.jxclient.sound.AudioFileLoader.getInputStream
InputStream getInputStream(@NotNull final CharSequence name)
Definition: AudioFileLoader.java:67
com.realtime.crossfire.jxclient.util.DebugWriter.debugProtocolWrite
void debugProtocolWrite(@NotNull final CharSequence str)
Definition: DebugWriter.java:68