Author Topic: Mantel Clock with hourly chime song without extra files  (Read 2840 times)

0 Members and 1 Guest are viewing this topic.

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
Re: Mantel Clock with hourly chime song without extra files
« Reply #15 on: January 04, 2021, 06:54:33 am »
I had fun modifying this. 

1) Clock continues to tick while playing sound.
2) Motion less like a electric clock (jump vs slide).
3) Markers at 5,10,15, etc. highlighted.
4) Printing of III, IV, and V offset.
5) PAINT used intead of lengthy subroutine.
6) Used _D2R function.
7) All hands different lengths.
8) Shorter code.

The colors SHOULD be _UNSIGNED LONG, but for short code like this that isn't doing comparisons, it doesn't matter.

If you wonder why I do VAL(string$) instead of just plunk in a number, it's because I like things to line up.

Code: QB64: [Select]
  1. _TITLE "Mantel Clock - Press Space Bar to play hour chimes at anytime."
  2. DIM black AS LONG, brown AS LONG, green AS LONG, hcol AS LONG, mcol AS LONG, scol AS LONG, c AS LONG
  3.  
  4. SCREEN _NEWIMAGE(800, 600, 32)
  5. DO: LOOP UNTIL _SCREENEXISTS '                          good practice
  6. _DELAY .3 '                                             because the move may not work without it
  7. _SCREENMOVE _MIDDLE '                                   center screen in desktop
  8.  
  9. cx = _WIDTH \ 2 '                                       clock center x
  10. cy = _HEIGHT \ 2 '                                      clock center y
  11. black = _RGB32(0)
  12. brown = _RGB32(183, 139, 100) '                         clock face and trim
  13. green = _RGB32(0, 55, 0) '                              clock bulk
  14. hcol = black '                                          hour hand color
  15. mcol = black '                                          minute hand color
  16. scol = _RGB32(255) '                                    second hand color
  17. '     D#     B      C#     F#            F#     C#     D#     B             D#     C3     B      F#            F#     C#     D#     B
  18. s$ = "311.13 246.94 277.18 185     0     185    277.18 311.13 246.94 0      311.13 277.18 246.94 185    0      185    277.18 311.13 246.94 0"
  19.  
  20.     a = _KEYHIT
  21.     IF a = 27 THEN SYSTEM '                             Esc ends program
  22.     GOSUB clock
  23.     IF NOT (((m = 0) AND (s = 0)) OR (a = 32)) THEN _CONTINUE
  24.  
  25.     FOR notes = 0 TO 19
  26.         note = VAL(MID$(s$, notes * 7 + 1, 6))
  27.         ttt = 0
  28.         DO
  29.             DO WHILE _SNDRAWLEN < 0.5 '                 you may wish to adjust this
  30.                 sample = SIN(ttt * note * ATN(1) * 8) ' 340Hz sine wave (ttt * 440 * 2p)
  31.                 sample = sample * EXP(-ttt * 3) '       fade out eliminates clicks after sound
  32.                 _SNDRAW sample
  33.                 ttt = ttt + 1 / _SNDRATE '              sound card sample frequency determines time
  34.             LOOP
  35.         LOOP WHILE ttt < 1 '                            play for 1 second
  36.         DO: LOOP UNTIL _SNDRAWLEN = 0 '                 finish any left over queued sound!
  37.         GOSUB clock
  38.     NEXT notes
  39.  
  40.     IF h = 0 THEN h = 12
  41.     FOR chimes = 1 TO h
  42.         ttt = 0
  43.         DO
  44.             DO WHILE _SNDRAWLEN < 0.1 '                 you may wish to adjust this
  45.                 sample = SIN(ttt * 240 * ATN(1) * 8) '  340Hz sine wave (ttt * 440 * 2p)
  46.                 sample = sample * EXP(-ttt * 3) '       fade out eliminates clicks after sound
  47.                 _SNDRAW sample
  48.                 ttt = ttt + 1 / _SNDRATE '              sound card sample frequency determines time
  49.             LOOP
  50.             GOSUB clock
  51.         LOOP WHILE ttt < 2 '                            play for 2 seconds
  52.         DO: LOOP UNTIL _SNDRAWLEN = 0 '                 finish any left over queued sound!
  53.     NEXT chimes
  54.  
  55. clock:
  56. CIRCLE (cx, cy), cy, green
  57. PAINT (cx, cy), green, green
  58. LINE (100, cy)-(700, 600), green, BF
  59. CIRCLE (cx, cy), 200, _RGB32(1)
  60. PAINT (cx, cy), brown, _RGB32(1)
  61. FOR lines = .2 TO 10 STEP .2
  62.     CIRCLE (cx, cy), 250 + lines, brown, 2 * _PI, _PI
  63. NEXT lines
  64. LINE (140, cy)-(150, 760), brown, BF
  65. LINE (650, cy)-(660, 760), brown, BF
  66. LINE (150, _HEIGHT - 10)-(650, _HEIGHT), brown, BF
  67. COLOR black, brown
  68. FOR sc = 1 TO 60
  69.     ss = (60 - sc) * 6 + 180
  70.     x2 = cx + 140 * SIN(_D2R(ss))
  71.     y2 = cy + 140 * COS(_D2R(ss))
  72.     CIRCLE (x2, y2), 3, _RGB32(230)
  73.     IF (sc MOD 5) = 0 THEN
  74.         PAINT (x2, y2), black, _RGB32(230)
  75.         z$ = MID$("I   II  III IV  V   VI  VII VIIIIX  X   XI  XII  ", (sc \ 5 - 1) * 4 + 1, 4)
  76.         x3 = (cx - VAL("10")) + 160 * SIN(_D2R(ss))
  77.         y3 = (cy - VAL("05")) + 160 * COS(_D2R(ss))
  78.         x3 = x3 - (sc = 30) * 3 + (sc = 40) * 10 - (sc = 50) * 8
  79.         _PRINTSTRING (x3, y3), RTRIM$(z$)
  80.     END IF
  81. NEXT sc
  82. COLOR , black
  83.  
  84. s = VAL(MID$(TIME$, 7, 2))
  85. m = VAL(MID$(TIME$, 4, 2))
  86. h = VAL(MID$(TIME$, 1, 2))
  87. h = h + (h > 12) * 12 + m / 60
  88. FOR hand = 1 TO 3
  89.     IF hand = 1 THEN t = h * VAL("30"): c = hcol: handlen = 80
  90.     IF hand = 2 THEN t = m * VAL("06"): c = mcol: handlen = 100
  91.     IF hand = 3 THEN t = s * VAL("06"): c = scol: handlen = 120
  92.     x = cx + handlen * COS(_D2R(t - 90))
  93.     y = cy + handlen * SIN(_D2R(t - 90))
  94.     FOR b = -5 TO 5 STEP .1
  95.         LINE (cx + b, cy)-(x, y), c
  96.         LINE (cx, cy + b)-(x, y), c
  97.     NEXT b
  98. NEXT hand
  99.  
  100. CIRCLE (cx, cy), 8, _RGB32(1)
  101. PAINT (cx, cy), black, _RGB32(1)
  102.  
« Last Edit: January 04, 2021, 07:03:51 am by Richard Frost »
It works better if you plug it in.