|
| 1 | +/** |
| 2 | + * The MIT License |
| 3 | + * Copyright (c) 2014-2016 Ilkka Seppälä |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | + * THE SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +package com.iluwatar.event.queue; |
| 25 | + |
| 26 | +import java.io.File; |
| 27 | +import java.io.IOException; |
| 28 | + |
| 29 | +import javax.sound.sampled.AudioInputStream; |
| 30 | +import javax.sound.sampled.AudioSystem; |
| 31 | +import javax.sound.sampled.Clip; |
| 32 | +import javax.sound.sampled.LineUnavailableException; |
| 33 | +import javax.sound.sampled.UnsupportedAudioFileException; |
| 34 | + |
| 35 | +/** |
| 36 | + * This class implements the Event Queue pattern. |
| 37 | + * @author mkuprivecz |
| 38 | + * |
| 39 | + */ |
| 40 | +public class Audio { |
| 41 | + |
| 42 | + private static final int MAX_PENDING = 16; |
| 43 | + |
| 44 | + private static int headIndex; |
| 45 | + |
| 46 | + private static int tailIndex; |
| 47 | + |
| 48 | + private static Thread updateThread = null; |
| 49 | + |
| 50 | + private static PlayMessage[] pendingAudio = new PlayMessage[MAX_PENDING]; |
| 51 | + |
| 52 | + /** |
| 53 | + * This method stops the Update Method's thread. |
| 54 | + */ |
| 55 | + public static synchronized void stopService() { |
| 56 | + if (updateThread != null) { |
| 57 | + updateThread.interrupt(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * This method stops the Update Method's thread. |
| 63 | + * @return boolean |
| 64 | + */ |
| 65 | + public static synchronized boolean isServiceRunning() { |
| 66 | + if (updateThread != null && updateThread.isAlive() ) { |
| 67 | + return true; |
| 68 | + } else { |
| 69 | + return false; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Starts the thread for the Update Method pattern if it was not started previously. |
| 75 | + * Also when the thread is is ready initializes the indexes of the queue |
| 76 | + */ |
| 77 | + public static void init() { |
| 78 | + if (updateThread == null) { |
| 79 | + updateThread = new Thread(new Runnable() { |
| 80 | + public void run() { |
| 81 | + while (!Thread.currentThread().isInterrupted()) { |
| 82 | + Audio.update(); |
| 83 | + } |
| 84 | + } |
| 85 | + }); |
| 86 | + } |
| 87 | + startThread(); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * This is a synchronized thread starter |
| 92 | + */ |
| 93 | + public static synchronized void startThread() { |
| 94 | + if (!updateThread.isAlive()) { |
| 95 | + updateThread.start(); |
| 96 | + headIndex = 0; |
| 97 | + tailIndex = 0; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * This method adds a new audio into the queue. |
| 103 | + * @param stream is the AudioInputStream for the method |
| 104 | + * @param volume is the level of the audio's volume |
| 105 | + */ |
| 106 | + public static void playSound(AudioInputStream stream, float volume) { |
| 107 | + init(); |
| 108 | + // Walk the pending requests. |
| 109 | + for (int i = headIndex; i != tailIndex; i = (i + 1) % MAX_PENDING) { |
| 110 | + if (getPendingAudio()[i].getStream() == stream) { |
| 111 | + // Use the larger of the two volumes. |
| 112 | + getPendingAudio()[i].setVolume(Math.max(volume, getPendingAudio()[i].getVolume())); |
| 113 | + |
| 114 | + // Don't need to enqueue. |
| 115 | + return; |
| 116 | + } |
| 117 | + } |
| 118 | + getPendingAudio()[tailIndex] = new PlayMessage(stream, volume); |
| 119 | + tailIndex = (tailIndex + 1) % MAX_PENDING; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * This method uses the Update Method pattern. |
| 124 | + * It takes the audio from the queue and plays it |
| 125 | + */ |
| 126 | + public static void update() { |
| 127 | + // If there are no pending requests, do nothing. |
| 128 | + if (headIndex == tailIndex) { |
| 129 | + return; |
| 130 | + } |
| 131 | + Clip clip = null; |
| 132 | + try { |
| 133 | + AudioInputStream audioStream = getPendingAudio()[headIndex].getStream(); |
| 134 | + headIndex++; |
| 135 | + clip = AudioSystem.getClip(); |
| 136 | + clip.open(audioStream); |
| 137 | + clip.start(); |
| 138 | + } catch (LineUnavailableException e) { |
| 139 | + System.err.println("Error occoured while loading the audio: The line is unavailable"); |
| 140 | + e.printStackTrace(); |
| 141 | + } catch (IOException e) { |
| 142 | + System.err.println("Input/Output error while loading the audio"); |
| 143 | + e.printStackTrace(); |
| 144 | + } catch (IllegalArgumentException e) { |
| 145 | + System.err.println("The system doesn't support the sound: " + e.getMessage()); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Returns the AudioInputStream of a file |
| 151 | + * @param filePath is the path of the audio file |
| 152 | + * @return AudioInputStream |
| 153 | + * @throws UnsupportedAudioFileException when the audio file is not supported |
| 154 | + * @throws IOException when the file is not readable |
| 155 | + */ |
| 156 | + public static AudioInputStream getAudioStream(String filePath) |
| 157 | + throws UnsupportedAudioFileException, IOException { |
| 158 | + return AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile()); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Returns with the message array of the queue |
| 163 | + * @return PlayMessage[] |
| 164 | + */ |
| 165 | + public static PlayMessage[] getPendingAudio() { |
| 166 | + return pendingAudio; |
| 167 | + } |
| 168 | + |
| 169 | +} |
0 commit comments