mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-13 12:04:06 +09:00
get length of mp3 files
This commit is contained in:
@@ -17,6 +17,7 @@ import net.torvald.terrarum.audio.AudioMixer
|
|||||||
import net.torvald.terrarum.audio.TerrarumAudioMixerTrack.Companion.SAMPLING_RATE
|
import net.torvald.terrarum.audio.TerrarumAudioMixerTrack.Companion.SAMPLING_RATE
|
||||||
import net.torvald.unicode.EMDASH
|
import net.torvald.unicode.EMDASH
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.io.FileInputStream
|
||||||
import javax.sound.sampled.AudioSystem
|
import javax.sound.sampled.AudioSystem
|
||||||
|
|
||||||
data class MusicContainer(
|
data class MusicContainer(
|
||||||
@@ -75,25 +76,56 @@ data class MusicContainer(
|
|||||||
samplesTotal = when (gdxMusic) {
|
samplesTotal = when (gdxMusic) {
|
||||||
is Wav.Music -> getWavFileSampleCount(file)
|
is Wav.Music -> getWavFileSampleCount(file)
|
||||||
is Ogg.Music -> getOggFileSampleCount(file)
|
is Ogg.Music -> getOggFileSampleCount(file)
|
||||||
|
is Mp3.Music -> getMp3FileSampleCount(file)
|
||||||
else -> Long.MAX_VALUE
|
else -> Long.MAX_VALUE
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getWavFileSampleCount(file: File): Long {
|
private fun getWavFileSampleCount(file: File): Long {
|
||||||
val ais = AudioSystem.getAudioInputStream(file)
|
return try {
|
||||||
val r = ais.frameLength
|
val ais = AudioSystem.getAudioInputStream(file)
|
||||||
ais.close()
|
val r = ais.frameLength
|
||||||
return r
|
ais.close()
|
||||||
|
r
|
||||||
|
}
|
||||||
|
catch (e: Throwable) {
|
||||||
|
Long.MAX_VALUE
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getOggFileSampleCount(file: File): Long {
|
private fun getOggFileSampleCount(file: File): Long {
|
||||||
try {
|
return try {
|
||||||
val vorbisFile = VorbisFile(file.absolutePath)
|
val vorbisFile = VorbisFile(file.absolutePath)
|
||||||
return vorbisFile.pcm_total(0)
|
vorbisFile.pcm_total(0)
|
||||||
}
|
}
|
||||||
catch (e: Throwable) {
|
catch (e: Throwable) {
|
||||||
return Long.MAX_VALUE
|
Long.MAX_VALUE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getMp3FileSampleCount(file: File): Long {
|
||||||
|
return try {
|
||||||
|
val fis = FileInputStream(file)
|
||||||
|
val bs = Bitstream(fis)
|
||||||
|
|
||||||
|
var header = bs.readFrame()
|
||||||
|
val rate = header.frequency()
|
||||||
|
var totalSamples = 0L
|
||||||
|
|
||||||
|
while (header != null) {
|
||||||
|
totalSamples += (header.ms_per_frame() * rate / 1000).toLong()
|
||||||
|
bs.closeFrame()
|
||||||
|
header = bs.readFrame()
|
||||||
|
}
|
||||||
|
|
||||||
|
bs.close()
|
||||||
|
fis.close()
|
||||||
|
|
||||||
|
totalSamples
|
||||||
|
}
|
||||||
|
catch (_: Throwable) {
|
||||||
|
Long.MAX_VALUE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user