Reputation: 3
I can't see what I'm doing wrong I'm new to processing and keeps giving me this error. It says this is the error code:
int sampleCount = (int) ((byteCount - 36) / (bitDepth * channels));
this is the full class it's from :
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();
}
}
This is from a standard class I downloaded from a class how do I fix this?
Upvotes: 0
Views: 161
Reputation: 31
First you need to find out which file is the problem. Uncomment the line:
//System.out.println("bytes in "+filename+" "+byteCount);
.. to find out which file is causing the problems. Also uncomment the line:
//System.out.println("File isPCM "+isPCM);
If isPCM is false
for the file for which the code breaks, open the wav file in audacity and export it again. In audacity it will include PCM explicitly when you save as type "WAV (Microsoft) signed 16 bit PCM".
That solved the problem for me.
Upvotes: 0