首页 > 解决方案 > 如何在 JYTHON 中混合两个声音文件?

问题描述

所以,我对编程真的很陌生,我目前正在学习JYTHON,我需要混合两种不同的声音并将它们变成一种声音。到目前为止,我真的尽力编写了这个代码,但是每当我尝试播放结果时,空的声音文件都不会播放任何声音:<

那么请你们看看我的代码并告诉我我做错了什么吗?

def blendTwoSounds():
  sound1 = pickAFile()
  antelope = makeSound(sound1)
  sound2 = pickAFile()
  rabbit = makeSound(sound2)
  jackalope = makeEmptySoundBySeconds(9)
  for index in range(0,20000):
    rabbitSample = getSampleValueAt(sound2, index)
    setSampleValueAt(jackalope, index , sound2Sample)
  for index in range(0,20000):
    sound2Sample = getSampleValueAt(sound2,index+20000)
    sound1Sample = getSampleValueAt(sound1, 20000)
    newSample =10.0*sound2Sample + 10.9*sound2Sample
    setSampleValueAt(jackalope, index+20000, newSample)
  for index in range(20000,40000):
    sound1Sample = getSampleValueAt(sound1,index)
    setSampleValueAt(jackalope, index+20000,sound1Sample) 
  play(jackalope)

我什至尝试过这段代码,听起来可以,但最终结果(jackalope)里面没有任何声音。也请看一下,看看哪个更好用

def blendTwoSounds():
  path1 = pickAFile()
  antelope = makeSound(path1)
  path2 = pickAFile()
  rabbit = makeSound(path2)
  jackalope = makeEmptySoundBySeconds(9)
  for index in range(0,20000):
    asample = getSampleValueAt(asample,index)
    rsample = getSampleValueAt(rsample,index)
    newSample = 9.5*asample+9.5*rsample
    setSampleValueAt(jackalope,index,newSample)
    for index in range(20000,40000):
      rsample = getSamleValueAt(rsample,index)
      setSampleValueAt(jackalope,index+20000,rsample)
  play(jackalope)
  return(jackalope)

所以看起来我让这段代码可以工作,但我可以让它变得更好吗?任何建议都会有帮助。

def blendTwoSounds():
  path1 = pickAFile()
  a = makeSound(path1)
  path2 = pickAFile()
  r = makeSound(path2)
  jackalope = makeEmptySoundBySeconds(3)
  for index in range(0,20000):
    rSample = getSampleValueAt(r,index)
    setSampleValueAt(jackalope,index,rSample)
  for index in range(0,20000):
      rSample = getSampleValueAt(r,index+20000)
      aSample = getSampleValueAt(a,index)
      newSample = 7.5*rSample + 7.5*aSample
      setSampleValueAt(jackalope,index+20000,newSample)
  for index in range(20000,40000):
      aSample = getSampleValueAt(a,index)
      setSampleValueAt(jackalope,index+20000,aSample)
  play(jackalope)
  folder=setMediaPath()
  writeSoundTo(jackalope,folder+'blending.wav')
  return jackalope
blendTwoSounds()

标签: pythonaudiojython

解决方案


推荐阅读