Python 占位符,用于设置每个按键的不同正弦波输出频率
我想使用占位符%f为每个键的正弦波声音输出设置不同的频率。 但是,我收到错误消息“SyntaxError:can't assign to literal”。我检查了我的语法是否不正确,尽管我发现了一个类似的例子——有人能看到问题是什么以及为什么我的代码不能运行吗? 回溯:文件“test.py”,第50行 (Python,Pygame) 字符串Python 占位符,用于设置每个按键的不同正弦波输出频率,python,pygame,placeholder,literals,assign,Python,Pygame,Placeholder,Literals,Assign,我想使用占位符%f为每个键的正弦波声音输出设置不同的频率。 但是,我收到错误消息“SyntaxError:can't assign to literal”。我检查了我的语法是否不正确,尽管我发现了一个类似的例子——有人能看到问题是什么以及为什么我的代码不能运行吗? 回溯:文件“test.py”,第50行 (Python,Pygame) 字符串“%f”可以是变量名。无法分配'%f'=261.6-这是您的错误“无法分配到文字” 例如,将其命名为freq,并用它代替“%f” 顺便说一句:占位符%f只
“%f”
可以是变量名。无法分配'%f'=261.6
-这是您的错误“无法分配到文字”
例如,将其命名为freq
,并用它代替“%f”
顺便说一句:占位符
%f
只能用于字符串格式化(或字符串解析)
编辑:
import pygame
from pygame.locals import *
import math
import numpy
def generate_sound(freq):
#setup our numpy array to handle 16 bit ints, which is what we set our mixer to expect with "bits" up above
buf = numpy.zeros((n_samples, 2), dtype = numpy.int16)
max_sample = 2**(bits - 1) - 1
for s in range(n_samples):
t = float(s)/sample_rate # time in seconds
buf[s][0] = int(round(max_sample*math.sin(2*math.pi*freq*t))) # left
buf[s][1] = int(round(max_sample*0.5*math.sin(2*math.pi*freq*t))) # right
return pygame.sndarray.make_sound(buf)
bits = 16
#the number of channels specified here is NOT
#the channels talked about here http://www.pygame.org/docs/ref/mixer.html#pygame.mixer.get_num_channels
pygame.mixer.pre_init(44100, -bits, 2)
pygame.init()
size = (1200, 720)
screen = pygame.display.set_mode(size, pygame.HWSURFACE | pygame.DOUBLEBUF)
duration = 1.0 # in seconds
#this sounds totally different coming out of a laptop versus coming out of headphones
sample_rate = 44100
n_samples = int(round(duration*sample_rate))
#default frequency - not used
sound = None
sound_261_6 = generate_sound(261.6)
sound_293_7 = generate_sound(293.7)
#This will keep the sound playing forever, the quit event handling allows the pygame window to close without crashing
_running = True
while _running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
_running = False
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
_running = False
elif event.key == K_a:
if not sound:
sound = sound_261_6
sound.play()
elif event.key == K_s:
if not sound:
sound = sound_261_6
sound.play()
if event.type == KEYUP:
if sound:
sound.stop()
sound = None
pygame.quit()
你应该给我们完整的追踪。只是错误消息没有知道它发生在哪里那么有用。
'%f'=261.6
没有意义-不能将浮点值分配给字符串文字。但是,不清楚您想做什么,所以我无法真正提供答案。文件“test.py”,第50行显示完整的错误消息(回溯)。有多行出现问题-请在代码中标记这一行。数字频率值必须根据每次按键进行更改,才能使用该频率播放声音。谢谢,这是我所需要的,但是,我已将其更改为变量名称,并为其提供了默认频率值440。当按on键时,该频率值应与键相对应,但不会改变。所有按键都有440频的声音。我将更新原始帖子中的代码,以显示它现在所说的内容。如果您更改freq
,那么您必须再次使用sin(2*math.pi*freq*t)
调用代码。那么,如何说呢,您必须为范围内的s调用所有代码-(n\u示例):
再次调用,非常感谢,不过,我还有一个问题:除了按键和播放音符之间有大约半秒的延迟之外,这个新代码工作得非常出色。这是因为执行for循环所花费的时间,有没有办法消除这种延迟?是的,我举了一个函数的例子,该函数在mainloop(运行时
)之前生成所有声音。看到我的答案了吗
freq = 261.6
sin(2*math.pi* freq *t)
import pygame
from pygame.locals import *
import math
import numpy
def generate_sound(freq):
#setup our numpy array to handle 16 bit ints, which is what we set our mixer to expect with "bits" up above
buf = numpy.zeros((n_samples, 2), dtype = numpy.int16)
max_sample = 2**(bits - 1) - 1
for s in range(n_samples):
t = float(s)/sample_rate # time in seconds
buf[s][0] = int(round(max_sample*math.sin(2*math.pi*freq*t))) # left
buf[s][1] = int(round(max_sample*0.5*math.sin(2*math.pi*freq*t))) # right
return pygame.sndarray.make_sound(buf)
bits = 16
#the number of channels specified here is NOT
#the channels talked about here http://www.pygame.org/docs/ref/mixer.html#pygame.mixer.get_num_channels
pygame.mixer.pre_init(44100, -bits, 2)
pygame.init()
size = (1200, 720)
screen = pygame.display.set_mode(size, pygame.HWSURFACE | pygame.DOUBLEBUF)
duration = 1.0 # in seconds
#this sounds totally different coming out of a laptop versus coming out of headphones
sample_rate = 44100
n_samples = int(round(duration*sample_rate))
#default frequency - not used
sound = None
sound_261_6 = generate_sound(261.6)
sound_293_7 = generate_sound(293.7)
#This will keep the sound playing forever, the quit event handling allows the pygame window to close without crashing
_running = True
while _running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
_running = False
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
_running = False
elif event.key == K_a:
if not sound:
sound = sound_261_6
sound.play()
elif event.key == K_s:
if not sound:
sound = sound_261_6
sound.play()
if event.type == KEYUP:
if sound:
sound.stop()
sound = None
pygame.quit()