{
   "id" : "1-5gx",
   "is_private" : null,
   "code" : "(\r\nw = Window(\"An Instant Sound File Player (Looper)\", Rect(0,Window.screenBounds.height-180,Window.screenBounds.width-100,Window.screenBounds.height-180)).front;\r\nw.view.decorator = FlowLayout( w.view.bounds, 10@10, 20@5 );\r\n\r\nq = Bus.control(s, 1);\r\n\r\na = Button(w, Rect(20, 20, 340, 30))\r\n.states_([\r\n\t[\"Load a Sound File into a Buffer\", Color.white, Color.black]\r\n])\r\n.action_({\r\n\ts.waitForBoot{\r\n\t\tif(y!=nil, {y.free; y=nil});\r\n\t\tb= Buffer.loadDialog(s,\r\n\t\t\taction:{\r\n\t\t\t\tb.loadToFloatArray(\r\n\t\t\t\t\taction: {\r\n\t\t\t\t\t\t|a|\r\n\t\t\t\t\t\t{ e.waveColors_(Color.green(1, 0.3)!b.numChannels);\r\n\t\t\t\t\t\t\tv.waveColors_(Color.green(1, 0.5)!b.numChannels);\r\n\t\t\t\t\t\t\te.setData(a, channels: b.numChannels);\r\n\t\t\t\t\t\t\tv.setData(a, channels: b.numChannels);\r\n\t\t\t\t\t\t\te.setSelection(0, [0,b.numFrames]);\r\n\t\t\t\t\t\t\te.zoomAllOut;\r\n\t\t\t\t\t\t\tv.setSelection(0, [0,0])\r\n\t\t\t\t}.defer })\r\n\t\t\t}\r\n\t\t);\r\n\t\t\"is loading a sound file\".postln }\r\n}\r\n);\r\nm = [10, 10, 30, 30];\r\nn = 100;\r\n\r\ne = SoundFileView(w, Rect(m[0], m[1], w.bounds.width-m[2], n));\r\ne.timeCursorOn_(true);\r\ne.setSelectionColor(0, Color.gray(0.8, 0.2));\r\ne.gridOn_(true);\r\ne.gridResolution_(60);\r\ne.mouseUpAction = {|view, char, modifiers, unicode, keycode, key|\r\n\tvar posData, posLo, posHi;\r\n\tposData = [e.selections[0][0], (e.selections[0][0] + e.selections[0][1])] / e.numFrames;\r\n\tposData;\r\n\t\tv.zoomToFrac(posData[1] - posData[0]);\r\n\t\tv.scrollTo (posData[0]);\r\n\t\tv.scroll (posData[0]);\r\n};\r\n\r\n\r\nv = SoundFileView(w, Rect(m[0], m[2], w.bounds.width-m[2], w.bounds.height-(m[3]*2)-n));\r\nv.timeCursorOn_(true).setSelectionColor(0, Color.gray(0.8, 0.5));\r\nv.gridOn_(true);\r\nv.gridResolution_(10);\r\n\r\nx = { |lo = 0, hi = 1|\r\n\tvar phasor = Phasor.ar(0, BufRateScale.kr(b), lo * BufFrames.kr(b), hi *BufFrames.kr(b));\r\n\tOut.kr(q.index,A2K.kr(phasor));\r\n\tBufRd.ar(b.numChannels, b, phasor) };\r\n\r\nv.mouseUpAction = {|view, char, modifiers, unicode, keycode, key|\r\n\tvar posData, posLo, posHi;\r\n\tposData = [v.selections[0][0], (v.selections[0][0] + v.selections[0][1])] / v.numFrames;\r\n\tposData;\r\n\tv.selections[0][0]; v.selections[0][1];\r\n\tif (y==nil, {y = x.play(args: [\\lo, posData[0], \\hi, posData[1]])}, {y.set(\\lo, posData[0], \\hi, posData[1]); });\r\n};\r\nw.onClose_({y.free; b=nil; c=nil; e=nil; y=nil; x=nil; v=nil; w=nil; AppClock.clear});\r\n\r\n(\r\nAppClock.sched(0.0, {\r\n\tq.get({arg val; {v.timeCursorPosition=val; e.timeCursorPosition=val}.defer;});\r\n\t0.05;\r\n});\r\n)\r\n)",
   "labels" : [
      "looper"
   ],
   "ancestor_list" : [],
   "description" : "originated from <https://listarc.cal.bham.ac.uk/lists/sc-users-2016/msg51590.html> and <https://scsynth.org/t/display-playhead-position-in-gui-in-a-synth-that-uses-playbuf/6548/17?u=prko>.",
   "author" : "prko",
   "name" : "Automatic looper by selecting a part of a sound file"
}
