Opus audio File to Wav conversion

Viewed 1716

Good morning,

I'm trying to convert an opus audio file to wav file on Java.

I've been seeking different solutions with different libraries, and finally I've found a possible solution. My problem is output wav audio file has a lot of noise, like distortion, and I don't know how to solve this.

Here is my code:

public void OpusToWav() {

    JOpusFile inFile = new JOpusFile(input_File);
    AudioFormat sourceFormat = inFile.format;
    JOpusDecodable decoder; // com.glester.jopus.JOpusDecodable
    ByteBuffer sampleBuffer;
    WaveFileWriter wavWriter; // net.sourceforge.jaad.util.wav.WaveFileWriter

    byte [] buf;
    int bytesRead = 0;

    try {
        decoder = JOpusBufferFile.loadFromFile(input_File);
        sampleBuffer = decoder.getSampleBuffer();
        wavWriter = new WaveFileWriter(outputFile, 44100, sourceFormat.getChannels(), sourceFormat.getSampleSizeInBits());

        buf = new byte[sampleBuffer.capacity()];

        while(true) {
            bytesRead = decoder.read();
            if(bytesRead <= 0) break;
            sampleBuffer.get(buf);

            // Its needed to reduce sample volume in byte array??

            wavWriter.write(buf, 0, bytesRead);
        }

        wavWriter.close();
        decoder.close();
        inFile.close();
    } catch(URISyntaxException | IOException e) {
        System.out.println("Error decoding opus file --> " +  e.getMessage());
    }
}

I've tried to modify sample volume in output byte array, but problem was still there.

Any idea how to solve this?

P.D: I have tried with Concentus library as well, but no difference.

3 Answers

There’s no Java library to do this, but you can use the opus-tools and execute opusdec using Java ProcessBuilder. See my answer or this article to learn how to use the ProcessBuilder.

Related