[{"data":1,"prerenderedAt":123},["ShallowReactive",2],{"posts:\u002Fposts\u002F2016-05-06-sample-slicing-in-tone-js":3},{"id":4,"title":5,"body":6,"categories":102,"date":104,"description":105,"extension":106,"image":16,"lang":105,"meta":107,"navigation":108,"path":109,"seo":110,"stem":111,"tags":112,"titleInPicture":121,"__hash__":122},"posts\u002Fposts\u002F2016-05-06-sample-slicing-in-tone-js.md","Sample slicing in Tone.js",{"type":7,"value":8,"toc":97},"minimark",[9,17,20,41,46,49,52,55,66,69,72,75,79,89],[10,11,12],"p",{},[13,14],"img",{"alt":15,"src":16},"Capture","\u002Fimages\u002F2016\u002F04\u002FCapture.png",[10,18,19],{},"Today I needed to make a sample that would play a portion of a sample instead of playing the whole sample, from a nexus ui waveshape element. There are three problems that disallowed this:",[21,22,23,35,38],"ul",{},[24,25,26,27],"li",{},"Tone.js player object can do this natively, but when you start a sample from the middle, you will get a clicking noise, because the buffer value at the defined position is most likely not zero. ",[28,29,31],"a",{"href":30},"\u002Fimages\u002F2016\u002F05\u002FIMG_20160506_1725101-e1462552930485.jpg",[13,32],{"alt":33,"src":34},"IMG_20160506_1725101","\u002Fimages\u002F2016\u002F05\u002FIMG_20160506_1725101-e1462552930485-300x160.jpg",[24,36,37],{},"Tone.js sampler includes an envelope: with this, you can soften the initial click; and ramp the start of the sample from zero, but doesn't allow playing a sample from a start and end point.",[24,39,40],{},"Actually, the sampler allows this, but only when the sample is in loop mode.",[42,43,45],"h2",{"id":44},"solution","Solution",[10,47,48],{},"So, I made a fork of the Tone.js library, that allows the sampler to have start and end points. They are inserted when triggering attack, which is out of the Tone logic; but it allowed me to solve the problem fast, to continue my collaborative MsComposer95 project.",[10,50,51],{},"line 18504; replacement for Tone.Sampler.prototype.triggerAttack:",[10,53,54],{},"\u002F**\n*  Start the sample and simultaneously trigger the envelopes.\n*  @param {string=} sample The name of the sample to trigger, defaults to\n*                          the last sample used.\n*  @param {Time} [time=now] The time when the sample should start\n*  @param {number} [velocity=1] The velocity of the note\n*  @param {range} [] Define start and end points within the sample player, if you don't want to play it entirely\n*  @returns {Tone.Sampler} this\n*  @example\n* sampler.triggerAttack(\"B.1\");\n*\u002F\nTone.Sampler.prototype.triggerAttack = function (name, time, velocity, range) {\ntime = this.toSeconds(time);\nif (name) {\nthis.sample = name;\n}\nif( (range||{}).hasOwnProperty(\"start\")&&(range||{}).hasOwnProperty(\"end\") ){\nthis.player.start(0,range.start,range.end);\n}else{\nconsole.log(\"no range\");\nthis.player.start(time);\n}",[56,57,62],"pre",{"className":58,"code":60,"language":61},[59],"language-text","  this.envelope.triggerAttack(time, velocity);\n  this.filterEnvelope.triggerAttack(time);\n  return this;\n","text",[63,64,60],"code",{"__ignoreMap":65},"",[10,67,68],{},"};",[10,70,71],{},"So, you would make it work by calling it this way:",[10,73,74],{},"\u002F\u002Fwhere sampler is a Tone Sampler, and loopStart & loopEnd are defined\nsampler.triggerAttack(false,0,1,{start:loopStart,end:loopEnd});",[42,76,78],{"id":77},"future-needs","Future needs",[10,80,81,82,88],{},"My future push will consist of using the sampler's loopStart and loopEnd points instead of injecting the range object. You can grab the tweaked version in ",[28,83,87],{"href":84,"rel":85},"https:\u002F\u002Fgithub.com\u002Fautotel\u002FTone.js\u002Ftree\u002Fmaster\u002Fbuild",[86],"nofollow","my Tone.js fork",".",[10,90,91,92,88],{},"You can download a ",[28,93,96],{"href":94,"rel":95},"https:\u002F\u002Fgithub.com\u002Fautotel\u002Fslicer-simple-example\u002F",[86],"working example here",{"title":65,"searchDepth":98,"depth":98,"links":99},2,[100,101],{"id":44,"depth":98,"text":45},{"id":77,"depth":98,"text":78},[103],"blog","2016-05-06",null,"md",{},true,"\u002Fposts\u002F2016-05-06-sample-slicing-in-tone-js",{"title":5,"description":65},"posts\u002F2016-05-06-sample-slicing-in-tone-js",[113,114,115,116,117,118,119,120],"year_2016","context_aalto","area_experiment","context_for-the-sake-of-doing","tool_javascript","area_development","synth","tool_web-audio-api",false,"SfCF3eFjEjBA8oBIGNemmE0bIac1xYcQmQMAXexRMWc",1792083596428]