我在IronPython中使用NAudio混合多个音频流来创建环境音频。这对于某些曲目来说工作得相当好,但对于其他一些曲目(风,雨),循环播放时可能会产生刺耳的停止/开始。IronPython NAudio循环交叉淡入淡出
因为我并不是100%确定如何在python中实现LoopStream类的例子,所以我开始在十分之一秒或更短的时间内完成位置检查。我知道我为什么在那里有空位。从那以后,我能够弄清楚如何在python中重新创建LoopStream,并且它可以工作,但是我仍然像以前一样在回放中存在差距。我正在尝试将曲目的末尾逐渐淡入相同曲目的开头,当我这样做时,音频完全消失。
下面的代码:
class LoopStream(WaveStream):
def __init__(self,WaveStream,AudioStream):
self.wavestream = WaveStream
self.audiostream = AudioStream
def get_WaveFormat(self):
return self.wavestream.WaveFormat
def get_Length(self):
return self.wavestream.Length
def get_Position(self):
return self.wavestream.Position
def HasData(count):
return True
def Read(self,buf,offset,count):
read = 0
while(read < count):
required = count - read
#print(str(self.audiostream.get_chan_id()) + " reading @ " + str(self.wavestream.Position) + "/" + str(self.wavestream.Length))
pos = self.wavestream.Position
readthistime = self.wavestream.Read(buf,offset+read,required)
if pos == 0:
self.startbuf = buf
if readthistime < required:
self.wavestream.Position = 0
#print(len(buf))
#buf = buf+self.startbuf
print(len(buf))
buf = FadeOut(self,buf,offset,readthistime) + FadeIn(self,self.startbuf,0,required)
print(len(buf))
readthistime+=required
print(str(self.audiostream.get_chan_id()) + " restarting1")
elif self.wavestream.Position + required > self.wavestream.Length:
#read += readthistime
#readthistime = self.wavestream.Read(buf,self.wavestream.Position,required)
#print(str(self.audiostream.get_chan_id()) + " restarting2")
pass
if self.wavestream.Position >= self.wavestream.Length:
self.wavestream.Position = 0
buf = buf + self.startbuf
print(str(self.audiostream.get_chan_id()) + " restarting3")
read += readthistime
return read
def FadeOut(self,buf,offset,count):
sample = 0
maxfadesamples = int((self.wavestream.WaveFormat.SampleRate * 75)/1000)
fadesamples = 0
while sample < count:
multiplier = 1.0 - (fadesamples/maxfadesamples)
for i in range(0,self.wavestream.WaveFormat.Channels):
buf[offset+sample] *= multiplier
sample+=1
fadesamples+=1
if fadesamples > maxfadesamples:
for j in range(0,self.wavestream.WaveFormat.Channels):
while sample < count:
buf[offset+sample] = 0
sample+=1
def FadeOut(self,buf,offset,count):
sample = 0
maxfadesamples = int((self.wavestream.WaveFormat.SampleRate * 75)/1000)
fadesamples = 0
while sample < count:
multiplier = (fadesamples/maxfadesamples)
for i in range(0,self.wavestream.WaveFormat.Channels):
buf[offset+sample] *= multiplier
sample+=1
fadesamples+=1
ERR ..已经注意到我只是需要习惯于在这里..添加错误处理语句,因为我发布后,立即发现了一些问题,所以我最终可能会解决自己1)淡出声明的两倍,因此FadeIn实际上并不存在。一旦我将FadeOut#2重命名为FadeIn,它仍然有毛病,但因为它陷入了无限循环。我解决了这个问题,但仍然存在问题,但我认为它在读取功能的其他地方出错。 –