Skip to content
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

Introduce play-cycle-complete? #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vemv
Copy link

@vemv vemv commented Dec 27, 2018

It can be hard to ensure play* never is running simultaneously from multiple threads (which would result in scrambled sound output).

The following wouldn't work:

(while @*playing* 
  (Thread/sleep default-sleep-duration))

...because it is possible that *playing* is set to false and yet play* is still engaged in IO operations:

(if (and (> cnt -1) @*playing*)
(do
(when (> cnt 0)
(.write source buffer 0 cnt))
(recur (.read audio-stream buffer 0 (alength buffer))
(+ total cnt)))

In the snippet above, .write and .read are blocking calls, so probably you can see there's a chance for race conditions.

This PR introduces a finer-grained variable, so one can:

(while (or @*playing* (not @play-cycle-complete?))
  (Thread/sleep default-sleep-duration))

Of course, one could prevent concurrent calls from the consumer side, but it also seems nice to provide a generic opt-in mechanism.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant