<commit_before>(ns overtone.repl.debug
  (:use [clojure.pprint]
        [overtone.sc.machinery.ugen.defaults]
        [overtone.sc.machinery.ugen.special-ops]
        [overtone.sc.machinery.ugen.specs]
        [overtone.sc.machinery.synthdef]
        [overtone.helpers seq lib]))

(defn- control-ugen-name?
  [ug-n]
  (or (= "trig-control" ug-n)
      (= "audio-control" ug-n)
      (= "control" ug-n)))

(defn- control-ugen?
  [ug]
  (control-ugen-name? (:name ug)))

(defn- mk-control-id
  [ug-id output-idx]
  (str "control_" ug-id "_" output-idx))

(defn- unify-input
  [input ugens constants]
  (if (= -1 (:src input))
    (nth constants (:index input))
    (let [idx (:src input)
          n (:name (nth ugens idx))
          id (if (control-ugen-name? (overtone-ugen-name n))
               (mk-control-id idx (:index input))
               idx)]
      {:name n
       :id id})))

(defn- unify-ugen-name
  [ugen]
  (let [name (:name ugen)]
    (cond
     (= "BinaryOpUGen" name) (REVERSE-BINARY-OPS (:special ugen))
     (= "UnaryOpUGen" name) (REVERSE-UNARY-OPS (:special ugen))
     :else (overtone-ugen-name name))))

