{
   "labels" : [
      "howto",
      "amplitude",
      "threshold",
      "detection",
      "attack",
      "onset",
      "gui"
   ],
   "is_private" : null,
   "id" : "1-G",
   "code" : "// VIDEO: http://www.youtube.com/watch?v=pFLj7yJzp60\r\n\r\n(\r\ns = Server.local;\r\n~threshTest = nil;\r\ns.waitForBoot({\r\n\t\r\n\tSynthDef(\\threshTest, {\r\n\t\targ thresh=(-30);\r\n\t\tvar sig, amp, trig;\r\n\t\tsig = SoundIn.ar(0);\r\n\t\tamp = Amplitude.kr(sig).ampdb;\r\n\t\tamp = K2A.ar(amp);\r\n\t\ttrig = amp >= thresh;\r\n\t\tSendReply.ar(Impulse.ar(50), 'trigVal', [trig]);\r\n\t}).add;\r\n\t\r\n\tw = Window.new(\"amplitude threshold tester\", Rect(600, 250, 950, 220)).front;\r\n\tw.view.background_(Color.gray(0.75));\r\n\tw.onClose_({s.freeAll});\r\n\t\r\n\t~numberBox = NumberBox.new(w, Rect(180, 25, 450, 120))\r\n\t\t.font_(Font(\"Helvetica\", 108))\r\n\t\t.value_(-30)\r\n\t\t.action_({\r\n\t\t\t|nb|\r\n\t\t\tnb.value.postln;\r\n\t\t\t~knob.value_((nb.value.dbamp) / (~maxValSlider.value.dbamp));\r\n\t\t\tif(~threshTest.notNil,\r\n\t\t\t\t{\r\n\t\t\t\t\tif(~threshTest.group.class == Group,\r\n\t\t\t\t\t\t{~threshTest.set(\\thresh, nb.value)},\r\n\t\t\t\t\t\t{}\r\n\t\t\t\t\t\t);\r\n\t\t\t\t},\r\n\t\t\t\t{}\r\n\t\t\t);\r\n\t\t});\r\n\t\t\r\n\t\t~knob = Knob.new(w, Rect(20, 20, 150, 150))\r\n\t\t\t.background_(Color.gray(0.75))\r\n\t\t\t.color_([Color.blue(0.9,0.5), Color.green(0.65), Color.gray(0.6), Color.black])\r\n\t\t\t.value_(~numberBox.value.dbamp)\r\n\t\t\t.action_({\r\n\t\t\t\t|knob|\r\n\t\t\t\t~numberBox.valueAction_((knob.value * ~maxValSlider.value.dbamp).ampdb);\r\n\t\t\t});\r\n\t\t\t\r\n\t\t~maxValSlider = EZSlider(\r\n\t\t\tw, Rect(20, 160, 612, 40), \"knob max\",\r\n\t\t\tControlSpec.new(-90,0,\\lin,0.1,0,\"\"),\r\n\t\t\t{\r\n\t\t\t\t|slider|\r\n\t\t\t\t~numberBox.valueAction_((~knob.value * slider.value.dbamp).ampdb)\r\n\t\t\t},\r\n\t\t\t1, false, 120, 65, 0, 20, \\horz, 2@2, 2@2\r\n\t\t\t)\r\n\t\t\t\t.setColors(knobColor:Color.blue(0.9,0.5))\r\n\t\t\t\t.font_(Font(\"Helvetica\", 24));\r\n\t\t\r\n\t\t~testButton = Button.new(w, Rect(650, 25, 160, 40))\r\n\t\t\t.states_([\r\n\t\t\t\t[\"start test\", Color.white, Color.green(0.65)],\r\n\t\t\t\t[\"stop test\", Color.white, Color.red(0.65)]\r\n\t\t\t])\r\n\t\t\t.action_({\r\n\t\t\t\t|but|\r\n\t\t\t\tcase\r\n\t\t\t\t{but.value == 0} {~threshTest.free}\r\n\t\t\t\t{but.value == 1} {~threshTest = Synth(\\threshTest, [\\thresh, ~numberBox.value])};\r\n\t\t\t})\r\n\t\t\t.font_(Font(\"Helvetica\", 32));\r\n\t\t\t\r\n\t\t~received = TextView(w, Rect(750, 95, 170, 60))\r\n\t\t\t.visible_(false)\r\n\t\t\t.editable_(false)\r\n\t\t\t.string_(\"received\")\r\n\t\t\t.font_(Font(\"Helvetica\", 42))\r\n\t\t\t.stringColor_(Color.black)\r\n\t\t\t.background_(Color.gray(0.725));\r\n\t\t\r\n\t\tw.drawHook = {\r\n\t\t\t|v|\r\n\t\t\tPen.fillColor = Color.gray(0.75);\r\n\t\t\tPen.strokeColor = Color.black;\r\n\t\t\tPen.width = 3;\r\n\t\t\tPen.addArc(700@125, 35, 0, 2pi);\r\n\t\t\tPen.fillStroke;\r\n\t\t};\r\n\t\tw.refresh;\r\n\t\t\r\n\t\t~oscr1.remove;\r\n\t\t\r\n\t\t~oscr1 = OSCresponder(nil, 'trigVal', {\r\n\t\t\t|time, resp, msg|\r\n\t\t\tif(\r\n\t\t\t\tmsg[3] == 1,\r\n\t\t\t\t{\r\n\t\t\t\t\tRoutine {\r\n\t\t\t\t\t\tw.drawHook = { |v|\r\n\t\t\t\t\t\t\tPen.fillColor = Color.green;\r\n\t\t\t\t\t\t\tPen.strokeColor = Color.black;\r\n\t\t\t\t\t\t\tPen.width = 3;\r\n\t\t\t\t\t\t\tPen.addArc(700@125, 35, 0, 2pi);\r\n\t\t\t\t\t\t\tPen.fillStroke;\r\n\t\t\t\t\t\t};\r\n\t\t\t\t\t\tw.refresh;\r\n\t\t\t\t\t\t~received.visible_(true);\r\n\t\t\t\t\t\t0.1.wait;\r\n\t\t\t\t\t\tw.drawHook = { |v|\r\n\t\t\t\t\t\t\tPen.fillColor = Color.gray(0.75);\r\n\t\t\t\t\t\t\tPen.strokeColor = Color.black;\r\n\t\t\t\t\t\t\tPen.width = 3;\r\n\t\t\t\t\t\t\tPen.addArc(700@125, 35, 0, 2pi);\r\n\t\t\t\t\t\t\tPen.fillStroke;\r\n\t\t\t\t\t\t};\r\n\t\t\t\t\t\tw.refresh;\r\n\t\t\t\t\t\t~received.visible_(false);\r\n\t\t\t\t\t}.play(AppClock);\r\n\t\t\t\t},\r\n\t\t\t\t{}\r\n\t\t\t);\r\n\t\t}).add;\t\t\r\n})\r\n)",
   "author" : "eli.fieldsteel",
   "name" : "Attack Detection and Amplitude Threshold Setter GUI",
   "description" : "A modular GUI designed to detect amplitude attacks and set a globally retrievable threshold value.",
   "ancestor_list" : []
}
