|
| 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.IOException; |
| 27 | + |
| 28 | +import javax.sound.sampled.AudioInputStream; |
| 29 | +import javax.sound.sampled.AudioSystem; |
| 30 | +import javax.sound.sampled.Clip; |
| 31 | +import javax.sound.sampled.LineUnavailableException; |
| 32 | + |
| 33 | +/** |
| 34 | + * This class implements the Event Queue pattern. |
| 35 | + * @author mkuprivecz |
| 36 | + * |
| 37 | + */ |
| 38 | +public class Audio { |
| 39 | + |
| 40 | + private static final int MAX_PENDING = 16; |
| 41 | + |
| 42 | + private static int headIndex; |
| 43 | + |
| 44 | + private static int tailIndex; |
| 45 | + |
| 46 | + private static Thread updateThread = null; |
| 47 | + |
| 48 | + private static PlayMessage[] pendingAudio = new PlayMessage[MAX_PENDING]; |
| 49 | + |
| 50 | + public static boolean isServiceRunning() { |
| 51 | + return updateThread.isAlive(); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * This method stops the Update Method's thread. |
| 56 | + */ |
| 57 | + public static void stopService() { |
| 58 | + if (updateThread != null) { |
| 59 | + updateThread.interrupt(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Starts the thread for the Update Method pattern if it was not started previously. |
| 65 | + * Also when the thread is is ready initializes the indexes of the queue |
| 66 | + */ |
| 67 | + public static void init() { |
| 68 | + if (updateThread == null) { |
| 69 | + updateThread = new Thread(new Runnable() { |
| 70 | + public void run() { |
| 71 | + while (!Thread.currentThread().isInterrupted()) { |
| 72 | + Audio.update(); |
| 73 | + } |
| 74 | + } |
| 75 | + }); |
| 76 | + } |
| 77 | + if (!updateThread.isAlive()) { |
| 78 | + updateThread.start(); |
| 79 | + headIndex = 0; |
| 80 | + tailIndex = 0; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * This method adds a new audio into the queue. |
| 86 | + * @param stream is the AudioInputStream for the method |
| 87 | + * @param volume is the level of the audio's volume |
| 88 | + */ |
| 89 | + public static void playSound(AudioInputStream stream, float volume) { |
| 90 | + init(); |
| 91 | + // Walk the pending requests. |
| 92 | + for (int i = headIndex; i != tailIndex; i = (i + 1) % MAX_PENDING) { |
| 93 | + if (pendingAudio[i].stream == stream) { |
| 94 | + // Use the larger of the two volumes. |
| 95 | + pendingAudio[i].volume = Math.max(volume, pendingAudio[i].volume); |
| 96 | + |
| 97 | + // Don't need to enqueue. |
| 98 | + return; |
| 99 | + } |
| 100 | + } |
| 101 | + pendingAudio[tailIndex] = new PlayMessage(); |
| 102 | + pendingAudio[tailIndex].stream = stream; |
| 103 | + pendingAudio[tailIndex].volume = volume; |
| 104 | + tailIndex = (tailIndex + 1) % MAX_PENDING; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * This method uses the Update Method pattern. |
| 109 | + * It takes the audio from the queue and plays it |
| 110 | + */ |
| 111 | + public static void update() { |
| 112 | + // If there are no pending requests, do nothing. |
| 113 | + if (headIndex == tailIndex) { |
| 114 | + return; |
| 115 | + } |
| 116 | + Clip clip = null; |
| 117 | + try { |
| 118 | + clip = AudioSystem.getClip(); |
| 119 | + clip.open(pendingAudio[headIndex].stream); |
| 120 | + } catch (LineUnavailableException e) { |
| 121 | + System.err.println("Error occoured while loading the audio: The line is unavailable"); |
| 122 | + e.printStackTrace(); |
| 123 | + } catch (IOException e) { |
| 124 | + System.err.println("Input/Output error while loading the audio"); |
| 125 | + e.printStackTrace(); |
| 126 | + } |
| 127 | + clip.start(); |
| 128 | + |
| 129 | + headIndex++; |
| 130 | + } |
| 131 | +} |
0 commit comments