Question

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?

Was it helpful?

Solution

even if you use a while loop like this:

 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

something you could do is use Pygame it will be more work but it is probably a much better way to create music

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

if you dont want to do all that take a look at Python In Music thats probably a good place to start

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top