<commit_before>(ns overtone.sc.buffer
  (:use [clojure.java.io :only [file]]
        [overtone.libs event]
        [overtone.sc server info defaults]
        [overtone.sc.machinery allocator]
        [overtone.sc.machinery.server connection comms]
        [overtone.sc server info]
        [overtone.helpers audio-file lib file]
        [overtone.sc.util :only [id-mapper]]))

(defrecord BufferInfo [id size n-channels rate n-samples rate-scale duration])

(defn buffer-info
  "Fetch the information for buffer associated with buf-id (either an
  integer or an associative with an :id key). Synchronous.

  Information returned is as follows:

  :size       - number of frames in the buffer
  :n-channels - number of audio channels stored in the buffer
  :rate       - rate of the buffer (typical rate is 44100 samples per
                second)
  :n-samples  - total number of samples in the buffer (* size n-channels)
  :rate-scale - rate to specify in order to play the buffer correctly
                according
                to the server's sample rate (/ rate (server-sample-rate))
  :duration   - duration of the buffer in seconds
  :id         - unique id for the buffer"
  [buf-id]
  (let [buf-id (id-mapper buf-id)
        prom   (recv "/b_info" (fn [msg]
                                 (= buf-id (first (:args msg)))))]
    (with-server-sync #(snd "/b_query" buf-id))
    (let [[id n-frames n-chans rate] (:args (deref! prom))
          server-rate                (server-sample-rate)
          n-samples                  (* n-frames n-chans)
          rate-scale                 (when (> server-rate 0)
                                       (/ rate server-rate))
          duration                   (when (> rate 0)
                                       (/ n-frames rate))]

      (map->BufferInfo
       {:id id
        :size n-frames
        :n-channels n-chans
        :rate rate
        :n-samples n-samples
        :rate-scale rate-scale
        :duration duration}))))

(defrecord Buffer [id size n-channels rate allocated-on-server])

(defn buffer
  "Synchronously allocate a new zero filled buffer for storing audio
  data with the specified size and num-channels."
  ([size] (buffer size 1))
  ([size num-channels]
     (let [id   (with-server-self-sync
                  (fn [uid]
                    (alloc-id :audio-buffer
                              1
                              (fn [id]
                                (snd "/b_alloc" id size num-channels)
                                (server-sync uid)))))
           info (buffer-info id)]

       (map->Buffer
        (assoc info
          :allocated-on-server (atom true))))))

(defrecord BufferFile [id size n-channels rate allocated-on-server path])

(defn buffer-alloc-read
  "Synchronously allocates a buffer with the same number of channels as
  the audio file given by 'path'. Reads the number of samples
  requested ('n-frames') into the buffer, or fewer if the file is
  smaller than requested. Reads sound file data from the given starting
  frame ('start') in the file. If 'n-frames' is less than or equal to
  zero, the entire file is read.

  May not be scheduled with the at macro. All inner OSC calls are sent
  immediately."
  ([path]
     (buffer-alloc-read path 0 -1))<commit_msg>Fix bug in buffer when passing a float as the size param.<commit_after>(ns overtone.sc.buffer
  (:use [clojure.java.io :only [file]]
        [overtone.libs event]
        [overtone.sc server info defaults]
        [overtone.sc.machinery allocator]
        [overtone.sc.machinery.server connection comms]
        [overtone.sc server info]
        [overtone.helpers audio-file lib file]
        [overtone.sc.util :only [id-mapper]]))

(defrecord BufferInfo [id size n-channels rate n-samples rate-scale duration])

(defn buffer-info
  "Fetch the information for buffer associated with buf-id (either an
  integer or an associative with an :id key). Synchronous.

  Information returned is as follows:

  :size       - number of frames in the buffer
  :n-channels - number of audio channels stored in the buffer
  :rate       - rate of the buffer (typical rate is 44100 samples per
                second)
  :n-samples  - total number of samples in the buffer (* size n-channels)
  :rate-scale - rate to specify in order to play the buffer correctly
                according
                to the server's sample rate (/ rate (server-sample-rate))
  :duration   - duration of the buffer in seconds
  :id         - unique id for the buffer"
  [buf-id]
  (let [buf-id (id-mapper buf-id)
        prom   (recv "/b_info" (fn [msg]
                                 (= buf-id (first (:args msg)))))]
    (with-server-sync #(snd "/b_query" buf-id))
    (let [[id n-frames n-chans rate] (:args (deref! prom))
          server-rate                (server-sample-rate)
          n-samples                  (* n-frames n-chans)
          rate-scale                 (when (> server-rate 0)
                                       (/ rate server-rate))
          duration                   (when (> rate 0)
                                       (/ n-frames rate))]

      (map->BufferInfo
       {:id id
        :size n-frames
        :n-channels n-chans
        :rate rate
        :n-samples n-samples
        :rate-scale rate-scale
        :duration duration}))))

(defrecord Buffer [id size n-channels rate allocated-on-server])

(defn buffer
  "Synchronously allocate a new zero filled buffer for storing audio
  data with the specified size and num-channels. Size will be
  automatically floored and converted to a Long - i.e. 2.7 -> 2"
  ([size] (buffer size 1))
  ([size num-channels]
     (let [size (long size)
           id   (with-server-self-sync
                  (fn [uid]
                    (alloc-id :audio-buffer
                              1
                              (fn [id]
                                (snd "/b_alloc" id size num-channels)
                                (server-sync uid)))))
           info (buffer-info id)]

       (map->Buffer
        (assoc info
          :allocated-on-server (atom true))))))

(defrecord BufferFile [id size n-channels rate allocated-on-server path])

(defn buffer-alloc-read
  "Synchronously allocates a buffer with the same number of channels as
  the audio file given by 'path'. Reads the number of samples
  requested ('n-frames') into the buffer, or fewer if the file is
  smaller than requested. Reads sound file data from the given starting
  frame ('start') in the file. If 'n-frames' is less than or equal to
  zero, the entire file is read.

  May not be scheduled with the at macro. All inner OSC calls are sent
  immediately."
  ([path]
     (buffer-alloc-read path 0 -1))
{'fraction_non_alphanumeric': 0.09487750556792873, 'fraction_numerical': 0.003711952487008166, 'mean_word_length': 1.8554472233997457, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5079453', 'n_tokens_mistral': 1943, 'n_tokens_neox': 1849, 'n_words': 708}