rsitunes.ss
(module rsitunes mzscheme
  (require (lib "process.ss")
           (lib "plt-match.ss"))
  (provide (all-defined))
  
  (define-struct interval (id frequency duration))
  
  (define (rsi-player start-interval stop-interval given-intervals)    
    (let loop ([intervals
                (map (let ([start (current-seconds)])
                       (lambda (i)
                         (list i start)))
                     given-intervals)])
        (apply sync
               (map (match-lambda
                      [(list i last-strike)
                      (handle-evt
                       (alarm-evt (* 1000
                                     (+ last-strike
                                        (interval-frequency i))))
                       (lambda (_)
                         (printf "~a: ~a~n" (interval-id i) (current-seconds))
                         (start-interval)
                         (sleep (interval-duration i))
                         (stop-interval)
                         (loop
                          (map (match-lambda
                                 ; Reset last strikes to credit for the duration sleep
                                 [(list an-i an-last-strike)
                                  (if (eq? an-i i)
                                      (list an-i (current-seconds))
                                      (list an-i (+ an-last-strike
                                                    (interval-duration i))))])
                               intervals))))])
                    intervals))))
  
  (define (workrave micro-freq micro-duration
                    macro-freq macro-duration)
    (list
     (make-interval 'micro (* 60 micro-freq) micro-duration)
     (make-interval 'macro (* 60 macro-freq) (* 60 macro-duration))))
 
  (define itunes-play
    (lambda ()
       (system* "/usr/bin/osascript"
                "-e"
                "tell application \"iTunes\" to play")))
  (define itunes-pause
     (lambda ()
       (system* "/usr/bin/osascript"
                "-e"
                "tell application \"iTunes\" to pause")))
  
  (define (rsitunes intervals)
    (rsi-player
     itunes-pause
     itunes-play
     intervals)))