Refaktor, postavljen reagent in secretary, popravki

master
Jure Podgoršek 2018-08-10 16:35:52 +02:00
parent 29da6fa0d3
commit c8b6ceebdc
12 changed files with 283 additions and 111 deletions

View File

@ -10,7 +10,9 @@
</head> </head>
<body style="margin: 0"> <body style="margin: 0">
<div id="container"></div> <div id="container"></div>
<!-- script type="text/javascript" src="js/three.min.js"></script--> <script type="text/javascript" src="js/three.min.js"></script>
<script src="js/compiled/sliva.js" type="text/javascript"></script> <script type="text/javascript" src="js/audioRecorder.js"></script>
<script type="text/javascript" src="js/audioRecorderWorker.js"></script>
<script type="text/javascript" src="js/compiled/sliva.js"></script>
</body> </body>
</html> </html>

View File

@ -0,0 +1,65 @@
(function(window) {
var AUDIO_RECORDER_WORKER = 'js/audioRecorderWorker.js';
var AudioRecorder = function(source, cfg) {
this.consumers = [];
var config = cfg || {};
var errorCallback = config.errorCallback || function() {};
var inputBufferLength = config.inputBufferLength || 4096;
var outputBufferLength = config.outputBufferLength || 4000;
this.context = source.context;
this.node = this.context.createScriptProcessor(inputBufferLength);
var worker = new Worker(config.worker || AUDIO_RECORDER_WORKER);
worker.postMessage({
command: 'init',
config: {
sampleRate: this.context.sampleRate,
outputBufferLength: outputBufferLength,
outputSampleRate: (config.outputSampleRate || 16000)
}
});
var recording = false;
this.node.onaudioprocess = function(e) {
if (!recording) return;
worker.postMessage({
command: 'record',
buffer: [
e.inputBuffer.getChannelData(0),
e.inputBuffer.getChannelData(1)
]
});
};
this.start = function(data) {
this.consumers.forEach(function(consumer, y, z) {
consumer.postMessage({ command: 'start', data: data });
recording = true;
return true;
});
recording = true;
return (this.consumers.length > 0);
};
this.stop = function() {
if (recording) {
this.consumers.forEach(function(consumer, y, z) {
consumer.postMessage({ command: 'stop' });
});
recording = false;
}
worker.postMessage({ command: 'clear' });
};
this.cancel = function() {
this.stop();
};
myClosure = this;
worker.onmessage = function(e) {
if (e.data.error && (e.data.error == "silent")) errorCallback("silent");
if ((e.data.command == 'newBuffer') && recording) {
myClosure.consumers.forEach(function(consumer, y, z) {
consumer.postMessage({ command: 'process', data: e.data.data });
});
}
};
source.connect(this.node);
this.node.connect(this.context.destination);
};
window.AudioRecorder = AudioRecorder;
})(window);

View File

@ -0,0 +1,59 @@
var recBuffers = [],
outputSampleRate = 16000,
inSampleRate;
this.onmessage = function(e){
switch(e.data.command){
case 'init':
init(e.data.config);
break;
case 'record':
record(e.data.buffer);
break;
case 'clear':
clear();
break;
}
};
function init(config){
inSampleRate = config.sampleRate;
outputBufferLength = config.outputBufferLength;
outputSampleRate = config.outputSampleRate || outputSampleRate;
}
function record(inputBuffer){
var isSilent = true;
for (var i = 0 ; i < inputBuffer[0].length ; i++) {
recBuffers.push((inputBuffer[0][i] + inputBuffer[1][i]) * 16383.0);
}
while(recBuffers.length * outputSampleRate / inSampleRate > outputBufferLength) {
var result = new Int16Array(outputBufferLength);
var bin = 0,
num = 0,
indexIn = 0,
indexOut = 0;
while(indexIn < outputBufferLength) {
bin = 0;
num = 0;
while(indexOut < Math.min(recBuffers.length, (indexIn + 1) * inSampleRate / outputSampleRate)) {
bin += recBuffers[indexOut];
num += 1;
indexOut++;
}
result[indexIn] = bin / num;
if(isSilent && (result[indexIn] != 0)) isSilent = false;
indexIn++;
}
var output = {};
output.command = 'newBuffer';
output.data = result;
if (isSilent) output.error = "silent";
this.postMessage(output);
recBuffers = recBuffers.slice(indexOut);
}
}
function clear(){
recBuffers = [];
}

View File

@ -1,10 +1,11 @@
(ns sliva.core (ns sliva.core
(:require (:require [secretary.core :as secretary :refer-macros [defroute]]
[secretary.core :as secretary :refer-macros [defroute]]
[reagent.core :as reagent] [reagent.core :as reagent]
[sliva.socket :refer [websocket-init click-test]]
[sliva.data :refer [appstate]] [sliva.data :refer [appstate]]
[sliva.routes :refer [app-routes]] [sliva.routes :refer [app-routes]]
[sliva.pages.hub :refer [hub]]) [sliva.pages.hub :refer [hub]]
[sliva.pages.visual :refer [visual]])
(:require-macros [cljs.core.async.macros :refer [go go-loop]])) (:require-macros [cljs.core.async.macros :refer [go go-loop]]))
(enable-console-print!) (enable-console-print!)
@ -13,20 +14,20 @@
;; Page switching ;; Page switching
(defmulti current-page #(@appstate :page)) (defmulti current-page #(@appstate :page))
(defmethod current-page :hub [] hub) (defmethod current-page :hub [] [hub])
(defmethod current-page :visual [] [visual])
;;(defn on-js-reload []
;; (stop-render)
;; (start-render)
;; (websocket-init))
;; On figwheel reload do this (reload routes, render currently active page) ;; On figwheel reload do this (reload routes, render currently active page)
(defn init-app [] (defn init-app []
(app-routes) (app-routes)
(console.log "App (re)init!")
(reagent/render [current-page] (.getElementById js/document "container"))) (reagent/render [current-page] (.getElementById js/document "container")))
;; Vstopna tocka ;; Vstopna tocka
(aset js/document "onreadystatechange" (aset js/document "onreadystatechange"
(fn [] (fn []
(if (= (.-readyState js/document) "complete") (if (= (.-readyState js/document) "complete")
(init-app)))) (do
(websocket-init)
(click-test)
(init-app)))))

View File

@ -5,6 +5,26 @@
;; App state atom ;; App state atom
(def appstate (def appstate
(reagent/atom (reagent/atom
{:clients []})) {:clients []
:vtic (chan)
(def vtic (atom (chan))) :vizual {:animiraj false
;;;;; ☭☭☭☭☭☭☭☭☭☭☭☭☭☭ ;;;;;;
;; ☭☭☭☭ Parametri razni ☭☭☭☭ ;;
;;;;; ☭☭☭☭☭☭☭☭☭☭☭☭☭☭ ;;;;;;
:odmik-kamere 100
:rotacija-kamere 1
:bg-barva 0x000000
:FOV 140
:lik-sirina 2
:obj-limit 1000
:objekti []
:stevec 0
:rotacija-x 0.006
:rotacija-y 0.001
:rotacija-z 0.003
:zamik-barve 0.0000666
:zacetna-barva 0.333
:saturacija 1
:svetlost 0.4
:w-diff 0.5
:gostota-obj 2}}))

View File

@ -1,37 +1,20 @@
(ns sliva.gfx) (ns sliva.gfx
(:require [sliva.data :refer [appstate]]))
;;;;; ☭☭☭☭☭☭☭☭☭☭☭☭☭☭ ;;;;;; (defn get-param [param]
;; ☭☭☭☭ Parametri razni ☭☭☭☭ ;; (get-in @appstate [:vizual param]))
;;;;; ☭☭☭☭☭☭☭☭☭☭☭☭☭☭ ;;;;;;
(def odmik-kamere 100) (defn set-param [param val]
(def rotacija-kamere 1) (swap! appstate assoc-in [:vizual param] val))
(def FOV 140)
(def lik-sirina 2)
(def obj-limit 1000)
(def objekti (atom []))
(def stevec (atom 0))
(def rotacija-x 0.006)
(def rotacija-y 0.001)
(def rotacija-z 0.003)
(def zamik-barve 0.0000666)
(def zacetna-barva 0.333)
(def saturacija 1)
(def svetlost 0.4)
(def w-diff 0.5)
(def gostota-obj 2)
;; Inicializacija ;; ;; Inicializacija ;;
(def scena (THREE.Scene.)) (def scena (THREE.Scene.))
(def kamera (THREE.PerspectiveCamera.
(get-param :FOV)
(/ (.-innerWidth js/window)
(.-innerHeight js/window)) 0.1 2000))
(def kamera (THREE.PerspectiveCamera. FOV (/ (.-innerWidth js/window) (aset kamera "position" "z" (get-param :odmik-kamere))
(.-innerHeight js/window)) 0.1 2000))
(aset kamera "position" "z" odmik-kamere)
(aset kamera "aspect" (/ (.-innerWidth js/window) (aset kamera "aspect" (/ (.-innerWidth js/window)
(.-innerHeight js/window))) (.-innerHeight js/window)))
(.updateProjectionMatrix kamera) (.updateProjectionMatrix kamera)
@ -40,7 +23,7 @@
(.setSize izris (.-innerWidth js/window) (.-innerHeight js/window)) (.setSize izris (.-innerWidth js/window) (.-innerHeight js/window))
;; Crno ozadje ;; Crno ozadje
(.setClearColor izris 0x000000 1) (.setClearColor izris (get-param :bg-barva) 1)
(defn dodaj-obj [sirina] (defn dodaj-obj [sirina]
(let [barva (js/THREE.Color.) (let [barva (js/THREE.Color.)
@ -51,7 +34,11 @@
-zamik (* -1 zamik)] -zamik (* -1 zamik)]
;; Nastavi barvo novega objekta ;; Nastavi barvo novega objekta
(.setHSL barva (* @stevec zacetna-barva) saturacija svetlost) (.setHSL barva
(* (get-param :stevec)
(get-param :zacetna-barva))
(get-param :saturacija)
(get-param :svetlost))
(aset mat "color" barva) (aset mat "color" barva)
;; Kvadratek (za nov objekt) ;; Kvadratek (za nov objekt)
@ -61,55 +48,47 @@
(.push koti (js/THREE.Vector3. zamik 0 0) (js/THREE.Vector3. 0 -zamik 0)) (.push koti (js/THREE.Vector3. zamik 0 0) (js/THREE.Vector3. 0 -zamik 0))
(let [obj (js/THREE.Line. geo mat (.-LineSegments js/THREE)) (let [obj (js/THREE.Line. geo mat (.-LineSegments js/THREE))
novi-objekti (conj (take obj-limit @objekti) novi-objekti (conj (take (get-param :obj-limit) (get-param :objekti))
obj)] obj)]
(.add scena obj) (.add scena obj)
;; pucaj sceno ane ;; pucaj sceno ane
(reset! objekti novi-objekti)))) (set-param :objekti novi-objekti))))
(defn obj-anim [obj] (defn obj-anim [obj]
(.rotateY obj rotacija-y) (.rotateY obj (get-param :rotacija-y))
(.rotateZ obj rotacija-z) (.rotateZ obj (get-param :rotacija-z))
(.rotateX obj rotacija-x) (.rotateX obj (get-param :rotacija-x))
(let [new-scale (+ (aget obj "scale" "x") (let [new-scale (+ (aget obj "scale" "x")
w-diff)] (get-param :w-diff))]
(aset obj "scale" "x" new-scale) (aset obj "scale" "x" new-scale)
(aset obj "scale" "y" new-scale) (aset obj "scale" "y" new-scale)
(aset obj "scale" "z" new-scale)) (aset obj "scale" "z" new-scale))
(.offsetHSL (aget obj "material" "color") zamik-barve 0 0)) (.offsetHSL (aget obj "material" "color") (get-param :zamik-barve) 0 0))
(defn cam-rotate [] (defn cam-rotate []
(.translateX kamera rotacija-kamere) (.translateX kamera (get-param :rotacija-kamere))
(.translateZ kamera (- odmik-kamere (.translateZ kamera (- (get-param :odmik-kamere)
(.sqrt js/Math (+ (.pow js/Math odmik-kamere 2) (.sqrt js/Math (+ (.pow js/Math (get-param :odmik-kamere) 2)
(.pow js/Math rotacija-kamere 2))))) (.pow js/Math (get-param :rotacija-kamere) 2)))))
(.lookAt kamera scena.position)) (.lookAt kamera (.-position scena)))
(defn render [] (defn render []
(.requestAnimationFrame js/window render) (if (get-in @appstate [:vizual :animiraj])
(.requestAnimationFrame js/window render))
(reset! stevec (inc @stevec)) (set-param :stevec (inc (get-param :stevec)))
(if (= 0 (rem @stevec gostota-obj)) (if (= 0 (rem (get-param :stevec) (get-param :gostota-obj)))
(dodaj-obj lik-sirina)) (dodaj-obj (get-param :lik-sirina)))
(.render izris scena kamera) (.render izris scena kamera)
(run! obj-anim @objekti) (run! obj-anim (get-param :objekti))
(cam-rotate)) (cam-rotate))
(defn start-render []
(let [container (.getElementById js/document "container")]
(.appendChild container (.-domElement izris))))
(defn stop-render []
(let [container (.getElementById js/document "container")]
(.removeChild container (.-firstChild container))))
;; Hendlaj risajz ;; Hendlaj risajz
(defn on-window-resize[] (defn on-window-resize[]
(let [sirina (.-innerWidth js/window) (let [sirina (.-innerWidth js/window)
@ -117,4 +96,15 @@
(aset kamera "aspect" (/ sirina visina)) (aset kamera "aspect" (/ sirina visina))
(.updateProjectionMatrix kamera) (.updateProjectionMatrix kamera)
(.setSize izris sirina visina))) (.setSize izris sirina visina)))
(.addEventListener js/window "resize" on-window-resize false)
(defn start-render [container]
(.appendChild container (.-domElement izris))
(swap! appstate assoc-in [:vizual :animiraj] true)
(.addEventListener js/window "resize" on-window-resize false)
(render))
(defn stop-render [container]
(swap! appstate assoc-in [:vizual :animiraj] false)
(.removeEventListener js/window "resize" on-window-resize false)
(.removeChild container (.-firstChild container)))

View File

@ -1,9 +1,6 @@
(ns sliva.pages.hub (ns sliva.pages.hub
(:require [sliva.socket :refer [websocket-init click-test]] (:require [sliva.data :refer [appstate]]
[sliva.data :refer [appstate]])) [sliva.pages.navigation :refer [navigation]]))
(websocket-init)
(click-test)
(defn hub [] (defn hub []
[:div [:div
@ -12,10 +9,14 @@
[:thead [:thead
[:tr [:tr
[:th "Client ID"] [:th "Client ID"]
[:th "Something else?"]]] [:th "status"]]]
[:tbody [:tbody
(map (fn [cid] (let [clients (:clients @appstate)]
[:tr {:key cid} (if (empty? clients)
[:td cid] [:tr [:td "No"] [:td "clients"]]
[:td "connected"]]) (->> clients
(keys (:clients @appstate)))]]]) (map-indexed (fn [idx cid]
[:tr {:key idx}
[:td cid]
[:td "connected"]])))))]]
(navigation)])

View File

@ -0,0 +1,8 @@
(ns sliva.pages.navigation)
(defn navigation []
[:div
[:h2 "Navigation"]
[:ul
[:li [:a {:href "#/hub"} "Hub"]]
[:li [:a {:href "#/visual"} "Visual"]]]])

View File

@ -0,0 +1,15 @@
(ns sliva.pages.visual
(:require [reagent.core :as reagent]
[sliva.gfx :refer [start-render stop-render]]
[sliva.pages.navigation :refer [navigation]]))
(defn visual []
(reagent/create-class
{:display-name "vizual-render-plac"
:reagent-render (fn [] [:div
[:div {:ref "vizual"}]
[navigation]])
:component-did-mount (fn [this] (start-render (.. this -refs -vizual)))
:component-will-unmount (fn [this]
(console.log "unmounting?")
(stop-render (.. this -refs -vizual)))}))

View File

@ -14,7 +14,10 @@
(.setEnabled true))) (.setEnabled true)))
(defn app-routes [] (defn app-routes []
(secretary/set-config! :prefix "#☭") ;;(secretary/set-config! :prefix "#☭")
(defroute "/" [] (secretary/set-config! :prefix "#")
(swap! appstate assoc :page :hub)) (defroute "/" [] (swap! appstate assoc :page :hub))
(defroute "/hub" [] (swap! appstate assoc :page :hub))
(defroute "/visual" [] (swap! appstate assoc :page :visual))
(defroute "/test" [] (console.log "test!"))
(hook-browser-navigation!)) (hook-browser-navigation!))

View File

@ -1,5 +1,6 @@
(ns sliva.server.handlers (ns sliva.server.handlers
(:require [compojure.core :refer [defroutes GET]] (:require [clojure.java.shell :refer [sh]]
[compojure.core :refer [defroutes GET]]
[compojure.route :refer [resources]] [compojure.route :refer [resources]]
[clojure.string :as str] [clojure.string :as str]
[clojure.core.async :refer [chan <! <!! >! put! close! go go-loop]] [clojure.core.async :refer [chan <! <!! >! put! close! go go-loop]]
@ -21,7 +22,7 @@
(go (go
(let [{:keys [message]} (<! ws-ch)] (let [{:keys [message]} (<! ws-ch)]
(println "new connection - " cid) (println "new connection - " cid)
(>! ws-ch (str "Hello " cid)) (>! ws-ch (str "hello:" cid))
(swap! clients assoc cid ws-ch) (swap! clients assoc cid ws-ch)
(send-all "open" cid) (send-all "open" cid)
(go-loop [] (go-loop []
@ -31,7 +32,11 @@
(println "command" cmd "(" args ")") (println "command" cmd "(" args ")")
(condp = cmd (condp = cmd
"ping" (>! ws-ch "pong") "ping" (>! ws-ch "pong")
(println "DEBUG: msg ignored: " cmd))) (keys @clients)
"Unknown command :/")) (keys @clients)
(recur)))))))) (recur))))))))
;;(close! ws-ch))))) ;;(close! ws-ch)))))
;;test
;;(while true
;; (let [return (sh "sudo" "bash" "-c" "iwlist scan | grep ESSID | sed 's/[[:space:]]\\+ESSID://' | sed 's/\"//g' | uniq")]
;; (println "GOT NETWORKS!" (:out return)))

View File

@ -1,35 +1,38 @@
(ns sliva.socket (ns sliva.socket
(:require [chord.client :refer [ws-ch]] (:require [chord.client :refer [ws-ch]]
[clojure.string :as str]
[cljs.core.async :refer [<! >! put! close!]] [cljs.core.async :refer [<! >! put! close!]]
[sliva.data :refer [vtic]]) [sliva.data :refer [appstate]])
(:require-macros [cljs.core.async.macros :refer [go go-loop]])) (:require-macros [cljs.core.async.macros :refer [go go-loop]]))
;; Zacetek ;; (defn handle-message [msg-str]
;;(aset js/document "onreadystatechange" (let [[msg & args] (str/split msg-str #":")]
;; (fn [] (console.info "msg" msg "args" args)
;; (if (= (.-readyState js/document) "complete") (condp = msg
;; (do "pong" (console.log "hura, server se odziva")
;; (start-render) "open" (swap! appstate assoc :clients (conj (:clients @appstate) (first args)))
;; (render) (console.info "msg ingored: " msg))))
;; (websocket-init)))))
(defn websocket-init [] (defn websocket-init []
(console.log "init webscoket") (console.log "init webscoket")
(go (let [{:keys [ws-channel error]} (<! (ws-ch "ws://localhost:3449/ws"))] (go (let [host (aget js/window "location" "hostname")
{:keys [ws-channel error]} (<! (ws-ch (str "ws://" host ":3449/ws")))]
(if-not error (if-not error
(do (do
(reset! vtic ws-channel) (swap! appstate assoc :vtic ws-channel)
(>! ws-channel "Hello server") (>! ws-channel "hello:server")
;; (go-loop [] (go-loop []
;; (let [{:keys [message]} (<! ws-channel)] (let [{:keys [message]} (<! ws-channel)]
;; (console.log "got msg" message) (handle-message message)
;; (socket-handler message) (recur))))
;; (recur))))
)
(js/console.log "Fejl websocket: " (pr-str error)))))) (js/console.log "Fejl websocket: " (pr-str error))))))
(defn click-test [] (defn click-test []
(.addEventListener js/window "click" (fn [] (.addEventListener
(go [] js/window
(>! @vtic "ping"))))) "click"
(fn []
(let [vtic (:vtic @appstate)]
(console.log "klik")
(put! vtic "ping")))))