Skip to content
This repository has been archived by the owner on Jan 23, 2021. It is now read-only.

Commit

Permalink
dont require libcsound as global
Browse files Browse the repository at this point in the history
  • Loading branch information
hlolli committed Mar 2, 2018
1 parent 0af2ed8 commit 2a17928
Show file tree
Hide file tree
Showing 21 changed files with 1,910 additions and 2,440 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ package-lock.json
*.wav
browser/cljs-runtime
browser/manifest.edn
.shadow-cljs
yarn.lock
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ node_modules
package-lock.json
*~
*.wav
deps.cljs
project.clj
shadow-cljs.edn
src
.shadow-cljs
yarn.lock
51 changes: 26 additions & 25 deletions browser/index.html
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
<html>
<!DOCTYPE html>
<html lang="en">
<head>
<title>csound-wasm dev</title>
</head>
<body>
<h1>csound-wasm dev</h1>
<script src="/libcsound.js"></script>
<script src="/main.js"></script>
<meta charset="utf-8">
<title>csound-wasm dev</title>
</head>
<body>
<h1>csound-wasm dev</h1>
<script src="main.js"></script>
<script>
var csound = csound_wasm;
// var csound = csound_wasm.browser.main;
const beeper = `
<CsoundSynthesizer>
<CsOptions>
-+rtmidi=null -M0
-+rtmidi=null -M0
</CsOptions>
<CsInstruments>
;Example by Andrés Cabrera
;Example by Andrés Cabrera
sr = 44100
ksmps = 32
nchnls = 2
0dbfs = 1
sr = 44100
ksmps = 32
nchnls = 2
0dbfs = 1
massign 0, 1 ;assign all MIDI channels to instrument 1
opcode declick, a, a
ain xin
aenv linseg 0, 0.02, 1, p3 - 0.05, 1, 0.02, 0, 0.01, 0
ain xin
aenv linseg 0, 0.02, 1, p3 - 0.05, 1, 0.02, 0, 0.01, 0
xout ain * aenv ; apply envelope and write output
endop
instr 1
iCps cpsmidi ;get the frequency from the key pressed
iAmp ampmidi 0dbfs * 0.3 ;get the amplitude
aOut poscil iAmp, iCps ;generate a sine tone
kenv mxadsr 0.1, 0, 1, 0.5
aOut *= kenv
instr 1
iCps cpsmidi ;get the frequency from the key pressed
iAmp ampmidi 0dbfs * 0.3 ;get the amplitude
aOut poscil iAmp, iCps ;generate a sine tone
kenv mxadsr 0.1, 0, 1, 0.5
aOut *= kenv
outs aOut, aOut ;write it to the output
endin
endin
</CsInstruments>
<CsScore>
Expand Down
1 change: 0 additions & 1 deletion browser/libcsound.js

This file was deleted.

3,748 changes: 1,607 additions & 2,141 deletions browser/main.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions deps.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{:foreign-libs
[{:file "csound_wasm/libcsound.js"
:provides ["libcsound"]}]}
2 changes: 1 addition & 1 deletion examples/example1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const csound = require('../index.js');
const csound = require('../');

const beeper = `
instr 1
Expand Down
2 changes: 1 addition & 1 deletion examples/example2.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const csound = require('../index.js');
const csound = require('../');

const beeper = `
instr 1
Expand Down
2 changes: 1 addition & 1 deletion examples/example3.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const csound = require('../index.js');
const csound = require('../');

const csd = `
<CsoundSynthesizer>
Expand Down
2 changes: 1 addition & 1 deletion examples/example4.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const csound = require('../index.js');
const csound = require('../');

const csd = `
<CsoundSynthesizer>
Expand Down
30 changes: 15 additions & 15 deletions examples/example5.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
const csound = require('../index.js');
const csound = require('../');

const csd = `
<CsoundSynthesizer>
<CsOptions>
-+rtmidi=null -M0
-+rtmidi=null -M0
</CsOptions>
<CsInstruments>
;Example by Andrés Cabrera
;Example by Andrés Cabrera
sr = 44100
ksmps = 32
nchnls = 2
0dbfs = 1
sr = 44100
ksmps = 32
nchnls = 2
0dbfs = 1
massign 0, 1 ;assign all MIDI channels to instrument 1
instr 1
iCps cpsmidi ;get the frequency from the key pressed
iAmp ampmidi 0dbfs * 0.3 ;get the amplitude
aOut poscil iAmp, iCps ;generate a sine tone
kenv mxadsr 0.1, 0, 1, 0.5
aOut *= kenv
instr 1
iCps cpsmidi ;get the frequency from the key pressed
iAmp ampmidi 0dbfs * 0.3 ;get the amplitude
aOut poscil iAmp, iCps ;generate a sine tone
kenv mxadsr 0.1, 0, 1, 0.5
aOut *= kenv
outs aOut, aOut ;write it to the output
endin
endin
</CsInstruments>
<CsScore>
Expand Down
229 changes: 112 additions & 117 deletions index.js → node/index.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions node/libcsound.wasm
53 changes: 27 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
{
"name": "csound-wasm",
"version": "6.10.0-3",
"author": "Hlödver Sigurdsson <[email protected]>",
"license": "GPL-3.0",
"keywords": [
"csound",
"csound-wasm",
"wasm",
"webassembly",
"audio",
"webaudio"
"name": "csound-wasm",
"version": "6.10.0-3",
"author": "Hlödver Sigurdsson <[email protected]>",
"license": "GPL-3.0",
"keywords": [
"csound",
"csound-wasm",
"wasm",
"webassembly",
"audio",
"webaudio"
],
"main": "index.js",
"browser": "browser/main.js",
"build": {
"appId": "csound-wasm"
},
"dependencies": {
"bufferutil": "^3.0.3",
"midi": "^0.9.5",
"speaker": "^0.3.0",
"utf-8-validate": "^4.0.0"
},
"devDependencies": {
"shadow-cljs": "^2.0.144",
"web-audio-api": "^0.2.2"
}
"main": "node/index.js",
"browser": "browser/main.js",
"build": {
"appId": "csound-wasm"
},
"dependencies": {
"bufferutil": "^3.0.3",
"libcsound": "^6.10.0-3",
"midi": "^0.9.5",
"speaker": "^0.3.0",
"utf-8-validate": "^4.0.0"
},
"devDependencies": {
"shadow-cljs": "^2.2.3",
"web-audio-api": "^0.2.2"
}
}
10 changes: 8 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
:license {:name "GNU GPL v3+"
:url "http://www.gnu.org/licenses/gpl-3.0.en.html"}
:source-paths ["src"]
:resource-paths ["libcsound.js" "libcsound.wasm" "package.json"]
:resource-paths ["package.json" "deps.cljs"
"libcsound.js" "libcsound.wasm"]
:scm {:name "git"
:url "https://github.com/hlolli/csound-wasm"})
:url "https://github.com/hlolli/csound-wasm"}

;; :plugins [[lein-cljsbuild "1.1.7"]]
;; :hooks [leiningen.cljsbuild]
;; :cljsbuild {:builds []}
)
19 changes: 11 additions & 8 deletions shadow-cljs.edn
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{:dependencies [[org.clojure/core.specs.alpha "0.1.24"]]
:source-paths ["src"]
:builds {:node {:target :node-library
:output-to "index.js"
:output-to "node/index.js"
:modules {:main {:entries [csound-wasm.node]}}
:js-options {:resolve {"libcsound"
{:target :file
:file "src/csound_wasm/libcsound.js"}}}
:exports {:startRealtime csound-wasm.public/start-realtime
:compileOrc csound-wasm.public/compile-orc
:renderToFile csound-wasm.node/render-to-file
Expand All @@ -25,18 +28,18 @@
:getKsmps csound-wasm.public/get-ksmps
:get0dbfs csound-wasm.public/get-0dbfs
:enableMidi csound-wasm.node/enable-midi
:pushMidi csound-wasm.public/push-midi-message
}}
:pushMidi csound-wasm.public/push-midi-message}}
:browser {:target :browser
:output-dir "browser"
:asset-path "./"
:js-options {:resolve {"libcsound" {:target :global
:global "Module"}}}
:compiler-options {:optimizations :simple
:js-options {:resolve {"libcsound"
{:target :file
:file "src/csound_wasm/libcsound.js"}}}
:compiler-options {:optimizations :advanced
:output-wrapper true}
:modules {:main {:entries [csound-wasm.browser]
:prepend "var csound_wasm = (function () {\n"
:append "\n return csound_wasm.browser.main;})()\n"}}
:append "\nvar csound = csound_wasm.browser.main;\n"}}
:exports {:csound csound-wasm.browser/main}
:devtools {:http-root "browser"
:http-resource-root "browser"
:http-port 8600
Expand Down
46 changes: 18 additions & 28 deletions src/csound_wasm/browser.cljs
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
(ns csound-wasm.browser
(:require [csound-wasm.public :as public]))
(:require [csound-wasm.public :as public]
["libcsound" :as libcsound]))

(enable-console-print!)

(declare csound-object)
(public/activate-init-callback libcsound/calledRun)

(defn wait-for-libcsound []
(if (and (exists? js/Module)
(nil? @public/csound-object))
(do (reset! public/csound-object js/Module)
(def csound-object js/Module)
(public/activate-init-callback (.-calledRun csound-object))
(set! (.-print csound-object)
(fn [log]
(.log js/console "%c%s" "background: #222; color: #bada55" log)))
(set! (.-printErr csound-object)
(fn [log]
(.log js/console "%c%s" "background: #222; color: #bada55" log)))
(set! (.-noExitRuntime csound-object) true))
(js/setTimeout
(fn [] (wait-for-libcsound))
1)))
(set! libcsound/print
(fn [log]
(.log js/console "%c%s" "background: #222; color: #bada55" log)))

(wait-for-libcsound)
(set! libcsound/printErr
(fn [log]
(.log js/console "%c%s" "background: #222; color: #bada55" log)))

(set! libcsound/noExitRuntime true)

(defn enable-midi []
(letfn [(handle-midi-input [event]
Expand Down Expand Up @@ -75,11 +66,11 @@
(def wasm-buffer-offset (volatile! 0))

(defn start-audio [csound-instance]
(let [ksmps ((.cwrap csound-object "CsoundObj_getKsmps" #js ["number"] #js ["number"])
(let [ksmps ((libcsound/cwrap "CsoundObj_getKsmps" #js ["number"] #js ["number"])
csound-instance)
input-count ((.cwrap csound-object "CsoundObj_getInputChannelCount" #js ["number"] #js ["number"])
input-count ((libcsound/cwrap "CsoundObj_getInputChannelCount" #js ["number"] #js ["number"])
csound-instance)
output-count ((.cwrap csound-object "CsoundObj_getOutputChannelCount" #js ["number"] #js ["number"])
output-count ((libcsound/cwrap "CsoundObj_getOutputChannelCount" #js ["number"] #js ["number"])
csound-instance)
audio-context-constructor (or js/window.AudioContext js/window.webkitAudioContext)
audio-context (new audio-context-constructor)
Expand All @@ -89,11 +80,11 @@
_ (do (set! (.-inputCount audio-process-node) input-count)
(set! (.-outputCount audio-process-node) output-count))
buffer-size (.-bufferSize audio-process-node)
output-pointer ((.cwrap csound-object "CsoundObj_getOutputBuffer" #js ["number"] #js ["number"])
output-pointer ((libcsound/cwrap "CsoundObj_getOutputBuffer" #js ["number"] #js ["number"])
csound-instance)
csound-output-buffer (new js/Float32Array (.-buffer (.-HEAP8 csound-object))
csound-output-buffer (new js/Float32Array (.-buffer (.-HEAP8 libcsound))
output-pointer (* ksmps output-count))
zerodbfs ((.cwrap csound-object "CsoundObj_getZerodBFS" #js ["number"] #js ["number"])
zerodbfs ((libcsound/cwrap "CsoundObj_getZerodBFS" #js ["number"] #js ["number"])
csound-instance)
range-output-cnt (range output-count)
process-buffers (fn [e sample-count src-offset dst-offset]
Expand All @@ -106,7 +97,7 @@
(+ j src-offset))))
zerodbfs)))))
perform-ksmps-fn (fn []
((.cwrap csound-object "CsoundObj_performKsmps" #js ["number"] #js ["number"])
((libcsound/cwrap "CsoundObj_performKsmps" #js ["number"] #js ["number"])
csound-instance))]
(vreset! wasm-buffer-offset ksmps)
(set! (.-onaudioprocess audio-process-node)
Expand All @@ -127,8 +118,7 @@
(recur sample-count
(+ index sample-count)))))))))
(.connect audio-process-node (.-destination audio-context))
nil
))
nil))

(vreset! public/start-audio-fn start-audio)

Expand Down
2 changes: 1 addition & 1 deletion libcsound.js → src/csound_wasm/libcsound.js
Original file line number Diff line number Diff line change
Expand Up @@ -10291,5 +10291,5 @@ run();

// {{MODULE_ADDITIONS}}


module.exports = Module;

File renamed without changes.
Loading

0 comments on commit 2a17928

Please sign in to comment.