Controlling Synths and GUI colors in array of buttons

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Controlling Synths and GUI colors in array of buttons

Adampultz
Hello,

I am creating tuning lattice and using FlowLayout to generate a number of
buttons that trigger Synths.
The buttons have two states; one is monophonic and the other Polyphonic.

In Mono mode, I wish to have the button currently playing turn grey and then
again white, when another button is pressed.
In Poly mode, I want to have any button currently playing be grey and then
turn white again, when pressed a second time. In Poly, I also want to let
each button create a Synth when pressed and to release that Synth when
pressed a second time.
My problem is, that I do not understand how to access the states of
individual buttons. i.postln gives me the button number, but how do I tell
button 3 to turn white, when button 5 is pressed?
In Poly mode how, can I map button numbers to running Synths so that they
can be turned off, when pushing the button for the second time?
I have spent days searching for answers in the help files and forums but I
think I miss some fundamental aspect of SC.

Thank you

SynthDef (\tone1,
        {
                arg freq=220, gate= 1, amp=0.3, da = 2;
                var tone, envctl, env, out;
                envctl = Env.asr(1);
                env = EnvGen.kr(envctl, gate, doneAction: da);
                tone = SinOsc.ar(freq!2);
                out = Out.ar(0, tone*env)*amp;
        };
).add;

(
var n, b, buts, synths, ratios, rationames;

w = Window("tonality diamond", Rect(200,500,600,300));
c = CompositeView(w, Rect(0, 0, 600,300));
        c.decorator = FlowLayout(c.bounds);
b = Button(w, Rect(5, 180, 50, 50))
.states_([
            ["Mono"],
            ["Poly"]
]);

g = Group.new;



rationames=[
        "50/27", "25/18", "25/24", "75/64", "225/128",
        "10/9","5/3", "5/4", "15/8", "45/32",
        "16/9","4/3", "1/1", "3/2", "9/8",
        "65/45", "16/15", "8/5", "6/5", "9/5",
        "256/225", "128/75", "32/25", "48/25", "36/25"

];

n=rationames.size;

x = Synth(\tone1, \amp, 0);

n.do({ |i|

        e = Button(c, Rect(20,20+(i*30),100,30))
       
                .states_([[rationames[i], Color.black, Color.white],
        [rationames[i], Color.black,
                Color.grey]
        ]
        )


.mouseDownAction_({ arg view, buttonNumber, changeColor, clickCount, butt,
string, stateArray, fund = 220;
                i.asSymbol.postln;
                if((b.value) ==  0,
                        {x.set(\freq, ((rationames[i]).interpret)*220, \da, 7); g.release},
                        {y = Synth(\tone1, [\freq, ((rationames[i]).interpret)*220], g,
\addToTail); e.states_([[rationames[i], Color.black, Color.grey],
        [rationames[i], Color.black,
                                        Color.white]]);
                        }
                )

})

});

w.front;
w.onClose={x.release; g.release;};
)



--
Sent from: http://new-supercollider-mailing-lists-forums-use-these.2681727.n2.nabble.com/SuperCollider-Users-New-Use-this-f2676391.html

_______________________________________________
sc-users mailing list

info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
search: http://www.listarc.bham.ac.uk/lists/sc-users/search/
Reply | Threaded
Open this post in threaded view
|

Re: Controlling Synths and GUI colors in array of buttons

Fredrik Olofsson
hi,
first a few small comments...

> out = Out.ar(0, tone*env)*amp;

here your *amp is in the wrong place.  probably what you meant was...
out = Out.ar(0, tone*env*amp);

> .mouseDownAction_

