所以我写了一个代码,创建了两个声音文件,提供了一些细节并对其进行了探索,但是我很难理解要创建第三个声音文件的部分,而第三个声音文件的长度与前两个声音文件中的较长者以及采样率有关两者(它们具有相同的sr)。该函数用于获取前两个声音文件的所有索引的样本值,将它们加在一起,然后将其复制到第三个声音文件中。我以为自己有了,但是当我浏览第三个文件时,它空白了。
def sound():
a = pickAFile()
b = pickAFile()
sound1 = makeSound(a)
sound2 = makeSound(b)
sr1 = getSamplingRate(sound1)
sr2 = getSamplingRate(sound2)
printNow(sr1)
printNow(sr2)
play(sound1)
play(sound2)
explore(sound1)
explore(sound2)
if sr1 == sr2:
printNow('Sampling Rate 1: %d \nSampling Rate 2: %d' % (sr1, sr2))
else:
printNow('Sampling Rates are not equal.')
c=getLength(sound1)
d=getLength(sound2)
printNow('sound1: %d, sound2: %d' % (c, d))
sound3 = Sound(getLength(sound2), int(sr1))
for index in range(0, getLength(sound1)):
value = getSampleValueAt(sound1, index)
for index in range(0, getLength(sound2)):
value2 = getSampleValueAt(sound2, index)
for index in range(0, getLength(sound3)):
setSampleValueAt(sound3, index, value+value2)
play(sound3)
explore(sound3)
最佳答案
我猜这是python(?)。我不了解python,但很明显问题出在代码的这一部分:
for index in range(0, getLength(sound1)):
value = getSampleValueAt(sound1, index)
for index in range(0, getLength(sound2)):
value2 = getSampleValueAt(sound2, index)
for index in range(0, getLength(sound3)):
setSampleValueAt(sound3, index, value+value2)
假设您已正确缩进,则使用了三个单独的循环,因此最后一个循环在目标文件中循环,并将所有样本值设置为找到的最后一个值的总和。您只能使用一个循环。像这样:
for index in range(0, MIN( getLength(sound1), getLength(sound2) ) ):
value = getSampleValueAt(sound1, index)
value2 = getSampleValueAt(sound2, index)
setSampleValueAt(sound3, index, value+value2)
(无论min在python中是什么),当然,您也应该将创建的文件的length值设置为最小值。当然,还有其他方法可以解决文件长度不匹配的问题,但请先解决循环问题。
关于file - 为什么我的声音文件没有被复制到?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10867262/