gpt4 book ai didi

python - 如何使用 pygame 正确实现音频延迟测试

转载 作者:太空宇宙 更新时间:2023-11-04 01:51:26 24 4
gpt4 key购买 nike

我有一个学校项目涉及用 Python 制作一个简单的节奏游戏。我目前正在尝试使用 pygame 将闪烁的点与已知 bpm(在本例中为 120)的歌曲同步。

闪烁的点一开始是同步的,但慢慢地与时间不同步。我在互联网上搜索了如何制作节奏游戏,发现我需要进行音频延迟测试。但是,我想出的代码并没有真正起作用,所以我真的很想得到一些帮助。我已经尝试删除 mp3 开头的间隙并将其导出为没有元数据的 ogg。

import sys, pygame, math, random, time

pygame.init()

size = width, height = 1000, 1000
speed = [2, 2]
black = 0, 0, 0

screen = pygame.display.set_mode(size)
pygame.display.update()
pygame.mixer.init()
pygame.mixer.music.load("metr.ogg")

continuer = True

bpm = 120

t = 60 / bpm

font = pygame.font.Font('freesansbold.ttf', 15)

lasttime = 0

delay = 0
red = (200, 0, 0)
green = (0, 200, 0)
colors = [red, red, red, red]
i = 0
av = []
drawn = False
clock = pygame.time.Clock()
pygame.mixer.music.play()
while continuer:

time = (pygame.mixer.music.get_pos()) / 1000
nextkey = lasttime + t + (delay / 1000)
if time > nextkey:
if drawn:
screen.fill((0, 0, 0))
drawn = False
else:
pygame.draw.circle(screen, colors[i], (500, 500), 20)
drawn = True
if i == 3:
i = 0
else:
i += 1
lasttime += t
screen.blit(font.render('delay = ' + str(delay) + "ms", True, (255, 255, 255), (0, 0, 0)), (500, 600))
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
delay += 1
elif event.key == pygame.K_h:
av.append(nextkey - time)
delay = sum(av) / len(av)
elif event.key == pygame.K_LEFT:
delay -= 1
elif event.key == pygame.K_SPACE:
continuer = False
pygame.display.flip()
clock.tick(60)

pygame.quit()

文件 metr.ogg 只是一个 120bpm 的节拍器

最佳答案

这是因为你的变量 nextkey 是不同变量的总和,每个变量都会包含一个小错误。所以你总是在添加小错误,直到它最终变得明显。

防止累积误差的方法是始终直接从源头进行计算。在你的情况下 pygame.mixer.music.get_pos()

在下面的例子中,我们首先创建一个变量ms_per_beat。该值是一个常量,用于定义每个节拍应该经过多少毫秒。对于每一帧,我们根据歌曲播放的时间计算time_since_last_beat。一个粗略的、不完整的例子:

bpm = 120
beats_per_ms = (bpm / 60) / 1000
ms_per_beat = 1 / beats_per_ms # How many milliseconds each beat takes.
current_beat = 0 # The beat we're currently on.
time_since_last_beat = 0 # How many milliseconds since last beat.

dot_display_time = 500 # Display the dot for 500 ms (half a second)
dot_timer = 0 # Keeps track on how long the dot has been displayed.
display_dot = False # Whether to display the dot or not.

clock = pygame.time.Clock()

while running:
dt = clock.tick(60)

current_play_time_ms = pygame.mixer.music.get_pos() / 1000
time_since_last_beat = current_play_time_ms - (current_beat * ms_per_beat)

if time_since_last_beat >= ms_per_beat:
print('Bop!')
current_beat += 1
display_dot = True
dot_timer = dot_display_time

screen.fill((0, 0, 0))

if display_dot:
dot_timer -= dt
if dot_timer <= 0:
display_dot = False
pygame.draw.circle(screen, colors[i], (500, 500), 20)

pygame.display.update()

上面的例子假设音频正好在节拍上开始。它还假设它实际上是 120 bpm。如果它是 120.1 bpm,它最终会失去同步。更合适的方法是分析音频的振幅峰值,然后只显示该点。如果您的音频只是节拍器,则可以使用 pygame.mixer.music.get_volume() 完成。