safer to use .action_ (see https://www.listarc.bham.ac.uk/lists/sc-dev/msg57975.html )

> c.decorator = FlowLayout(c.bounds);

instead of FlowLayout try out the newer VLayout and HLayout.  they're easier to work with (see examples in HLayout helpfile).

> w.onClose={x.release; g.release;};

actually, one of the points of a group is to release all synths (nodes) including it.  so if you create your synths inside the group like this...
Synth(\tone1, [\freq, rationames[i].interpret*220, \gate, 0, \da, 0], g);
then you can skip x.release and it's enough with g.release

> x = Synth(\tone1, \amp, 0);

this is not correct.  arguments should be inside of an array and in key, value pairs.  like this...
Synth(\tone1, [\amp, 0]);


here's your example with the above suggestions and rewritten using MVC.  note there are other, simpler ways to code what you want but SimpleController is a neat and useful class that's worth studying.

SynthDef (\tone1,
        {
                arg freq=220, gate= 0, amp=0.3;
                var tone, envctl, env, out;
                envctl = Env.asr(1);
                env = EnvGen.kr(envctl, gate);
                tone = SinOsc.ar(freq!2);
                out = Out.ar(0, tone*env*amp);
        };
).add;

(
var w, c, b, g, n, rationames, synths, models, controllers, views;

w = Window("tonality diamond", Rect(200,500,600,300));
c = CompositeView(w, Rect(0, 0, 600,300));
c.decorator = FlowLayout(c.bounds);
b = Button(w, Rect(5, 180, 50, 50))
.states_([
        ["Mono"],
        ["Poly"]
]);

g = Group.new;

rationames=[
        "50/27", "25/18", "25/24", "75/64", "225/128",
        "10/9","5/3", "5/4", "15/8", "45/32",
        "16/9","4/3", "1/1", "3/2", "9/8",
        "65/45", "16/15", "8/5", "6/5", "9/5",
        "256/225", "128/75", "32/25", "48/25", "36/25"
       
];

n=rationames.size;

synths= Array.fill(n, {|i|
        Synth(\tone1, [\freq, rationames[i].interpret*220, \gate, 0], g);
});

models= Array.fill(n, {|i|
        Ref(0);
});

controllers= Array.fill(n, {|i|
        SimpleController(models[i]).put(\value, {|ref|
                views[i].value= ref.value;
                synths[i].set(\gate, ref.value);
        });
});

views= Array.fill(n, { |i|
        Button(c, Rect(20,20+(i*30),100,30))
        .states_([[rationames[i], Color.black, Color.white],
                [rationames[i], Color.black, Color.grey]
        ])
        .action_({ arg view;
                if((b.value) ==  0,
                        {
                                models.do{|r, j|
                                        if(i==j, {
                                                r.value_(view.value).changed(\value);
                                        }, {
                                                if(view.value==1, {
                                                        if(r.value==view.value, {
                                                                r.value_(1-view.value).changed(\value);
                                                        });
                                                });
                                        });
                                };
                        },
                        {
                                models.do{|r, j|
                                        if(i==j, {
                                                r.value_(view.value).changed(\value);
                                        });
                                };
                        }
                )
        });
       
});

w.front;
w.onClose={g.release; controllers.do{|o| o.remove}};
CmdPeriod.doOnce({w.close});
)


> 14 jan. 2018 kl. 03:56 skrev [hidden email]:
>
> Hello,
>
> I am creating tuning lattice and using FlowLayout to generate a number of
> buttons that trigger Synths.
> The buttons have two states; one is monophonic and the other Polyphonic.
>
> In Mono mode, I wish to have the button currently playing turn grey and then
> again white, when another button is pressed.
> In Poly mode, I want to have any button currently playing be grey and then
> turn white again, when pressed a second time. In Poly, I also want to let
> each button create a Synth when pressed and to release that Synth when
> pressed a second time.
> My problem is, that I do not understand how to access the states of
> individual buttons. i.postln gives me the button number, but how do I tell
> button 3 to turn white, when button 5 is pressed?
> In Poly mode how, can I map button numbers to running Synths so that they
> can be turned off, when pushing the button for the second time?
> I have spent days searching for answers in the help files and forums but I
> think I miss some fundamental aspect of SC.
>
> Thank you
>
> SynthDef (\tone1,
> {
> arg freq=220, gate= 1, amp=0.3, da = 2;
> var tone, envctl, env, out;
> envctl = Env.asr(1);
> env = EnvGen.kr(envctl, gate, doneAction: da);
> tone = SinOsc.ar(freq!2);
> out = Out.ar(0, tone*env)*amp;
> };
> ).add;
>
> (
> var n, b, buts, synths, ratios, rationames;
>
> w = Window("tonality diamond", Rect(200,500,600,300));
> c = CompositeView(w, Rect(0, 0, 600,300));
> c.decorator = FlowLayout(c.bounds);
> b = Button(w, Rect(5, 180, 50, 50))
> .states_([
>            ["Mono"],
>            ["Poly"]
> ]);
>
> g = Group.new;
>
>
>
> rationames=[
> "50/27", "25/18", "25/24", "75/64", "225/128",
> "10/9","5/3", "5/4", "15/8", "45/32",
> "16/9","4/3", "1/1", "3/2", "9/8",
> "65/45", "16/15", "8/5", "6/5", "9/5",
> "256/225", "128/75", "32/25", "48/25", "36/25"
>
> ];
>
> n=rationames.size;
>
> x = Synth(\tone1, \amp, 0);
>
> n.do({ |i|
>
> e = Button(c, Rect(20,20+(i*30),100,30))
>
> .states_([[rationames[i], Color.black, Color.white],
> [rationames[i], Color.black,
> Color.grey]
> ]
> )
>
>
> .mouseDownAction_({ arg view, buttonNumber, changeColor, clickCount, butt,
> string, stateArray, fund = 220;
> i.asSymbol.postln;
> if((b.value) ==  0,
> {x.set(\freq, ((rationames[i]).interpret)*220, \da, 7); g.release},
> {y = Synth(\tone1, [\freq, ((rationames[i]).interpret)*220], g,
> \addToTail); e.states_([[rationames[i], Color.black, Color.grey],
> [rationames[i], Color.black,
> Color.white]]);
> }
> )
>
> })
>
> });
>
> w.front;
> w.onClose={x.release; g.release;};
> )
>
>
>
> --
> Sent from: http://new-supercollider-mailing-lists-forums-use-these.2681727.n2.nabble.com/SuperCollider-Users-New-Use-this-f2676391.html
>
> _______________________________________________
> sc-users mailing list
>
> info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
> archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
> search: http://www.listarc.bham.ac.uk/lists/sc-users/search/


  #|
     fredrikolofsson.com     musicalfieldsforever.com
  |#


_______________________________________________
sc-users mailing list

info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
search: http://www.listarc.bham.ac.uk/lists/sc-users/search/
Reply | Threaded
Open this post in threaded view
|

Re: Controlling Synths and GUI colors in array of buttons

Adampultz
Thank you so much, this is hugely helpful and informative!


Fredrik Olofsson wrote

> hi,
> first a few small comments...
>
>> out = Out.ar(0, tone*env)*amp;
>
> here your *amp is in the wrong place.  probably what you meant was...
> out = Out.ar(0, tone*env*amp);
>
>> .mouseDownAction_
>
> safer to use .action_ (see
> https://www.listarc.bham.ac.uk/lists/sc-dev/msg57975.html )
>
>> c.decorator = FlowLayout(c.bounds);
>
> instead of FlowLayout try out the newer VLayout and HLayout.  they're
> easier to work with (see examples in HLayout helpfile).
>
>> w.onClose={x.release; g.release;};
>
> actually, one of the points of a group is to release all synths (nodes)
> including it.  so if you create your synths inside the group like this...
> Synth(\tone1, [\freq, rationames[i].interpret*220, \gate, 0, \da, 0], g);
> then you can skip x.release and it's enough with g.release
>
>> x = Synth(\tone1, \amp, 0);
>
> this is not correct.  arguments should be inside of an array and in key,
> value pairs.  like this...
> Synth(\tone1, [\amp, 0]);
>
>
> here's your example with the above suggestions and rewritten using MVC.
> note there are other, simpler ways to code what you want but
> SimpleController is a neat and useful class that's worth studying.
>
> SynthDef (\tone1,
> {
> arg freq=220, gate= 0, amp=0.3;
> var tone, envctl, env, out;
> envctl = Env.asr(1);
> env = EnvGen.kr(envctl, gate);
> tone = SinOsc.ar(freq!2);
> out = Out.ar(0, tone*env*amp);
> };
> ).add;
>
> (
> var w, c, b, g, n, rationames, synths, models, controllers, views;
>
> w = Window("tonality diamond", Rect(200,500,600,300));
> c = CompositeView(w, Rect(0, 0, 600,300));
> c.decorator = FlowLayout(c.bounds);
> b = Button(w, Rect(5, 180, 50, 50))
> .states_([
> ["Mono"],
> ["Poly"]
> ]);
>
> g = Group.new;
>
> rationames=[
> "50/27", "25/18", "25/24", "75/64", "225/128",
> "10/9","5/3", "5/4", "15/8", "45/32",
> "16/9","4/3", "1/1", "3/2", "9/8",
> "65/45", "16/15", "8/5", "6/5", "9/5",
> "256/225", "128/75", "32/25", "48/25", "36/25"
>
> ];
>
> n=rationames.size;
>
> synths= Array.fill(n, {|i|
> Synth(\tone1, [\freq, rationames[i].interpret*220, \gate, 0], g);
> });
>
> models= Array.fill(n, {|i|
> Ref(0);
> });
>
> controllers= Array.fill(n, {|i|
> SimpleController(models[i]).put(\value, {|ref|
> views[i].value= ref.value;
> synths[i].set(\gate, ref.value);
> });
> });
>
> views= Array.fill(n, { |i|
> Button(c, Rect(20,20+(i*30),100,30))
> .states_([[rationames[i], Color.black, Color.white],
> [rationames[i], Color.black, Color.grey]
> ])
> .action_({ arg view;
> if((b.value) ==  0,
> {
> models.do{|r, j|
> if(i==j, {
> r.value_(view.value).changed(\value);
> }, {
> if(view.value==1, {
> if(r.value==view.value, {
> r.value_(1-view.value).changed(\value);
> });
> });
> });
> };
> },
> {
> models.do{|r, j|
> if(i==j, {
> r.value_(view.value).changed(\value);
> });
> };
> }
> )
> });
>
> });
>
> w.front;
> w.onClose={g.release; controllers.do{|o| o.remove}};
> CmdPeriod.doOnce({w.close});
> )
>
>
>> 14 jan. 2018 kl. 03:56 skrev

> mail@

> :
>>
>> Hello,
>>
>> I am creating tuning lattice and using FlowLayout to generate a number of
>> buttons that trigger Synths.
>> The buttons have two states; one is monophonic and the other Polyphonic.
>>
>> In Mono mode, I wish to have the button currently playing turn grey and
>> then
>> again white, when another button is pressed.
>> In Poly mode, I want to have any button currently playing be grey and
>> then
>> turn white again, when pressed a second time. In Poly, I also want to let
>> each button create a Synth when pressed and to release that Synth when
>> pressed a second time.
>> My problem is, that I do not understand how to access the states of
>> individual buttons. i.postln gives me the button number, but how do I
>> tell
>> button 3 to turn white, when button 5 is pressed?
>> In Poly mode how, can I map button numbers to running Synths so that they
>> can be turned off, when pushing the button for the second time?
>> I have spent days searching for answers in the help files and forums but
>> I
>> think I miss some fundamental aspect of SC.
>>
>> Thank you
>>
>> SynthDef (\tone1,
>> {
>> arg freq=220, gate= 1, amp=0.3, da = 2;
>> var tone, envctl, env, out;
>> envctl = Env.asr(1);
>> env = EnvGen.kr(envctl, gate, doneAction: da);
>> tone = SinOsc.ar(freq!2);
>> out = Out.ar(0, tone*env)*amp;
>> };
>> ).add;
>>
>> (
>> var n, b, buts, synths, ratios, rationames;
>>
>> w = Window("tonality diamond", Rect(200,500,600,300));
>> c = CompositeView(w, Rect(0, 0, 600,300));
>> c.decorator = FlowLayout(c.bounds);
>> b = Button(w, Rect(5, 180, 50, 50))
>> .states_([
>>            ["Mono"],
>>            ["Poly"]
>> ]);
>>
>> g = Group.new;
>>
>>
>>
>> rationames=[
>> "50/27", "25/18", "25/24", "75/64", "225/128",
>> "10/9","5/3", "5/4", "15/8", "45/32",
>> "16/9","4/3", "1/1", "3/2", "9/8",
>> "65/45", "16/15", "8/5", "6/5", "9/5",
>> "256/225", "128/75", "32/25", "48/25", "36/25"
>>
>> ];
>>
>> n=rationames.size;
>>
>> x = Synth(\tone1, \amp, 0);
>>
>> n.do({ |i|
>>
>> e = Button(c, Rect(20,20+(i*30),100,30))
>>
>> .states_([[rationames[i], Color.black, Color.white],
>> [rationames[i], Color.black,
>> Color.grey]
>> ]
>> )
>>
>>
>> .mouseDownAction_({ arg view, buttonNumber, changeColor, clickCount,
>> butt,
>> string, stateArray, fund = 220;
>> i.asSymbol.postln;
>> if((b.value) ==  0,
>> {x.set(\freq, ((rationames[i]).interpret)*220, \da, 7); g.release},
>> {y = Synth(\tone1, [\freq, ((rationames[i]).interpret)*220], g,
>> \addToTail); e.states_([[rationames[i], Color.black, Color.grey],
>> [rationames[i], Color.black,
>> Color.white]]);
>> }
>> )
>>
>> })
>>
>> });
>>
>> w.front;
>> w.onClose={x.release; g.release;};
>> )
>>
>>
>>
>> --
>> Sent from:
>> http://new-supercollider-mailing-lists-forums-use-these.2681727.n2.nabble.com/SuperCollider-Users-New-Use-this-f2676391.html
>>
>> _______________________________________________
>> sc-users mailing list
>>
>> info (subscription, etc.):
>> http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
>> archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
>> search: http://www.listarc.bham.ac.uk/lists/sc-users/search/
>
>
>   #|
>      fredrikolofsson.com     musicalfieldsforever.com
>   |#
>
>
> _______________________________________________
> sc-users mailing list
>
> info (subscription, etc.):
> http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
> archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
> search: http://www.listarc.bham.ac.uk/lists/sc-users/search/





--
Sent from: http://new-supercollider-mailing-lists-forums-use-these.2681727.n2.nabble.com/SuperCollider-Users-New-Use-this-f2676391.html

_______________________________________________
sc-users mailing list

info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
search: http://www.listarc.bham.ac.uk/lists/sc-users/search/
Reply | Threaded
Open this post in threaded view
|

Re: Controlling Synths and GUI colors in array of buttons

BROKEN CODE
In reply to this post by Fredrik Olofsson