0

I'm trying to read a .wav file, shift the pitch, and write it to a new file on android. I'm using TarsosDSP to do this, but kept getting weird artifacts in the new file. I tried on a single sine wave and this is how the first few samples of the files compare: image comparison

Orange being the original file, blue generated by tarsos. As far as I can tell, I have no processing differences compared to the pitch shift example on github. this is my code:

    File directory = getDir("audio", Context.MODE_PRIVATE);

    String source = directory.toString() + "/" + "in.wav";
    String target = directory.toString() + "/" + "out.wav";
    RandomAccessFile outputFile = null;
    try {
        outputFile = new RandomAccessFile(target, "rw");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    int bitsPerSample = 16;
    int channels = 1;
    TarsosDSPAudioFormat format = new TarsosDSPAudioFormat(SAMPLE_RATE, bitsPerSample, channels, true, false);

    double cents = 0;
    double factor = centToFactor(cents);
    RateTransposer rateTransposer = new RateTransposer(factor);
    WaveformSimilarityBasedOverlapAdd wsola = new WaveformSimilarityBasedOverlapAdd(WaveformSimilarityBasedOverlapAdd.Parameters.musicDefaults(factor, SAMPLE_RATE));

    WriterProcessor writer = new WriterProcessor(format, outputFile);

    try {
        InputStream inputStream = new FileInputStream(source);
        UniversalAudioInputStream uStream = new UniversalAudioInputStream(inputStream, format);
        print(wsola.getInputBufferSize());
        print(wsola.getOverlap());

        AudioDispatcher audioDispatcher = new AudioDispatcher(uStream, wsola.getInputBufferSize(), wsola.getOverlap());
        wsola.setDispatcher(audioDispatcher);
        audioDispatcher.addAudioProcessor(wsola);
        audioDispatcher.addAudioProcessor(rateTransposer);
        audioDispatcher.addAudioProcessor(writer);
        audioDispatcher.run();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

and:

public static double centToFactor(double cents) {
    return 1 / Math.pow(Math.E, cents * Math.log(2) / 1200 / Math.log(Math.E));
}

I tried making actual change to the sound (cents != 0) but the results didn't get better. Do I have to hope the artifacts always become unnoticable in real recordings, or is there a way to fix this? I'm very new to audio processing.

4

0 回答 0