From: David ‘Bombe’ Roden Date: Sun, 17 Mar 2013 16:24:12 +0000 (+0100) Subject: Add resampling filter that uses sox for the heavy lifting. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=6d43ae93cd0174757b7bad996572fb469eb3e154;p=sonitus.git Add resampling filter that uses sox for the heavy lifting. --- 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(); + } + +}