-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound.clj
36 lines (31 loc) · 923 Bytes
/
sound.clj
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
(ns quip.sound
(:require [clojure.java.io :as io])
(:import javax.sound.sampled.AudioSystem
javax.sound.sampled.Clip
javax.sound.sampled.DataLine$Info))
(defonce ^:dynamic *music* (atom nil))
(defn play
([sound]
(play sound false))
([sound loop?]
(let [input-stream (io/input-stream (io/resource (str "sound/" sound)))
audio-stream (AudioSystem/getAudioInputStream input-stream)
audio-format (.getFormat audio-stream)
audio-info (DataLine$Info. Clip audio-format)
audio-clip (cast Clip (AudioSystem/getLine audio-info))]
(.open audio-clip audio-stream)
(when loop?
(.loop audio-clip Clip/LOOP_CONTINUOUSLY))
(.start audio-clip)
audio-clip)))
(defn stop
[clip]
(.stop clip))
(defn stop-music
[]
(stop @*music*))
(defn loop-music
[track]
(when @*music*
(stop-music))
(reset! *music* (play track true)))