<commit_before>(ns overtone.inst.sampled-piano
  (:use [overtone.core]))

(defn- registered-samples []
  (->> (registered-assets ::MISStereoPiano)
       (filter #(.contains % "LOUD"))))

(defn- download-samples []
  (->> (freesound-searchm [:id] "LOUD" :f "pack:MISStereoPiano")
       (map freesound-path)))

(defonce piano-samples
  (doall (map load-sample (or (registered-samples)
                              (download-samples)))))

(defn- note-index
  "Returns a map of midi-note values [0-127] to buffer id's. Uses the provided
  note-fn to determine the midi-note value of a buffer."
  [buffers note-fn]
  (reduce (fn [index buf]
            (let [note (note-fn buf)
                  id   (-> buf :id)]
              (assoc index note id)))
          {}
          buffers))

(defn- piano-note-fn [buf] (-> buf :name match-note :midi-note))

;; Silent buffer used to fill in the gaps.
(defonce ^:private silent-buffer (buffer 0))

(defonce ^:private index-buffer
  (let [tab (note-index piano-samples piano-note-fn)
        buf (buffer 128)]
    (buffer-fill! buf (:id silent-buffer))
    (doseq [[idx val] tab]
      (buffer-set! buf idx val))
    buf))

(definst sampled-piano
  [note 60 level 1 rate 1 loop? 0
   attack 0 decay 1 sustain 1 release 0.1 curve -4 gate 1]
  (let [buf (index:kr (:id index-buffer) note)
        env (env-gen (adsr attack decay sustain release level curve)
                     :gate gate
                     :action FREE)]
    (* env (scaled-play-buf 2 buf :level level :loop loop? :action FREE))))
<commit_msg>Fix up sampled-piano to download samples if not previously cached.<commit_after>(ns overtone.inst.sampled-piano
  (:use [overtone.core]))

(defn- registered-samples
  "Fetch piano samples from the asset store (returns empty list if not
  previously downloaded"
  []
  (filter #(.contains % "LOUD")
          (registered-assets ::MISStereoPiano)))

(defn- download-samples
  "Download piano samples from freesound and store them in the asset
  store."
  []
  (map freesound-path
       (freesound-searchm [:id] "LOUD" :f "pack:MISStereoPiano")))

(defn- get-samples
  "Either fetch samples from the registered store or download them if
  necessary"
  []
  (let [samples (registered-samples)]
    (if (empty? samples)
      (download-samples)
      samples)))

(defonce piano-samples
  (doall (map load-sample (get-samples))))

(defn- note-index
  "Returns a map of midi-note values [0-127] to buffer ids. Uses the
  provided note-fn to determine the midi-note value of a buffer."
  [buffers note-fn]
  (reduce (fn [index buf]
            (let [note (note-fn buf)
                  id   (-> buf :id)]
              (assoc index note id)))
          {}
          buffers))

(defn- piano-note-fn [buf] (-> buf :name match-note :midi-note))

;; Silent buffer used to fill in the gaps.
(defonce ^:private silent-buffer (buffer 0))

(defonce ^:private index-buffer
  (let [tab (note-index piano-samples piano-note-fn)
        buf (buffer 128)]
    (buffer-fill! buf (:id silent-buffer))
    (doseq [[idx val] tab]
      (buffer-set! buf idx val))
    buf))

(definst sampled-piano
  [note 60 level 1 rate 1 loop? 0
   attack 0 decay 1 sustain 1 release 0.1 curve -4 gate 1]
  (let [buf (index:kr (:id index-buffer) note)
        env (env-gen (adsr attack decay sustain release level curve)
                     :gate gate
                     :action FREE)]
    (* env (scaled-play-buf 2 buf :level level :loop loop? :action FREE))))

{'fraction_non_alphanumeric': 0.1295977011494253, 'fraction_numerical': 0.01206896551724138, 'mean_word_length': 2.592363261093911, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2538352', 'n_tokens_mistral': 1176, 'n_tokens_neox': 1118, 'n_words': 385}