Ошибка: неподдерживаемый аудиоформат при конвертации .wav в double [] - PullRequest
0 голосов
/ 15 сентября 2018

Я пытаюсь преобразовать файл с расширением .wav в массив double, но получаю сообщение об ошибке:

09-15 05: 09: 47.222 22358-22358/com.R1100.bluetooth D / R1100Err: неподдерживаемый аудиоформат: '/storage/emulated/0/HeartSounds/a0002.wav'

Файл действительно .wav, но я понятия не имеюпочему это происходит.

Вот метод, который я использовал:

public static double[] read(String filename) {
    byte[] data = readByte(filename);
    int n = data.length;
    double[] d = new double[n/2];
    for (int i = 0; i < n/2; i++) {
        d[i] = ((short) (((data[2*i+1] & 0xFF) << 8) + (data[2*i] & 0xFF))) / ((double) MAX_16_BIT);
    }
    return d;
}

// return data as a byte array
private static byte[] readByte(String filename) {
    byte[] data = null;
    AudioInputStream ais = null;
    try {

        // try to read from file
        File file = new File(filename);
        if (file.exists()) {
            ais = AudioSystem.getAudioInputStream(file);
            int bytesToRead = ais.available();
            data = new byte[bytesToRead];
            int bytesRead = ais.read(data);
            if (bytesToRead != bytesRead)
                throw new IllegalStateException("read only " + bytesRead + " of " + bytesToRead + " bytes");
        }

        // try to read from URL
        else {
            URL url = Wav.class.getResource(filename);
            ais = AudioSystem.getAudioInputStream(url);
            int bytesToRead = ais.available();
            data = new byte[bytesToRead];
            int bytesRead = ais.read(data);
            if (bytesToRead != bytesRead)
                throw new IllegalStateException("read only " + bytesRead + " of " + bytesToRead + " bytes");
        }
    }
    catch (IOException e) {
        throw new IllegalArgumentException("could not read '" + filename + "'", e);
    }

    catch (UnsupportedAudioFileException e) {
        throw new IllegalArgumentException("unsupported audio format: '" + filename + "'", e);
    }

    return data;
}

Спасибо.

...