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