Issues when attempting to resample 32Khz sample to 22.05khz #75
-
I am trying to use the Resample class to resample audio data in a double array to 22.05Khz, the resampling code seems to work on 32khz files that are under two minutes long but seemingly fails on all other 32khz files but seems to work fine with 44.1kHz, 48kHz and 84kHz files of any size, here is the stack trace: The code from my program: if (originalFormat.getSampleRate() != desiredSampleRate) {
Resample resampler = new Resample(desiredSampleRate.intValue(), (int) originalFormat.getSampleRate(), "constant");
double[] preResamplingArray = convertFloatArrayToDouble(trimmedValues);
ArrayList<double[]> splitResampledArrays = new ArrayList<>();
int outerArraySize = (int) Math.ceil((double) preResamplingArray.length / (double) MAX_ARRAY_RESAMPLE_SIZE);
for (int x = 0; x < outerArraySize; x++) {
int endOfRange;
if (x == (outerArraySize - 1)) {
endOfRange = preResamplingArray.length;
} else {
endOfRange = ((x + 1) * MAX_ARRAY_RESAMPLE_SIZE);
}
double[] splitArray = Arrays.copyOfRange(preResamplingArray, x * MAX_ARRAY_RESAMPLE_SIZE, endOfRange);
double[] resampledDoubleArray = resampler.resampleSignal(splitArray); //error occurs here
splitResampledArrays.add(resampledDoubleArray);
}
double[] recombinedDoubleArray = recombineDoubleArray(splitResampledArrays);
resampledValues = convertDoubleArrayToFloat(recombinedDoubleArray);
} else {
resampledValues = trimmedValues;
} for reference: Relevant methods public static double[] convertFloatArrayToDouble (float[] floatArray) {
double[] doubleArray = new double[floatArray.length];
int index = 0;
for(float floatValue : floatArray) {
doubleArray[index] = (double) floatValue;
index++;
}
return doubleArray;
} The error occurs on We are specifically using this library because of the anti-aliasing unlike the default java resampling when using AudioInputStream We know about the decimate function but since we are trying to convert to 22.05khz we do not think it will work for our purposes, additionally we sometime would need to up sample I'm not really familiar with the mathematics of resampling, so I'm not really sure what could be going wrong |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 12 replies
-
|
Beta Was this translation helpful? Give feedback.
-
Hi @Ewan-Stacey-BTO-IS , I have used the file that you had shared and Polyphase Resampling is working as expected. Sharing the properties here:
Which if you calculate: (22050/32000) * 128000 is 88200 Also attaching the data (input and output) as a ZIP file here. Finally, the sample code I used is here: package org.example;
import com.github.psambit9791.jdsp.io.WAV;
import com.github.psambit9791.jdsp.misc.UtilMethods;
import com.github.psambit9791.jdsp.signal.Resample;
import com.github.psambit9791.wavfile.WavFileException;
import org.apache.commons.math3.util.ArithmeticUtils;
import java.io.IOException;
import java.util.Hashtable;
public class Main {
public static void printProps(Hashtable h1, double[] signal, String signalType) {
System.out.println(signalType + " Properties: ");
System.out.println("Sample Rate: " + h1.get("SampleRate"));
System.out.println("Channels: " + h1.get("Channels"));
System.out.println("Number of Points: " + signal.length);
System.out.println("\n\n");
}
public static void main(String[] args) throws IOException, WavFileException {
// Reading the Source Audio File and getting the properties
WAV objReadSource = new WAV();
objReadSource.readWAV("data/audio_32000.wav");
double[][] signal = objReadSource.getData("int");
double[] signalFlat = UtilMethods.flattenMatrix(signal);
Hashtable<String, Long> propsOut = objReadSource.getProperties();
printProps(propsOut, signalFlat, "Source Signal");
// This is where the key operation happens
long desiredSampleRate = 22050;
long sourceSampleRate = propsOut.get("SampleRate");
long maxFrequency = ArithmeticUtils.lcm(sourceSampleRate, desiredSampleRate);
int upScaler = (int)(maxFrequency/sourceSampleRate);
int downScaler = (int)(maxFrequency/desiredSampleRate);
Resample pr = new Resample(upScaler, downScaler, "constant");
double[] newSignalFlat = pr.resampleSignal(signalFlat);
double[][] newSignal = new double[newSignalFlat.length][1];
for (int i=0; i<newSignalFlat.length; i++) {
newSignal[i][0] = newSignalFlat[i];
}
// Writing the resampled signal into a new WAV file
WAV objWrite = new WAV();
String outputFilename = "data/audio_22050.wav";
objWrite.putData(newSignal, (long)desiredSampleRate, "int", outputFilename);
// Reading the new WAV file and getting its properties for comparison
WAV objReadDesired = new WAV();
objReadDesired.readWAV("data/audio_22050.wav");
double[][] signalDesired = objReadDesired.getData("int");
double[] signalDesiredFlat = UtilMethods.flattenMatrix(signalDesired);
Hashtable<String, Long> propsOutDesired = objReadDesired.getProperties();
printProps(propsOutDesired, signalDesiredFlat, "Desired Signal");
}
} I have also run the same code on a WAV file with a duration of 02:48 (which is where the key issue had been encountered according to your first post) and that has worked fine as well. Sharing the input and output files for your reference as a zip attachment. Can you please check this out and let me know if you are still encountering the issue? |
Beta Was this translation helpful? Give feedback.
Hi @Ewan-Stacey-BTO-IS ,
I have used the file that you had shared and Polyphase Resampling is working as expected.
Sharing the properties here:
Which if you calculate: (22050/32000) * 128000 is 88200
Also attaching the data (input and output) as a ZIP file here.
data.zip
Finally, the sample code I used is here: