Pull all interfaces into a single interface: Filter.
[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 import java.util.concurrent.atomic.AtomicReference;
23
24 import net.pterodactylus.sonitus.data.AbstractFilter;
25 import net.pterodactylus.sonitus.data.Filter;
26 import net.pterodactylus.sonitus.data.Metadata;
27
28 /**
29  * {@link Filter} implementation that uses the number of bytes that have been
30  * {@link #process(byte[]) processed} together with the {@link Metadata} to
31  * calculate how long a source is already playing.
32  *
33  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34  */
35 public class TimeCounterFilter extends AbstractFilter implements Filter {
36
37         /** The byte counter. */
38         private final AtomicLong counter = new AtomicLong();
39
40         /** The parent’s metdata. */
41         private final AtomicReference<Metadata> parentMetadata = new AtomicReference<Metadata>();
42
43         /** Whether to reset the counter on a metadata update. */
44         private final boolean resetOnMetadataUpdate;
45
46         /** The last displayed timestamp. */
47         private final AtomicLong lastTimestamp = new AtomicLong(-1);
48
49         /**
50          * Creates a new time counter filter that automatically resets the counter when
51          * the metadata is {@link #metadataUpdated(Metadata) updated}.
52          *
53          * @param name
54          *              The name of the filter
55          */
56         public TimeCounterFilter(String name) {
57                 this(name, true);
58         }
59
60         /**
61          * Creates a new time counter filter.
62          *
63          * @param name
64          *              The name of the filter
65          * @param resetOnMetadataUpdate
66          *              {@code true} if the counter should automatically be reset if the metadata
67          *              is updated, {@code false} otherwise
68          */
69         public TimeCounterFilter(String name, boolean resetOnMetadataUpdate) {
70                 super(name);
71                 this.resetOnMetadataUpdate = resetOnMetadataUpdate;
72         }
73
74         //
75         // ACTIONS
76         //
77
78         /**
79          * Returns the number of milliseconds worth of data that has been passed into
80          * {@link #process(byte[])}. If no metadata has yet been set, {@code 0} is
81          * returned.
82          *
83          * @return The number of milliseconds the current source is already playing
84          */
85         public long getMillis() {
86                 Metadata metadata = super.metadata();
87                 if (metadata == null) {
88                         return 0;
89                 }
90                 return 1000 * counter.get() / (metadata.channels() * metadata.frequency() * 2);
91         }
92
93         /** Resets the counter to 0. */
94         public void reset() {
95                 counter.set(0);
96         }
97
98         //
99         // FILTER METHODS
100         //
101
102         @Override
103         public void metadataUpdated(Metadata metadata) {
104                 parentMetadata.set(metadata);
105                 if (resetOnMetadataUpdate) {
106                         reset();
107                 }
108                 updateTimestamp(true);
109         }
110
111         @Override
112         public void process(byte[] buffer) throws IOException {
113                 super.process(buffer);
114                 counter.getAndAdd(buffer.length);
115                 updateTimestamp(false);
116         }
117
118         //
119         // PRIVATE METHODS
120         //
121
122         /** Updates the timestamp in the metadata. */
123         private void updateTimestamp(boolean now) {
124                 long timestamp = getMillis() / 1000;
125                 if (now || (lastTimestamp.get() != timestamp)) {
126                         super.metadataUpdated(parentMetadata.get().comment(String.format("%02d:%02d", timestamp / 60, timestamp % 60)));
127                         lastTimestamp.set(timestamp);
128                 }
129         }
130
131 }