In order to play a note, we need to know its frequency and how long it should play. To do this, in exercise I have the following two variables:
A dictionary called freqs where the keys are strings representing a character name and the values are numbers representing frequencies.
freqs = {"la": 220,
"si": 247,
"do": 261,
"re": 293,
"mi": 329,
"fa": 349,
"sun": 392,
}
And strings that include the song notes:
notes =
"sol,250-mi,250-mi,500-fa,250-re,250-re,500-do,250-re,250-mi,250-fa,250-sol,250-sol,250-sol,500"
The strings are constructed as follows:
A comma appears after each character, followed by a number representing the length of time the character should play. Each character is separated from one another by a dash. in the first stage I used the split method to split the character strings by the hyphen. And I checked that it was iterable
In order to play a character, I'm supposed to use a skeleton Next code:
import winsound
...
winsound.Beep (frequency, duration)
When the Beep function receives two parameters: the frequency of a character and the length of time to be played. (duration, frequency)
I need to write a program that plays the song "Little Jonathan" according to the string of notes in the notes (which signifies the structure of the song) to create iterable from the above character strings and run it for loop, or use the next function.
That's my code so far, didn't work well. How do I proceed from here?
def Beep(frequency, duration ):
for word in duration:
yield frequency[word]
frequency={"la": 220,"si": 247,"do": 261,"re": 293,"mi": 329,"fa":
349,"sol": 392,
}
res = Beep(['sol,250', 'mi,250', 'mi,500', 'fa,250', 're,250', 're,500', 'do,250', 're,250', 'mi,250', 'fa,250', 'sol,250', 'sol,250', 'sol,500'],frequency)
print(next(res))
the desired output :
250,392
500,329
250,349
250,293
500,293
250,261
250,293
250,329
250,349
250,392
250,392
500,392