|
| 1 | +package com.goxr3plus.streamplayer.stream; |
| 2 | + |
| 3 | +import javax.sound.sampled.*; |
| 4 | +import java.util.logging.Logger; |
| 5 | + |
| 6 | +/** |
| 7 | + * Owner of the SourceDataLine which is the output line of the player. |
| 8 | + * Also owns controls for the SourceDataLine. |
| 9 | + * Future goal is to move all handling of the SourceDataLine to here, |
| 10 | + * so that the StreamPlayer doesn't have to call {@link #getSourceDataLine()}. |
| 11 | + * Another goal is to remove some of the setter and getter methods of this class, |
| 12 | + * by moving all code that needs them to this class. |
| 13 | + */ |
| 14 | +public class Outlet { |
| 15 | + |
| 16 | + private final Logger logger; |
| 17 | + private FloatControl balanceControl; |
| 18 | + private FloatControl gainControl; |
| 19 | + private BooleanControl muteControl; |
| 20 | + private FloatControl panControl; |
| 21 | + private SourceDataLine sourceDataLine; |
| 22 | + |
| 23 | + /** |
| 24 | + * @param logger used to log messages |
| 25 | + */ |
| 26 | + public Outlet(Logger logger) { |
| 27 | + this.logger = logger; |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | + /** |
| 32 | + * @return the balance control of the {@link #sourceDataLine} |
| 33 | + */ |
| 34 | + public FloatControl getBalanceControl() { |
| 35 | + return balanceControl; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @return the gain control of the {@link #sourceDataLine} |
| 40 | + */ |
| 41 | + public FloatControl getGainControl() { |
| 42 | + return gainControl; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @return the mute control of the {@link #sourceDataLine} |
| 47 | + */ |
| 48 | + public BooleanControl getMuteControl() { |
| 49 | + return muteControl; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @return the pan control of the {@link #sourceDataLine} |
| 54 | + */ |
| 55 | + public FloatControl getPanControl() { |
| 56 | + return panControl; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @return the {@link #sourceDataLine}, which is the output audio signal of the player |
| 61 | + */ |
| 62 | + public SourceDataLine getSourceDataLine() { |
| 63 | + return sourceDataLine; |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + /** |
| 68 | + * @param balanceControl to be set on the {@link #sourceDataLine} |
| 69 | + */ |
| 70 | + public void setBalanceControl(FloatControl balanceControl) { |
| 71 | + this.balanceControl = balanceControl; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @param gainControl to be set on the {@link #sourceDataLine} |
| 76 | + */ |
| 77 | + public void setGainControl(FloatControl gainControl) { |
| 78 | + this.gainControl = gainControl; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @param muteControl to be set on the {@link #sourceDataLine} |
| 83 | + */ |
| 84 | + public void setMuteControl(BooleanControl muteControl) { |
| 85 | + this.muteControl = muteControl; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @param panControl to be set on the {@link #sourceDataLine} |
| 90 | + */ |
| 91 | + public void setPanControl(FloatControl panControl) { |
| 92 | + this.panControl = panControl; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @param sourceDataLine representing the audio output of the player. |
| 97 | + * Usually taken from {@link AudioSystem#getLine(Line.Info)}. |
| 98 | + */ |
| 99 | + public void setSourceDataLine(SourceDataLine sourceDataLine) { |
| 100 | + this.sourceDataLine = sourceDataLine; |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + /** |
| 105 | + * Check if the <b>Control</b> is Supported by m_line. |
| 106 | + * |
| 107 | + * @param control the control |
| 108 | + * @param component the component |
| 109 | + * |
| 110 | + * @return true, if successful |
| 111 | + */ |
| 112 | + public boolean hasControl(final Control.Type control, final Control component) { |
| 113 | + return component != null && (sourceDataLine != null) && (sourceDataLine.isControlSupported(control)); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Returns Gain value. |
| 118 | + * |
| 119 | + * @return The Gain Value |
| 120 | + */ |
| 121 | + public float getGainValue() { |
| 122 | + |
| 123 | + if (hasControl(FloatControl.Type.MASTER_GAIN, getGainControl())) { |
| 124 | + return getGainControl().getValue(); |
| 125 | + } else { |
| 126 | + return 0.0F; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Stop the {@link #sourceDataLine} in a nice way. |
| 132 | + * Also nullify it. (Is that necessary?) |
| 133 | + */ |
| 134 | + void drainStopAndFreeDataLine() { |
| 135 | + // Free audio resources. |
| 136 | + if (sourceDataLine != null) { |
| 137 | + sourceDataLine.drain(); |
| 138 | + sourceDataLine.stop(); |
| 139 | + sourceDataLine.close(); |
| 140 | + this.sourceDataLine = null; // TODO: Is this necessary? Will it not be garbage collected? |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Flush and close the {@link #sourceDataLine} in a nice way. |
| 146 | + * Also nullify it. (Is that necessary?) |
| 147 | + */ |
| 148 | + void flushAndFreeDataLine() { |
| 149 | + if (sourceDataLine != null) { |
| 150 | + sourceDataLine.flush(); |
| 151 | + sourceDataLine.close(); |
| 152 | + this.sourceDataLine = null; // TODO: Is this necessary? Will it not be garbage collected? |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Flush and stop the {@link #sourceDataLine}, if it's running. |
| 158 | + */ |
| 159 | + void flushAndStop() { |
| 160 | + // Flush and stop the source data line |
| 161 | + if (sourceDataLine != null && sourceDataLine.isRunning()) { // TODO: Risk for NullPointerException? |
| 162 | + sourceDataLine.flush(); |
| 163 | + sourceDataLine.stop(); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * @return true if the {@link #sourceDataLine} is startable. |
| 169 | + */ |
| 170 | + boolean isStartable() { |
| 171 | + return sourceDataLine != null && !sourceDataLine.isRunning(); |
| 172 | + } |
| 173 | + |
| 174 | + |
| 175 | + /** |
| 176 | + * Start the {@link #sourceDataLine} |
| 177 | + */ |
| 178 | + void start() { |
| 179 | + sourceDataLine.start(); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Open the {@link #sourceDataLine}. |
| 184 | + * Also create controls for it. |
| 185 | + * @param format The wanted audio format. |
| 186 | + * @param bufferSize the desired buffer size for the {@link #sourceDataLine} |
| 187 | + * @throws LineUnavailableException |
| 188 | + */ |
| 189 | + void open(AudioFormat format, int bufferSize) throws LineUnavailableException { |
| 190 | + logger.info("Entered OpenLine()!:\n"); |
| 191 | + |
| 192 | + if (sourceDataLine != null) { |
| 193 | + sourceDataLine.open(format, bufferSize); |
| 194 | + |
| 195 | + // opened? |
| 196 | + if (sourceDataLine.isOpen()) { |
| 197 | + |
| 198 | + // Master_Gain Control? |
| 199 | + if (sourceDataLine.isControlSupported(FloatControl.Type.MASTER_GAIN)) |
| 200 | + setGainControl((FloatControl) sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN)); |
| 201 | + else setGainControl(null); |
| 202 | + |
| 203 | + // PanControl? |
| 204 | + if (sourceDataLine.isControlSupported(FloatControl.Type.PAN)) |
| 205 | + setPanControl((FloatControl) sourceDataLine.getControl(FloatControl.Type.PAN)); |
| 206 | + else setPanControl(null); |
| 207 | + |
| 208 | + // Mute? |
| 209 | + BooleanControl muteControl1 = sourceDataLine.isControlSupported(BooleanControl.Type.MUTE) |
| 210 | + ? (BooleanControl) sourceDataLine.getControl(BooleanControl.Type.MUTE) |
| 211 | + : null; |
| 212 | + setMuteControl(muteControl1); |
| 213 | + |
| 214 | + // Speakers Balance? |
| 215 | + FloatControl balanceControl = sourceDataLine.isControlSupported(FloatControl.Type.BALANCE) |
| 216 | + ? (FloatControl) sourceDataLine.getControl(FloatControl.Type.BALANCE) |
| 217 | + : null; |
| 218 | + setBalanceControl(balanceControl); |
| 219 | + } |
| 220 | + } |
| 221 | + logger.info("Exited OpenLine()!:\n"); |
| 222 | + } |
| 223 | + |
| 224 | +} |
0 commit comments