Search code examples
javapythonaudiojython

How to decrease of a sound in Jython?


I want to decrease the sample value in a sound linearly, starting from the very first sample [0], which would have the max value (32767), and the last one [sound length end] having the smallest possible value (-32767). To do this I'd assume you could use the format y=mx+b. Since m = y2-y1/x2-x1 I have:

def decreasing(sound):
  length = getLength(sound)
  max = 32767.0
  min= -32767.0
  for sample in getSamples(sound):
    setSampleValue(sound, int((((min-max) / (length)) * (index)) + max))

The problem I'm having is with the x in y=mx+b, x is the index I think as it is the value on the x plain you get to find the sample value (y). However when I run the code I get:

The error was:index ... Name not found globally

Since I'm not sure how to define it. So basically what I'm asking is how do I make this number:

x in my equation.


Solution

  • Assuming that getSamples() returns a list, you probably want something like this:

    for index, sample in enumerate(getSamples(sound)):
        setSampleValue(sample, int((((min-max)/length) * index) + max))