Add name to all controlled components.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / filter / TimeCounterFilter.java
1 /*
2  * Sonitus - TimeCounterFilter.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sonitus.data.filter;
19
20 import java.io.IOException;
21 import java.util.concurrent.atomic.AtomicLong;
22
23 import net.pterodactylus.sonitus.data.Filter;
24 import net.pterodactylus.sonitus.data.Metadata;
25
26 /**
27  * {@link Filter} implementation that uses the number of bytes that have been
28  * {@link #process(byte[]) processed} together with the {@link Metadata} to
29  * calculate how long a source is already playing.
30  *
31  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32  */
33 public class TimeCounterFilter extends DummyFilter {
34
35         /** The byte counter. */
36         private final AtomicLong counter = new AtomicLong();
37
38         /** Whether to reset the counter on a metadata update. */
39         private final boolean resetOnMetadataUpdate;
40
41         /**
42          * Creates a new time counter filter that automatically resets the counter when
43          * the metadata is {@link #metadataUpdated(Metadata) updated}.
44          *
45          * @param name
46          *              The name of the filter
47          */
48         public TimeCounterFilter(String name) {
49                 this(name, true);
50         }
51
52         /**
53          * Creates a new time counter filter.
54          *
55          * @param name
56          *              The name of the filter
57          * @param resetOnMetadataUpdate
58          *              {@code true} if the counter should automatically be reset if the metadata
59          *              is updated, {@code false} otherwise
60          */
61         public TimeCounterFilter(String name, boolean resetOnMetadataUpdate) {
62                 super(name);
63                 this.resetOnMetadataUpdate = resetOnMetadataUpdate;
64         }
65
66         //
67         // ACTIONS
68         //
69
70         /**
71          * Returns the number of milliseconds worth of data that has been passed into
72          * {@link #process(byte[])}. If no metadata has yet been set, {@code 0} is
73          * returned.
74          *
75          * @return The number of milliseconds the current source is already playing
76          */
77         public long getMillis() {
78                 Metadata metadata = super.metadata();
79                 if (metadata == null) {
80                         return 0;
81                 }
82                 return 1000 * counter.get() / (metadata.channels() * metadata.frequency() * 2);
83         }
84
85         /** Resets the counter to 0. */
86         public void reset() {
87                 counter.set(0);
88         }
89
90         //
91         // DUMMYFILTER METHODS
92         //
93
94         @Override
95         public void metadataUpdated(Metadata metadata) {
96                 super.metadataUpdated(metadata);
97                 if (resetOnMetadataUpdate) {
98                         reset();
99                 }
100         }
101
102         @Override
103         public void process(byte[] buffer) throws IOException {
104                 super.process(buffer);
105                 counter.getAndAdd(buffer.length);
106         }
107
108 }