Coder Social home page Coder Social logo

Comments (8)

oampo avatar oampo commented on July 21, 2024

Yeah, this is a bit ugly - I go back and forth over how the right way to do this is. I don't really want to have to pass sampleRate around all the time. Possibly what should happen is that the Audiolet object is should be a singleton, which the nodes can grab if they need the sample rate, scheduler, graph etc. That does seem a little bit close to just having a massive global though.

I think that any changes to this will wait until a good sized rewrite of the code takes place (I have vague plans for Audiolet 2), as IMO the API win from this is negated by losing compatibility with existing code. Will leave the issue open for now though and have a think about it.

from audiolet.

mattdiamond avatar mattdiamond commented on July 21, 2024

Wouldn't it be easiest to have all object creation occur through the instantiated Audiolet object? In other words, instead of:

var sine = new Sine(audiolet, 440)

do this:

var sine = new audiolet.Sine(440)

or even this:

var sine = audiolet.createSine(440)

This seems like the most obvious strategy to me, though perhaps I'm missing something.

from audiolet.

kn0ll avatar kn0ll commented on July 21, 2024

i considered that route as well, but ultimately, it doesn't solve the inconvenience. this would still require anything creating nodes to have reference to that audiolet graph object, forcing you to pass it all over the place.

while on one hand i realize in a scenario with multiple graphs, this might be totally unavoidable... it might not be bad for audiolet to only assume 1 graph in any application. i realize this is also not ideal, but given the sink.js limitations, it does make some sense. (ie. if you can only have 1 master output, assuming 1 graph might not be inherently bad).

from audiolet.

mattdiamond avatar mattdiamond commented on July 21, 2024

@catshirt wouldn't creating nodes via audiolet object methods allow you to use/store that reference via this within the node constructor? Why would you still need to pass around an audiolet object?

Edit: I suppose your point is that you would still need the root audiolet object to call those methods on it. Yes, that's true. But I think anyone using this should be maintaining that object in an easily accessible place, and it just seems cleaner to call a method on it rather than pass it in as a parameter.

I don't think keeping around the root audiolet object is really an inconvenience... most javascript libaries require you to refer to either an instance of a library object, or to call static methods on a globally available library object. So whatever you do, you're probably going to need something that's globally accessible. The question is more how you use that object, rather than worrying about scope.

from audiolet.

kn0ll avatar kn0ll commented on July 21, 2024

you're right- but there are many parts of my application which create nodes outside of the context of an audiolet object. what it really boils down to here is that audiolet.Sine(440) and Sine(audiolet, 440), for all intents and purposes, are no different. i'm hoping for a simpler Sine(440).

from audiolet.

mattdiamond avatar mattdiamond commented on July 21, 2024

@catshirt see my edit above... and yeah, in that case you would need to maintain some sort of singleton Audiolet graph object.

Edit: I also think it's better to force the user to rely on a single library object, rather than pollute the global scope with a whole bunch of new object constructors. Just my opinion though.

I would also note that this is how the Web Audio API does it... you create a single audio context, and then use that to generate all of your nodes via object methods.

from audiolet.

kn0ll avatar kn0ll commented on July 21, 2024

a single graph would be the easiest solution. but i don't think it's the only one.

audiolet objects don't actually need the reference to the graph, as much as they need references to properties of that graph (ie. sampleRate), which could be obtained through other methods. for instance, audiolet could pass these parameters to the generate function during the recursive tick. a bit of work is involved here, and the generate() api would grow substantially, but it is a solution.

from audiolet.

oampo avatar oampo commented on July 21, 2024

How about using some sort of global namespace object, and getting rid of the Audiolet class all together? So something like:

var audiolet = {};

audiolet.Output= function() {
    audiolet.sampleRate = 44100;
};

audiolet.Sine = function(frequency) {
    print(frequency);
    print(audiolet.sampleRate);
};

output = new audiolet.Output();
sine = new audiolet.Sine(300);

If, like Nic, you just wanted each object as a global then we could maintain an optionally imported file which says:

var Output = audiolet.Output;
var Sine = audiolet.Sine;

I'm don't like the createWhateverNode style very much, because in Audiolet you can create custom nodes using the same syntax as it uses internally. So if you wanted to make a custom node you would also have to write a createWhateverNode function to keep a consistent API, which seems like an unnecessary step.

The obvious issue with the style I've suggested is that it doesn't allow multiple outputs running at different sample rates. That said, I don't see that happening in the Web Audio API any time soon, as I can see that it would present significant architectural challenges for not too much gain.

from audiolet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.