aboutsummaryrefslogtreecommitdiff
path: root/sound.scm
blob: c78f6e7232647be9ac4e017e4431027a494545ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
(module downstroke-sound *
(import scheme
        (chicken base)
        (only srfi-1 for-each)
        downstroke-mixer)

(define *sound-registry* '())
(define *music* #f)

(define (init-audio!)
  (mix-open-audio! 44100 mix-default-format 2 512))

(define (load-sounds! sound-alist)
  (set! *sound-registry*
    (map (lambda (pair)
           (cons (car pair) (mix-load-chunk (cdr pair))))
         sound-alist)))

(define (play-sound sym)
  (let ((entry (assq sym *sound-registry*)))
    (when (and entry (cdr entry))
      (mix-play-channel -1 (cdr entry) 0))))

(define (load-music! path)
  (set! *music* (mix-load-mus path)))

(define (play-music! volume)
  (when *music*
    (mix-play-music *music* -1)
    (mix-volume-music (inexact->exact (round (* volume 128))))))

(define (stop-music!) (mix-halt-music))

(define (set-music-volume! volume)
  (mix-volume-music (inexact->exact (round (* volume 128)))))

(define (cleanup-audio!)
  (when *music*
    (mix-halt-music)
    (mix-free-music! *music*)
    (set! *music* #f))
  (for-each (lambda (pair) (mix-free-chunk! (cdr pair)))
            *sound-registry*)
  (set! *sound-registry* '())
  (mix-close-audio!)))