clj-audio icon indicating copy to clipboard operation
clj-audio copied to clipboard

Introduce `play-cycle-complete?`

Open vemv opened this issue 5 years ago • 0 comments

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:

https://github.com/budu/clj-audio/blob/b2450470c1b307184948dd1f35d6ceb85dfc7f2b/src/clj_audio/core.clj#L157-L162

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.

vemv avatar Dec 27 '18 22:12 vemv