Advertisement
Atdiy

SoundPal Object

Sep 21st, 2012
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.22 KB | None | 0 0
  1. '' Propeller SoundPAL Driver
  2. ''
  3. ''Copyright (C) 2007 Philip C. Pilgrim (PhiPi)
  4. ''
  5. ''This program is free software; you can redistribute it and/or modify
  6. ''it under the terms of the GNU General Public License, version 2, as
  7. ''published by the Free Software Foundation.
  8. ''
  9. ''This program is distributed in the hope that it will be useful,
  10. ''but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ''MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ''GNU General Public License for more details.
  13. ''
  14. ''You should have received a copy of the GNU General Public License
  15. ''along with this program; if not, write to the Free Software
  16. ''Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. ''
  18. ''CONTACT
  19. ''
  20. '' propeller AT phipi DOT com
  21. ''
  22. ''VERSION HISTORY
  23. ''
  24. '' 0.01 alpha: released 2007.11.23
  25. ''
  26.  
  27. CON
  28.  
  29. '' The constants in this block can either be used in the calling program from here
  30. '' (e.g. sp#play, sp#rept, etc.), or copied and pasted into the program that uses
  31. '' them, so they can be used without the "sp#".
  32.  
  33. 'Commands
  34.  
  35. play = $01 'Play the segment at the following address ($01-$FF).
  36. rept = $02 'Begin a repeat block.
  37. ' Next arg is repeat count (1-254; 255 = endlessly).
  38. again = $03 'End the repeat block.
  39.  
  40. 'Playing styles.
  41.  
  42. marcato = $04 'Normal, separated notes.
  43. staccato = $05 'Very short notes.
  44. legato = $06 'Connected notes.
  45. glissando = $07 'Connected, sliding notes.
  46.  
  47. 'Tempos
  48.  
  49. tmp0 = $08 'Quarter note = 133 beats/min.
  50. tmp1 = $09 'Quarter note = 266 beats/min. (default)
  51. tmp2 = $0A 'Quarter note = 532 beats/min.
  52. tmp3 = $0B 'Quarter note = 1064 beats/min.
  53.  
  54. 'Octaves
  55.  
  56. oct0 = $0C 'A_0 = 110Hz
  57. oct1 = $0D 'A_0 = 220Hz
  58. oct2 = $0E 'A_0 = 440Hz (default)
  59. oct3 = $0F 'A_0 = 880Hz
  60.  
  61. 'Slur or tie, connecting two subsequent notes only.
  62.  
  63. slur = $10
  64.  
  65. 'Glissando rates: gl1 is fastest; gl15 is slowest.
  66.  
  67. gl1 = $11
  68. gl2 = $12
  69. gl3 = $13
  70. gl4 = $14
  71. gl5 = $15
  72. gl6 = $16
  73. gl7 = $17
  74. gl8 = $18
  75. gl9 = $19
  76. gl10 = $1A
  77. gl11 = $1B
  78. gl12 = $1C
  79. gl13 = $1D
  80. gl14 = $1E
  81. gl15 = $1F
  82.  
  83. 'Notes. When unmodified by addition of duration, these are all quarter notes.
  84.  
  85. ZZZ = $80 'Rest
  86. C_0 = $81 'Low C natural. (Middle C in oct2.)
  87. Cs0 = $82 'Low C sharp.
  88. Df0 = $82 'Low D flat.
  89. D_0 = $83 'Low D natural.
  90. Ds0 = $84 'Low D sharp.
  91. Ef0 = $84 'Low E flat.
  92. E_0 = $85 'Low E natural.
  93. F_0 = $86 'Low F natural.
  94. Fs0 = $87 'Low F sharp.
  95. Gf0 = $87 'Low G flat.
  96. G_0 = $88 'Low G natural.
  97. Gs0 = $89 'Low G sharp.
  98. Af0 = $89 'Low A flat.
  99. A_0 = $8A 'Low A natural.
  100. As0 = $8B 'Low A sharp.
  101. Bf0 = $8B 'Low B flat.
  102. B_0 = $8C 'Low B natural.
  103. C_1 = $8D 'Medium C natural. (Middle C in oct1.)
  104. Cs1 = $8E 'Medium C sharp.
  105. Df1 = $8E 'Medium D flat.
  106. D_1 = $8F 'Medium D natural.
  107. Ds1 = $90 'Medium D sharp.
  108. Ef1 = $90 'Medium E flat.
  109. E_1 = $91 'Medium E natural.
  110. F_1 = $92 'Medium F natural.
  111. Fs1 = $93 'Medium F sharp.
  112. Gf1 = $93 'Medium G flat.
  113. G_1 = $94 'Medium G natural.
  114. Gs1 = $95 'Medium G sharp.
  115. Af1 = $95 'Medium A flat.
  116. A_1 = $96 'Medium A natural.
  117. As1 = $97 'Medium A sharp.
  118. Bf1 = $97 'Medium B flat.
  119. B_1 = $98 'Medium B natural.
  120. C_2 = $99 'High C natural. (Middle C in oct0.)
  121. Cs2 = $9A 'High C sharp.
  122. Df2 = $9A 'High D flat.
  123. D_2 = $9B 'High D natural.
  124. Ds2 = $9C 'High D sharp.
  125. Ef2 = $9C 'High E flat.
  126. E_2 = $9D 'High E natural.
  127. F_2 = $9E 'High F natural.
  128. Fs2 = $9F 'High F sharp.
  129. Gf2 = $9F 'High G flat.
  130.  
  131. 'Duration modifiers. Add value to note to change duration.
  132.  
  133. s = $20-$80 'Sixteenth note.
  134. e = $40-$80 'Eighth note.
  135. de = $60-$80 'Dotted eighth note.
  136. q = $80-$80 'Quarter note.
  137. dq = $C0-$80 'Dotted quarter note.
  138. h = $A0-$80 'Half note.
  139. dh = $E0-$80 'Dotted half note.
  140.  
  141. 'Substitute end-of-string (high G-flat).
  142.  
  143. EOF = $FF
  144.  
  145. 'Addresses of canned sequences.
  146.  
  147. charge = $40 'Charge!
  148. taps = $44 'Taps
  149. reveille = $5D 'Reveille
  150. firstpost = $7D 'First Post (horse race bugle call)
  151. intro = $8D 'Doo-doot doo doot doot DOOT
  152. nyah = $93 'Nyah nyah nyah nyah NYAH nyah!
  153. dead = $97 'Funeral dirge
  154. batthymn = $9D 'Battle Hymn of the Republic
  155. dixie = $A5 'Dixie
  156. cucaracha = $AC 'La Cucaracha
  157. popweasel = $AF 'Pop! Goes the Weasel
  158. marsell = $B3 'Marsellaise
  159. rulebrit = $B9 'Rule Brittania
  160. matilda = $C0 'Walzing Matilda
  161. kradoucha = $C6 'Kradoutcha ("There's a place in France...")
  162. wedding = $CD 'Wedding March
  163. ode2joy = $D2 'Ode to Joy
  164. dudu = $DA 'Du, Du Liegst Mir im Herzen
  165. notme = $E1 'Rude sound
  166. uhoh = $E5 'Uh oh!
  167. siren = $E8 'American style siren. Infinite loop: reset to exit.
  168. phone = $EE 'Rings once.
  169. whistle = $F3 'Wolf whistle.
  170. cricket = $FA 'Play using oct3 for cricket; oct0 for frog.
  171.  
  172.  
  173. VAR
  174.  
  175. long BaudClock
  176. long IOPin
  177.  
  178. PUB start(pin)
  179.  
  180. '' Selects the IO pin and sets the output bit for that pin low. DIRA for that pin stays
  181. '' low, since I/O is open drain. DIRA is thus controlled by the output routine.
  182. '' Waits for pin to go high before returning, signifying that SoundpPAL is ready.
  183.  
  184. BaudClock := clkfreq / 9600 'Set baurate to 9600.
  185. IOPin := pin 'Set IOPin variable.
  186. dira[IOPin]~ 'Tristate IOPin.
  187. outa[IOPin]~ 'Set driven state of IOPin low.
  188. repeat until ina[IOPin] 'Wait for SoundPAL to come out of hardware reset.
  189. reset 'Soft reset the soundPAL.
  190. return true
  191.  
  192. PUB reset | time
  193.  
  194. '' Reset the SoundPAL, halting any sequences that are playing.
  195.  
  196. dira[IOPin]~~ 'Pull IOPin low.
  197. time := cnt
  198. repeat until cnt > time + clkfreq / 133 'Wait 7.5ms
  199. dira[IOPin]~ 'Release IOPin.
  200.  
  201. PUB sendstr(str_addr) | char
  202.  
  203. '' Send string to the SoundPAL. Because the SoundPAL's EOF and Spin's string
  204. '' terminator are both the same character (0), the dotted-half high G-flat ($FF)
  205. '' has been sacrificed to stand in as a substitute EOF for the SoundPAL.
  206.  
  207. repeat strsize(str_addr) 'Iterate over the string.
  208. if ((char := byte[str_addr++]) == $ff) ' Substitute EOF?
  209. char~ ' Yes: Replace with real EOF (0).
  210. sendbyte(char) ' Send the character.
  211.  
  212. PUB waitdone | time, busy
  213.  
  214. '' Wait for the current sequence to finish.
  215.  
  216. repeat 'Loop until not busy...
  217. sendbyte("?") ' Send enquiry.
  218. time := cnt ' Set time.
  219. repeat
  220. busy := ina[IOPin] ' Look for start bit (NOT busy) from SoundPAL.
  221. while busy and cnt - time < clkfreq / 500 'Loop while still busy and elapsed time < 2ms.
  222. while busy
  223.  
  224. PUB sendbyte(txbyte) | time
  225.  
  226. '' Send a single character with two stops bits.
  227.  
  228. txbyte := (txbyte | $300) << 2 'Set up character with start and stop bits.
  229. time := cnt 'Set initial time.
  230. repeat 11 'Send 11 bits total.
  231. waitcnt(time += BaudClock) 'Wait for next bit time.
  232. dira[IOPin] := ((txbyte >>= 1) & 1 == 0)'Send next bit via dira.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement