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