dot_display_time = 500  # Display the dot for 500 ms (half a second)
dot_timer = 0 # Keeps track on how long the dot has been displayed.
display_dot = False # Whether to display the dot or not.

beat_volume_threshold = 0.7 # The volume the audio has to overcome to count as a beat.

clock = pygame.time.Clock()

while running:
dt = clock.tick(60)

if pygame.mixer.music.get_volume() >= beat_volume_threshold:
print('Bop!')
display_dot = True
dot_timer = dot_display_time

screen.fill((0, 0, 0))

if display_dot:
dot_timer -= dt
if dot_timer <= 0:
display_dot = False
pygame.draw.circle(screen, colors[i], (500, 500), 20)

pygame.display.update()

但是,我会推荐一种完全不同的方法。同步总是一件麻烦事,所以尽可能避免它。让您的游戏成为节拍器,而不是让您的游戏与节拍器同步。有一个特定时间过去时播放的“bop”音频。然后您的音频和图形将始终同步,因为它们使用相同的时钟。下面的示例在 pygame.time.set_timer 的帮助下播放安排用户事件的音频。

import pygame

# This fixes the latency issue with the pygame mixer.
pygame.mixer.pre_init(22050, -16, 2, 1024)
pygame.init()

PLAY_CLICK = pygame.USEREVENT + 1

screen = pygame.display.set_mode((400, 400))

dot_display_time = 200 # Display the dot for 500 ms (half a second)
dot_timer = 0 # Keeps track on how long the dot has been displayed.
display_dot = False # Whether to display the dot or not.

bpm = 120
beats_per_ms = (bpm / 60) / 1000
ms_per_beat = 1 / beats_per_ms # How many milliseconds each beat takes.

clock = pygame.time.Clock()
sound = pygame.mixer.Sound('bop.wav')

pygame.time.set_timer(PLAY_CLICK, int(ms_per_beat)) # Play sound repeatedly every 'beats_per_ms'.

running = True
while running:
dt = clock.tick(60)

for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == PLAY_CLICK:
sound.play()
display_dot = True
dot_timer = dot_display_time

screen.fill((0, 0, 0))

if display_dot:
dot_timer -= dt
if dot_timer <= 0:
display_dot = False
pygame.draw.circle(screen, pygame.Color('green'), (200, 200), 20)

pygame.display.update()

如果您不习惯安排事件,您可以自己计算时间。

import pygame

# This fixes the latency issue with the pygame mixer.
pygame.mixer.pre_init(22050, -16, 2, 1024)
pygame.init()

font = pygame.font.Font('freesansbold.ttf', 15)
screen = pygame.display.set_mode((400, 400))

dot_display_time = 250 # Display the dot for 250 ms (quarter of a second)
dot_timer = 0 # Keeps track on how long the dot has been displayed.
display_dot = False # Whether to display the dot or not.

bpm = 120

clock = pygame.time.Clock()
sound = pygame.mixer.Sound('bop.wav')

time = 0

running = True
while running:
dt = clock.tick(60)
time += dt

for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
bpm -= 5
elif event.key == pygame.K_RIGHT:
bpm += 5

# Calculate how long to wait based on the bpm.
beats_per_ms = (bpm / 60) / 1000
ms_per_beat = 1 / beats_per_ms # How many milliseconds each beat takes.

if time >= ms_per_beat:
time -= ms_per_beat
sound.play()
display_dot = True
dot_timer = dot_display_time

screen.fill((0, 0, 0))
screen.blit(font.render('BPM {}'.format(bpm), True, (255, 255, 255), (0, 0, 0)), (160, 20))

if display_dot:
dot_timer -= dt
if dot_timer <= 0:
display_dot = False
pygame.draw.circle(screen, pygame.Color('green'), (200, 200), 20)

pygame.display.update()

但是请注意,pygame 在特定时间播放声音时似乎有困难。如果您减少缓冲区,效果会更好(如 documentation 中所述)。

pygame.mixer.pre_init(22050, -16, 2, 1024)
pygame.init()

关于python - 如何使用 pygame 正确实现音频延迟测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57934955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com