0

嘿,所以我正在使用 JES 为我的 Python 类做一些编码作业。我们的任务是获取声音,在背景中添加一些白噪声并添加回声。还有一些更精确的,但我相信我对此很好。我们正在制作四个不同的函数:主函数、基于用户定义的时间长度和回声量的回声方程、白噪声生成函数和合并噪声的函数。

这是我到目前为止所拥有的,还没有开始合并或主要。

#put the following line at the top of your file. This will let
#you access the random module functions
import random

#White noise Generation functiton, requires a sound to match sound length
def whiteNoiseGenerator(baseSound) :
 noise = makeEmptySound(getLength(baseSound))
 index = 0  
 for index in range(0, getLength(baseSound)) :
  sample = random.randint(-500, 500)
  setSampleValueAt(noise, index, sample)
 return noise


def multipleEchoesGenerator(sound, delay, number) :
  endSound = getLength(sound)
  newEndSound = endSound +(delay * number)

  len = 1 + int(newEndSound/getSamplingRate(sound))
  newSound = makeEmptySound(len)

  echoAmplitude = 1.0
  for echoCount in range (1, number) :
    echoAmplitude = echoAmplitude * 0.60
    for posns1 in range (0, endSound):
    posns2 = posns1 + (delay * echoCount)
    values1 = getSampleValueAt(sound, posns1) * echoAmplitude
    values2 = getSampleValueAt(newSound, posns2)
    setSampleValueAt (newSound, posns2, values1 + values2)
return newSound

每当我尝试加载它时都会收到此错误。

错误是:

Inappropriate argument value (of correct type).
An error occurred attempting to pass an argument to a function.
Please check line 38 of C:\Users\insanity180\Desktop\Work\Winter Sophomore\CS 140\homework3\homework_3.py

那行代码是:

setSampleValueAt (newSound, posns2, values1 + values2)

有人知道这里可能会发生什么吗?任何帮助都会很棒,因为我希望给自己足够的时间来完成这项任务的编码。我之前遇到过类似的错误,通常是语法错误,但是我在这里没有看到任何此类错误。

声音是在我运行这个程序之前发出的,我将延迟和数字分别定义为值 1 和 3。

4

1 回答 1

1

检查参数setSampleValueAt; 您的样本值必须超出范围(应在-32768-范围内32767)。你需要为你的算法做某种输出钳位。

另一种可能性(根据进一步的输入,确实是错误)是您的回声将超出样本的范围 - 也就是说,如果您的样本长 5 秒,并且回声长 0.5 秒;或posns1 + delay超出样品长度;新声音的长度计算不正确。

于 2015-03-01T08:13:55.853 回答