0

This is the code I have:

import winsound from myro import *

def main():

    HftM1 = makeSong("REST 1; REST 1; REST 1; REST 1; REST 1; REST 1; REST 1; REST 1; D4 1/6; F4 1/6; D5 2/3; D4 1/6; F4 1/6; D5 2/3; E5 1/2; F5 1/6; E5 1/6; F5 1/6; E5 1/6; C5 1/6; A4 2/3; A4 1/3; D4 1/3; F4 1/6; G4 1/6; A4 1; A4 1/3; D4 1/3; F4 1/6; G4 1/6; E4 1; D4 1/6; F4 1/6; D5 2/3; E5 1/2; F5 1/6; E5 1/6; F5 1/6; E5 1/6; C5 1/6; A4 2/3; A4 1/3; D4 1/3; F4 1/6; G4 1/6; A4 2/3; A4 1/3; D4 1; REST 1; REST 1; REST 1")
    saveSong(HftM1, "WindmillHut.txt", append=1)
    song = readSong("WindmillHut.txt")

    play = []

    for n in range(len(song)):
        play = song[n]
        note = play[0]
        duration = play[1]
        winsound.Beep(int(note), int(duration*2000))
main()

When I try to run this, I keep getting the error:

Traceback (most recent call last):
  File "C:/Users/Gerren.Kids-PC/Desktop/Gerren's Files/School/Programming 1/Mod 5/Code/WindmillHut.py", line 23, in -toplevel-
    main()
  File "C:/Users/Gerren.Kids-PC/Desktop/Gerren's Files/School/Programming 1/Mod 5/Code/WindmillHut.py", line 22, in main
    winsound.Beep(int(note), int(duration*2000))
ValueError: frequency must be in 37 thru 32767

What am I doing wrong and what do I need to change it to? Please be specific.

The Professor
  • 51
  • 1
  • 10

2 Answers2

3

the winsound.beep function is just a wrapper around the windows api beep function. the windows function requires the first parameter (the frequency), be between 37 and 32767. i suspect any frequency outside of the range is out of humans range of hearing. it could also be that way because the old sound cards that this function was meant for only supported that range.

you are calling winsound.beep() and whatever int(note) is returning is out of that range. you should check for note being valid before calling beep, probably.

note = int(play[0])
if note > 37 and note < 32767:
    winsound.Beep(note, int(duration*2000))
else:
    print("error in input")
Mike Corcoran
  • 14,072
  • 4
  • 37
  • 49
  • Ok, it actually played the song (Yay!), but it also printed out 11 "error in input". The error is coming from the "REST 1" functions (there are 11 of them). How would I fix that? I kinda need them in there. – The Professor Oct 18 '12 at 19:38
  • i'm not really sure what REST is supposed to represent in regards to music, is it supposed to be a pause? if so, you wouldn't want to play anything for those since they're out of range, so you could just do `else: pass` or something to that effect – Mike Corcoran Oct 18 '12 at 20:54
  • Yes, it is a pause. I could try pass, but I think I'm just going to time it since the rests are only at the beginning and end of the program and I'm just trying to play a "duet" with the program. – The Professor Oct 18 '12 at 20:58
0

From the winsound documentation:

The frequency parameter specifies frequency, in hertz, of the sound, and must be in the range 37 through 32,767.

The output of myro.makeSong isn't a list of frequencies, it's a list of notes. You'll need to do a lookup. http://wiki.roboteducation.org/Song_File_Format

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622