(defn- unify-ugen
  [ugen ugens sdef]
  {:name      (unify-ugen-name ugen)
   :rate      (REVERSE-RATES (:rate ugen))
   :special   (:special ugen)
   :inputs    (map #(unify-input % ugens (:constants sdef)) (:inputs ugen))
   :n-outputs (count (:outputs ugen))
   :outputs   (:outputs ugen)
   :id        (:id ugen)})

(defn- fix-input-refs
  "Resolve input references to inline their name if another ugen or the
   value if a float."
  [inputs ugens]
  (map (fn [input]
         (if (number? input)
           input
           (let [n (overtone-ugen-name (:name input))]
             {:name n
              :id (index-of ugens (find-first #(= (:id %) (:id input)) ugens))})))
       inputs))

(defn- unify-ugen-inputs
  [ugens]
  (map (fn [ug idx]
         (let [inputs      (fix-input-refs (:inputs ug) ugens)
               input-specs (or (:args (get-ugen-spec (:name ug)))
                               [{:name "a"}
                                {:name "b"}])]
           (assoc ug
             :id idx
             :inputs (loop [res         {}
                            inputs      inputs
                            input-specs input-specs]
                       (if (empty? input-specs)
                         res
                         (if (ugen-sequence-mode? (:mode (first input-specs)))
                           (let [arg-seq (drop (dec (count input-specs)) inputs)
                                 inputs  (drop-last (count arg-seq) inputs)]
                             (recur (assoc res (keyword (:name (first input-specs))) (vec arg-seq))
                                    inputs
                                    (rest input-specs)))

                           (recur (assoc res (keyword (:name (first input-specs))) (first inputs))
                                  (rest inputs)
                                  (rest input-specs))))))))
       ugens
       (range)))

(defn- find-control-offset
  "Calculate the output offset of the specified control ugen in the list
   of ugens. The first control ugen has an offset of 0, the second has
   an offest equalling the number of outputs of the first control ugen
   and the third equalling the summation of the number of outputs of the
   first and second control ugens, etc."
  [c-name sdef]
  (let [ctl-ugs (filter (fn [ug]
                          (control-ugen-name? (overtone-ugen-name (:name ug))))
                        (:ugens sdef))]
    (loop [res 0
           ugs ctl-ugs]
      (when (empty? ugs)
        (throw (Exception. (str "Couldn't find ugen with name " c-name<commit_msg>treat ugens args with :array set to true as sequences<commit_after>(ns overtone.repl.debug
  (:use [clojure.pprint]
        [overtone.sc.machinery.ugen.defaults]
        [overtone.sc.machinery.ugen.special-ops]
        [overtone.sc.machinery.ugen.specs]
        [overtone.sc.machinery.synthdef]
        [overtone.helpers seq lib]))

(defn- control-ugen-name?
  [ug-n]
  (or (= "trig-control" ug-n)
      (= "audio-control" ug-n)
      (= "control" ug-n)))

(defn- control-ugen?
  [ug]
  (control-ugen-name? (:name ug)))

(defn- mk-control-id
  [ug-id output-idx]
  (str "control_" ug-id "_" output-idx))

(defn- unify-input
  [input ugens constants]
  (if (= -1 (:src input))
    (nth constants (:index input))
    (let [idx (:src input)
          n (:name (nth ugens idx))
          id (if (control-ugen-name? (overtone-ugen-name n))
               (mk-control-id idx (:index input))
               idx)]
      {:name n
       :id id})))

(defn- unify-ugen-name
  [ugen]
  (let [name (:name ugen)]
    (cond
     (= "BinaryOpUGen" name) (REVERSE-BINARY-OPS (:special ugen))
     (= "UnaryOpUGen" name) (REVERSE-UNARY-OPS (:special ugen))
     :else (overtone-ugen-name name))))

(defn- unify-ugen
  [ugen ugens sdef]
  {:name      (unify-ugen-name ugen)
   :rate      (REVERSE-RATES (:rate ugen))
   :special   (:special ugen)
   :inputs    (map #(unify-input % ugens (:constants sdef)) (:inputs ugen))
   :n-outputs (count (:outputs ugen))
   :outputs   (:outputs ugen)
   :id        (:id ugen)})

(defn- fix-input-refs
  "Resolve input references to inline their name if another ugen or the
   value if a float."
  [inputs ugens]
  (map (fn [input]
         (if (number? input)
           input
           (let [n (overtone-ugen-name (:name input))]
             {:name n
              :id (index-of ugens (find-first #(= (:id %) (:id input)) ugens))})))
       inputs))

(defn- unify-ugen-inputs
  [ugens]
  (map (fn [ug idx]
         (let [inputs      (fix-input-refs (:inputs ug) ugens)
               input-specs (or (:args (get-ugen-spec (:name ug)))
                               [{:name "a"}
                                {:name "b"}])]
           (assoc ug
             :id idx
             :inputs (loop [res         {}
                            inputs      inputs
                            input-specs input-specs]
                       (if (empty? input-specs)
                         res
                         (if (or (ugen-sequence-mode? (:mode (first input-specs)))
                                 (= true (:array (first input-specs))))
                           (let [arg-seq (drop (dec (count input-specs)) inputs)
                                 inputs  (drop-last (count arg-seq) inputs)]
                             (recur (assoc res (keyword (:name (first input-specs))) (vec arg-seq))
                                    inputs
                                    (rest input-specs)))

                           (recur (assoc res (keyword (:name (first input-specs))) (first inputs))
                                  (rest inputs)
                                  (rest input-specs))))))))
       ugens
       (range)))

(defn- find-control-offset
  "Calculate the output offset of the specified control ugen in the list
   of ugens. The first control ugen has an offset of 0, the second has
   an offest equalling the number of outputs of the first control ugen
   and the third equalling the summation of the number of outputs of the
   first and second control ugens, etc."
  [c-name sdef]
  (let [ctl-ugs (filter (fn [ug]
                          (control-ugen-name? (overtone-ugen-name (:name ug))))
                        (:ugens sdef))]
    (loop [res 0
           ugs ctl-ugs]
      (when (empty? ugs)
        (throw (Exception. (str "Couldn't find ugen with name " c-name
{'fraction_non_alphanumeric': 0.13405893283780196, 'fraction_numerical': 0.0007963897000265463, 'mean_word_length': 1.6494374120956399, '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': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1179355', 'n_tokens_mistral': 2549, 'n_tokens_neox': 2362, 'n_words': 691}