开发者

Basic Python Sound Programming

开发者 https://www.devze.com 2023-04-13 04:52 出处:网络
How can I 开发者_如何学Cmake a program that plays a tone that I define while I hold down a key? I can play different notes with winsound.Beep(), but I don\'t think this really helps.If you just want t

How can I 开发者_如何学Cmake a program that plays a tone that I define while I hold down a key? I can play different notes with winsound.Beep(), but I don't think this really helps.


If you just want to use the standard library and youre using, you can use msvcrt to get the current keypress and map that to a frequency.

import msvcrt
import time
import winsound

notes = {'a': 440, 's': 935, 'd': 1039}

while True:
    key = msvcrt.getch()
    try:
        note = notes[key]
    except KeyError:
        note = 0

    winsound.Beep(note, 10)
    time.sleep(0.01)


The winsound module allows you to play more than just beeps, have a look at winsound.PlaySound:

winsound.PlaySound('mySound.wav', winsound.SND_FILENAME)

When the user holds down a key, you will generally get multiple key press events in a short time.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号