改变 pygame.mixer 中的音量(几乎)没有效果 [英] Changing volume in pygame.mixer (almost) no effect

查看:72
本文介绍了改变 pygame.mixer 中的音量(几乎)没有效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 tkinter 中制作一个应用程序,它有许多按钮,您可以在上面分配声音并稍后播放.按钮的点击本身只调用 play() 方法,所以声音的加载是预先完成的.

I'm trying to make an application in tkinter that has a number of buttons you can assign sound on and play it later. The click of the button itself only calls play() method, so loading of the sound is done beforehand.

我尝试使用滑块 (tk.Scale) 进行某种音量控制,我注意到大多数音量值之间没有明显差异,直到我非常接近于零(考虑到滑块分辨率为 0.01,从 0.0 到 0.1).在 0.02 左右,我想我注意到音量显着降低,如果我调到零,声音就会静音.请注意,如果我在没有播放声音的情况下移动滑块会发生这种情况.

I tried making some kind of volume control with sliders (tk.Scale) and I noticed there is no noticeable difference between most volume values until I get very close to zero (take into consideration that slider resolution is 0.01 from 0.0 to 0.1). At around 0.02 I think I notice the sound volume is significantly lower and if I get to zero, the sound is muted. Please note that this happens if I move the slider while no sounds are playing.

有趣的是,如果我尝试播放足够长的声音以让我在播放时移动滑块,我可以立即注意到差异,但是如果声音停止播放并且我再次尝试播放它再次进入默认"音量.

The interesting thing is, if I try playing a sound that is long enough to let me move the slider while it's playing, I can notice the difference right away, but if the sound stops playing and I try playing it again, it goes to the "default" volume again.

由于我根据它们的作用(录制声音、播放声音、GUI)将我的应用程序分成多个脚本,所以我认为可能是我没有直接初始化 pygame 混音器的问题,而是从导入的模块中初始化的,所以我制作了一个新的 python 脚本并在以下位置输入了这段代码:

Since I divided my application into multiple scripts according to what they do (recording sound, playing sound, GUI) I thought it could be the problem that I haven't directly initialized pygame mixer, but rather from the imported module, so I made a new python script and typed this code in:

import pygame
import time
pygame.mixer.pre_init(frequency=44100, size=-16, channels=1, buffer=512)
pygame.mixer.init()

sound1=pygame.mixer.Sound("sound.wav")
sound1.set_volume(1.0)
print sound1.get_volume()
sound1.play()
time.sleep(sound1.get_length())
sound1.set_volume(0.5)
print sound1.get_volume()
sound1.play()
time.sleep(sound1.get_length())
sound1.set_volume(0.08)
print sound1.get_volume()
sound1.play()
time.sleep(sound1.get_length())

输出如下:1.0,0.5,0.078125(一个低于另一个)确认音量确实已设置(我希望正确).

The output is the following: 1.0,0.5,0.078125 (one below the other) confirming that the volume has indeed been set (I hope properly).

我唯一能注意到的区别是第三种情况,这真的不是很明显,我希望音量增加是线性的,这远非如此.

The only time I can notice the difference is the third case, which is not that noticeable really, I want the volume increase to be linear, this is far from it.

我用频道尝试了同样的事情:

I tried the same thing with a channel:

sound1=pygame.mixer.Sound("sound.wav")
channel=pygame.mixer.find_channel(True)
channel.set_volume(1.0)
channel.play(sound1)
time.sleep(sound1.get_length()/2)
channel.set_volume(0.5)
print "Volume set"
time.sleep(sound1.get_length()/2)

不走运,同样的事情也发生在这里.

No luck, the same thing happens here too.

我花了一整天的时间在谷歌上搜索pygame 混合器音量问题"pygame 混合器音量设置问题"和类似的短语,但没有运气.希望这里有人可以提供帮助,考虑到我的文凭取决于 python 方法.:)

I spent all day googling "pygame mixer volume problem" "pygame mixer volume set problem" and similar phrases, but no luck. Hopefully someone here can be of help, considering my diploma depends on a python method. :)

提前致谢.

推荐答案

我找到了答案(感谢来自 PyGame IRC 的 Gummbum).

I found the answer (thank you Gummbum from PyGame IRC).

问题不在于 Python 或 Pygame 本身,而在于 Windows.似乎声音增强以某种方式摆弄了我的脚本播放的声音(或任何其他 Pygame 脚本).

The problem is not in Python or Pygame itself, but rather in Windows. It seems sound enhancements are somehow fiddling with the way the sound my script is playing (or any other Pygame script for that matter).

我使用的是 Windows 10,我是这样做的:

I'm on Windows 10 and this is how I did it:

  1. 右键单击任务栏中的扬声器图标

  1. Right click on the speaker icon in the taskbar

选择播放设备

就是这样.

这篇关于改变 pygame.mixer 中的音量(几乎)没有效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