我一直得到ValueError:在Winsound的Python上,频率必须在37到32767之间

时间:2012-10-18 19:18:56

标签: python python-2.4 traceback myro

这是我的代码:

导入winsound 来自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()

当我尝试运行时,我不断收到错误:

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

我做错了什么,我需要将其更改为什么?请具体说明。

2 个答案:

答案 0 :(得分:3)

winsound.beep函数只是windows api beep函数的包装器。 windows函数需要第一个参数(频率),介于37和32767之间。我怀疑该范围之外的任何频率超出人类听觉范围。它也可能是那种方式,因为这个功能的旧声卡仅支持该范围。

你正在调用winsound.beep()并且返回的任何int(注释)超出了该范围。你应该在打电话之前检查音符是否有效。

note = int(play[0])
if note > 37 and note < 32767:
    winsound.Beep(note, int(duration*2000))
else:
    print("error in input")

答案 1 :(得分:0)

来自the winsound documentation

  

频率参数指定声音的频率(以赫兹为单位),且必须在37到32,767的范围内。

myro.makeSong的输出不是频率列表,而是一个音符列表。你需要进行查找。 http://wiki.roboteducation.org/Song_File_Format