I want to do real time audio processing in ClojureScript.
The MDN documentation points me to the AudioWorkletProcessor Class, but from what I googled, there is no obvious way to inherit from a JavaScript Class in ClojureScript. Am I missing something?
There is also the ScriptProcessorNode, but it is deprecated and I can not picture how to use it in ClojureScript. An example would really be helpful.
Last but not least, if somebody has another way to do real time audio processing, please let me know.
I had to deal with something similar recently, but using an AnalyserNode.
Maybe this helps you get started:
(require '[promesa.core :as p])
(defn initialize-audio []
(let [ctx (js/window.AudioContext.)
analyser (.createAnalyser ctx)]
(-> (.getUserMedia (.-mediaDevices js/navigator) (clj->js {:audio true}))
(p/chain
(fn[stream]
(.createMediaStreamSource ctx stream))
(fn[mss]
(.connect mss analyser))))))
You probably want to look at this, too https://developer.mozilla.org/en-US/docs/Web/API/AudioWorkletNode
Related
first post on here so apologies if I don't give enough information to get a clear picture.
I'm attempting to write a basic VST in Rust, the example I'm following has used a deprecated gtk method to actually build out the app and so I'm having to rewrite it. The thing I'm confused about is trait bounding (I think it's trait bounding) in functions.
How the function is written in the example:
fn run_main<Module: AudioModule>() {
//Stuff going on in here using Module::
}
What is going on here? Why am I doing this? Is there another way to achieve the same thing here?
I need to rewrite this function and pass an &app argument into it but I'm not allowed to do that because the AudioModule and gtk_app aren't related in anyway.
I tried to do something like this:
fn build_ui(app: &Application) {
//Doing all my UI stuff
fn defining_module<Module: AudioModule>() {
//Doing all my Module:: stuff
}
}
But honestly I haven't got a clue what I'm doing. It's throwing a load of errors at me. And I'm at a loss. I'd really appreciate any help.
I'm evolving my way towards a suitable design for state with reagent given my project, and while still using distinct atoms (i'll probably switch to a single atom, but trying to explore the space of possibilities at present), I've tried to set up all state in the lexical context of the root component, as illustrated below. Idea is that this app-state is an ordinary clojure map where the value of each key is a reagent atom.
It receives an attrs map which is a prop (coming from data- attrs on the html element), and its job is to initialise an app-state map, which is not itself an r/atom, but contains r/atoms. The problem is, all of these r/atoms were defined with defonce when declared at the top of the file, which is what I've just switched from, and now the defonce semantics is lost & figwheel hot reloading is broken.
Is there a way to preserve reloadable code while setting up state in this style?
I cannot put a defonce inside a let, because subsequent times that is evaluated it will evaluate to nil.
Or is there some pattern I'm missing here.
(defn a-root-component [attrs]
(let [app-state {:some-state (r/atom (:some-state attrs))}]
(fn [attrs]
[some-component app-state])))
Technically you could use memoize, but I wouldn't. By using memoize your introducing hidden global state, cleverly hidden where no one will think to look for it. Better to defonce your state outside your main component and be explicit about it.
Given the direction your moving I'd look carefully at things like re-frame and Keemcha which provide established patterns and tooling for working with centralized state in a sane way.
I'm a bit amazed I haven't been able to find an explanation for how to do this as it seems like it's fairly elemental to debugging, but I can't find anywhere how to print the attributes of an object in Polymer.
I'm learning Polymer and I keep running into situations where I have an object, but I have no idea what the attributes are of the object. (Ex. I print to the window, and I get [object Object]. I've found some explanations for how to print a list of the keys/attributes of an object (I know how to print the values for those keys if I know what they are), but I have no idea how to get the keys if I don't already know the format of my data. Every example presumes you already know what the attributes are.
I've seen solutions recommending adding a script like:
getKeys : function(o){
return Object.keys(o);
}
And then they recommend something like this:
<template is="dom-repeat" items="{{ item in obj | getKeys}}">
{{item}}
</template>
But I think they must work off maybe an earlier version of polymer. Most are from 2014-ish and I know the library has changed a lot since then.
This is the closest thing I get to an error with this code:
Polymer::Attributes: couldn`t decode Array as JSON
Here's an example post recommending this strategy. I understand I could dig deeper into the documentation and try to understand what response is supposed to be coming back, but I'm more curious what the general strategy is for this situation - I've multiple times wanted to check to see how polymer was modeling something vs how I thought it was.
The post you mention recommends a method that is no longer possible with post-1.0 Polymer, which does not support that syntax of filtering/pipes (as of the current release, 1.5.0).
You could use DevTools to select the Polymer element and then run console.dir($0). This works in the following browsers (and maybe older versions):
Chrome 50
Firefox 45
Safari 9.1
Opera 39
Chrome and Opera display all keys (even inherited ones from HTMLElement) in sorted order, so it can be tedious to scan through the long list of keys for a Polymer-specific property. However, Firefox and Safari list Polymer-specific keys first and then the inherited ones.
One workaround for Chrome/Opera is to use this snippet:
((o) => {
let obj = {};
Object.keys(o).sort().forEach((x) => {
obj[x] = o[x];
});
console.dir(obj);
})($0);
Here's a codepen that logs the attributes of a paper-button. You don't need to click the button. Open the browser's console log (not the Codepen console) to see something like the screenshot below. You can expand the fields in the console log to see the attributes of the Polymer object.
The solution I have been using is the following:
Place a button somewhere on the visible page.
When that button is tapped, print the object to the console.
my-element.html
<button on-tap="show">Click here to see user</button>
...
show: function() {
console.log('user', this.user);
},
...
You can also use console.dir() as follows.
<my-element id="foo"></my-element>
...
bar: function() {
console.dir( this.$.foo );
}
I am trying to use gestouch library in my project and i can't seem to get it to work properly.
This is how i am mapping it:
var tapGesture:TapGesture = new TapGesture(stage);
tapGesture.addEventListener(GestureEvent.GESTURE_RECOGNIZED, onTap);
As you can see it is mapped to the stage and this way it works...
The class where i am using it extends a flash Sprite, and i would like to map the gesture to
this, but when i do, gestouch doesn't seem to work...
I even tried putting a sprite with stage width and height on top of my view and still doesn't work...
Anyone knows what could be the problem?
Also, maybe it is important to note, i am using a ASC2.0, so i had to remove the mario mushroom operator from this part : Gestouch.inputAdapter ||= new NativeInputAdapter(stage);
I (am the author of Gestouch) haven't tested it with ASC 2.0 because it's still in development, but yea, at least it's known that there issues with "mushroom operator" and -inline compiler argument.
So the answer: stick to MXMLC or use sources and find out why it doesn't work.
Also it is better to ask questions on Github. I found this almost by accident — Google reports me of Gestouch mentions =)
I'm playing around a bit with ActionScript. What I want is that I can display a mathematical function from a string.
E.g. in my working python script I do something like that:
formula = 'x**2 + 3*x'
for x in range( 0, 100 ):
y = eval( formula )
graph.display( x, y )
I want to port this to ActionScript, but it seems like there is no more eval since version 3. How can I compute my function values anyway?
Something that might also work in your case, is using Javascript eval instead. You can use something like:
var result = ExternalInterface.call(myEvalFunctionInJS,formula)
to evaluate math functions.
This is an somewhat easy and useful workaround as javascript is quite close to actionscript.
If you put the ExternalInterface call inside an loop, it may become sluggish. To avoid that, you can write the loop in javascript. (You can even write the entire javascript inside as3, so that you do not need to touch the actual html page.)
edit:
Here's an link for that.
http://www.actionscript.org/resources/articles/745/2/JavaScript-and-VBScript-Injection-in-ActionScript-3/Page2.html
You will have to write an eval yourself. You will have to parse the string and invoke the right operators.
Here's a link to get you started.
The Tamarin project has a ECMAScript parser written in ES4. Try this as well.
"You can even write the entire javascript inside as3, so that you do not need to touch the actual html page." Do you have links / tutorials? – okoman
Both AS and JS are based on the same ECMAScript standard. So, if you pass a string of AS3 to a container, and use JS's eval on this string, it should work just fine.
Just noticed this question and realized I answered almost the exact same thing here: https://stackoverflow.com/a/11460839/1449525
To paraphrase myself, you can definitely use D.eval, AS3Eval, or ExternalInterface (as seen in the currently chosen answer) assuming you're running in a web page. However, all it seems like you really need is something like this simple MathParser (More info about the MathParser)
Here's how you'd use the MathParser:
package {
import bkde.as3.parsers.*;
import flash.display.Sprite;
public class MathTest extends Sprite {
public function MathTest() {
var parser:MathParser = new MathParser([]);
var compiledObj:CompiledObject = parser.doCompile("(10/3)*4+10");
var answer:Number = parser.doEval(compiledObj.PolishArray, []);
var xyParser:MathParser = new MathParser(["x", "y"]);
var xyCompiledObj:CompiledObject = xyParser.doCompile("(x/3)*y+10");
var xyAnswer:Number = xyParser.doEval(xyCompiledObj.PolishArray, [10, 4]);
}
}
}
I'm sure ExternalInterface stuff works just fine, but I have personal reservations about the cross language communication (especially in terms of efficiency and security) as well as just the awkward nature of it. I feel like a wholly-contained, same-language solution is typically preferable in most situations.
A bit late, but for reference, the D.eval library does what you are asking for:
http://www.riaone.com/products/deval/
It is free and works great for me, but doesn't come with source. I found this question looking for an alternative built-in or source-available solution.
There is also a seemingly abandoned project to port Tamarin to Flash itself:
http://eval.hurlant.com/
Would be awesome if more progress was made, but seems like a curiosity for now.