简体   繁体   English

在Pygame.mixer中防止声音结束时“弹出”

[英]Prevent “popping” at end of sound in Pygame.mixer

This has been solved - see solution at bottom. 这已经解决了 - 请参见底部的解决方案。

While using pygame.mixer, every time a sound finishes playing (whether queued in a channel, not queued but still played via a channel, or played directly via Sound.play() ), an audible "popping" noise can be heard. 在使用pygame.mixer时,每次声音播放完毕(无论是在频道中排队,还是排队但仍然通过频道播放,或通过Sound.play()直接播放),都可以听到声音“砰砰”的声音。 Is there any way to prevent this? 有什么方法可以防止这种情况吗?

Example code: 示例代码:

import math
import pygame
import array

pygame.mixer.init()

rawArr=[]
for i in range(41000):
    freq1=int(math.sin(i/220.0*math.pi*2)*32767.0)
    if i>40900: freq1=int(freq1*(41000-i)/100.0) #Solution - see Final Solution
    rawArr.append(freq1)

sndArr=array.array('h',rawArr)

snd=pygame.mixer.Sound(sndArr)

snd.play()

Update: Fading the sound out causes no noticeable difference. 更新:淡出声音会导致无明显差异。 Tested by using mixer.fadeout(fadeTime) , Sound.fadeout(fadeTime) , and Sound.play(0,maxTime,fadeTime) for fadeTime=1,10,100,200,1000 and maxTime=0,1,10,100,1000 . 通过使用mixer.fadeout(fadeTime)Sound.fadeout(fadeTime)Sound.play(0,maxTime,fadeTime) fadeTime=1,10,100,200,1000maxTime=0,1,10,100,1000

Update 2: Neither mixer.stop() nor mixer.pause() remove the popping sound. 更新2: mixer.stop()mixer.pause()都不会删除弹出声音。

Update 3: Implementing a manual fadeout by slowly reducing the volume is effective with sufficiently slow fade times, however it is ineffective for fade times of less than about .1 seconds. 更新3:通过缓慢减小音量来实现手动淡出是有效的,具有足够慢的淡入淡出时间,但是对于小于约0.1秒的淡入淡出时间它是无效的。 Furthermore, during the fade time, there is a series of smaller "clicks", which, while quieter than the original, remain audible and are more numerous than the original "popping". 此外,在淡入淡出时间期间,存在一系列较小的“咔嗒声”,其虽然比原始的更安静,但仍然可听见并且比原始的“砰砰”更多。 Test code, which was appended to the end of the original code: 测试代码,附加到原始代码的末尾:

while snd.get_volume():
     snd.set_volume(snd.get_volume()-0.005)
     pygame.time.wait(5)

FINAL SOLUTION: Thanks to user msw, was able to remove "popping" by implementing a manual fadeout within the raw audio data. 最终解决方案:感谢用户msw,能够通过在原始音频数据中实现手动淡出来删除“弹出”。 if i>40900: freq1=int(freq1*(41000-i)/100.0) was added to the generation loop - this approximately 0.0024 second fade time was just about the shortest that could be used before "popping" occurred. if i>40900: freq1=int(freq1*(41000-i)/100.0)被添加到生成循环中 - 这个大约0.0024秒的淡入淡出时间几乎是在“弹出”发生之前可以使用的最短时间。 The example code has been updated with the fixed code. 示例代码已使用固定代码进行更新。

This is known as the "dread audio cliff of noise". 这被称为“噪音的恐惧音频悬崖”。

When you have a digital signal which is higher than 0 and you abruptly cut it off, the waveform must plummet to 0. As people who deal with analog signals will tell you, the digital cliff has (in principle) all frequencies present at some amplitude. 当你有一个高于0的数字信号并且你突然切断它时,波形必然会下降到0.当处理模拟信号的人会告诉你时,数字悬崖(原则上)所有频率都存在于某个幅度。 This transition sounds like a "pop" by the time it gets to the speakers. 这种过渡听起来像是一个“流行”的声音。

Almost all audio is mastered so that the amplitude is faded to zero by the time the sample ends. 几乎所有音频都被掌握,以便在样本结束时幅度逐渐变为零。 If your sample doesn't have this characteristic, fade the sample to zero upon playback. 如果您的样品没有此特性,请在播放时将样品淡化为零。 As was noted by the OP, even a 2.5 millisecond fade can be enough to avoid the pop. 正如OP所指出的,即使2.5毫秒的淡入淡出也足以避免流行音乐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM