简体   繁体   English

Python 的 winsound.Beep() 暂停问题

[英]Python's winsound.Beep() pause issues

So my code is as such:所以我的代码是这样的:

import winsound
import time

length = 250

class Notes():
    def frequency(halfNotesFromA4):
        value = 440 * ((2)**(1.0/12.0))**halfNotesFromA4
        return (int)(value)
    def processNote(note):
        if(note == 'C'):return Notes.frequency(3)
        if(note == 'D'):return Notes.frequency(5)
        if(note == 'E'):return Notes.frequency(7)
        if(note == 'F'):return Notes.frequency(8)
        if(note == 'G'):return Notes.frequency(10)
        if(note == 'A'):return Notes.frequency(12)
        if(note == 'B'):return Notes.frequency(14)

song = "EDCDEEE"
noteList = list(song)
print(noteList)

for note in noteList:
    print("Doing ", note)
    frequency = Notes.processNote(note)
    winsound.Beep(frequency, length)

It works fine but the problem I'm having is there is a pause between each beep.它工作正常,但我遇到的问题是每次哔声之间都有暂停。 I was hoping to play the sound continuously without pauses so that it sounds like real music.我希望不间断地连续播放声音,这样听起来就像真正的音乐。 Is this possible with the winsound.Beep() library?这可以通过 winsound.Beep() 库实现吗?

even if you use a while loop like this:即使您使用这样的 while 循环:

 import winsound

 while True:
    winsound.Beep(100, 100)

there is still a small pause between and it wont sound like a solid note and you cant get much faster than that之间仍然有一个小的停顿,它听起来不像一个坚实的音符,你不能比这更快

You can still try it but i dont think you can get it to just sound like one long beep but winsound does have winsound.PlaySound(sound, flags) so you can load a sound here are the Winsound Docs你仍然可以尝试,但我认为你不能让它听起来像一声长哔声,但 winsound 确实有winsound.PlaySound(sound, flags)所以你可以在这里加载声音是Winsound Docs

something you could do is use Pygame it will be more work but it is probably a much better way to create music你可以做的就是使用Pygame,它会做更多的工作,但它可能是一种更好的音乐创作方式

you can use Pygame Music to load and play the music and im sure you could make it much more interactive the thing is you have to load each sound from a file你可以使用Pygame Music来加载和播放音乐,我相信你可以让它更具交互性,因为你必须从文件中加载每个声音

if you dont want to do all that take a look at Python In Music thats probably a good place to start如果你不想做所有这些,看看Python In Music 那可能是一个很好的起点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM