User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 391,922 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,722 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:
Oct 28th, 2005
Views: 4,891
You need the Python tkSnack module to play notes of a given frequency and duration on your external sound system (attached to the PC's sound card). The code snippet gives just a tiny hint about the capabilities of tkSnack. It works with Tkinter, normally part of the Python installation. The module comes to you courtesy of the researchers at KTH and the University of Stockholm in Sweden. More information is at their website http://www.speech.kth.se/snack/
python Syntax
  1. # play a note of a given frequency and duration via the PC sound card
  2. # needs Python module tkSnack developed at KTH in Stockholm, Sweden
  3. # free download: snack229-py.zip
  4. # from: http://www.speech.kth.se/snack/
  5. # on my system I copied tkSnack.py to D:\Python24\Lib and the folder snacklib to D:\Python24\tcl
  6. # tested with Python24 vegaseat 28oct2005
  7.  
  8. """
  9. Frequency Note Table:
  10. Frequency Note MIDI#
  11. 27.5000 A0 21
  12. 29.1352 A#0 22
  13. 30.8677 B0 23
  14. 32.7032 C1 24
  15. 34.6478 C#1 25 # C#1 = C1 * 1.059463094
  16. 36.7081 D1 26 # 1.059463094 = 12th root of 2
  17. 38.8909 D#1 27
  18. 41.2034 E1 28
  19. 43.6535 F1 29
  20. 46.2493 F#1 30
  21. 48.9994 G1 31
  22. 51.9131 G#1 32
  23. 55.0000 A1 33
  24. 58.2705 A#1 34
  25. 61.7354 B1 35
  26. 65.4064 C2 36
  27. 69.2957 C#2 37
  28. 73.4162 D2 38
  29. 77.7817 D#2 39
  30. 82.4069 E2 40
  31. 87.3071 F2 41
  32. 92.4986 F#2 42
  33. 97.9989 G2 43
  34. 103.8262 G#2 44
  35. 110.0000 A2 45
  36. 116.5409 A#2 46
  37. 123.4708 B2 47
  38. 130.8128 C3 48
  39. 138.5913 C#3 49
  40. 146.8324 D3 50
  41. 155.5635 D#3 51
  42. 164.8138 E3 52
  43. 174.6141 F3 53
  44. 184.9972 F#3 54
  45. 195.9977 G3 55
  46. 207.6523 G#3 56
  47. 220.0000 A3 57
  48. 233.0819 A#3 58
  49. 246.9417 B3 59
  50. 261.6256 C4 60
  51. 277.1826 C#4 61
  52. 293.6648 D4 62
  53. 311.1270 D#4 63
  54. 329.6276 E4 64
  55. 349.2282 F4 65
  56. 369.9944 F#4 66
  57. 391.9954 G4 67
  58. 415.3047 G#4 68
  59. 440.0000 A4 69
  60. 466.1638 A#4 70
  61. 493.8833 B4 71
  62. 523.2511 C5 72
  63. 554.3653 C#5 73
  64. 587.3295 D5 74
  65. 622.2540 D#5 75
  66. 659.2551 E5 76
  67. 698.4565 F5 77
  68. 739.9888 F#5 78
  69. 783.9909 G5 79
  70. 830.6094 G#5 80
  71. 880.0000 A5 81
  72. 932.3275 A#5 82
  73. 987.7666 B5 83
  74. 1046.5023 C6 84
  75. 1108.7305 C#6 85
  76. 1174.6591 D6 86
  77. 1244.5079 D#6 87
  78. 1318.5102 E6 88
  79. 1396.9129 F6 89
  80. 1479.9777 F#6 90
  81. 1567.9817 G6 91
  82. 1661.2188 G#6 92
  83. 1760.0000 A6 93
  84. 1864.6550 A#6 94
  85. 1975.5332 B6 95
  86. 2093.0045 C7 96
  87. 2217.4610 C#7 97
  88. 2349.3181 D7 98
  89. 2489.0159 D#7 99
  90. 2637.0205 E7 100
  91. 2793.8259 F7 101
  92. 2959.9554 F#7 102
  93. 3135.9635 G7 103
  94. 3322.4376 G#7 104
  95. 3520.0000 A7 105
  96. 3729.3101 A#7 106
  97. 3951.0664 B7 107
  98. 4186.0090 C8 108
  99. """
  100.  
  101. import Tkinter
  102. import tkSnack
  103.  
  104. def setVolume(volume=50):
  105. """set the volume of the sound system"""
  106. if volume > 100:
  107. volume = 100
  108. elif volume < 0:
  109. volume = 0
  110. tkSnack.audio.play_gain(volume)
  111.  
  112. def playNote(freq, duration):
  113. """play a note of freq (hertz) for duration (seconds)"""
  114. snd = tkSnack.Sound()
  115. filt = tkSnack.Filter('generator', freq, 30000, 0.0, 'sine', int(11500*duration))
  116. snd.stop()
  117. snd.play(filter=filt, blocking=1)
  118.  
  119. def soundStop():
  120. """stop the sound the hard way"""
  121. try:
  122. root = root.destroy()
  123. filt = None
  124. except:
  125. pass
  126.  
  127.  
  128. root = Tkinter.Tk()
  129.  
  130. # have to initialize the sound system, required!!
  131. tkSnack.initializeSnack(root)
  132. # set the volume of the sound system (0 to 100%)
  133. setVolume(30)
  134. # play a note of requency 440 hertz (A4) for a duration of 5 seconds
  135. playNote(440, 5)
  136. # play a note of requency 261.6 hertz (C4) for a duration of 5 seconds
  137. playNote(261.6, 5)
  138. # optional
  139. soundStop()
  140.  
  141. root.withdraw()
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 8:16 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC