2

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.

CharlieHollow
  • 441
  • 1
  • 6
  • 12

3 Answers3

0

Try this: https://github.com/louisyonge/opus_android

Encode from opus to wav

OpusTool oTool = new OpusTool();
oTool.decode(fileName,fileNameOut, null);
oTool.encode(fileName, fileNameOut, null);
0

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.

Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
-1

I'm guessing it's because you are reading the audio data as bytes with bytesRead = decoder.read() and either 1) the output is getting clamped to 8 bits of depth (not sure why it would do that) or 2) the endianness or byte offset is getting mixed up somehow.

Concentus does have a method to decode opus packets into 16-bit short values which unambiguously represent the waveform. Only problem is that support for reading ogg packets from a .opus file was never really completed. However it can be done by modifying VorbisJava slightly to make it support opus decoding.

Logan S.
  • 195
  • 1
  • 5