编程语言
首页 > 编程语言> > python 节拍器

python 节拍器

作者:互联网

孩子的节拍器坏了, 实现一个节拍器临时用用.  实际精度大约1毫秒

mport time

import pygame
from time import sleep

pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.mixer.init()
pygame.init()

BPM = 66
sound = pygame.mixer.Sound('sounds/7863.wav')
speed = 60.0/BPM
print("base = ", speed)
start0 = start = cur = time.time()
# start = time.time()
# cur = time.time()
index = 0
while True:
    all_diff = (cur-start0) - speed*index
    sound.play(maxtime=500)
    n = time.time()
    # print("diff ", speed-(n-start))
    sleep(speed-(n-start)-all_diff)
    cur = time.time()
    # print(cur - start, " vs ", speed)
    print("%.5f" %((cur - start) - speed))
    start = cur
    index += 1
    print("== %.6f " %((cur-start0) - speed*index) )

 

标签:cur,python,节拍器,start,pygame,time,print,speed
来源: https://www.cnblogs.com/kennethjia/p/16366476.html