算术错误:/ 0

时间:2014-07-19 16:22:54

标签: java processing bufferedinputstream dividebyzeroexception

我看不出我做错了什么我是新来的处理并且一直给我这个错误。它说这是错误代码:

int sampleCount = (int) ((byteCount - 36) / (bitDepth * channels));

这是完整的课程:

    public AudioPlayer (String filename, float sampleRate, PApplet processing) {
//super(filename);
this(sampleRate);
try {
    // how long is the file in bytes?
    //long byteCount = getAssets().openFd(filename).getLength();
    File f = new File(processing.dataPath(filename));
    long byteCount = f.length();
    //System.out.println("bytes in "+filename+" "+byteCount);

    // check the format of the audio file first!
    // only accept mono 16 bit wavs
    //InputStream is = getAssets().open(filename); 
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));

    // chop!!

    int bitDepth;
    int channels;
    boolean isPCM;
    // allows us to read up to 4 bytes at a time 
    byte[] byteBuff = new byte[4];

    // skip 20 bytes to get file format
    // (1 byte)
    bis.skip(20);
    bis.read(byteBuff, 0, 2); // read 2 so we are at 22 now
    isPCM = ((short)byteBuff[0]) == 1 ? true:false; 
    //System.out.println("File isPCM "+isPCM);

    // skip 22 bytes to get # channels
    // (1 byte)
    bis.read(byteBuff, 0, 2);// read 2 so we are at 24 now
    channels = (short)byteBuff[0];
    //System.out.println("#channels "+channels+" "+byteBuff[0]);
    // skip 24 bytes to get sampleRate
    // (32 bit int)
    bis.read(byteBuff, 0, 4); // read 4 so now we are at 28
    sampleRate = bytesToInt(byteBuff, 4);
    //System.out.println("Sample rate "+sampleRate);
    // skip 34 bytes to get bits per sample
    // (1 byte)
    bis.skip(6); // we were at 28...
    bis.read(byteBuff, 0, 2);// read 2 so we are at 36 now
    bitDepth = (short)byteBuff[0];
    //System.out.println("bit depth "+bitDepth);
    // convert to word count...
    bitDepth /= 8;
    // now start processing the raw data
    // data starts at byte 36
    int sampleCount = (int) ((byteCount - 36) / (bitDepth * channels));
    audioData = new short[sampleCount];
    int skip = (channels -1) * bitDepth;
    int sample = 0;
    // skip a few sample as it sounds like shit
    bis.skip(bitDepth * 4);
    while (bis.available () >= (bitDepth+skip)) {
    bis.read(byteBuff, 0, bitDepth);// read 2 so we are at 36 now
    //int val = bytesToInt(byteBuff, bitDepth);
    // resample to 16 bit by casting to a short
    audioData[sample] = (short) bytesToInt(byteBuff, bitDepth);
    bis.skip(skip);
    sample ++;
    }

    float secs = (float)sample / (float)sampleRate;
    //System.out.println("Read "+sample+" samples expected "+sampleCount+" time "+secs+" secs ");      
    bis.close();


    // unchop
    readHead = 0;
    startPos = 0;
    // default to 1 sample shift per tick
    dReadHead = 1;
    isPlaying = false;
    isLooping = true;
    masterVolume = 1;
} 
catch (FileNotFoundException e) {

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

这是我从课程下载的标准课程,如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

我的猜测是(bitDepth * channels)= 0.你不能除以零。

答案 1 :(得分:0)

首先,您需要找出问题所在的文件。取消注释该行:

//System.out.println("bytes in "+filename+" "+byteCount);

..找出导致问题的文件。 也取消注释该行:

//System.out.println("File isPCM "+isPCM);

如果isPCM对于代码中断的文件是false,请以audacity打开wav文件并再次导出。在大胆时,当你保存为类型" WAV(微软)签名16位PCM"时,它将明确包括PCM。 这解决了我的问题。