Skip to content

Introduce play-cycle-complete? #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/clj_audio/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,30 @@
(ref false)
)

(def play-cycle-complete?
"True if `play*` is not blocking due to I/O operations.
You can observe this variable for ensuring `play*` is not being used concurrently."
(ref true))

(defn play*
"Write the given audio stream bytes to the given source data line
using a buffer of the given size. Returns the number of bytes played."
[#^SourceDataLine source audio-stream buffer-size]
(let [buffer (byte-array buffer-size)]
(dosync (ref-set *playing* true))
(loop [cnt 0 total 0]
(dosync (ref-set play-cycle-complete? false))
(if (and (> cnt -1) @*playing*)
(do
(when (> cnt 0)
(.write source buffer 0 cnt))
(recur (.read audio-stream buffer 0 (alength buffer))
(recur (let [v (.read audio-stream buffer 0 (alength buffer))]
(dosync (ref-set play-cycle-complete? true))
v)
(+ total cnt)))
(dosync
(ref-set *playing* false)
(ref-set play-cycle-complete? true)
total)))))

(defn stop
Expand Down