【问题标题】:Sound echo function Python声音回声函数 Python
【发布时间】:2015-10-01 12:42:49
【问题描述】:

我需要编写函数echo,它接受一个文件名和一个浮点值time_delay,它代表秒数。然后,echo应该处理声音,原始声音被一个副本覆盖自身在时间上向前移动了 time_delay。

这是我目前所拥有的:

def add_scale_2(L, M, L_scale, M_scale):
""" add_scale_2 has as intput list L and list M and rertuns a new list LC, 
    with a linear sum of the two lists times their scale respectively. LC will use the length of the 
    shortest list
"""       
    if len(L) >= len (M):
        N = len(M)
    else:
        N = len(L)

    LC = [L[i]*L_scale + M[i]*M_scale for i in range(N)]
    return LC

还有:

def echo(filename, time_delay):
    print "Playing filename1 ..."
    play(filename)

    print "Reading in the sound data..."
    samps1, sr = readwav(filename)
    samps2 = [0]*float(samps1*time_delay) + samps1

    print "Computing new sound..."
    newsamps = add_scale_2(samps1, samps2, 0.5, 0.5)
    newsr = sr # no change to the sr

    writewav( newsamps, newsr, "out.wav" )
    print "Playing new sound..."
    play( 'out.wav' )

谁能帮帮我,因为我想不通!

    samps2 = [0]*int(samps1*time_delay) + samps1
TypeError: can't multiply sequence by non-int of type 'float'

【问题讨论】:

  • 问题/问题是什么?
  • samps2 = [0]*float(samps1*time_delay) + samps1 TypeError: can't multiply sequence by non-int of type 'float'
  • 请编辑您的问题,并指定该行 (292) 在您的示例中的位置。

标签: python function audio echo delay


【解决方案1】:

您的线路:

[0]*float(samps1*time_delay) + samps1

尝试将序列[0]float 相乘。导致错误TypeError: can't multiply sequence by non-int of type 'float'

您可以改为转换为 int:

[0]*int(len(samps1)*time_delay) + samps1

【讨论】:

  • samps2 = [0]*int(samps1*time_delay) + samps1 TypeError: can't multiply sequence by non-int of type 'float'
  • 使用int时同样的问题
  • samps1 也是一个序列...而time_delay 是一个浮点数...使用samps1*int(time_delay)
  • samps2 = [0]*samps1*int(time_delay) + samps1 TypeError: can't multiply sequence by non-int of type 'list'
  • [0]*int(len(samps1)*time_delay) + samps1 是我认为您正在努力实现的目标。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-26
  • 1970-01-01
  • 1970-01-01
  • 2019-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多