我怎样才能做一个精确的节拍器?

编程入门 行业动态 更新时间:2024-10-11 23:22:53
本文介绍了我怎样才能做一个精确的节拍器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我尝试制作一个非常简单的节拍器,它的工作速度为 30 bpm:

I tried to do a very simple metronome that is working at 30 bpm:

While True:
    winsound.Beep(1000, 200)
    time.sleep(2 - 0.2)

然后,我打开了手机上的节拍器应用程序,同时电脑中的哔哔声随着时间的推移变得越来越不准确.还有另一种方法可以做time.sleep()"吗?甚至是time.sleep()"内疚?

Then, I turned on a metronome app on my phone together with the beep in the computer, and it became less precise as the time went on. Is there another way to do the "time.sleep()"? Is it even the "time.sleep()" guilt?

推荐答案

这在多任务操作系统上很难做到:系统给你准确的 sleep 延迟并不容易要求.有关详细信息,请参阅 python 的 time.sleep() 有多准确?.

This is difficult to do on a multitasking operating system: it's not easy for the system to give you the exact sleep delay that you request. See How accurate is python's time.sleep()? for details.

但是我们可以通过测量时间跨度和调整睡眠延迟来相当接近.Python 3.3+ 提供 time.perf_counter,它应该非常擅长测量时间间隔,尽管确切的精度取决于您的操作系统和硬件.

But we can get fairly close by measuring the time span and adjusting the sleep delay. Python 3.3+ provides time.perf_counter, which is supposed to be pretty good at measuring time intervals, although the exact precision depends on your OS and hardware.

这是一个简单的演示,它只打印每个滴答的请求时间延迟和测量时间延迟之间的差异.最初的输出有点草率,但很快就会稳定下来,在请求间隔的 10 微秒内给出滴答声.但是,播放声音比打印到终端需要更多的系统资源,这可能会影响该技术的精度.为获得最佳结果,请尽量减少机器上的任务负载,尤其是在单核的情况下.

Here's a simple demo that just prints the difference between the requested and the measured time delay for each tick. The initial outputs are a bit sloppy, but it soon settles down to giving ticks that are within 10 microseconds of the requested interval. However, playing a sound takes more system resources than printing to the terminal, and that may impact the precision of this technique. For best results, minimize the task load on your machine, especially if it's single core.

我已将此处的延迟设置为 0.2 秒 = 300 bpm.较慢的节拍率可能会降低精度,因为在每个睡眠周期内,CPU 有更多时间来执行其他可能导致睡眠时间比请求长一点的任务.

I've set the delay here to 0.2 seconds = 300 bpm. Slower beat rates may give less precision because there's more time during each sleep cycle for the CPU to do other tasks that may cause the sleep to be a little longer than requested.

from time import sleep, perf_counter

delay = d = 0.2
print(60 / delay, 'bpm')
prev = perf_counter()
for i in range(20):
    sleep(d)
    t = perf_counter()
    delta = t - prev - delay
    print('{:+.9f}'.format(delta))
    d -= delta
    prev = t

典型输出

300.0 bpm
+0.000262488
+0.000151862
-0.000019085
-0.000011358
+0.000023078
-0.000015817
-0.000004357
+0.000009283
-0.000012252
+0.000020515
-0.000009061
+0.000003249
-0.000011482
+0.000029230
+0.000001554
-0.000023614
-0.000003286
+0.000000127
-0.000003732
+0.000016311

这些结果来自一台运行 Linux,YMMV 的旧单核 32 位 2GHz 机器.

These results are from an old single core 32 bit 2GHz machine running Linux, YMMV.

这篇关于我怎样才能做一个精确的节拍器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 12:58:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1394456.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节拍器   做一个   精确

发布评论

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

>www.elefans.com

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