944,149 Members | Top Members by Rank

Ad:
  • Python Code Snippet
  • Views: 13810
  • Python RSS
2

Play a Musical Note (Python)

by on Oct 28th, 2005
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 Code Snippet (Toggle Plain Text)
  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()
Comments on this Code Snippet
Sep 12th, 2010
0

Re: Play a Musical Note (Python)

Firs at all, I'm sorry: my English is not good. I'm using Ubuntu Linux 10.04 and downloaded tkSnack without problem but, after running your code, appears an error: "UNRESOLVED IMPORT: tkSnack". Please, What should I do?
Newbie Poster
Alex Sarria is offline Offline
2 posts
since Sep 2010
Sep 13th, 2010
0

Re: Play a Musical Note (Python)

On Linux you have to install tkSnack into directories that Python is looking into.
Python Syntax (Toggle Plain Text)
  1. # show the system path for Python = PYTHONPATH
  2.  
  3. import sys
  4. print sys.path
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
May 12th, 2011
0

Re: Play a Musical Note (Python)

I went and looked up the tksnack from the website posted and the latest version is for Python 2.2. Considering Python latest is 3.1, will this library still work? I'm very interested in this for my final project and would prefer to use this or something similar / more up to date.

Thanks.
Newbie Poster
Layra is offline Offline
2 posts
since May 2011
May 12th, 2011
1

Re: Play a Musical Note (Python)

Click to Expand / Collapse  Quote originally posted by Layra ...
I went and looked up the tksnack from the website posted and the latest version is for Python 2.2. Considering Python latest is 3.1, will this library still work? I'm very interested in this for my final project and would prefer to use this or something similar / more up to date.

Thanks.
Apparently, a very small part of snack is written in python (20k) and this part contains basic interaction with tkinter, so if it does not work with python 3, it should be relatively easy to adapt it. On the snack site http://www.speech.kth.se/snack/ there is a link to another program called WaveSurfer http://www.speech.kth.se/wavesurfer/ which uses snack and which last release was 1 month ago. Wavesurfer is described in this wikipedia page http://en.wikipedia.org/wiki/WaveSurfer, and apparently wavesurfer accepts plugins (in python ? it's worth exploring this direction).
Anyway, the existence of wavesurfer proves that snack still works in 2011.
Last edited by Gribouillis; May 12th, 2011 at 1:06 pm.
Posting Maven
Gribouillis is offline Offline
2,656 posts
since Jul 2008
May 12th, 2011
1

Re: Play a Musical Note (Python)

See:
"Getting tkSnack sound module to work with Python3"
http://www.daniweb.com/software-deve...on/code/357467
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
May 12th, 2011
0

Re: Play a Musical Note (Python)

Click to Expand / Collapse  Quote originally posted by vegaseat ...
See:
"Getting tkSnack sound module to work with Python3"
http://www.daniweb.com/software-deve...on/code/357467
Yep, I just found this link -- thank's for the confirmation! Now only to get the environment setup correctly and I'll be all set. Thanks a bunch to both of you!
Newbie Poster
Layra is offline Offline
2 posts
since May 2011
Message:
Previous Thread in Python Forum Timeline: Online graph plotting using python
Next Thread in Python Forum Timeline: Properly encoding filenames and paths in python





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC