summaryrefslogtreecommitdiff
path: root/examples/audio.scm
blob: 4ea697f577000270805adfad11bf7f23cb9e3c2b (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
(use-modules (chickadee)
             (chickadee audio)
             (chickadee math vector)
             (chickadee graphics font)
             (ice-9 match))

(define effect #f)
(define music #f)
(define music-source #f)
(define playlist
  #("audio/the-forgotten-land.mp3"
    "audio/venus.wav"
    "audio/spooky-dungeon.ogg"))
(define playlist-index -1)
(define status-message "")
(define status-position (vec2 200.0 240.0))
(define instructions
  "VAMP (Very Advanced Music Player)

CONTROLS:

SPACE - play/pause
N - next music file
P - previous music file
L - play an explosion sound just because
Q - quit")
(define instructions-position (vec2 200.0 400.0))

(define (playlist-move n)
  (source-stop music-source)
  (set! playlist-index (modulo (+ playlist-index n) (vector-length playlist)))
  (let ((file-name (vector-ref playlist playlist-index)))
    (set! music (load-audio file-name #:mode 'stream))
    (set-source-audio! music-source music)
    (source-play music-source)
    (set! status-message (string-append "NOW PLAYING: " (basename file-name)))))

(define (load)
  (set! effect (load-audio "audio/explosion.wav"))
  (set! music-source (make-source))
  (playlist-move 1))

(define (update dt)
  (when (source-stopped? music-source)
    (playlist-move 1)))

(define (draw alpha)
  (when (>= playlist-index 0)
    (draw-text status-message status-position))
  (draw-text instructions instructions-position))

(define (key-press key modifiers repeat?)
  (match key
    ('q (abort-game))
    ('space (source-toggle music-source))
    ('l (audio-play effect))
    ('n
     (playlist-move 1))
    ('p
     (playlist-move -1))
    (_ #t)))

(run-game #:load load #:update update #:key-press key-press #:draw draw)