0

I am writing a program for school that reduces noise in a sound file. So far i have written this code which I think takes n number of samples before a set one and n after and then averages the two. My problem is that everytime my second for loop runs i get a sampleoutofboundexception. I am guessing this means that it cant find the sample that i am asking it to look for, but i dont understand why.

for (int s = 0; s<= aSound.getNumSamples(); s++){    

  for ( int i=0; i<=level ; i++ ) {
    nSamp = aSound.getSample(i);
    sSize = nSamp.getValue();
    total=total + sSize;
  }

  for (int j = 0; j >= -level; j--){
    sSize2 = aSound.getSample(j).getValue();
    total1 = total1 + sSize2;
  }
  avg = (total1 + total) / level*2;

  for (int i = 0; i <= level*2+1; i++){
    result.getSample(i).setValue(avg);
  }
 }
 return aSound;
  }

I get the error every single time this line is run and I can't understand why. any help? thank you

sSize2 = aSound.getSample(j).getValue();
1
  • 1
    I think aSound is a collection, and an array or collection do not have negative index Commented Nov 13, 2010 at 3:33

2 Answers 2

2

What is the value of level? I'm assuming it's positive, in which case

for (int j = 0; j >= -level; j--)

loops over negative values of j, and negative indices are generally invalid. That's why you're getting an index out of bounds exception.

If this doesn't solve your problem, you should post some more details, such as what type of object aSound is.

Sign up to request clarification or add additional context in comments.

2 Comments

level = 5; so my logic that that -level = -5; so 0 > -5 and the loop should run correctly? aSound is an object that holds a wav file with 55125 samples
@Cheesegraterr: It should run ok for j = 0, but when you do j--, your j values run through -1, -2, -3, -4, -5 and that's where the trouble begins. You don't have a -1th sample, do you?
0

aSound is an object that holds a wav file with 55125 samples

As aSound is collection. it don't have negative index as written in my comment

I think aSound is a collection, and an array or collection do not have negative index

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.