<commit_before>(ns
    ^{:doc "An interface to the SuperCollider synthesis server.
          This is at heart an OSC client library for the SuperCollider
          scsynth DSP engine."
      :author "<NAME>"}
  overtone.sc.server
  (:import [java.util.concurrent TimeoutException])
  (:use [overtone.sc.machinery.server connection comms ]
   [overtone.util.lib :only [await-promise!]]
        [overtone.libs event]
;;        [overtone.sc.machinery defaults allocator]
;;
        )
  (:require [overtone.util.log :as log]))

(def OVERTONE-VERSION {:major 0
                       :minor 4
                       :patch 0
                       :snapshot true})

(defonce synth-group*   (ref nil))
(defonce osc-log*       (atom []))

(defn server-status
  []
  @server-status*)

(defn connected? []
  (= :connected (server-status)))

(defn disconnected? []
  (= :disconnected (server-status)))

(defmacro at
  "All messages sent within the body will be sent in the same timestamped OSC
  bundle.  This bundling is thread-local, so you don't have to worry about
  accidentally scheduling packets into a bundle started on another thread."
  [time-ms & body]
  `(in-osc-bundle @server-osc-peer* ~time-ms (do ~@body)))

(defn snd
  "Sends an OSC message to the server. If the message path is a known scsynth
  path, then the types of the arguments will be checked according to what
  scsynth is expecting. Automatically converts any args which are longs to ints.

  (snd \"/foo\" 1 2.0 \"eggs\")"
  [path & args]
  (when-not (connected?)
    (throw (Exception. "Unable to send messages to an disconnected server. Please boot or connect to a server.")))
  (apply server-snd path args))

(defn recv
  "Register your intent to wait for a message associated with given path to be
  received from the server. Returns a promise that will contain the message once
  it has been received. Does not block current thread (this only happens once
  you try and look inside the promise and the reply has not yet been received).

  If an optional matcher-fn is specified, will only deliver the promise when
  the matcher-fn returns true. The matcher-fn should accept one arg which is
  the incoming event info."
  ([path] (recv path nil))
  ([path matcher-fn]
     (server-recv path matcher-fn)))

(defn connect-external-server
  "Connect to an externally running SC audio server listening to port on host.
  Host defaults to localhost.

  (connect 57710)                  ;=> connect to an external server on the
                                       localhost listening to port 57710
  (connect \"192.168.1.23\" 57110) ;=> connect to an external server with ip
                                       address 192.168.1.23 listening to port
                                       57110"
  ([port] (connect-external-server "127.0.0.1" port))
  ([host port] (connect host port)))

(defn boot-external-server
  ([] (boot :external (+ (rand-int 50000) 2000)))
  ([port] (boot :external port)))

(defn boot-server
  []<commit_msg>stop users from attempting to receive osc messages from the server when it's not connected<commit_after>(ns
    ^{:doc "An interface to the SuperCollider synthesis server.
          This is at heart an OSC client library for the SuperCollider
          scsynth DSP engine."
      :author "<NAME>"}
  overtone.sc.server
  (:import [java.util.concurrent TimeoutException])
  (:use [overtone.libs event]
        [overtone.sc.machinery.server connection comms ]
        [overtone.util.lib :only [await-promise!]])
  (:require [overtone.util.log :as log]))

(def OVERTONE-VERSION {:major 0
                       :minor 4
                       :patch 0
                       :snapshot true})

(defonce synth-group*   (ref nil))
(defonce osc-log*       (atom []))

(defn server-status
  []
  @server-status*)

(defn connected?
  "Returns true if the server is currently connected"
  []
  (= :connected (server-status)))

(defn disconnected?
  "Returns true if the server is currently disconnected"
  []
  (= :disconnected (server-status)))

(defmacro at
  "All messages sent within the body will be sent in the same timestamped OSC
  bundle.  This bundling is thread-local, so you don't have to worry about
  accidentally scheduling packets into a bundle started on another thread."
  [time-ms & body]
  `(in-osc-bundle @server-osc-peer* ~time-ms (do ~@body)))

(defn snd
  "Sends an OSC message to the server. If the message path is a known scsynth
  path, then the types of the arguments will be checked according to what
  scsynth is expecting. Automatically converts any args which are longs to ints.

  (snd \"/foo\" 1 2.0 \"eggs\")"
  [path & args]
  (when-not (connected?)
    (throw (Exception. "Unable to send messages to a disconnected server. Please boot or connect to a server.")))
  (apply server-snd path args))

(defn recv
  "Register your intent to wait for a message associated with given path to be
  received from the server. Returns a promise that will contain the message once
  it has been received. Does not block current thread (this only happens once
  you try and look inside the promise and the reply has not yet been received).

  If an optional matcher-fn is specified, will only deliver the promise when
  the matcher-fn returns true. The matcher-fn should accept one arg which is
  the incoming event info."
  ([path] (recv path nil))
  ([path matcher-fn]
     (when-not (connected?)
       (throw (Exception. "Unable to receive messages from a disconnected server. Please boot or connect to a server.")))
     (server-recv path matcher-fn)))

(defn connect-external-server
  "Connect to an externally running SC audio server listening to port on host.
  Host defaults to localhost.

  (connect 57710)                  ;=> connect to an external server on the
                                       localhost listening to port 57710
  (connect \"192.168.1.23\" 57110) ;=> connect to an external server with ip
                                       address 192.168.1.23 listening to port
                                       57110"
  ([port] (connect-external-server "127.0.0.1" port))
  ([host port] (connect host port)))

(defn boot-external-server
  ([] (boot :external (+ (rand-int 50000) 2000)))
  ([port] (boot :external port)))

(defn boot-server
  []
{'fraction_non_alphanumeric': 0.09470840616558081, 'fraction_numerical': 0.018750993167010965, 'mean_word_length': 2.724260355029586, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3365889', 'n_tokens_mistral': 1906, 'n_tokens_neox': 1755, 'n_words': 756}