0

I've tried the following approaches:

mp3-to-wav-conversion-in-java

convert-mp3-to-pcm-in-java

Current Implementation:

...
var mp3URI = Uri.fromFile(file)
var mp3InputStream = this.contentResolver.openInputStream(mp3URI)
var byteArray = mp3InputStream!!.readBytes()

var pcmBytes = getAudioDataBytes(byteArray, AudioFormat(SAMPLE_RATE_HZ.toFloat(),16,1,true,false))
...

@Throws(UnsupportedAudioFileException::class, IllegalArgumentException::class, Exception::class)
fun getAudioDataBytes(sourceBytes: ByteArray?, audioFormat: AudioFormat?): ByteArray? {
    require(!(sourceBytes == null || sourceBytes.size == 0 || audioFormat == null)) { "Illegal Argument passed to this method" }
    var bais: ByteArrayInputStream? = null
    var baos: ByteArrayOutputStream? = null
    var sourceAIS: AudioInputStream? = null
    var convert1AIS: AudioInputStream? = null
    var convert2AIS: AudioInputStream? = null
    return try {
        bais = ByteArrayInputStream(sourceBytes)
        sourceAIS = AudioSystem.getAudioInputStream(bais)
        val sourceFormat: AudioFormat = sourceAIS.getFormat()
        val convertFormat = AudioFormat(
            AudioFormat.Encoding.PCM_SIGNED,
            sourceFormat.sampleRate,
            16,
            sourceFormat.channels,
            sourceFormat.channels * 2,
            sourceFormat.sampleRate,
            false
        )
        convert1AIS = AudioSystem.getAudioInputStream(convertFormat, sourceAIS)
        convert2AIS = AudioSystem.getAudioInputStream(audioFormat, convert1AIS)
        baos = ByteArrayOutputStream()
        val buffer = ByteArray(8192)
        while (true) {
            val readCount: Int = convert2AIS.read(buffer, 0, buffer.size)
            if (readCount == -1) {
                break
            }
            baos.write(buffer, 0, readCount)
        }
        baos.toByteArray()
    } catch (uafe: UnsupportedAudioFileException) {
        //uafe.printStackTrace();
        throw uafe
    } catch (ioe: IOException) {
        //ioe.printStackTrace();
        throw ioe
    } catch (iae: IllegalArgumentException) {
        //iae.printStackTrace();
        throw iae
    } catch (e: Exception) {
        //e.printStackTrace();
        throw e
    } finally {
        if (baos != null) {
            try {
                baos.close()
            } catch (e: Exception) {
            }
        }
        if (convert2AIS != null) {
            try {
                convert2AIS.close()
            } catch (e: Exception) {
            }
        }
        if (convert1AIS != null) {
            try {
                convert1AIS.close()
            } catch (e: Exception) {
            }
        }
        if (sourceAIS != null) {
            try {
                sourceAIS.close()
            } catch (e: Exception) {
            }
        }
        if (bais != null) {
            try {
                bais.close()
            } catch (e: Exception) {
            }
        }
    }
}

the approaches I've tried are giving me javax.sound.sampled.UnsupportedAudioFileException at the line

sourceAIS = AudioSystem.getAudioInputStream(bais)

I take it some of the .jar files need to be added to the run-time class-path. I figured putting the jar files in the libs folder and doing the gradle implementation was all that would be needed but apparently not. Where are the .jar files supposed to go so they are in the run-time class-path? Or if you have an easier way to convert an mp3 into pcm bytes I'd love to hear it.

enter image description here

Gradle dependancies:

dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation files('libs\\mp3plugin.jar')
implementation files('libs\\net.sourceforge.lame-3.98.4.jar')
implementation files('libs\\tritonus-share-0.3.7-2.jar')
implementation files('libs\\jl-1.0.1.jar')
implementation files('libs\\tritonus_mp3-0.3.6.jar')
implementation files('libs\\mp3spi-1.9.5-2.jar')
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

}

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Not_Bluffing
  • 78
  • 2
  • 7

1 Answers1

0

Turns out the jLayer libraby has a converter that some people find too slow but works fine for my purposes

var converter = Converter()
converter.convert(mp3File, wavFile)

From there just need to load the wav file, remove the wav header and fix the edieness

Not_Bluffing
  • 78
  • 2
  • 7