<commit_before>(ns overtone.sc.buffer
  (:use [overtone.sc core allocator]))

;; ## Buffer functions
;;
; TODO: Look into multi-channel buffers.  Probably requires adding multi-id allocation
; support to the bit allocator too...
; size is in samples
(defn buffer
  "Allocate a new buffer for storing audio data."
  ([size] (buffer size 1))
  ([size num-channels]
     (let [id     (alloc-id :audio-buffer)
           ready? (atom false)]
       (on-done "/b_alloc" #(reset! ready? true))
       (snd "/b_alloc" id size num-channels)
       (with-meta {:id id
                   :size size
                   :ready? ready?}
         {:type ::buffer}))))

(defn buffer-ready?
  "Check whether a sample or a buffer has completed allocating and/or loading data."
  [buf]
  @(:ready? buf))

(defn sbuffer
  "Allocate a new buffer synchronously. Halts the current thread until the buffer has been succesfully allocated"
  ([size] (sbuffer size 1))
  ([size num-channels]
     (wait-until-booted)
     (let [buf (buffer size num-channels)]
       (while (not (buffer-ready? buf))
         (Thread/sleep 50)))
     buf))

(defn buffer? [buf]
  (isa? (type buf) ::buffer))

(defn- buf-or-id [b]
  (cond
    (buffer? b) (:id b)
    (number? b) b
    :default (throw (Exception. "Not a valid buffer: " b))))

(defn buffer-free
  "Free an audio buffer and the memory it was consuming."
  [buf]
  (snd "/b_free" (:id buf))
  (free-id :audio-buffer (:id buf))
  :done)

; TODO: Test me...<commit_msg>fix bug that managed to slip in<commit_after>(ns overtone.sc.buffer
  (:use [overtone.sc core allocator]))

;; ## Buffer functions
;;
; TODO: Look into multi-channel buffers.  Probably requires adding multi-id allocation
; support to the bit allocator too...
; size is in samples
(defn buffer
  "Allocate a new buffer for storing audio data."
  ([size] (buffer size 1))
  ([size num-channels]
     (let [id     (alloc-id :audio-buffer)
           ready? (atom false)]
       (on-done "/b_alloc" #(reset! ready? true))
       (snd "/b_alloc" id size num-channels)
       (with-meta {:id id
                   :size size
                   :ready? ready?}
         {:type ::buffer}))))

(defn buffer-ready?
  "Check whether a sample or a buffer has completed allocating and/or loading data."
  [buf]
  @(:ready? buf))

(defn sbuffer
  "Allocate a new buffer synchronously. Halts the current thread until the buffer has been succesfully allocated"
  ([size] (sbuffer size 1))
  ([size num-channels]
     (wait-until-booted)
     (let [buf (buffer size num-channels)]
       (while (not (buffer-ready? buf))
         (Thread/sleep 50))
       buf)))

(defn buffer? [buf]
  (isa? (type buf) ::buffer))

(defn- buf-or-id [b]
  (cond
    (buffer? b) (:id b)
    (number? b) b
    :default (throw (Exception. "Not a valid buffer: " b))))

(defn buffer-free
  "Free an audio buffer and the memory it was consuming."
  [buf]
  (snd "/b_free" (:id buf))
  (free-id :audio-buffer (:id buf))
  :done)

; TODO: Test me...
{'fraction_non_alphanumeric': 0.1409485637942552, 'fraction_numerical': 0.0026720106880427524, 'mean_word_length': 2.739076154806492, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7629271', 'n_tokens_mistral': 1009, 'n_tokens_neox': 970, 'n_words': 357}