2 * Sonitus - AbstractFilter.java - Copyright © 2013 David Roden
4 * This program 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 3 of the License, or
7 * (at your option) any later version.
9 * This program 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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sonitus.data.filter;
20 import java.io.EOFException;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.io.PipedInputStream;
25 import java.io.PipedOutputStream;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.List;
30 import net.pterodactylus.sonitus.data.AbstractControlledComponent;
31 import net.pterodactylus.sonitus.data.Controller;
32 import net.pterodactylus.sonitus.data.Filter;
33 import net.pterodactylus.sonitus.data.Metadata;
34 import net.pterodactylus.sonitus.data.event.MetadataUpdated;
36 import com.google.common.eventbus.EventBus;
37 import com.google.common.io.Closeables;
40 * Dummy {@link Filter} implementation that pipes its input to its output.
42 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public class DummyFilter extends AbstractControlledComponent implements Filter {
46 /** The name of this filter. */
47 private final String name;
50 private final EventBus eventBus;
52 /** The input stream from which to read. */
53 private InputStream inputStream;
55 /** The output stream to which to write. */
56 private OutputStream outputStream;
58 /** The current metadata. */
59 private Metadata metadata;
62 * Creates a new dummy filter with the given name.
67 * The name of the filter
69 public DummyFilter(EventBus eventBus, String name) {
70 this.eventBus = eventBus;
79 public String name() {
84 public List<Controller<?>> controllers() {
85 return Collections.emptyList();
93 public void open(Metadata metadata) throws IOException {
94 metadataUpdated(metadata);
95 inputStream = createInputStream();
96 outputStream = createOutputStream();
100 public void close() {
102 Closeables.close(outputStream, true);
103 Closeables.close(inputStream, true);
104 } catch (IOException e) {
110 public Metadata metadata() {
115 public void metadataUpdated(Metadata metadata) {
116 this.metadata = metadata;
117 fireMetadataUpdated(metadata);
118 eventBus.post(new MetadataUpdated(this, metadata));
122 public void process(byte[] buffer) throws IOException {
123 outputStream.write(buffer);
124 outputStream.flush();
128 public byte[] get(int bufferSize) throws IOException {
129 byte[] buffer = new byte[bufferSize];
130 int read = inputStream.read(buffer);
132 throw new EOFException();
134 return Arrays.copyOf(buffer, read);
142 * Creates the input stream from which {@link net.pterodactylus.sonitus.data.Pipeline}
143 * will read the audio data. If you override this, you have to override {@link
144 * #createOutputStream()}, too!
146 * @return The input stream to read from
147 * @throws IOException
148 * if an I/O error occurs
150 protected InputStream createInputStream() throws IOException {
151 return new PipedInputStream();
155 * Creates the output stream to which {@link net.pterodactylus.sonitus.data.Pipeline}
156 * will write the audio data. If you override this, you have to override {@link
157 * #createInputStream()}, too!
159 * @return The output stream to write to
160 * @throws IOException
161 * if an I/O error occurs
163 protected OutputStream createOutputStream() throws IOException {
164 return new PipedOutputStream((PipedInputStream) inputStream);