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