Pygame.mixer.Sound .set

系统教程 行业动态 更新时间:2024-06-14 16:57:17
Pygame.mixer.Sound .set_volume在Raspberry Pi中返回错误(Pygame.mixer.Sound .set_volume returns error in Raspberry Pi)

我尝试在Raspberry Pi B2中使用pygame创建一些短信号 这是我的代码:

#!/usr/bin/python import pygame import time from array import array from pygame.locals import * pygame.mixer.pre_init(44100, -16, 1, 1024) pygame.init() class ToneSound(pygame.mixer.Sound): def __init__(self, frequency, volume): self.frequency = frequency pygame.mixer.Sound.__init__(self, self.build_samples()) self.set_volume(volume) def build_samples(self): period = int(round(pygame.mixer.get_init()[0] / self.frequency)) samples = array("h", [0] * period) amplitude = 2 ** (abs(pygame.mixer.get_init()[1]) - 1) - 1 for time in xrange(period): if time < period / 2: samples[time] = amplitude else: samples[time] = -amplitude return samples tone_obj = ToneSound(frequency = 800, volume = .5) tone_obj.play(-1) #the -1 means to loop the sound time.sleep(2) tone_obj.stop()

当我运行它时,我得到:

Traceback (most recent call last): File "beep.py", line 29, in <module> tone_obj = ToneSound(frequency = 800, volume = .5) File "beep.py", line 15, in __init__ self.set_volume(volume) TypeError: fromfile() takes exactly 2 arguments (1 given)

当我评论set_volume函数时,我得到的下一个错误:

Traceback (most recent call last): File "beep.py", line 29, in <module> tone_obj.play(-1) #the -1 means to loop the sound TypeError: fromfile() takes exactly 2 arguments (1 given)

可能是什么问题? 我更新了pygame,更新了所有需要的库 - 相同的结果。 根据pygame文档,set_volume只接受一个参数。 我真的不知道该怎么办......

I try to create some short signal with pygame in Raspberry Pi B2 Here is my code:

#!/usr/bin/python import pygame import time from array import array from pygame.locals import * pygame.mixer.pre_init(44100, -16, 1, 1024) pygame.init() class ToneSound(pygame.mixer.Sound): def __init__(self, frequency, volume): self.frequency = frequency pygame.mixer.Sound.__init__(self, self.build_samples()) self.set_volume(volume) def build_samples(self): period = int(round(pygame.mixer.get_init()[0] / self.frequency)) samples = array("h", [0] * period) amplitude = 2 ** (abs(pygame.mixer.get_init()[1]) - 1) - 1 for time in xrange(period): if time < period / 2: samples[time] = amplitude else: samples[time] = -amplitude return samples tone_obj = ToneSound(frequency = 800, volume = .5) tone_obj.play(-1) #the -1 means to loop the sound time.sleep(2) tone_obj.stop()

When I run it, I get:

Traceback (most recent call last): File "beep.py", line 29, in <module> tone_obj = ToneSound(frequency = 800, volume = .5) File "beep.py", line 15, in __init__ self.set_volume(volume) TypeError: fromfile() takes exactly 2 arguments (1 given)

When I commented the set_volume function, the next error I get:

Traceback (most recent call last): File "beep.py", line 29, in <module> tone_obj.play(-1) #the -1 means to loop the sound TypeError: fromfile() takes exactly 2 arguments (1 given)

What can be the problem? I updated the pygame, updated all needed libraries - the same result. According to pygame documentation, set_volume takes one argument only. I really don't know what to do...

最满意答案

在Raspberry Pi网站上发布了类似的问题,请参阅“莫尔斯电码项目无效......” 。

那里的解决方案是:

更改: pygame.mixer.Sound.init(self, self.build_samples()) 收件人: pygame.mixer.Sound.init(self, buffer=self.build_samples())

添加buffer=为我工作。

There is a similar problem posted to the Raspberry Pi site, see "Morse-code Project not working...".

The solution there was:

Change: pygame.mixer.Sound.init(self, self.build_samples()) To: pygame.mixer.Sound.init(self, buffer=self.build_samples())

Adding buffer= worked for me.

更多推荐

本文发布于:2023-04-12 19:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/f65df0ae524f122a69d2f972b31aa15a.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:mixer   Pygame   set   Sound

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!