X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsonitus%2Fdata%2Ffilter%2FSoxResampleFilter.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsonitus%2Fdata%2Ffilter%2FSoxResampleFilter.java;h=69daa99603358d423a1ceb08b03dbb81c4f4fae2;hb=6d43ae93cd0174757b7bad996572fb469eb3e154;hp=0000000000000000000000000000000000000000;hpb=d9a909a0e39b2925aece35c2298cfc3c35d35051;p=sonitus.git
diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/SoxResampleFilter.java b/src/main/java/net/pterodactylus/sonitus/data/filter/SoxResampleFilter.java
new file mode 100644
index 0000000..69daa99
--- /dev/null
+++ b/src/main/java/net/pterodactylus/sonitus/data/filter/SoxResampleFilter.java
@@ -0,0 +1,104 @@
+/*
+ * Sonitus - ResampleFilter.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+package net.pterodactylus.sonitus.data.filter;
+
+import static com.google.common.base.Preconditions.*;
+
+import net.pterodactylus.sonitus.data.ConnectException;
+import net.pterodactylus.sonitus.data.Metadata;
+import net.pterodactylus.sonitus.data.Source;
+
+import com.google.common.collect.ImmutableList;
+
+/**
+ * {@link net.pterodactylus.sonitus.data.Filter} implementation that uses {@code
+ * sox} to resample a PCM-encoded source.
+ *
+ * @author David âBombeâ Roden
+ */
+public class SoxResampleFilter extends ExternalFilter {
+
+ /** The location of the binary. */
+ private final String binary;
+
+ /** The final sampling rate. */
+ private final int rate;
+
+ /**
+ * Creates a new resample filter.
+ *
+ * @param binary
+ * The location of the binary
+ * @param rate
+ * The new sampling rate
+ */
+ public SoxResampleFilter(String binary, int rate) {
+ this.binary = binary;
+ this.rate = rate;
+ }
+
+ //
+ // FILTER METHODS
+ //
+
+ @Override
+ public Metadata metadata() {
+ return super.metadata().frequency(rate);
+ }
+
+ @Override
+ public void connect(Source source) throws ConnectException {
+ checkNotNull(source, "source must not be null");
+ checkArgument(source.metadata().encoding().equalsIgnoreCase("PCM"), "source must be PCM-encoded");
+
+ super.connect(source);
+ }
+
+ //
+ // EXTERNALFILTER METHODS
+ //
+
+ @Override
+ protected String binary(Metadata metadata) {
+ return binary;
+ }
+
+ @Override
+ protected Iterable parameters(Metadata metadata) {
+ ImmutableList.Builder parameters = ImmutableList.builder();
+ /* the input file. */
+ parameters.add("--bits").add("16");
+ parameters.add("--channels").add(String.valueOf(metadata.channels()));
+ parameters.add("--encoding").add("signed-integer");
+ parameters.add("--rate").add(String.valueOf(metadata.frequency()));
+ parameters.add("--type").add("raw");
+ parameters.add("--endian").add("little");
+ parameters.add("-");
+ /* the output file. */
+ parameters.add("--bits").add("16");
+ parameters.add("--channels").add(String.valueOf(metadata.channels()));
+ parameters.add("--encoding").add("signed-integer");
+ parameters.add("--type").add("raw");
+ parameters.add("--endian").add("little");
+ parameters.add("-");
+ /* rate effect. */
+ parameters.add("rate").add("-h").add(String.valueOf(rate));
+ return parameters.build();
+ }
+
+}