def fadeout(self, seconds, target_volume=0.0):
"""Fade the end of the sample out to the target volume (usually zero) in the given time."""
assert not self.__locked
seconds = min(seconds, self.duration)
i = self.frame_idx(self.duration-seconds)
begin = self.__frames[:i]
end = self.__frames[i:] # we fade this chunk
numsamples = len(end)/self.__samplewidth
decrease = 1.0-target_volume
_sw = self.__samplewidth # optimization
_getsample = audioop.getsample # optimization
faded = Sample.get_array(_sw, [int(_getsample(end, _sw, i)*(1.0-i*decrease/numsamples)) for i in range(int(numsamples))])
end = faded.tobytes()
if sys.byteorder == "big":
end = audioop.byteswap(end, self.__samplewidth)
self.__frames = begin + end
return self
评论列表
文章目录