0

I am trying to calculate the duration of .wav files using java sound api . Java sound api supports .wav files with PCM S16LE codec but it doesn't seem to support .wav file with IMA WAV ADPCM codec. Is there any library that can help me get the duration of all kinds of .wav files with any kind of codec or is there any way that I can calculate the duration of the .wav files without using any library?

So far i have tried so much :

private Long getDuration(File file)
{
    AudioFileFormat fileFormat = null;

    long duration = 0;
    try
    {
        fileFormat = AudioSystem.getAudioFileFormat(file);

        if(fileFormat.getType().toString().equalsIgnoreCase(StringConstants.MP3)) {
            Map<?, ?> properties = fileFormat.properties();
            Long microseconds = (Long) properties.get(StringConstants.DURATION);

            //converting to milliseconds
            duration = microseconds / 1000;
        }
        else if(fileFormat.getType().toString().equalsIgnoreCase(StringConstants.AUDIO_WAVE)) {
            long frames = fileFormat.getFrameLength();
            double durationInSeconds = (frames + 0.0) / fileFormat.getFormat().getFrameRate();

            duration = Math.round(durationInSeconds * 1000);
        }
    }

    catch (UnsupportedAudioFileException e)
    {
        e.printStackTrace();
    }

    catch (IOException e)
    {
        e.printStackTrace();
    }

    return duration;
}

is there any way i can use the same logic that i have used here to calculate duration for .wav files of IMA WAV ADPCM AUDIO ? if yes, how do i get the frame length and frame rate of the file?

Divyaa
  • 45
  • 1
  • 6
  • this seems to be an obscure Microsoft codec and as with all the Microssoft such stuff it goes into extinction - I dont know may be you can convert these files before processing - or write your own codec try to decode – gpasch Apr 11 '19 at 17:09
  • Is there any java library using which I can convert .wav file to .mp3? Or Is there any java library to convert any .wav file to standard PCM S16LE ? – Divyaa Apr 12 '19 at 06:23
  • Yes I think ffmpeg can convert – gpasch Apr 12 '19 at 16:29

0 Answers0