1864b1c6856917eeb923c527d440cde0cb7c6d08
[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 import com.google.common.eventbus.EventBus;
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 DummyFilter {
36
37         /** The byte counter. */
38         private final AtomicLong counter = new AtomicLong();
39
40         /** Whether to reset the counter on a metadata update. */
41         private final boolean resetOnMetadataUpdate;
42
43         /**
44          * Creates a new time counter filter that automatically resets the counter when
45          * the metadata is {@link #metadataUpdated(Metadata) updated}.
46          *
47          * @param eventBus
48          *              The event bus
49          * @param name
50          *              The name of the filter
51          */
52         public TimeCounterFilter(EventBus eventBus, String name) {
53                 this(eventBus, name, true);
54         }
55
56         /**
57          * Creates a new time counter filter.
58          *
59          * @param eventBus
60          *              The event bus
61          * @param name
62          *              The name of the filter
63          * @param resetOnMetadataUpdate
64          *              {@code true} if the counter should automatically be reset if the metadata
65          *              is updated, {@code false} otherwise
66          */
67         public TimeCounterFilter(EventBus eventBus, String name, boolean resetOnMetadataUpdate) {
68                 super(eventBus, name);
69                 this.resetOnMetadataUpdate = resetOnMetadataUpdate;
70         }
71
72         //
73         // ACTIONS
74         //
75
76         /**
77          * Returns the number of milliseconds worth of data that has been passed into
78          * {@link #process(byte[])}. If no metadata has yet been set, {@code 0} is
79          * returned.
80          *
81          * @return The number of milliseconds the current source is already playing
82          */
83         public long getMillis() {
84                 Metadata metadata = super.metadata();
85                 if (metadata == null) {
86                         return 0;
87                 }
88                 return 1000 * counter.get() / (metadata.channels() * metadata.frequency() * 2);
89         }
90
91         /** Resets the counter to 0. */
92         public void reset() {
93                 counter.set(0);
94         }
95
96         //
97         // DUMMYFILTER METHODS
98         //
99
100         @Override
101         public void metadataUpdated(Metadata metadata) {
102                 super.metadataUpdated(metadata);
103                 if (resetOnMetadataUpdate) {
104                         reset();
105                 }
106         }
107
108         @Override
109         public void process(byte[] buffer) throws IOException {
110                 super.process(buffer);
111                 counter.getAndAdd(buffer.length);
112         }
113
114 }