fb644d3de140e879a7843563983020f87ffbef49
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / filter / FlacDecoder.java
1 /*
2  * Sonitus - FlacDecoder.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 net.pterodactylus.sonitus.data.Metadata;
21
22 import com.google.common.collect.ImmutableList;
23 import com.google.common.eventbus.EventBus;
24
25 /**
26  * Decoder {@link net.pterodactylus.sonitus.data.Filter} for FLAC files.
27  *
28  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
29  */
30 public class FlacDecoder extends ExternalFilter {
31
32         /** The location of the binary. */
33         private final String binary;
34
35         /** Whether to swap the bytes. */
36         private boolean swapBytes;
37
38         /**
39          * Creates a new FLAC decoder.
40          *
41          * @param eventBus
42          *              The event bus
43          * @param binary
44          *              The location of the binary
45          */
46         public FlacDecoder(EventBus eventBus, String binary) {
47                 super(eventBus, "FLAC Decoder");
48                 this.binary = binary;
49         }
50
51         //
52         // EXTERNALFILTER METHODS
53         //
54
55         @Override
56         protected String binary(Metadata metadata) {
57                 return binary;
58         }
59
60         @Override
61         protected Iterable<String> parameters(Metadata metadata) {
62                 ImmutableList.Builder<String> parameters = ImmutableList.builder();
63
64                 parameters.add("--decode");
65                 parameters.add("--stdout");
66                 parameters.add("--silent");
67                 parameters.add("--output-name=-");
68                 parameters.add("--force-raw-format");
69                 if (swapBytes) {
70                         parameters.add("--endian=little");
71                 } else {
72                         parameters.add("--endian=big");
73                 }
74                 parameters.add(String.format("--channels=%d", metadata.channels()));
75                 parameters.add("--bps=16");
76                 parameters.add(String.format("--sample-rate=%d", metadata.frequency()));
77                 parameters.add("--sign=signed");
78
79                 return parameters.build();
80         }
81
82 }