Error with if statements and operators


#1

I’m a beginner, so I hope this is simple and doesn’t take much of your time.

I’m working on a simple program to route keyboard inputs to specific notes and midi controls (octave, volume, etc.).

The notes themselves register just fine, but when I try to give it any other type of input, it gives an error.
Now that I’ve changed the code a little bit more, I managed to get both not working.

I get: ERROR: binary operator ‘+’ failed.

////1 Creates the keyboard -> midi from keyboard codes
(
e = Dictionary.new;
~midi = Dictionary.new;
~keyBoard = [
	[ $z, 122 ],
	[ $s, 115 ],
	[ $x , 120 ],
	[ $d , 100 ],
	[ $c , 99 ],
	[ $v ,118 ],
	[ $g ,103 ],
	[ $b ,98 ],
	[ $h ,104 ],
	[ $n ,110 ],
	[ $j ,106 ],
	[ $m ,109 ],
	[ $q ,113 ],
	[ $2 ,50 ],
	[ $w ,119 ],
	[ $3 ,51 ],
	[ $e ,101 ],
	[ $r ,114 ],
	[ $5 ,53 ],
	[ $t ,116 ],
	[ $6 ,54 ],
	[ $y ,121 ],
	[ $7 ,55 ],
	[ $u ,117 ],
	[ $i ,105 ],
	[ $9 ,57 ],
	[ $o ,111 ],
	[ $0 ,48 ],
	[ $p ,112 ]] 
;
~midiBtns = Dictionary.newFrom(List["l" ,[-1,"control"], "l" ,[1,"control"]]);

~interval=0;
~keyBoard.do{
	arg i;
	i[0].postln;
	~interval.postln;
	~midi.put(i[0].asString, [~interval, "note"]);
	~interval = ~interval+1;
};

~oct = 0;
~synth = \default;
~currentNote = 0;
)

~midi["z"][0] + 10
(
w = Window.new("I catch keystrokes");
~oct = 0;
w.view.keyDownAction_({ arg view, char;
	var type=~midi[char.asString][1], key=~midi[char.asString][0]; 
	
	type.postln;
	key.postln;
	if( type == "note", {
		var sig,env, note;
		note = 12*~oct + ~midi[key] + 60 ;
		sig = LFTri.ar(note.midicps)!2;
		env = EnvGen.kr(Env.perc, doneAction:2);
		sig = sig*env;
	}.play);

	if( type == "control" , {~oct = ~oct + ~midi[char.asString][0]});

});

w.front;
)

Any input is appreciated.


#2

I think that there is a typo or that you forgot that you already extracted the number in the definition of the variable “key”. The following part

note = 12*~oct + ~midi[key] + 60 ;

should be

note = 12 * ~oct + key + 60 ;

It is amazing that this code is written by a beginner! Congratulation!


#3

Thanks for the feedback. It was that and the fact I was using 2 different dictionaries.

But it’s still evaluating the if statement when I don’t want it to. It’s changing the note value by + or - 1, when it should only be changing the octave.

New Code:
////1 Creates the keyboard -> midi from keyboard codes
(
e = Dictionary.new;
~midi = Dictionary.new;
~keyBoard = [
[ $z, 122 ],
[ $s, 115 ],
[ $x , 120 ],
[ $d , 100 ],
[ $c , 99 ],
[ $v ,118 ],
[ $g ,103 ],
[ $b ,98 ],
[ $h ,104 ],
[ $n ,110 ],
[ $j ,106 ],
[ $m ,109 ],
[ $q ,113 ],
[ $2 ,50 ],
[ $w ,119 ],
[ $3 ,51 ],
[ $e ,101 ],
[ $r ,114 ],
[ $5 ,53 ],
[ $t ,116 ],
[ $6 ,54 ],
[ $y ,121 ],
[ $7 ,55 ],
[ $u ,117 ],
[ $i ,105 ],
[ $9 ,57 ],
[ $o ,111 ],
[ $0 ,48 ],
[ $p ,112 ]]
;
~midi.put("," ,[-1,“control”]);
~midi.put("." ,[1,“control”]);
//~midiBtns = Dictionary.newFrom(List["," ,[-1,“control”], “.” ,[1,“control”]]);

~interval=0;
~keyBoard.do{
	arg i;
	i[0].postln;
	~interval.postln;
	~midi.put(i[0].asString, [~interval, "note"]);
	~interval = ~interval+1;
};

~oct = 0;
~synth = \default;
~currentNote = 0;
)

~midi["z"][0] + 10
(
w = Window.new("I catch keystrokes");
~oct = 0;
w.view.keyDownAction_({ arg view, char;
	var type=~midi[char.asString][1], key=~midi[char.asString][0];

	type.postln;
	key.postln;
	if( type == "note", {
		var sig,env, note;
		note = 12 * ~oct + key + 60 ;
		sig = LFTri.ar(note.midicps)!2;
		env = EnvGen.kr(Env.perc, doneAction:2);
		sig = sig*env;
	}.play);

	if( type == "control" , {~oct = ~oct + key});

});

w.front;
)