<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Benjamin Farrell</title>
	<atom:link href="http://www.benfarrell.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.benfarrell.com</link>
	<description>{ creative developer }</description>
	<lastBuildDate>Tue, 15 May 2012 01:50:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Javascript Continuous Integration with Google Closure</title>
		<link>http://www.benfarrell.com/2012/05/14/javascript-continuous-integration-with-google-closure/</link>
		<comments>http://www.benfarrell.com/2012/05/14/javascript-continuous-integration-with-google-closure/#comments</comments>
		<pubDate>Mon, 14 May 2012 02:13:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[dojo]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.benfarrell.com/?p=693</guid>
		<description><![CDATA[In less than a week, I&#8217;ll be speaking at cfObjective!  Why am I psyched about this, when in fact I don&#8217;t use Adobe&#8217;s server side tech Coldfusion at all?  Well &#8211; see, cfObjective features a whole track dedicated to Javascript&#8230;they dub it jsObjective.  Looks like I&#8217;m up there with some fantastic speakers! If you&#8217;re short [...]]]></description>
			<content:encoded><![CDATA[<p>In less than a week, I&#8217;ll be speaking at <a href="http://www.cfobjective.com/">cfObjective</a>!  Why am I psyched about this, when in fact I don&#8217;t use Adobe&#8217;s server side tech Coldfusion at all?  Well &#8211; see, cfObjective features a whole track dedicated to Javascript&#8230;they dub it <a href="http://www.cfobjective.com/schedule/jsobjective/">jsObjective</a>.  Looks like I&#8217;m up there with some fantastic speakers!</p>
<p>If you&#8217;re short on time, already saw my preso, or whatever&#8230; here&#8217;s some demos I showed:</p>
<p><a href="https://github.com/bengfarrell/cfo-typeenforce">Using Type Enforcement with Google Closure and JSDoc style comments</a></p>
<p><a href="https://github.com/bengfarrell/cfo-typeenforce">Using a Two-Pass Compilation Process to heavily compact your code but leave things like jQuery intact</a></p>
<p>Lastly, here&#8217;s my <a href="https://docs.google.com/presentation/d/1_tEFa7CU1Dgt52H73jDmAOc6WxJBELukQzMKo9xgSNQ/edit">presentation slides</a></p>
<p>What is this that I&#8217;m speaking about? Well I&#8217;m talking about a workflow I adopted when working on some fairly large or fairly complex applications.  I&#8217;m used to workflows where I can create a project with many files &#8211; all organized to work towards a company library.  This works great when you compile everything down to a binary &#8211; but not so well with Javascript when we aren&#8217;t in the practice of doing so.</p>
<p>With Javascript, you write it and run it.  While it can be pretty easy to tweak and then refresh the page to view your work, it&#8217;s not the best for my workflow because when I complete a project I might have 10, 30, 50, or even more Javascript files that are in use in my project.</p>
<p>Without any sort of compilation process, we&#8217;re talking about tons of separate file requests, a large bloated &lt;head&gt; tag full of &lt;script&gt; tags, and a fairly decent thrashing on our browser and server.</p>
<p>So, why not compile our JS?!</p>
<h3>Spacebag</h3>
<p>Have you ever heard of <a href="http://www.spacebag.com">Spacebag</a>?  Basically it&#8217;s a product that features a clear plastic air-tight bag.  You put your clothes in there, suck out all the air, and your left with a flattened bag that takes up much less space.  It&#8217;s easy to carry, organize, store, etc!</p>
<p>To me that&#8217;s just like Google&#8217;s Closure!  It takes your Javascript&#8230; all the different files you have&#8230;puts them together, and compresses it all down to be one tiny, organized, packaged file.  You can easily email it, send it to a friend, and use it on multiple projects or websites.</p>
<h3>Google Closure</h3>
<p>What exactly IS Google Closure?  It&#8217;s rather unfortunately named actually, but only because there&#8217;s two other Clo(j/s)ures in our web development world.  First there&#8217;s the concept of a <a href="http://en.wikipedia.org/wiki/Closure_(computer_science)">Closure</a>.  It&#8217;s commonly described as a function in a function, but there&#8217;s more to it than that.  Worse, Closures are very core to the Javascript language, which makes working with Google Closure a little confusing.</p>
<p>Then of course there&#8217;s <a href="http://clojure.org/">Clojure</a> - a language that runs on top of Java.  Lots of people love the functional syntax of Clojure, and it seems to be the new hotness in the same vein as Python, Scala, and Ruby.  Clojure even has a<a href="https://github.com/clojure/clojurescript"> Javascript port </a>which even further muddies up the Clo(s/j)ure waters.</p>
<p>Google Closure is a set of tools used internally on many of Google&#8217;s project which have been open sourced to the wider world.  In my presentation, I focus on one particular aspect &#8211; but to cover it all, here goes:</p>
<ul>
<li><a href="https://developers.google.com/closure/library/">Closure is a Library</a> &#8211; Much like jQuery, Dojo, Sencha, Backbone, Angular, etc&#8230;the Closure Library is a set of widgets, UI components, and utilities to help you create an awesome web application.</li>
<li><a href="https://developers.google.com/closure/templates/">Closure is a Templating Engine</a> - These templates can work on the client or server, in Javascript or Java.  The idea is that you create a template, it gets consumed by the templating engine, and this enables you to create reusable UI widgets.  Not having used it, I can&#8217;t say for certain, but it does look like an attractive alternative to tweaking/styling currently existing UI widgets that something like Dojo or Sencha would give you.</li>
<li><a href="http://code.google.com/p/closure-stylesheets/">Closure can do Stylesheets</a> &#8211; I&#8217;m not a big fan of LESS or SASS, but there seems to be a movement in our web development world to create stylesheets with logic.  That is, you take your existing CSS, and add conditional logic, variables, loops and more.  Google can take these logic laden GSS files and compile them out to CSS for use on your site</li>
<li><a href="https://developers.google.com/closure/utilities/">Closure is a Linter </a>- Linters are pretty cool.  Javascript lets you do a lot of things &#8211; good or bad.  They might break your application or not.  Some browsers might forgive these things, others might not.  You can even add some comments that dictate what your variable types are, and the Linter will listen.  Linters look at all of this stuff, and warn you about your mistakes.  You can choose to ignore if you want, but it&#8217;s usually a good idea to not ignore!</li>
<li><a href="https://developers.google.com/closure/compiler/">Closure is a Compiler</a> &#8211; The compiler is the thing I want to use, and the compiler can encompass all of the above.  Basically I want to take my 50 files, make them into one, and shrink things down (minify).  Yes, I want to run the Linter in the process, please!  The compiler will work with the Library and Templating Engine&#8230;.hell, it would probably work with most ANY library and ANY templating engine!</li>
</ul>
<h3>Lets Compile!</h3>
<p>Let&#8217;s look at some of the different ways to compile your JS via Google Closure:</p>
<ul>
<li><strong>Whitespace Only</strong> &#8211; This is the least complex, safest way to go.  When you do whitespace only, you&#8217;re essentially just minifying your Javascript. This means that your many files get compiled into one file.  Also, all unnecessary whitespace is removed.   This creates a lean, mean, easy to load file for your web application.  It&#8217;s only one file so you aren&#8217;t blocking tons of images from loading while you wait for your 50 js files to load.  It also shaves your filesize down.  That 15kb file you have that used to be 80kb, might not SEEM like that much savings, but it can save you and your users precious bandwidth.  It also makes your code unreadable.  This is good if you&#8217;d rather not have people stealing your intellectual property (unless they figure out that <a href="http://jsbeautifier.org/">JS Beautifiers</a> exist), and can be bad if you&#8217;re trying to debug code on that website you launched.</li>
<li><strong>Simple Mode</strong> &#8211; This is &#8220;Whitespace only&#8221; mode PLUS some.  So it does everything that whitespace mode does, but it also starts renaming your variables!  This sounds worse than it is &#8211; but it can still be a little terrifying to trust.  In this case, Closure will analyze your code.  If it sees local variables and methods (those that can&#8217;t be accessed outside of the scope), it will rename them.  Your variable named &#8220;reallyAwesomeVariableThatTellsMeIfSomethingIsTrue&#8221; will be renamed and shortened to &#8220;a&#8221;.  This is pretty cool because it encourages you to use expressive and clear variable names (hopefully not as expressive as my example), but also gives you the benefit of a small compiled file.  Again, it can get harder to debug your code in production if your variables have had their names changed!</li>
<li><strong>Advanced Mode</strong> &#8211; So this is the MOST terrifying of all the modes.  This mode will do &#8220;Whitespace only&#8221; + &#8220;Simple mode&#8221; + MORE!  It will get rid of your whitespace and rename your local variables.  It will in fact, rename ALL your variables.  And it will cut unused code &#8211; reducing your code to it&#8217;s bare minimum logical essence.  I don&#8217;t prefer to use this mode because in addition to going farther with things than I really need, I prefer to create Javascript libraries.  These standalone packages, when they get compiled, my page will not be using most of the code &#8211; or maybe even all of the code.  So, like you might expect, I&#8217;d end up with nothing.  There are things that you can do to enforce the parts you want to stay &#8211; but that&#8217;s overkill to me.  I want simplicity especially for my smaller projects.</li>
</ul>
<p>To see the compilation in action, go ahead and try out the <a href="http://closure-compiler.appspot.com/home">Compiler Service web page</a>.  Even just using the example they have setup on the page is illuminating!</p>
<h3>Linting and Documentation</h3>
<p>A side effect of the Linting is the warnings that the compiler will output for you.  You can choose to ignore them if you like, but they can be pretty useful.  I left a semi-colon out once that would have made IE fail, and the Linting caught it saving me from hours of frustration.</p>
<p>You can go further though.  Using JSDoc markup lets you type your variables, methods, and parameters.  As you probably know, Javascript is not a typed language.  This means that I can have a variable &#8220;var myVar = something&#8221; and put whatever I want in there&#8230;a string, a number, an object, whatever.  Javascript won&#8217;t complain.  Compare this to a typed language where &#8220;var myVar:int = something&#8221; is used.  Here, if you set &#8220;myVar&#8221; to a string and it&#8217;s expecting an integer, your code will likely produce an error.</p>
<p>It can feel a little freer to be able to put whatever we want in, and it is!  However, a lot of the time you&#8217;re using one variable as one type of thing.  If you accidentally pass another type of thing to that variable you might get unexpected behavior even though it doesn&#8217;t crash.</p>
<p>For example x = y + z where y=1 and z=1 would make x = 2&#8230;right?  Well what if y=&#8221;1&#8243; and z=&#8221;1&#8243; (strings)?  This would concatenate the strings and make x=&#8221;11&#8243; &#8211; Not what you were probably looking for!  Javascript wouldn&#8217;t alert you that you were doing something funny here, but you can use JSDoc to make some comments that y is an integer, and so is z:</p>
<pre>/** @type {number} */</pre>
<pre>var y = 1;</pre>
<pre>/** @type {number} */</pre>
<pre>var z = 1;</pre>
<p>This type of notation wouldn&#8217;t be picked up by Javascript, so wouldn&#8217;t affect your code performance at all (it&#8217;s just a comment after all).  It WOULD get picked up by the Linter.  If you passed one variable of type number to a method that only accepted strings, you&#8217;d get an error!</p>
<p>Check out this <a href="https://github.com/bengfarrell/cfo-typeenforce">GitHub repo</a> for a demo</p>
<p>Here&#8217;s a little bit about the different types of comments you can use:</p>
<p><a href="https://developers.google.com/closure/compiler/docs/js-for-compiler#types">https://developers.google.com/closure/compiler/docs/js-for-compiler#types</a></p>
<p>Another side effect of using JSDoc is that you can also use the JSDoc Toolkit.  This is a separate process which is outside of the Google Compiler, however since you&#8217;ve already done the work of marking your code with JSDoc, you can automatically use the toolkit.  Basically, this is another &#8220;compilation&#8221; process.  It doesn&#8217;t change your code &#8211; but it does read it, and create some nice HTML based documentation for you that describes how your code works.</p>
<p>The Google Compiler strives to follow the JSDoc Toolkit specs exactly, so you can be sure that what you comment will work in both the Google Compiler and the toolkit at all times.</p>
<p>Go ahead and check out my examples on Github &#8211; they all generate JSDocs!</p>
<p>&nbsp;</p>
<h3>Running the Compiler</h3>
<p>This theory is all well and good, but how to we actually run the compiler?  The Closure Compiler is a Java JAR, so you can use the compiler commands against the JAR file, but I&#8217;m not one for memorizing commands and constantly typing.</p>
<p>Instead, I like to start with an ANT file.  Apache ANT is a Java based build tool.  It automates our process.  We tell things how we&#8217;d like to build using an XML like syntax, and then run the ANT build file.</p>
<p>My very first Closure Compiler project was a <a href="https://github.com/bengfarrell/synth.js">music synthesizer</a> that builds some music theory off of the awesome <a href="https://github.com/jussi-kalliokoski/audiolib.js/">Audiolib.js project</a>.</p>
<p>Anyway, here&#8217;s my ANT build file that both compiles my project and builds documentation around it using JSDoc Toolkit:</p>
<p><a href="https://github.com/bengfarrell/synth.js/blob/master/build.xml">https://github.com/bengfarrell/synth.js/blob/master/build.xml</a></p>
<p>Why stop there though!?  We have a decent build script in place.  To use it, just type &#8220;ant&#8221; into your command shell in the project directory.  ANT assumes you mean to use &#8220;build.xml&#8221; as your build script and goes on it&#8217;s merry way creating your build.</p>
<p>What if I don&#8217;t want to use the command shell, but wanted to use a nice web interface to build my project?  Well that&#8217;s where<a href="http://jenkins-ci.org/"> Jenkins</a> comes in.  Jenkins is a &#8220;Continuous Integration&#8221; system that runs task from a web application shell.  What&#8217;s so &#8220;continuous&#8221; about it?  Well that&#8217;s the cool part!  Jenkins, in addition to offering a simple one-click button to run your build, can integrate with SVN, GIT, and other source control management tools.  We can tell it to run our Javascript project every time somebody checks in new code.  If the Google Linter fails, it will produce an error, marking the build as broken.  The person who broke the build, and anyone subscribed, will be notified immediately what actually happen.  It&#8217;s pretty sweet!</p>
<div id="attachment_697" class="wp-caption alignnone" style="width: 310px"><a href="http://www.benfarrell.com/wp-content/uploads/2012/05/Screenshot-at-2012-05-13-214740.png"><img class="size-medium wp-image-697" title="Screenshot at 2012-05-13 21:47:40" src="http://www.benfarrell.com/wp-content/uploads/2012/05/Screenshot-at-2012-05-13-214740-300x168.png" alt="" width="300" height="168" /></a><p class="wp-caption-text">Output from a Successful Jenkins Build</p></div>
<h3>Beyond Simple Compilation</h3>
<p>One thing I started getting into after getting a handle on Google Closure was <a href="http://dojotoolkit.org/">Dojo</a>.  Dojo is interesting because it&#8217;s an awesome toolkit, but makes the compilation process a little bit complicated.  Some of the more modern and grandiose frameworks like Dojo are based on <a href="http://requirejs.org/">Require.js</a> or something like it.  What Require aims to solve is code dependency on the fly.</p>
<p>Say I have some code which automatically launches a pop-up panel.  My panel will have a bazillion dependencies &#8211; and I don&#8217;t want to include them as separate script tags in my HTML header.  I also probably don&#8217;t want to compile it into a huge Javascript file that I load at the start of my project.  What if I don&#8217;t use this panel right away?  Why not load the Javascript when I need it?</p>
<p>Require.js does just that &#8211; when you require a dependency, it will check if it&#8217;s loaded.  If it&#8217;s not loaded, it will load it then use it.  Dojo is heavily based on Require.js so we can talk about both at the same time here.</p>
<p>So you can leave things alone here, and not use a compiler at all.  Tons of scripts will just load on demand, and I&#8217;m sure many projects are done this way.  You can be smarter though!  Dojo has it&#8217;s own build process to handle smarter loading.  You can group Javascript files and functionality together into what&#8217;s called &#8220;layers&#8221;.  You would then load these layers on demand as you need them.</p>
<p>For example, if you had an address book in your application and also a stock widget &#8211; you might group all of your address book Javascript files and widgets into an &#8220;addressbook&#8221; layer.  Likewise with the stock widget.   Since you don&#8217;t use the stock widget at the same time as the address book (and you might never use the address book at all) it makes since not to load these modules until you need to use them.</p>
<p>Require.js will function the same way &#8211; it will load up the dependencies.  However, now, the dependencies just happened to be packaged up differently.  Require.js and Dojo still knows where to look though!</p>
<p>But wouldn&#8217;t it be a nightmare to have to know which files have which dependencies?  If those dependencies have other dependencies, and so on and so on?  The Google Closure compilation process requires you to know which files to package &#8211; but with Require.js you just don&#8217;t know the files you need unless you spend hours and hours researching every little bit of your code and framework you use.</p>
<p>That&#8217;s why Dojo has it&#8217;s own builder.  You tell the build profile which files you want in which layers, but the good thing is that you don&#8217;t have to worry about dependencies &#8211; thank goodness.  The Dojo builder will figure out all of that for you.</p>
<p>But it all comes back full circle!  The Dojo builder uses either the <a href="http://www.mozilla.org/rhino/">Mozilla Rhino compiler</a> or the Google Closure compiler (whichever you choose).</p>
<p>It gets a little complicated when you get into Require.js, but the Google Closure stuff is pretty easy when you keep it simple.  Check out my examples on github to gander at my build scripts and setup!</p>
<p><a href="https://github.com/bengfarrell">https://github.com/bengfarrell</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.benfarrell.com/2012/05/14/javascript-continuous-integration-with-google-closure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The CoffeeScript Dilemma</title>
		<link>http://www.benfarrell.com/2012/04/24/the-coffeescript-dilemma/</link>
		<comments>http://www.benfarrell.com/2012/04/24/the-coffeescript-dilemma/#comments</comments>
		<pubDate>Tue, 24 Apr 2012 17:32:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.benfarrell.com/?p=689</guid>
		<description><![CDATA[So, CoffeeScript is pretty darn interesting.  A couple years back when I made a concerted effort to take Javascript more seriously as Flash&#8217;s stranglehold grip on the UI industry was nearing an end, I didn&#8217;t like Javascript. It felt quirky and weird, and messed with my OO view of the world.  Contrast that to Java, [...]]]></description>
			<content:encoded><![CDATA[<p>So, CoffeeScript is pretty darn interesting.  A couple years back when I made a concerted effort to take Javascript more seriously as Flash&#8217;s stranglehold grip on the UI industry was nearing an end, I didn&#8217;t like Javascript.</p>
<p>It felt quirky and weird, and messed with my OO view of the world.  Contrast that to Java, which I&#8217;ve been picking up a bit lately for Android development &#8211; and I feel right at home with that.  I think a lot of people feel this way when it comes to Javascript.  To me, it was an acquired taste.</p>
<p>I do dig JS now, and there&#8217;s tons of little helpers you can use to make it fit whatever workflow you choose.  I&#8217;m really digging on both Dojo and Google Closure now.  There&#8217;s tons of stuff out there.</p>
<p>Before I liked Javascript, and it was still a bit frustrating, I strongly felt that JS would go away in 7-10 years &#8211; it would simply be a compiler target.  Both Google Dart and Coffeescript were available, so it made sense.  JS would be the &#8220;assembly language&#8221; of the web.</p>
<p>I don&#8217;t know that I feel much different now about Javascript &#8211; except now that I use it, and feel it&#8217;s not frustrating anymore, I do feel like the language will survive more than I gave it credit for.  As in, maybe for lots of application, Javascript may be phased out in favor of things like Google Web Toolkit (Java), Dart, and possibly Coffeescript.</p>
<p>Meanwhile for other small projects and various snippets on your web page, JS will be in full effect.</p>
<p><strong>The Implementation Phase</strong></p>
<p>What I didn&#8217;t daydream on, was the implementation phase as these various languages see the light of day and gain more traction.  I said hey, &#8220;We&#8217;re using Javascript now &#8211; and in 10 years something else will be popular&#8221; (though still compiling to Javascript).  I didn&#8217;t even think about what happens in between.</p>
<p>I think we&#8217;re starting to see this answer in the form of a dilemma in regards to CoffeeScript.</p>
<p><strong>I Don&#8217;t Really Like CoffeeScript</strong></p>
<p>There I said it!  It&#8217;s probably no surprise to those that know me that I don&#8217;t really care for it.  But I did go ahead and learn a little about it, and why someone would use it.  The best pro-CoffeeScript argument was this 44 minute video from 37Signals Sam Stephenson called <a href="http://vimeo.com/35258313">&#8220;Better JS Through CoffeeScript&#8221;</a>.</p>
<p>The arguments presented in the video were largely &#8220;Look at how sexy this code is&#8221;,  &#8221;You save so much typing&#8221;, etc etc.  The underlying argument was that it&#8217;s more readable.</p>
<p>Well, it&#8217;s not&#8230;.to me anyway &#8211; it was pretty hard to read, even as Sam was explaining it.  I can definitely see if you have a Ruby background that it IS much more readable.  From my perspective we&#8217;re bringing JS into the Ruby comfort zone.  I think, for me personally, Google&#8217;s Dart would bring JS more into MY comfort zone.</p>
<p>Anyway, just keep that in mind.  There is no &#8220;best&#8221; language in the world.  There&#8217;s just the best language for you and/or your job.  And that&#8217;s totally cool.</p>
<p>One VERY compelling reason for CoffeeScript, on the other hand, was the whole compilation thing.  Think about jQuery.  If you do a jQuery &#8220;each&#8221; method, you&#8217;re using the underlying framework to do something for you.  It adds computational overhead.</p>
<p>Contrast this with CoffeeScript &#8211; it will write better code out for you, and that&#8217;s what your browser will be running.   So that&#8217;s a great thing in my mind &#8211; no framework overhead, and you get to write things that are easier for you to read and understand as a developer.</p>
<p>But, again, to each his own.  Maybe someday I&#8217;ll get around to using Coffeescript and it will be another acquired taste.</p>
<p><strong>The Dilemma</strong></p>
<p>I cracked open Twitter before lunch today and I see a comment thread on an Ember.js related blog post:</p>
<p><a href="http://www.thesoftwaresimpleton.com/blog/2012/04/22/ember-js-routemanager/#comment-507972544">http://www.thesoftwaresimpleton.com/blog/2012/04/22/ember-js-routemanager/#comment-507972544</a></p>
<p>The blog post itself was independently written (important for this conversation), and contained some how-tos for using the Ember.js framework,.</p>
<p>Unfortunately for me and @commadelimited (Andy Matthews), the examples were written in CoffeeScript, so they were pretty hard to read (for me anway).</p>
<p>This might be where you expect me to rail on the author for using CoffeeScript&#8230;.but no, that would be silly.  This was written by a blogger in his spare time &#8211; we should be supporting him and appreciative regardless of if he wrote the example in Objective J.</p>
<p>On the other hand, it really throws off my game!  Part of me goes all American Redneck complaining why I have to press #1 on my phone for English.</p>
<p>Using other languages both detract from and make Javascript awesome at the same time.  Its so troubling that if I met this blogger in a bar and said &#8220;I will buy you a beer if you follow this advice&#8221;&#8230;.I don&#8217;t know what that advice would be.  Obviously the dude likes CoffeeScript, why nay say?</p>
<p>Did we have these problems before?  Usually, in my experience, we started at the language, and built frameworks on top of those languages).  Now anything goes!  And we need to mix and match.</p>
<p>I guess the best advice would be for me personally to learn CoffeeScript even though I don&#8217;t plan to do things with it.  It&#8217;s a hard pill to swallow because learning has always equaled productivity for me.  Now, it&#8217;s learning for the sake of understanding our foreign language friends, who have something important to say.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.benfarrell.com/2012/04/24/the-coffeescript-dilemma/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Front Enders vs Creative Developers</title>
		<link>http://www.benfarrell.com/2012/04/13/frontenders-vs-creative-devs/</link>
		<comments>http://www.benfarrell.com/2012/04/13/frontenders-vs-creative-devs/#comments</comments>
		<pubDate>Fri, 13 Apr 2012 02:07:16 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[c++]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[ios]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[ui]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.benfarrell.com/?p=676</guid>
		<description><![CDATA[Today, Rebecca Murphey wrote a post called &#8220;A Baseline for Front-End Developers&#8221;.  The post had some good advice, it was well written, and it was well received, judging from the comments. Something about it really didn&#8217;t sit right with me though.  I was very uneasy about it, and I didn&#8217;t know why until I thought [...]]]></description>
			<content:encoded><![CDATA[<p>Today, Rebecca Murphey wrote a post called <a href="http://rmurphey.com/blog/2012/04/12/a-baseline-for-front-end-developers/">&#8220;A Baseline for Front-End Developers&#8221;</a>.  The post had some good advice, it was well written, and it was well received, judging from the comments.</p>
<p>Something about it really didn&#8217;t sit right with me though.  I was very uneasy about it, and I didn&#8217;t know why until I thought about it on my bike ride home from work.</p>
<p>Here&#8217;s the essence of the problem: THEY STOLE OUR WORD!!!!</p>
<p>&nbsp;</p>
<h2>What is a &#8220;Front-End Developer&#8221;?</h2>
<p>Frankly, I thought I already knew.  After all, the terms <em>front end</em> and <em>back end</em> have been bandied about for at least a couple decades now.  To make sure I wasn&#8217;t deluding myself, I turned to <a href="http://en.wikipedia.org/wiki/Front_and_back_ends">Wikipedia</a>:</p>
<p>&#8220;In <a title="Software architecture" href="http://en.wikipedia.org/wiki/Software_architecture">software architecture</a> there may be many layers between the hardware and <a title="End-user (computer science)" href="http://en.wikipedia.org/wiki/End-user_(computer_science)#End_user">end user</a>. Each can be spoken of as having a front end and a back end. The <em>front</em> is an abstraction, simplifying the underlying component by providing a <a title="User-friendly" href="http://en.wikipedia.org/wiki/User-friendly">user-friendly</a> interface.&#8221;</p>
<p>&#8220;In <a title="Web content management system" href="http://en.wikipedia.org/wiki/Web_content_management_system">Web content management systems</a> the terms <em>front end</em> and <em>back end</em> may refer to the end-user facing views of the CMS and the administrative views respectively.&#8221;</p>
<p>&#8220;For major computer subsystems, a graphical <a title="File manager" href="http://en.wikipedia.org/wiki/File_manager">file manager</a> is a front end to the computer&#8217;s <a title="File system" href="http://en.wikipedia.org/wiki/File_system">file system</a>, and a <a title="Shell (computing)" href="http://en.wikipedia.org/wiki/Shell_(computing)">shell</a> interfaces the <a title="Operating system" href="http://en.wikipedia.org/wiki/Operating_system">operating system</a> — the front end faces the user and the back end launches the programs of the operating system in response.&#8221;</p>
<p>When I read the various definitions, one thing seemed very clear to me: a<em> front end</em> is any user-facing view.  It is the layer of software that a human must go through to use a larger software or hardware system.</p>
<p>So, by definition, a <em>front-end developer</em> must be someone who develops, scripts, programs, or codes a user interface. which is a pretty broad job description.  By contrast, Rebecca gives a pretty narrow list of technologies and skills that a front-end developer must possess.</p>
<p>Guess what, though?  There is nothing on her list but web technologies.  More specifically, it&#8217;s all about HTML, CSS,  and Javascript.  Maybe I&#8217;m right, maybe I&#8217;m wrong, but more to the point, we&#8217;re talking about a very specific, though popular, subset of HTML usage.  Rebecca isn&#8217;t alone, she just seems to have encapsulated very well what I&#8217;m seeing in blogs, job postings, and more about the role of Front-End Developers.  Honestly, I&#8217;d call what she defined as Front-End Developers (for lack of a better word), <em>boutique web developers</em>.</p>
<p>The boutique web is a subset of smallish-scale sites that are quite attractively designed and use the latest HTML5 fantasticness.  Its developers are striving for pixel perfect designs and may have some sexy animated jQuery widgets.  Lots of money is being poured into this area right now given that people are running away from Flash . . . you know, the technology that we used to do boutique websites with.</p>
<p>I might be pigeonholing front enders by calling them boutiquers, but think about the following types of sites:</p>
<ul>
<li>Wikipedia &#8211; Awesome site, but not very attractive.  If you&#8217;re using your pixel perfect CSS skills or a CSS preprocesser and you work on this site, you&#8217;re probably just extremely bored with your job.  The interesting and fun work here is probably on the back end, dealing with big data.</li>
<li>Any newspaper site &#8211; A horrible mess.  I&#8217;m not so sure these people don&#8217;t just copy and paste code in there and hope it works.</li>
<li>Games &#8211; There&#8217;s a wealth of specialized knowledge in this area.  Does a game developer need to worry about stylesheets at all?  No. Collision detection, physics, game theory? Yes.</li>
<li>Video &#8211; We worry about the video tag, encoding specs, ad networks, reporting networks, etc.  Javascript is king here.  Who cares so much about CSS and HTML?</li>
<li>Enterprise applications &#8211; There&#8217;s a whole other level of abstraction here.  A number of large-scale applications are built with Java with GWT and compiled down.</li>
</ul>
<p>Do many of these skills overlap with Front-End Development?  Sure.  But we&#8217;re talking definitions here.  Developers are awesome and adaptable, so I can&#8217;t pigeonhole them, but I can try to pigeonhole the term front-end developer. I think the baseline for front-end developers is geared towards boutique sites and only those that use HTML, CSS, or Javascript . . . and probably Ruby on Rails.</p>
<p>Again, the people with these skills and passions are awesome, but it&#8217;s a little confusing for people like me who have been developing old-school front ends since the late 90s.  I really thought I could include myself in the front-end cool kids club, but I don&#8217;t care about semantic markup or CSS preprocessors.</p>
<h2>The Other Front Ends</h2>
<p>There are TONS of other things I consider to be front end.  There&#8217;s Flash, Corona, Android, iOS, OSX, Windows, 3D games . . . the list goes on and on.  Even inside the HTML/CSS/Javascript stack, there are other front ends I mentioned like games, video, and enterprise applications.</p>
<p>What do you call those developers?  Well, lots of folks say, &#8220;I&#8217;m an Android developer&#8221;, &#8220;I&#8217;m a C# developer&#8221;, or &#8220;I&#8217;m a Corona developer.&#8221;  I find those to be a little narrow in scope, though.  I definitely got burned calling myself a Flash developer when <a href="http://seb.ly/2011/11/flashageddon-the-aftermath/">Flashageddon</a> went down last November.  I don&#8217;t want to make that mistake again, tying my professional identity to a specific technology. Technology changes, evolves, and falls out of favor; unless you feel like rebranding yourself every few years, it might be wise to come up with a broader term for what you do.</p>
<p>Before reading Rebecca&#8217;s post, I thought front-end developer was a pretty decent broad term!  I felt I could associate myself with it. After all, I really enjoy pushing graphics around and making users happy. But it turns out I can&#8217;t, or rather, I don&#8217;t really want to.  If I&#8217;m reading Rebecca&#8217;s post right, a front-end developer is more of the same.  You&#8217;re tying yourself to a specific technology, or rather, three of them.  Yes, it&#8217;s totally the hotness now, and it&#8217;s been around for a while. You&#8217;ll probably have a place in the world for the next 15 to 20 years if it&#8217;s all you do and you keep up with the trends.  But new hotnesses will inevitably come along, and HTML, CSS, and Javascript might get a little stale even if they&#8217;re still relevant.</p>
<p>If I don&#8217;t identify as a front-end developer, what can I identify as?</p>
<p>&nbsp;</p>
<h2>The Creative Developer</h2>
<p>So <em>creative developer</em> is a term that I came up with on my own, only to find out it wasn&#8217;t original in the least.</p>
<p>I occasionally listen to the <a href="http://creativecodingpodcast.com/">Creative Coding Podcast</a>.  Both of the hosts, Seb Lee Delisle and Ian Lob, identify as <em>creative coders</em>.  I have a feeling they dumb it down a little to be more approachable, but the perception I get from them is that creative coders sort of copy and paste code and tweak it by trial and error until it meets their needs.  There are also folks calling themselves <em>creative technologists.</em>  The impression I get from these folks is that they might be all sorts of brilliant but don&#8217;t necessarily code at all.</p>
<p>I work with a bunch of people that call themselves <em>software engineers</em>.  BORING.  It evokes the right tone of capability, but I just don&#8217;t prefer huge projects where I take data, manipulate it, and move it around.  I like a little more razzle dazzle.  I like getting in front of users, creating nifty things that people can see in front of their faces.  I like doing things that make noncoders go &#8220;WOW!&#8221;</p>
<p>So I took the <em>creative</em> part, since I like making creative applications, and I put something after it that evokes a little more confidence in building a software solution.  Hence <em>creative developer.</em>  But again, after I coined the term, I saw blogs and job postings for creative developers.  Cool!  But what IS a creative developer?  What is our baseline, as Rebecca outlined for front-end developers?</p>
<p>&nbsp;</p>
<h2>A Baseline for Creative Developers</h2>
<p>I think that a baseline for our type of developer isn&#8217;t so easy.  You should probably know a lot, or at least not be afraid to learn a lot.  Get to know a lot of different techs. Here are my suggestions:</p>
<p>&nbsp;</p>
<p><strong>Front-End Developers Are in Demand</strong></p>
<p>What Rebecca said?  Yeah, learn most of that.  You should probably be a competent front-end developer because that is what&#8217;s going on right now. Personally, I don&#8217;t know in what universe I&#8217;d think CSS preprocessors are useful for my daily work.  But whatever tech you don&#8217;t learn on Rebecca&#8217;s list, you should be able to talk intelligently about.</p>
<p>&nbsp;</p>
<p><strong>Get Linux</strong></p>
<p>The biggest realization I had last year was that if you use Windows, you&#8217;re at a disadvantage. It&#8217;s not something you can&#8217;t overcome, but make things easier for yourself &#8212; dual boot Linux. If you want to make Jesus cry, fine, get an Apple.  The only thing making me wish I had OSX right now is that I need to switch OSs just to boot up Photoshop or Illustrator.</p>
<p>&nbsp;</p>
<p><strong>Do the Mobile Thing</strong></p>
<p>You should also do some mobile, of course.  Phonegap is taking the easy way out.  Useful yes, but learn some native development.  Objective C and Java have their quirks, but they don&#8217;t seem too bad.</p>
<p>&nbsp;</p>
<p><strong>Don&#8217;t Forget Flash</strong></p>
<p>Flash, on a technical level, is pretty awesome.  AIR + Flash, like Phonegap, is taking the easy way out, but it&#8217;s still awesome.  I feel very fortunate that I already know Flash, because I don&#8217;t want anything more to do with it voluntarily. Taking development advice from Adobe is like taking religious advice from <a href="http://en.wikipedia.org/wiki/Heaven's_Gate_(religious_group)">Heaven&#8217;s Gate</a>.  But there will be work in Flash for years to come.  Who knows if the doomsayers are right and it&#8217;s dying &#8212; but it&#8217;s still useful today.</p>
<p>&nbsp;</p>
<p><strong>The Server Can Be Creative</strong></p>
<p>I was so resistant to server-side tech for the longest time.  After all, I&#8217;m a creative developer; I need some cool stuff on the screen to keep me occupied.  I can do heavy lifting in the client &#8212; it saves me from scaling my back-end architecture if people are doing something computationally expensive.  Yes, clientside tech is slower, but it forces me to do some problem solving.</p>
<p>I was wrong and resistant for no reason.  I dabbled a little with PHP just to get the job done, but it really wasn&#8217;t fun. What&#8217;s fun?  Node.js is a freaking blast. People are so excited over it, and if you&#8217;re not a server-side guru, everything in Node.js is new so there are no experts right now, just people having fun and getting awesome things done.  It also has some very good conceptual parallels to other technologies like Python and Ruby.  You know how the Node Package Manager works?  Good, then you know conceptually how Ruby Gems and the Python Cheese Shop works.</p>
<p>Speaking of Python, learn it!  Or learn Ruby . . . but I&#8217;m seriously turned off by Ruby developers claiming they are god&#8217;s gift.  I hope those that do are the loud minority, but it&#8217;s really off-putting.  If I were a real creative developer, though, I would probably just get over it.</p>
<p>Why is the server creative?  Collaboration!  Multiplayer games!  Live video encoding!  It&#8217;s another tool in your arsenal.  Also, the environment is a lot more mature than the clientside environment. People use languages in it that we really don&#8217;t use on the clientside today.  It will expand your horizons &#8211; - BELIEVE IT!</p>
<p>&nbsp;</p>
<p><strong>Learn C++</strong></p>
<p>What is C++ good for?  Everything.  Remember what I said about learning the server because the client side is slower?  You&#8217;ll run into that less with C++.  Chrome is leading the charge so you can run C++ in your browser.  If you use Flash, you can use Alchemy (don&#8217;t get me started on the <a href="http://news.cnet.com/8301-30685_3-57405606-264/adobe-to-charge-flash-coders-to-use-premium-features/">Flash Premium feature debacle</a>. Again, Adobe = Heaven&#8217;s Gate).  C++ will work on your Android phone, in your iOS application, everywhere.  Bonus: You can learn leading creative coding frameworks like <a href="http://libcinder.org/">Cinder</a> or <a href="http://www.openframeworks.cc/">Open Frameworks</a>.  A good basis in C++ will let you do some very cool, but very processor-intensive things.</p>
<p>&nbsp;</p>
<p><strong>Users Aren&#8217;t Everything</strong></p>
<p>With Flash and Javascript, I was stuck in such a rut.  It felt like if I couldn&#8217;t put it on the Web and let my legions of fans adore my work, it wasn&#8217;t worth anything.  Guess what? I don&#8217;t have legions of adoring fans, and if I did, I should just freaking Vimeo or YouTube a shaky cam of my masterpiece.</p>
<p>This mind-set prevented me from doing some cool things with some very cool technologies.  Why use OpenFrameworks if I can&#8217;t run it on the Web? <a href="http://tacocopter.com/"> Tacocopters</a> are cool, but I can&#8217;t give tacos to everyone who visits my site!</p>
<p>Play around with stuff; you&#8217;ll have fun and get some ideas. If you do anything cool, your fans will adore you with a YouTube video instead of a working demo in your browser.</p>
<p>&nbsp;</p>
<p><strong>The Physical Side</strong></p>
<p>So there&#8217;s the client side, the server side, and the PHYSICAL side!  This is probably another term I&#8217;m coining that&#8217;s in widespread use.  Not everything you do has to be on a screen.  If it does have to be on a screen, why not use a few projectors and a Microsoft Kinect?  The Kinect is awesome, but explore <a href="http://www.arduino.cc/">Arduino</a> as well.You can make some damn cool things if you don&#8217;t constrain yourself to the screen and the Web.  Remember, users aren&#8217;t everything &#8212; just put your masterpiece on YouTube.</p>
<p>&nbsp;</p>
<p><strong>Talk Big. Do Half of It.</strong></p>
<p>There are so many cool things to learn.  A few of the above items are a to-do list for myself.  I&#8217;m getting into native Android development, iOS development, Node.js, Python, and lots of Javascript.  I&#8217;ve been meaning to do C++, Open Frameworks, Arduino, and creative installations for some time.  I hope to have time and opportunity to explore these techs, but it might not happen.  That&#8217;s OK; there are just too many cool things to learn.  Try to get to at least half the ones on your list.</p>
<h2>Sure I Can Geek Out, But Will I Get Hired?</h2>
<p>Short answer: Nobody knows!  By all means, be a front-end developer and reach Rebecca&#8217;s baseline.  You&#8217;ll look good to HR people and be better able to answer the standard f<a href="http://darcyclarke.me/development/front-end-job-interview-questions/">ront-end interview questions</a>.  You&#8217;ll be good for several years, I&#8217;m sure.  But for me, those skills ares only part of my passion. Frankly, I&#8217;d rather creatively develop.</p>
<p>The fact is, we&#8217;re seeing more smaller companies and startups bypass HR and recruiters. There are too many technologies to count &#8212; if you don&#8217;t know technology A, that&#8217;s OK, you&#8217;ll learn.  The important thing is that you&#8217;re passionate, driven, play well with others, and are proficient in something to prove you can be proficient in something else.</p>
<p>Also, there&#8217;s a developer shortage. You&#8217;ll probably be juuuuuuust fine.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.benfarrell.com/2012/04/13/frontenders-vs-creative-devs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The long and short of a simple UI Component in Dojo</title>
		<link>http://www.benfarrell.com/2012/03/06/the-long-and-short-of-a-simple-ui-component-in-dojo/</link>
		<comments>http://www.benfarrell.com/2012/03/06/the-long-and-short-of-a-simple-ui-component-in-dojo/#comments</comments>
		<pubDate>Tue, 06 Mar 2012 02:31:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[ui]]></category>

		<guid isPermaLink="false">http://www.benfarrell.com/?p=662</guid>
		<description><![CDATA[Understanding Dojo is sorta like understanding Star Wars&#8230;you need the entire backstory to be able to figure out what&#8217;s going on. On the surface, using Dojo elements are as simple as using HTML.  You sprinkle a little bit of Dojo-ness in the div tag and it works.  Pretty straightforward! On the same token, Star Wars [...]]]></description>
			<content:encoded><![CDATA[<p>Understanding Dojo is sorta like understanding Star Wars&#8230;you need the entire backstory to be able to figure out what&#8217;s going on.</p>
<p>On the surface, using Dojo elements are as simple as using HTML.  You sprinkle a little bit of Dojo-ness in the div tag and it works.  Pretty straightforward!</p>
<p>On the same token, Star Wars seems like a pretty simple story of good guy fighting the bad guy to get the girl.  It&#8217;s only when you get the backstory, you understand why Luke and Leia kissing is kinda gross, and why Vader doesn&#8217;t want to outright murder Luke.</p>
<p>With Dojo, it&#8217;s the same deal &#8211; if you don&#8217;t understand the backstory, you&#8217;re only going to get things working by sheer dumb luck.</p>
<p>Say I Google how to do a simple progress bar in Dojo.  My first hit is this <a href="http://dojotoolkit.org/reference-guide/dijit/ProgressBar.html">page</a>.  I even have code I can copy and paste that looks like HTML!</p>
<pre>&lt;div data-dojo-type="dijit.ProgressBar" style="width:300px"
     data-dojo-id="jsProgress" id="downloadProgress" data-dojo-props="maximum:10"&gt;&lt;/div&gt;</pre>
<p>Unfortunately, if I put this on my page, it doesn&#8217;t work.  No I&#8217;m not dumb, I went to the Dojo <a href="www.dojotoolkit.org/downloads">download</a> page and copied the script tag they provide in my HTML header:</p>
<p>&lt;script src=&#8221;//ajax.googleapis.com/ajax/libs/dojo/1.7.2/dojo/dojo.js&#8221;&gt;&lt;/script&gt;</p>
<p>By all accounts, I&#8217;ve done what needs to be done, right?</p>
<p>Not exactly.</p>
<p>First of all, I might be thrown off by Googling &#8220;dojo progress bar&#8221; and coming up with dijit.ProgressBar.  That&#8217;s lesson the first!</p>
<p>Dojo is divided up into 3 separate projects: Dojo, Dijit, and Dojox.  Dojo is the center of it all.  In it&#8217;s (almost) 90kb core, it offers a lot of the same stuff something like jQuery would offer.  The rest of the Dojo namespace has extra things which we can pull in as we need them (more on this in a bit).</p>
<p>Dijits, or &#8220;Dojo Widgets&#8221;, are the UI components to the Dojo team.  ProgressBar, being a UI component, lives here in this namespace.</p>
<p>Dojox features are the more experimental features.  What&#8217;s here might not be central to the Dojo vision, but pretty close &#8211; close enough to be shipped with the rest of Dojo.</p>
<p>What you should understand, is that all of these features put together weigh in at 5.3MB when you grab the zip.  You might say that&#8217;s too large to include in your project.  You&#8217;d be right.</p>
<p>Fortunately you don&#8217;t have to force this on your users all at once (nor should you).  The 90kb Dojo core is all you need at the start, but as you need the other features of Dojo, you can pull them in realtime.</p>
<p>In other languages this might be an &#8220;include&#8221; or &#8220;import&#8221;.  In Dojo, it&#8217;s a &#8220;require&#8221;.  So now the next piece of this puzzle, is to require the progress bar:</p>
<pre>&lt;script type="text/javascript"&gt;
        require(["dijit.ProgressBar"]);
&lt;/script&gt;</pre>
<p>Fortunately for us, we can require the progress bar and anything the progress bar depends on is pulled in automatically because the Progress bar has its own include directives in the component.</p>
<p>So now, we&#8217;ve written the HTML/Dojo markup to include the progress bar, and we&#8217;ve required the necessary classes to support it.  But it still doesn&#8217;t work!</p>
<p>There&#8217;s one last piece to this puzzle &#8211; the script tag.  If you recall, I did say I wasn&#8217;t an idiot &#8211; I did include Dojo.js here&#8230;something like this in my head tag:</p>
<pre>&lt;script src="//ajax.googleapis.com/ajax/libs/dojo/1.7.2/dojo/dojo.js"&gt;&lt;/script&gt;</pre>
<p>If we were simply using Dojo programmatically, this is all we&#8217;d need for basic functionality of a component&#8230;but we aren&#8217;t! We&#8217;re using some nifty declarative HTML markup, so there&#8217;s one last step.  We need to tell Dojo to scan the tags on the page and look for Dijit components.  We do this by altering our script tag to be:</p>
<pre>&lt;script src="//ajax.googleapis.com/ajax/libs/dojo/1.7.2/dojo/dojo.js"
   data-dojo-config="parseOnLoad: true"&gt;
&lt;/script&gt;</pre>
<p>Telling our script to parse on load, means that when Dojo is loaded, it will scan and register all of our tags.</p>
<p>Fantastic!  Here&#8217;s what we have so far:</p>
<pre>&lt;html&gt;
    &lt;head&gt;
        &lt;script src="//ajax.googleapis.com/ajax/libs/dojo/1.7.2/dojo/dojo.js"
                data-dojo-config="parseOnLoad: true"&gt;
        &lt;/script&gt;

        &lt;script type="text/javascript"&gt;
            require(["dijit/ProgressBar"]);
        &lt;/script&gt;
    &lt;/head&gt;

    &lt;body&gt;
        &lt;div data-dojo-type="dijit.ProgressBar"
             style="width: 300px;"
             id="discoveryProgress"&gt;&lt;/div&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre>
<p>Unfortunately, while our progress bar exists, and it technically works, it&#8217;s looking kinda silly.  That&#8217;s because we haven&#8217;t bothered to load our themes in yet.  Without a theme picked, we get a barebones component which kind of looks like a broken wireframe.</p>
<p>At this point, I&#8217;m going to go to my handy <a href="http://archive.dojotoolkit.org/nightly/dojotoolkit/dijit/themes/themeTester.html">Dojo Theme Tester</a> and pick the Dojo theme that suits me best for the immediate future.  Claro looks good, so I&#8217;ll use that.  Not only do I need to include the Claro CSS and the Document CSS, but I also need to assign my body tag with a Claro class so that my entire document uses this theme.</p>
<pre>&lt;html&gt;
    &lt;head&gt;
        &lt;script src="//ajax.googleapis.com/ajax/libs/dojo/1.7.2/dojo/dojo.js"
                data-dojo-config="parseOnLoad: true"&gt;
        &lt;/script&gt;

        &lt;script type="text/javascript"&gt;
            require(["dijit/ProgressBar"]);
        &lt;/script&gt;
        &lt;link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.7/dijit/themes/claro/document.css"&gt;
        &lt;link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.7/dijit/themes/claro/claro.css"&gt;
    &lt;/head&gt;

    &lt;body class="claro"&gt;
        &lt;div data-dojo-type="dijit.ProgressBar"
             style="width: 300px;"
             id="discoveryProgress"&gt;&lt;/div&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre>
<p>Now we&#8217;re rocking! We have a Dijit Progress bar functioning on our webpage. Even though the point of this post is to simply get a UI component onto the page, I really can&#8217;t leave it hanging at 0%. Let&#8217;s go ahead and crank it to 80%.</p>
<p>For this, we&#8217;ll need a bit of script. For the sake of ease, I&#8217;m going to ignore all good sense and just put it in action in my HTML script tag.</p>
<p>We&#8217;re going to change our Javascript slightly &#8211; instead of a simple &#8220;require&#8221; directive, we&#8217;re going to have it do something after the required thing has loaded.</p>
<p>The simplest form of this is to write the following:</p>
<pre> &lt;script type="text/javascript"&gt;
    require(["dijit/ProgressBar"], function() {
       console.log("ready");
    });
 &lt;/script&gt;</pre>
<p>There are a couple of cool variations to this require script. First of all, you&#8217;ll notice that we&#8217;re requiring an array of Dojo components.  Typically, we&#8217;ll just want to listen for &#8220;dojo/ready&#8221; and perhaps a custom script  and start our application from there.</p>
<p>Secondly, you&#8217;ll notice that we didn&#8217;t pass in any parameters through our function.  Each index in our require list corresponds to each function parameter.  So I could do the following:</p>
<pre>    require(["dijit/ProgressBar"], function(ProgressBar) {</pre>
<p>See, when you require something, the parameter that comes through in the function is the constructor for that component.  We can instantiate this object from this parameter!</p>
<p>However, what we REALLY want to do is tweak the progress bar we already have.  So we need to find the existing progress bar on the page.  You&#8217;ll see two references in the Dojo docs to &#8220;byID&#8221;.  One is dijit.byId, the other is dojo.byId.  The rule of thumb here is that if you want a reference to the DOM element, you would use dojo.byId, and if you want a component reference use dijit.byId.</p>
<p>Unfortunately, this is allllll old news in Dojo 1.7!  It&#8217;s very confusing &#8211; but this way of selection is in lots of basic documentation!  Now, we use the &#8220;dom&#8221; object to find something in the DOM, and the &#8220;registry&#8221; object to find something in Dojo&#8217;s component registry.</p>
<p>We&#8217;re going to need to step up our game here, and require the &#8220;dom&#8221;, the &#8220;ProgressBar&#8221;, and the &#8220;ready&#8221; component.  In this way, it&#8217;s kind of a lot to ask and checklist for basic setup, however once you get rolling, I PROMISE it gets easier.</p>
<p>Our new script tag reads:</p>
<pre>        &lt;script type="text/javascript"&gt;
            require(["dojo/ready", "dijit/registry", "dijit/ProgressBar"], function(ready, registry){
                ready(function() {
                    var myWidget = registry.byId("discoveryProgress");
                    myWidget.update({ maximum: 100, progress: 80});
                });
            });
        &lt;/script&gt;</pre>
<p>Here, we require all these necessary components.  We then hinge the usage of our script on when dojo is &#8220;ready&#8221; and has loaded all that we required.  We get a reference to the HTML declared component through the registry.  From there we can update the progress bar as we want!</p>
<p>A full working example is here:</p>
<pre>&lt;html&gt;

    &lt;head&gt;
        &lt;script src="//ajax.googleapis.com/ajax/libs/dojo/1.7.2/dojo/dojo.js"
                data-dojo-config="parseOnLoad: true"&gt;
        &lt;/script&gt;

        &lt;script type="text/javascript"&gt;
            require(["dojo/ready", "dijit/registry", "dijit/ProgressBar"], function(ready, registry){
                ready(function() {
                    var myWidget = registry.byId("discoveryProgress");
                    myWidget.update({ maximum: 100, progress: 80});
                });
            });
        &lt;/script&gt;
        &lt;link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.7/dijit/themes/claro/document.css"&gt;
        &lt;link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.7/dijit/themes/claro/claro.css"&gt;
    &lt;/head&gt;

    &lt;body class="claro"&gt;
        &lt;div data-dojo-type="dijit.ProgressBar"
             style="width: 300px;"
             id="discoveryProgress"&gt;&lt;/div&gt;
    &lt;/body&gt;

&lt;/html&gt;</pre>
<p>So there you go!  A functional progress bar.</p>
<p><a href="http://www.benfarrell.com/wp-content/uploads/2012/03/progbar.png"><img class="alignnone size-full wp-image-667" title="progbar" src="http://www.benfarrell.com/wp-content/uploads/2012/03/progbar.png" alt="" width="324" height="74" /></a></p>
<p>It&#8217;s unfortunate we had to go through such a long journey and learn so much to get to such a basic state &#8211; and that, I think, is Dojo&#8217;s biggest weakness.  If you choose to power through this initial learning and setup phase I really think you&#8217;ll find Dojo to be absolutely fantastic.</p>
<p>There will be the moments when you bang your head on the desk because you missed something really stupid or the Dojo docs weren&#8217;t clear enough, however once you get into the rhythm of writing the code, it&#8217;s a fun and productive toolkit to work with.</p>
<p>I hope this post and future ones will help with that!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.benfarrell.com/2012/03/06/the-long-and-short-of-a-simple-ui-component-in-dojo/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The Seven Stages of Learning Dojo</title>
		<link>http://www.benfarrell.com/2012/02/21/the-seven-stages-of-learning-dojo/</link>
		<comments>http://www.benfarrell.com/2012/02/21/the-seven-stages-of-learning-dojo/#comments</comments>
		<pubDate>Tue, 21 Feb 2012 03:55:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[ui]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.benfarrell.com/?p=654</guid>
		<description><![CDATA[So, yah I&#8217;m learning Dojo, though from what I&#8217;ve heard this could easily apply to ExtJS.  It&#8217;s a different mindset to get accustomed to an enterprise Javascript framework, and comes with a bit of a steep learning curve. And, so it begins&#8230;. &#8220;Ooooooh look at all these awesome features on http://dojotoolkit.org!  They have a hot themetester, full documentation for [...]]]></description>
			<content:encoded><![CDATA[<p>So, yah I&#8217;m learning Dojo, though from what I&#8217;ve heard this could easily apply to ExtJS.  It&#8217;s a different mindset to get accustomed to an enterprise Javascript framework, and comes with a bit of a steep learning curve.</p>
<p><strong>And, so it begins&#8230;.</strong></p>
<p>&#8220;Ooooooh look at all these awesome features on <a href="http://dojotoolkit.org/">http://dojotoolkit.org</a>!  They have a hot <a href="http://archive.dojotoolkit.org/nightly/dojotoolkit/dijit/themes/themeTester.html">themetester</a>, <a href="http://dojotoolkit.org/api/">full documentation</a> for like every single part of their toolkit.  And GLORIOUS <a href="http://dojotoolkit.org/documentation/">quickstarts!</a>&#8221;</p>
<p><strong>1. Shock and Denial</strong></p>
<p>&#8220;WTF, I&#8217;ve copied this code EXACTLY, but it spews tons of errors, and doesn&#8217;t even come close to working.&#8221;</p>
<p>&#8220;I&#8217;ve looked up all these error messages, but I can&#8217;t find a straightforward answer.  Everyone references concepts I&#8217;m not familiar with&#8230;.and they keep telling me to hop on an IRC channel to talk to the developers.  Is IRC still a thing?  Do they have a BBS as well?  Maybe I can learn the language of my 14.4 kilo-baud modem and ask IT how to do stuff in Dojo by chriping and clicking at it&#8221;</p>
<p>&#8220;It&#8217;s gotta be me&#8230;.I&#8217;m an idiot, it can&#8217;t be this difficult to get basic stuff working&#8221;</p>
<p><strong>2. Pain and Guilt</strong></p>
<p>&#8220;Screw, it &#8211; let&#8217;s man up and just read massive quantities of documents and tutorials.  Hell, I&#8217;m going to buy a book from 2008 even thought it&#8217;s horribly out of date.  I&#8217;m just going to sit through this initial pain to get a better understanding of the whole system.&#8221;</p>
<p>&#8220;Am I really this dumb?&#8221;</p>
<p><strong>3. Anger and Bargaining</strong></p>
<p>&#8220;Dojo SUCKS, I hate it!  It doesn&#8217;t work. I need to be a freakin rocket scientist to understand this system&#8221;</p>
<p>&#8220;jQuery, will you take me back?  Please?  I just want some data stores, and some binding, and some well written and documented plugins.&#8221;</p>
<p><strong>4. Depression, Reflection, and Loneliness</strong></p>
<p>&#8220;So lonely&#8230;.the only materials I see online are from 2008, and from SitePen who charges for training, why would they help and give it all away for free?&#8221;</p>
<p>&#8220;Maybe if I had studied harder in school, this wouldn&#8217;t be a problem&#8221;</p>
<p><strong>5. The Upward Turn</strong></p>
<p>&#8220;Going back to the beginning and reading a bunch of stuff seems to have paid off, I think I&#8217;m beginning to understand now&#8221;</p>
<p>&#8220;Where in the hell did they tell you about this parseOnLoad attribute in the script tag?  It&#8217;s not in any of the quickstart examples, but it&#8217;s freaking mandatory to get anything to work&#8221;</p>
<p>&#8220;Well, no WONDER, this example didn&#8217;t work, it was written in Dojo 1.6 before the AMD Loader, and I&#8217;m mixing and matching my code between 1.6 and 1.7 &#8211; and 1.7 has REALLY changed the game&#8221;</p>
<p>&#8220;Ahhhhhh&#8230;..I need to nest the requires, cause my require doesn&#8217;t know what the ready object is before it loads the core stuff&#8221;</p>
<p><strong>6. Acceptance and Hope</strong></p>
<p>&#8220;Now that I understand the basics, I really like Dojo.  It rocks pretty hard!&#8221;</p>
<p>&#8220;There are things I don&#8217;t yet understand, but now that I have the big picture, I can roll with anything Dojo can throw at me, but all in all, I&#8217;m very psyched about all these new toys!&#8221;</p>
<p><strong>7. Reconstruction and Working Through</strong></p>
<p>&#8220;Dude, did I really just wire up a data store to a div which I made into a repeater, all inside a freaking accordion?  Damn&#8230;.awesome&#8221;</p>
<p>&#8220;I made a build system, and moved all my layers somewhere else, so I&#8217;m completely relying on my own custom build&#8230;.I only get like 2 errors, and none of them block functionality.  This will get better, but for now, it&#8217;s awesome enough&#8221;</p>
<p><strong>Fin</strong></p>
<p>And here I am.  Though, I think I cycled through steps 3, 4, and 5 a few times.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.benfarrell.com/2012/02/21/the-seven-stages-of-learning-dojo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why Dojo?  From a Flash/Flexer&#8217;s Perspective</title>
		<link>http://www.benfarrell.com/2012/02/21/why-dojo-from-a-flashflexers-perspective/</link>
		<comments>http://www.benfarrell.com/2012/02/21/why-dojo-from-a-flashflexers-perspective/#comments</comments>
		<pubDate>Tue, 21 Feb 2012 02:56:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flash/flex]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[ui]]></category>

		<guid isPermaLink="false">http://www.benfarrell.com/?p=642</guid>
		<description><![CDATA[Hey you guys&#8230;  Is your Twitter stream bombarded with Ted Patrick tweets and retweets about Sencha?  Do you get Sencha ads targeted at you when you visit websites, like EVERYWHERE you go? I am.  It&#8217;s kind of annoying since I&#8217;m not looking to learn Sencha.  Truthfully, though, you can hardly blame the current Sencha evangelist. [...]]]></description>
			<content:encoded><![CDATA[<p>Hey you guys&#8230;  Is your Twitter stream bombarded with Ted Patrick tweets and retweets about Sencha?  Do you get Sencha ads targeted at you when you visit websites, like EVERYWHERE you go?</p>
<p>I am.  It&#8217;s kind of annoying since I&#8217;m not looking to learn Sencha.  Truthfully, though, you can hardly blame the current Sencha evangelist.  After Adobe abandoned Flex sending it off to Apache, Ted seems to be leading a HUUUUUUGE marketing push trying to turn former Flex devs into Sencha/ExtJS devs.  Ted is leading webinars, reaching out to people, posting on G+ threads, the whole deal.</p>
<p>I can hardly blame him because:</p>
<p>a) I follow him on Twitter, so it&#8217;s mostly my own damn fault</p>
<p>b) This is the perfect time to grab a bunch of disillusioned and lost Flex devs, and Ted is doing it in a perfectly ethical (although a bit overzealous way).</p>
<p>I looked into going &#8220;beyond&#8221; jQuery because of a G+ post by Jesse Warden claiming that ExtJS is the perfect compliment to enterprise development.  He made the very apt analogy that Flash is to jQuery as Flex is to ExtJS/Sencha.  It really made me re-think things because it had been drilled into my brain that jQuery &#8220;won&#8221;, and given the tide of developers (<a href="http://w3techs.com/technologies/cross/javascript_library/ranking">84.8% of all websites using a library use jQuery</a>) it seemed like there was no reason to use anything else.</p>
<p>Turns out, there really is a use for the lesser used libraries/frameworks/toolkits.  We can see this because even if jQuery is and always will be your library of choice, as you build more advanced applications you might want some binding, templating, MVC, or other action.  AngularJS by Google is getting more widely used.  It gives you binding, declarative HTML markup for data, and other MVC goodies.  There&#8217;s Backbone.JS, Underscore.JS, and tons of others.  A lot of folks seem to be combining all of these separate libraries together to custom fit their project.  And I&#8217;m sure that works fantastically &#8211; I was doing similar things for a time.</p>
<p>As these separate projects get more and more features, you may now have feature overlap between 3 different libraries you&#8217;ve used in your project.  It seems a LITTLE wasteful at best.  At worst, maybe your dev team does a very basic thing 3 different ways because binding is available 3 different ways given each of the libraries you&#8217;re using.</p>
<p>Wouldn&#8217;t it be better, or even just worth looking into, if you could find a cohesive library/framework that offered all of these things together? Maybe it could even do a little more and help you make a massive Javascript application that loaded in components and scripts AS YOU NEED THEM instead of all at once in the beginning.</p>
<p>It looked like Dojo and ExtJS were the most mature offerings in terms of what I describe.  I took a look at ExtJS by Sencha first.  At first glance, it looks pretty awesome.  You&#8217;ve got designer and animator tools.  They are a wee pricey, but considering my Flash background, spending $279 on Sencha Designer and $199 on Sencha Animator doesn&#8217;t seem obscene.</p>
<p>What I can&#8217;t get past is having to spend $329 for a Javascript library license!  When I&#8217;m doing Flash/Flex development, I use the Flash Builder IDE for coding and sometimes Flash Professional if I&#8217;m doing animations or graphics.  And that&#8217;s cool.  I chose to buy into these tools.  I didn&#8217;t have to pay to use Actionscript 3 though.  My fellow developers who may not do Flash all the time can crack open any text editor and edit my code.  They can check it back into SVN and contribute to the project.  They can launch the Flex SDK command line and build my project.  None of this costs a dime.</p>
<p>If and when they get more invested in Flash/Flex, they might then want to buy Flash Builder or even IntelliJ for the advanced featureset including debugging, profiling, etc.</p>
<p>Not so with ExtJS.  I&#8217;m buying a license to use the code base.  Kind icky in my mind.  If I start a project in ExtJS/Sencha and I pay my $329 for the seat, but then I want some more help on the project occasionally, each developer will then need to pay out $329 just to use/edit the code no matter what tools they use.  If you are starting a major project, and plan the ExtJS cost into the budget, then great.  But if you&#8217;re a lone developer who wants to start something which blossoms into something bigger &#8211; the licensing can be a major roadblock.</p>
<p>Moreso, if I&#8217;m a consultant and I&#8217;m paid x dollars to develop a web application, I can easily choose and pay to use ExtJS.  After two months, I make a deliverable.  It might be the type of project where I do the major architecture and first phase, but then it gets shipped off internationally for maintenance mode or a lower cost second phase of the project.  Well, if you use ExtJS, you&#8217;ve just upped maintenance mode by $329 for every developer that needs to touch the project.  You&#8217;ve just cost your client lots of money.</p>
<p>This is why I discounted Sencha and ExtJS and moved on to exploring Dojo.  Dojo seems to offer MOST of what Sencha does.  They both have a rich set of components, offer lazy loading of scripts so that your massive enterprise application doesn&#8217;t load several hundred scripts (or a huge 2MB block of script) before the application starts up.  Both have a build system &#8211; Dojo&#8217;s runs on Rhino or Node, and is built on Closure or Shrinksafe.  ExtJS seems to be built off of a Yahoo builder.</p>
<p>In terms of scripting, both seem to nudge you in the direction of organizing your Javascript into classes which can be based on straight inheritance, or mixins.</p>
<p>Both seem to offer a visual tool for design.  Dojo&#8217;s <a href="http://maqetta.org/">Maqetta</a> is free and can be used for jQuery, YUI, or Dojo.  I don&#8217;t see myself being very productive in it though &#8211; I can lay things out, but it wasn&#8217;t obvious to me if I could wire in data or do anything extensive.  Though I haven&#8217;t used Ext Designer, I have to imagine it&#8217;s a better product than Maqetta.  But then I seriously question how much I&#8217;d use a good tool for this anyway.  It&#8217;s like when everyone complains that design mode in Flex really sucks &#8211; but what if it didn&#8217;t? Would you actually use it, or just code everything?  I think we&#8217;d use it only like 5% of the time.  But this is all personal preference &#8211; ExtJS wins here, but only if you feel like you need a good design tool, which I don&#8217;t.</p>
<p>Dojo seems to offer one thing that ExtJS doesn&#8217;t though &#8211; and that&#8217;s declarative HTML markup.  This may be where my ExtJS ignorance comes in, so feel free to correct me if I&#8217;m wrong, but to mark up a component like you might do with Flex&#8217;s MXML, you write a JSON object describing the component or layout you&#8217;re introducing in ExtJS.  This is pretty cool &#8211; but with Dojo, you get to write REAL HTML.  I can tag existing divs or other elements as a Dojo component and they&#8217;ll act like Dojo components.  Yes, Dojo does offer templating, but you can choose which to use&#8230;or even to use both.</p>
<p>The worst part of Dojo?  They don&#8217;t have a MASSIVE marketing and tutorial push.  There is tons of ExtJS and Sencha material out there to learn from.  Yes, there is tons of Dojo material, but much of it is older and not up to date.  The material on the Dojo site is pretty technical and not very &#8220;quickstart-ish&#8221;.  What quickstart material they do have, don&#8217;t seem to be big picture enough to inform you what&#8217;s really going on.  I always had bad luck copying and pasting code from tutorials and having it work without spending hours debugging.  Most of the trouble with Dojo material is the switch to new loading routines that happened with the 1.7 release in December 2011.  I feel like when they release 2.0 sometime this year, we&#8217;ll have some better effort from the Dojo Foundation in terms of educating people on how to use the toolkit</p>
<p>All in all, Dojo and Sencha seem eerily similar from everything I read.  I feel so lucky that by learning Dojo, I&#8217;ll probably feel right at home in ExtJS as well.  For me it really comes down to the licensing that drives me to Dojo.  If that wasn&#8217;t an obstacle for me, there were more than a few times I&#8217;ve been frustrated by Dojo in the past couple weeks of learning it that I&#8217;d been open to abandoning ship if there was another choice.  I get the feeling though that there are similar frustrations with understanding ExtJS (helped in large part by their tutorials).</p>
<p>That&#8217;s why I&#8217;m team Dojo right now!  However, I&#8217;ll continue using jQuery for the smaller projects.  Dojo could easily replace jQuery, but for the smaller stuff when something minimal is all you need, there&#8217;s no sense in going against the grain and using something that not many people have experience with.</p>
<p>I&#8217;ll be posting more technical stuff on Dojo, but for now, this is how I got where I am!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.benfarrell.com/2012/02/21/why-dojo-from-a-flashflexers-perspective/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>iOS Video is Cranky!</title>
		<link>http://www.benfarrell.com/2012/01/28/ios-video-is-cranky/</link>
		<comments>http://www.benfarrell.com/2012/01/28/ios-video-is-cranky/#comments</comments>
		<pubDate>Sat, 28 Jan 2012 19:45:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[ios]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://www.benfarrell.com/?p=647</guid>
		<description><![CDATA[I&#8217;m close to wrapping up a decent sized iOS project, and my part was video playback.  Prior to this, I&#8217;ve done tons of work with Flash video. I hate to be labeled biased, but I&#8217;ve never appreciated the level of detail put into the Flash API before working with both HTML5 and iOS.  Flash has [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m close to wrapping up a decent sized iOS project, and my part was video playback.  Prior to this, I&#8217;ve done tons of work with Flash video.</p>
<p>I hate to be labeled biased, but I&#8217;ve never appreciated the level of detail put into the Flash API before working with both HTML5 and iOS.  Flash has tons of quality of service queries you can make, you have explicit control of bitrate on multibitrate streams, you have complete seek accuracy in streams.  Virtually anything you could ever want, you got it!</p>
<p>Compare that to the AVPlayerFramework API for iOS.  You can listen to status messages and put an observer on the current playhead time.  That&#8217;s pretty much it.  And those status messages come in the form of ready to &#8220;play&#8221;, &#8220;failed&#8221;, &#8220;played to the end&#8221;, &#8220;failed to play to the end&#8221; and &#8220;unknown&#8221;.</p>
<p>The lack of features actually isn&#8217;t too much of a problem for streaming playback.  Streaming content is handled like a champ by the framework.  A bad network connection might cause the stream to stutter, freeze, and stall &#8211; but as the connection gets better the video is recovered quite well automatically.</p>
<p>One of the complications presented by Apple&#8217;s guidelines is that an application that streams must provide an audio only low-bitrate stream to compliment the higher bitrate video/audio streams.  The AVPlayer framework does a great job doing what it needs to do switching the user to the best available stream.  Unfortunately, if bandwidth gets really bad, the audio only stream is a valid option &#8211; and iOS WILL switch to it.</p>
<p>It presents an interesting conundrum.  Video looks broken to the user, and especially your clients who are footing the bill.  I&#8217;d almost rather have the video stammer and stutter than to switch to this audio only bitrate &#8211; at least then, most tech savvy users would know that they are having network connectivity issues.</p>
<p>The other problem is the ability to seek to exact positions in a stream using the AVPlayer in anything less than iOS 5.  A stream is broken up into chunks.  Those chunks are transport segment (.ts) files.  Typically those chunks will be around 7-10 seconds each.  Turns out that if you want to seek to a specific second of your stream, it better lie at the beginning of a segment, otherwise your seek accuracy is (at worst) 7-10 seconds off.</p>
<p>Past that, streams are pretty decent, like I said.  What got frustrating though, was simple progressive video!</p>
<p>Progressive is fairly easy to play.  You load, play and go.  After loading, you need to wait for the item ready status, and then tell the content to play.  By nature, progressive loads welllll&#8230;.progressively.  You can start playing back the video before the entire thing finishes loading.</p>
<p>The limited API of AVPlayer seems to present some problems here.  Your video is only &#8220;ready&#8221; once.  What happens if you are progressively playing, but suddenly your connection drops low, and you can&#8217;t quite finish playing the content because you&#8217;ve reached the end of whats been playing?  I&#8217;ll tell you what happens&#8230;.your video pauses.  While paused, the rest of the video will finish loading.  Unfortunately, there is no status message to inform you that the video is ready to play again.  So there it sits.  Paused.</p>
<p>Drastic shortcomings call for stupid hacks.   Such as setting up a timer to fire off every few seconds and tell the content to play.  If the content is currently playing, the call to play will do nothing.  However, if it&#8217;s stalled and paused, we can keep the video moving along automatically.</p>
<p>Another shortcoming I ran across is an unfortunate reality with the AVQueuePlayer.  The AVQueuePlayer is like the AVPlayer, but you can playlist things.  I chose to use this type of player due to other requirements in my project.  For my work, I wasn&#8217;t really using it as a queue.  Instead, I&#8217;d simply replace the current item in the queue to play a new asset.  Unfortunately this method of doing things didn&#8217;t seem very reliable.  If an item failed in my queue, I could add further items and make them play, however I wouldn&#8217;t get any status events about the item.  That means I&#8217;d never get told when it was ready to play, when it completed, when it failed.  It was really a showstopper.  Sometime, even if the item didn&#8217;t fail to play, I&#8217;d see that subsequent items wouldn&#8217;t produce status changes.</p>
<p>I ended up having to destroy the player and starting fresh each time &#8211; oh well.  Even though I wasn&#8217;t using it as a queue before, now I REALLY wasn&#8217;t using it as a queue.</p>
<p>So there it is.  I got through it, but video was  a little cranky!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.benfarrell.com/2012/01/28/ios-video-is-cranky/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The ADSR Envelope with Audiolib.js</title>
		<link>http://www.benfarrell.com/2011/12/17/the-adsr-envelope-with-audiolib-js/</link>
		<comments>http://www.benfarrell.com/2011/12/17/the-adsr-envelope-with-audiolib-js/#comments</comments>
		<pubDate>Sat, 17 Dec 2011 21:01:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[music]]></category>
		<category><![CDATA[ui]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.benfarrell.com/?p=582</guid>
		<description><![CDATA[So, let&#8217;s talk envelopes. First what they are, and what they can do to the quality of a sound. Demo here: http://www.benfarrell.com/labs/examples/envelopes-12-17/, but read on for how it all works! An envelope is a real simple concept actually. Take any signal, like a sound. Maybe that sound plays at a constant volume. When you apply an [...]]]></description>
			<content:encoded><![CDATA[<p>So, let&#8217;s talk envelopes. First what they are, and what they can do to the quality of a sound.</p>
<p>Demo here: <a href="http://www.benfarrell.com/labs/examples/envelopes-12-17/">http://www.benfarrell.com/labs/examples/envelopes-12-17/</a>, but read on for how it all works!</p>
<p>An envelope is a real simple concept actually. Take any signal, like a sound. Maybe that sound plays at a constant volume. When you apply an &#8220;envelope&#8221; to that sound, you are changing the volume of that sound while it&#8217;s played. It might go up and down, back up again, whatever.</p>
<p>How it goes up and down, and the speed at which the volume is changed is up to the details of the envelope used.</p>
<p>You could create an envelope that takes 8 hours to complete. Maybe you want to go to sleep with some music, and then wake up in the morning with music. If you know it takes you 30 minutes to fall asleep, you&#8217;ll start the music playing at a loud volume. Over the next 30 minutes, you envelope your sound from loud to quiet, to off. In the morning, 30 minutes before you wake up, the envelope makes the music go from off, to quiet, to loud again. It wakes you up!</p>
<p>While this 8 hours illustrates how an envelope works, it&#8217;s a bit different when talking musical tones.</p>
<p>It&#8217;s not that different, however. We&#8217;re still talking about volume over time, but we&#8217;re talking milliseconds instead of hours or even minutes or seconds.</p>
<p>The character or personality of a musical tone can be changed greatly by altering the envelope. While we talk about this in terms of 1/1000th of a second, you don&#8217;t really notice the volume changing as you listen to the tone. Your ear doesn&#8217;t necessarily detect that the volume is going up and down.</p>
<p>Instead, the sound just has a different tonal quality! A piano for example wouldn&#8217;t sound as sharp if it didn&#8217;t go from no sound to loud that quickly. An accordion though, has a longer time as it goes from quiet to loud. And that quality &#8211; the &#8220;attack&#8221; (amongst other factors) create the personality of the tone.</p>
<p>Let&#8217;s talk specifics now. ADSR.</p>
<p>That&#8217;s <strong>A</strong>ttack, <strong>D</strong>ecay, <strong>S</strong>ustain, <strong>R</strong>elease. The ADSR envelope is just one type of envelope, but it&#8217;s a popular one that&#8217;s been used in electronic music for decades.</p>
<ul>
<li>Attack is the period of time after the initial release, it&#8217;s typically the loudest part of the sound</li>
<li>Decay is the phase while you&#8217;re going from the attack to the sustain &#8211; you&#8217;re &#8220;decaying&#8221;  the volume from this sharp initial phase to the normal volume phase</li>
<li>Sustain is the normal phase of the sound.  It is typically less volume than the attack, and go on for an indefinite period of time, or for a specific amount of time</li>
<li>Release is the draw down from the sustain period to no sound.  A fade out</li>
</ul>
<div id="attachment_583" class="wp-caption alignnone" style="width: 310px"><a href="http://www.benfarrell.com/wp-content/uploads/2011/12/M4C_intro.ADSR_.png"><img class="size-medium wp-image-583" title="M4C_intro.ADSR" src="http://www.benfarrell.com/wp-content/uploads/2011/12/M4C_intro.ADSR_-300x127.png" alt="" width="300" height="127" /></a><p class="wp-caption-text">the attack, decay, sustain, and release phases</p></div>
<p>I&#8217;ve talked about <a href="https://github.com/jussi-kalliokoski/audiolib.js/">Audiolib.js</a> in previous posts.  Audiolib is the Javascript library that enables you to make these dynamic sounds in Chrome and Firefox.</p>
<p>Audio programming isn&#8217;t easy though!  So while Audiolib helps out in awesome ways, it doesn&#8217;t have concepts of notes and music theory.  You have to tell it what frequencies to play, and if playing a chord, which individual frequencies make up the chord &#8211; <a title="Chords and Arpeggiators with Audiolib.js" href="http://www.benfarrell.com/2011/12/13/chords-and-arpeggiators-with-audiolib-js/">which I explored and created my own helpers for</a>.</p>
<p>The ADSR envelope is another example of something that Audiolib.js <a href="https://github.com/jussi-kalliokoski/audiolib.js/blob/master/js/controls/adsr.js">provides</a>, however, it doesn&#8217;t provide any obvious usage for it.</p>
<p>There&#8217;s actually a VERY good reason for this.  While, we&#8217;re talking about envelopes on musical tones, the 8 hour sleepy-time envelope is another good usage example.  And that&#8217;s restricting envelopes to the volume of a sound.  There are tons more examples in audio synthesis that envelopes can be applied.  Like effects &#8211; you can apply a distortion effect to a sound (like a rock guitar).  But you can envelope the amount of distortion which is applied to the guitar.  This has nothing to do with volume, and everything to do with just how much of something is applied to something else.</p>
<p>So, I&#8217;d like to create a usage of our ADSR envelope that is limited to producing a musical tone &#8211; especially in the example of producing live sound by using a trigger (here it will be your computer/laptop keyboard).</p>
<p>Let&#8217;s start with our previous example where we extend the Audiolib.js Oscillator via a plugin.  We extended it to simply take a musical notation, like an &#8220;A&#8221; and set the correct frequency:</p>

<div class="wp_code"><div class="pre"><pre class="javascript" style="font-family:'Times New Roman',Garamond, Times;">audioLib.<span style="color: #660066;">generators</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Note'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>sampleRate<span style="color: #339933;">,</span> notation<span style="color: #339933;">,</span> octave<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #006600; font-style: italic;">// extend Oscillator</span>
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> prop <span style="color: #000066; font-weight: bold;">in</span> audioLib.<span style="color: #660066;">generators</span>.<span style="color: #660066;">Oscillator</span>.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span>prop<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> audioLib.<span style="color: #660066;">generators</span>.<span style="color: #660066;">Oscillator</span>.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#91;</span>prop<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #006600; font-style: italic;">// do constructor routine for Note and Oscillator</span>
<span style="color: #003366; font-weight: bold;">var</span> that <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// are we defining the octave separately? If so add it</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>octave<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
notation <span style="color: #339933;">+=</span> octave<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
that.<span style="color: #660066;">frequency</span> <span style="color: #339933;">=</span> Note.<span style="color: #660066;">getFrequencyForNotation</span><span style="color: #009900;">&#40;</span>notation<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
that.<span style="color: #660066;">waveTable</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Float32Array<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
that.<span style="color: #660066;">sampleRate</span> <span style="color: #339933;">=</span> sampleRate<span style="color: #339933;">;</span>
that.<span style="color: #660066;">waveShapes</span> <span style="color: #339933;">=</span> that.<span style="color: #660066;">waveShapes</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So let&#8217;s figure out how to work in an ADSR envelope. The usage for the Audiolib.js version is like so:</p>
<p><strong>myEnvelope = audioLib.ADSREnvelope(sampleRate, attack, decay, sustain, release, sustainTime, releaseTime);</strong></p>
<p>The parameters work like so:</p>
<ol>
<li>Sample Rate:  The sample rate of the audio &#8211; I won&#8217;t go into it here, as it&#8217;s a basic setting for audiolib</li>
<li>attack &#8211; the amount of time (in milliseconds) that the attack phase takes to complete</li>
<li>decay &#8211; the amount of time (in milliseconds) that the decay phase takes to complete</li>
<li>sustain &#8211; the level of volume during the sustain phase (from 0 to 1)  - the default is 1</li>
<li>release &#8211; the amount of time it takes for the release phase to complete (in milliseconds)</li>
<li>sustainTime &#8211; the amount of time it takes for the sustain phase to complete (in milliseconds).  This param is pretty important though, because if you pass in null, the sustain period is indefinite.  Unless you call into the envelope with a trigger, it will continue being in the sustain phase forever</li>
<li>releaseTime &#8211; the amount of time between the release phase and the envelope looping around to the attack phase again</li>
</ol>
<p>The envelope has 6 states of being (0-indexed inside the code):</p>
<ol>
<li>Attack Phase</li>
<li>Decay Phase</li>
<li>Sustain Phase</li>
<li>Release Phase</li>
<li>Timed Sustain Phase</li>
<li>Timed Release Phase</li>
</ol>
<p>To kick off our envelope, you trigger it:</p>
<p><strong>myEnvelope.triggerGate(true);</strong></p>
<p>Now we can start using our envelope.  The usage is a little weird to me, as the Audiolib.js library treats it like a &#8220;generator&#8221; which seems a bit complicated for what it does.  I just want a stream of numbers, but OK I&#8217;ll bite.  I&#8217;ll use it with the byte arrays and whatnot, as if it&#8217;s an Oscillator.</p>
<p><strong>var buffer = new Float32Array(1);</strong><br />
<strong>myEnvelope.append(buffer, 1);</strong></p>
<p>So, I&#8217;m just pulling one value at a time from the envelope, and putting it into my &#8220;buffer&#8221;.  But my buffer only has one value in it at any time.  Like I said, I feel like I&#8217;m being forced into using it in more complicated of a way than I need!  Maybe there&#8217;s something I&#8217;m missing.</p>
<p>Now, every time, I get create an audio data point in my sound, I can multiply the data point by my envelope.  Thus my envelope is applied!</p>
<p><strong>this[this.waveShape]() * buffer[0];</strong></p>
<p>To do this, I overrode the &#8220;getMix&#8221; function in the Audiolib Oscillator.  But I needed to do other things too.</p>
<p>Since I trigger the envelope with triggerGate, it will cycle through the attack and release to the sustain phase as the envelope is used, automatically.</p>
<p>It gets complicated at the release phase though.  After your computer/laptop keyboard is released, we need to enter the release phase.  But we&#8217;re still grabbing sound, because the release phase still produces sound as it fades.  So our Oscillator needs to track that it&#8217;s in the release phase (it knows by keeping track of the envelope.state, which is 3 or 5 here for release or timed release).</p>
<p>Then finally when it gets back to state 0, or the cycle begins again, we mark this note as &#8220;released&#8221;, so our buffer knows that it doesn&#8217;t need to pull from it anymore.  We have to be very careful of note pulling from more notes than we need, cause all this music stuff is hard work, and too many notes slows down your CPU and breaks the audio processing.</p>
<p>The above is if the sustain phase goes on as long as you hold the key!  What if it&#8217;s a timed sustain &#8211; then we need the logic in there to release the key when the envelope is done, rather than when our user releases the key.</p>
<p>Here&#8217;s our final <a href="http://www.benfarrell.com/labs/examples/envelopes-12-17/js/generation/note.js">Note.js code</a>.  And <a href="http://www.benfarrell.com/labs/examples/envelopes-12-17/js/controllers/keyboard.js">here&#8217;s a controller for keeping track of keys being pressed</a>.</p>
<p>It all comes together in my (Chrome only) demo:</p>
<p><a href="http://www.benfarrell.com/labs/examples/envelopes-12-17/">http://www.benfarrell.com/labs/examples/envelopes-12-17/</a></p>
<p>The demo starts out by not using an envelope at all.  You&#8217;ll hear some clicky-ness when you press and release a key.  That&#8217;s because you&#8217;re hearing the transition between no sound and the abrupt start in the phase of the waveform.  It&#8217;s EXACTLY one of the reasons why envelopes are useful &#8211; to ease these transitions in and out.</p>
<p>When you turn on the envelope, you can start adjusting parameters to see how the different properties of attack, decay, sustain, and release affect the overall personality of the tone.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.benfarrell.com/2011/12/17/the-adsr-envelope-with-audiolib-js/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Chords and Arpeggiators with Audiolib.js</title>
		<link>http://www.benfarrell.com/2011/12/13/chords-and-arpeggiators-with-audiolib-js/</link>
		<comments>http://www.benfarrell.com/2011/12/13/chords-and-arpeggiators-with-audiolib-js/#comments</comments>
		<pubDate>Tue, 13 Dec 2011 03:19:25 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[ui]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.benfarrell.com/?p=573</guid>
		<description><![CDATA[So, as you may have noticed with my recent flurry of blog posts I&#8217;ve been playing with Audiolib.js.  I can&#8217;t thank the author Jussi Kalliokoski enough for getting this project going.  When I did my Flash version, I spent many moons getting the audio buffer working correctly, then the oscillators, and I hadn&#8217;t even gotten [...]]]></description>
			<content:encoded><![CDATA[<p>So, as you may have noticed with my recent flurry of blog posts I&#8217;ve been playing with <a href="https://github.com/jussi-kalliokoski/audiolib.js/">Audiolib.js</a>.  I can&#8217;t thank the author Jussi Kalliokoski enough for getting this project going.  When I did my <a href="http://code.google.com/p/flashamaphone">Flash version</a>, I spent many moons getting the audio buffer working correctly, then the oscillators, and I hadn&#8217;t even gotten started on effects so much yet.</p>
<p>But audiolib.js has all of this, which means I can focus on the usage with the API rather than making the low level audio stuff work.</p>
<p><a href="http://benfarrell.com/labs/examples/arpeggiator-12-11/">For example, Chords and Arpeggiation!</a><br />
<em>Sorry! This only works in the latest version of Chrome</em></p>
<p>After a couple nights with the project, I extended out the standard <a href="http://www.benfarrell.com/2011/12/05/making-an-audiolib-js-plugin/">Oscillator with a Note</a>.  I&#8217;ve since refactored a bit to do the minimum amount of music theory in the Note Oscillator object itself, and offload into a different object &#8211; but the net effect is that instead of instantiating a new Oscillator with a frequency, we can instantiate a new Note with a musical notation.  For example with Oscillator, we&#8217;d say 440hz, with Note, we&#8217;d say &#8220;A&#8221; or &#8220;A4&#8243; (if you don&#8217;t want to assume the 4th octave).</p>
<p>The obvious extension to this is groups of notes &#8211; or rather Chords.  There are two challenges here.  The first is to convert a chord notation to array of notes.  For example, if I say &#8220;Cm&#8221;, or C Minor, I want an array such as ["C", "Eb", "G"].  We need to convert the C Minor notation into the implied triad of 3 notes using the minor scale.  Also possible is a 6th, 7th, 9th, 11th, or 13th.  In fact, now that I type, I realize I never did a 5th, which if I recall correctly, omits the middle note of a triad.</p>
<p>It all came out fairly well.  I don&#8217;t think augmentation, sustain, or diminish work properly yet, but the chord structures are there. The simplest way to use this is to use ChordFactory instead of Chord. Simply call ChordFactory.createNotations(&#8220;C&#8221;, 4) to get a list of notes (strings) or ChordFactory.createNotes(&#8220;C&#8221;, 4) to get an array of Note Oscillators.</p>

<div class="wp_code"><div class="pre"><pre class="javascript" style="font-family:'Times New Roman',Garamond, Times;">ChordConstants <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
   MAJOR_TRIAD<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;maj&quot;</span><span style="color: #339933;">,</span>
   MINOR_TRIAD<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;m&quot;</span><span style="color: #339933;">,</span>
   SEVENTH<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;7&quot;</span><span style="color: #339933;">,</span>
   MINOR_SEVENTH<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;m7&quot;</span><span style="color: #339933;">,</span>
   MAJOR_SEVENTH<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;maj7&quot;</span><span style="color: #339933;">,</span>
   NINTH<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;9&quot;</span><span style="color: #339933;">,</span>
   MINOR_NINTH<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;m9&quot;</span><span style="color: #339933;">,</span>
   MAJOR_NINTH<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;maj9&quot;</span><span style="color: #339933;">,</span>
   ELEVENTH<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;11&quot;</span><span style="color: #339933;">,</span>
   THIRTEENTH<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;13&quot;</span><span style="color: #339933;">,</span>
   SIXTH<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;6&quot;</span><span style="color: #339933;">,</span>
   MINOR_SIXTH<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;m6&quot;</span><span style="color: #339933;">,</span>
   SUSTAIN<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;sus&quot;</span><span style="color: #339933;">,</span>
   AUGMENTED<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;aug&quot;</span><span style="color: #339933;">,</span>
   DIMINISHED<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;dim&quot;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
ChordFactory <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">/**
     * create a list of notations from chord
     * @param chord notation
     * @param notation array (individual notes)
     */</span>
    createNotations<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> createNotations<span style="color: #009900;">&#40;</span>notation<span style="color: #339933;">,</span> octave<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> chord <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Chord<span style="color: #009900;">&#40;</span>notation<span style="color: #339933;">,</span> octave<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> chord.<span style="color: #660066;">getNotations</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * create an array of note oscillators using the audiolib framework
     * @param sampleRate
     * @param notation
     */</span>
    createNotes<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> createNotes<span style="color: #009900;">&#40;</span>sampleRate<span style="color: #339933;">,</span> notation<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> chord <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Chord<span style="color: #009900;">&#40;</span>notation<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        nts <span style="color: #339933;">=</span> chord.<span style="color: #660066;">getNotations</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> osc<span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> oscs <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> nt <span style="color: #000066; font-weight: bold;">in</span> nts<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            osc <span style="color: #339933;">=</span> audioLib.<span style="color: #660066;">generators</span>.<span style="color: #660066;">Note</span><span style="color: #009900;">&#40;</span>sampleRate<span style="color: #339933;">,</span> nts<span style="color: #009900;">&#91;</span>nt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
            oscs.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>osc<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">return</span> oscs<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> Chord<span style="color: #009900;">&#40;</span>notation<span style="color: #339933;">,</span> octave<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> that <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009966; font-style: italic;">/** root note of chord */</span>
    that._root <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;C&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009966; font-style: italic;">/** octave of root */</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>octave<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        that._rootOctave <span style="color: #339933;">=</span> octave<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        that._rootOctave <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009966; font-style: italic;">/** chord notation */</span>
    that._notation <span style="color: #339933;">=</span> notation <span style="color: #339933;">?</span> notation <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Cmaj&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009966; font-style: italic;">/** notes in built chord */</span>
    that._notes <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * get notes from built chords
     *
     * @return notes
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getNotations</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>._notes<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * chord notation setter
     *
     * @param notation
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setNotation</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>._notation <span style="color: #339933;">=</span> value<span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildChord</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * chord notation getter
     *
     * @return notation
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getNotation</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>._notation<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * root note setter
     *
     * @param root
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setRoot</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>._root <span style="color: #339933;">=</span> value<span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildChord</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * root note getter
     *
     * @return root note
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getRoot</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>._root<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * root octave setter
     *
     * @param octave
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setRootOctave</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>._rootOctave <span style="color: #339933;">=</span> value<span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildChord</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * root octave getter
     *
     * @return root octave
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getRootOctave</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>._rootOctave<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * get notes in major triad
     *
     * @param root note
     * @param root octave
     * @return notes
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">majorTriad</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getStandardNotesInChordMakeup</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * get notes in minor triad
     *
     * @param root note
     * @param root octave
     * @return notes
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">minorTriad</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getStandardNotesInChordMakeup</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * get notes in seventh chord
     *
     * @param root note
     * @param root octave
     * @return notes
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">seventh</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getStandardNotesInChordMakeup</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * get notes in major seventh chord
     *
     * @param root note
     * @param root octave
     * @return notes
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">majorSeventh</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getStandardNotesInChordMakeup</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * get notes in minor seventh chord
     *
     * @param root note
     * @param root octave
     * @return notes
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">minorSeventh</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getStandardNotesInChordMakeup</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * get notes in ninth chord
     *
     * @param root note
     * @param root octave
     * @return notes
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">ninth</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getStandardNotesInChordMakeup</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * get notes in major ninth chord
     *
     * @param root note
     * @param root octave
     * @return notes
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">majorNinth</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getStandardNotesInChordMakeup</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * get notes in minor ninth chord
     *
     * @param root note
     * @param root octave
     * @return notes
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">minorNinth</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getStandardNotesInChordMakeup</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * get notes in eleventh chord
     *
     * @param root note
     * @param root octave
     * @return notes
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">eleventh</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getStandardNotesInChordMakeup</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * get notes in major eleventh chord
     *
     * @param root note
     * @param root octave
     * @return notes
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">majorEleventh</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getStandardNotesInChordMakeup</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * get notes in minor eleventh chord
     *
     * @param root note
     * @param root octave
     * @return notes
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">minorEleventh</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getStandardNotesInChordMakeup</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * get notes in thirteenth chord
     *
     * @param root note
     * @param root octave
     * @return notes
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">thirteenth</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getStandardNotesInChordMakeup</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">7</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * get notes in major thirteenth chord
     *
     * @param root note
     * @param root octave
     * @return notes
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">majorThirteenth</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getStandardNotesInChordMakeup</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">7</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * get notes in minor thirteenth chord
     *
     * @param root note
     * @param root octave
     * @return notes
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">minorThirteenth</span><span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getStandardNotesInChordMakeup</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">7</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * get notes in sixth chord
     *
     * @param root note
     * @param root octave
     * @return notes
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">sixth</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> keySig <span style="color: #339933;">=</span> Note.<span style="color: #660066;">notesInKeySignature</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> keys <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        keys.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>keySig<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> keySig<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> keySig<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> keySig<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> keys<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * get notes in minor sixth chord
     *
     * @param root note
     * @param root octave
     * @return notes
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">minorSixth</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> keySig <span style="color: #339933;">=</span> Note.<span style="color: #660066;">notesInKeySignature</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> keys <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        keys.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>keySig<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> keySig<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> keySig<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> keySig<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> keys<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * sustain chord
     *
     * @param notes
     * @param direction to sustain
     * @return notes
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">sustain</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>notes<span style="color: #339933;">,</span> sus<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        sus <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>sus <span style="color: #339933;">==</span> undefined<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #CC0000;">4</span> <span style="color: #339933;">:</span> sus<span style="color: #339933;">;</span>
        <span style="color: #006600; font-style: italic;">// grab the third in the chord</span>
        <span style="color: #003366; font-weight: bold;">var</span> third <span style="color: #339933;">=</span> notes<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> notations <span style="color: #339933;">=</span> Note.<span style="color: #660066;">sharpNotations</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> thirdIndex <span style="color: #339933;">=</span> Note.<span style="color: #660066;">sharpNotations</span>.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span>third<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>thirdIndex <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            notations <span style="color: #339933;">=</span> Note.<span style="color: #660066;">flatNotations</span><span style="color: #339933;">;</span>
            thirdIndex <span style="color: #339933;">=</span> Note.<span style="color: #660066;">flatNotations</span>.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span>third<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>sus<span style="color: #339933;">==</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// lower the third one half step</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>thirdIndex<span style="color: #339933;">-</span><span style="color: #CC0000;">1</span> <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                notes<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> notations<span style="color: #009900;">&#91;</span>notations.<span style="color: #660066;">length</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                notes<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> notations<span style="color: #009900;">&#91;</span>thirdIndex<span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// assume sus == 4</span>
            <span style="color: #006600; font-style: italic;">// raise the third one half step</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>thirdIndex<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span> <span style="color: #339933;">&gt;=</span> notations.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                notes<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> notations<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                notes<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> notations<span style="color: #009900;">&#91;</span>thirdIndex<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">return</span> notes<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * augment chord
     *
     * @param notes
     * @return notes
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">augment</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>notes<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// grab the fifth in the chord</span>
        <span style="color: #003366; font-weight: bold;">var</span> fifth <span style="color: #339933;">=</span> notes<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> notations <span style="color: #339933;">=</span> Note.<span style="color: #660066;">sharpNotations</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> fifthIndex <span style="color: #339933;">=</span> Note.<span style="color: #660066;">sharpNotations</span>.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span>fifth<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fifthIndex <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            notations <span style="color: #339933;">=</span> Note.<span style="color: #660066;">flatNotations</span><span style="color: #339933;">;</span>
            fifthIndex <span style="color: #339933;">=</span> Note.<span style="color: #660066;">flatNotations</span>.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span>fifth<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// raise the fifth one half step</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fifthIndex<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span> <span style="color: #339933;">&gt;=</span> notations.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            notes<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> notations<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            notes<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> notations<span style="color: #009900;">&#91;</span>fifthIndex<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">return</span> notes<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * get all standard notes in a chord, from triad to thirteenth
     *
     * @param root note
     * @param major key (true/false)
     * @param major chord (true/false)
     * @param root octave
     * @return notes array
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getStandardNotesInChordMakeup</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> majorKey<span style="color: #339933;">,</span> majorChord<span style="color: #339933;">,</span> octave<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        majorKey <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>majorKey <span style="color: #339933;">==</span> undefined<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #003366; font-weight: bold;">true</span> <span style="color: #339933;">:</span> majorKey<span style="color: #339933;">;</span>
        majorChord <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>majorChord <span style="color: #339933;">==</span> undefined<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #003366; font-weight: bold;">false</span> <span style="color: #339933;">:</span> majorChord<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> majKeySig <span style="color: #339933;">=</span> Note.<span style="color: #660066;">notesInKeySignature</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> octave<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> minKeySig <span style="color: #339933;">=</span> Note.<span style="color: #660066;">notesInKeySignature</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> octave<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// grab the next octave if we need it</span>
        <span style="color: #003366; font-weight: bold;">var</span> majKeySig2 <span style="color: #339933;">=</span> Note.<span style="color: #660066;">notesInKeySignature</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> octave<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> minKeySig2 <span style="color: #339933;">=</span> Note.<span style="color: #660066;">notesInKeySignature</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> octave<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> notes<span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>majorKey <span style="color: #339933;">&amp;&amp;</span> majorChord<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// C Major Seventh for example</span>
            notes <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span> majKeySig<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> majKeySig<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> majKeySig<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> majKeySig<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">6</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> majKeySig2<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> majKeySig2<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>majorKey <span style="color: #339933;">&amp;&amp;</span> majorChord<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// C Minor Seventh for example</span>
            notes <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span> minKeySig<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> minKeySig<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> minKeySig<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> minKeySig<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">6</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> minKeySig2<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> minKeySig2<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>majorKey <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>majorChord<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// C Seventh for example</span>
            notes <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span> majKeySig<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> majKeySig<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> majKeySig<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> minKeySig<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">6</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> majKeySig2<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> minKeySig2<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>majorKey <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>majorChord<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// C Seventh for example</span>
            notes <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span> majKeySig<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> minKeySig<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> majKeySig<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> minKeySig<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">6</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> majKeySig2<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> minKeySig2<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">return</span> notes<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * convert notation to note list
     *
     * @param notation
     * @param use the octave in the notation
     * @return note list
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">notesFromChordNotation</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>notation<span style="color: #339933;">,</span> octave<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> root<span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> major <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> chordType<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// find root</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>notation.<span style="color: #660066;">charAt</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;#&quot;</span> <span style="color: #339933;">||</span> notation.<span style="color: #660066;">charAt</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;b&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            root <span style="color: #339933;">=</span> notation.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            notation <span style="color: #339933;">=</span> notation.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            root <span style="color: #339933;">=</span> notation.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            notation <span style="color: #339933;">=</span> notation.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// major or minor? (3 states - 1 is on, -1 is off, 0 is unspecified)</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> notation.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;maj&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            major <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
            notation <span style="color: #339933;">=</span> notation.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>notation.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;m&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            major <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
            notation <span style="color: #339933;">=</span> notation.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// set chord type</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> notation.<span style="color: #660066;">charAt</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;6&quot;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>major <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                chordType <span style="color: #339933;">=</span> ChordConstants.<span style="color: #660066;">MINOR_SIXTH</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                chordType <span style="color: #339933;">=</span> ChordConstants.<span style="color: #660066;">SIXTH</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            notation <span style="color: #339933;">=</span> notation.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> notation.<span style="color: #660066;">charAt</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;7&quot;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>major <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                chordType <span style="color: #339933;">=</span> ChordConstants.<span style="color: #660066;">SEVENTH</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>major <span style="color: #339933;">==</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                chordType <span style="color: #339933;">=</span> ChordConstants.<span style="color: #660066;">MAJOR_SEVENTH</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>major <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                chordType <span style="color: #339933;">=</span> ChordConstants.<span style="color: #660066;">MINOR_SEVENTH</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            notation <span style="color: #339933;">=</span> notation.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> notation.<span style="color: #660066;">charAt</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;9&quot;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>major <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                chordType <span style="color: #339933;">=</span> ChordConstants.<span style="color: #660066;">NINTH</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>major <span style="color: #339933;">==</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                chordType <span style="color: #339933;">=</span> ChordConstants.<span style="color: #660066;">MAJOR_NINTH</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>major <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                chordType <span style="color: #339933;">=</span> ChordConstants.<span style="color: #660066;">MINOR_NINTH</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            notation <span style="color: #339933;">=</span> notation.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> notation.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;11&quot;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            chordType <span style="color: #339933;">=</span> ChordConstants.<span style="color: #660066;">ELEVENTH</span><span style="color: #339933;">;</span>
            notation <span style="color: #339933;">=</span> notation.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> notation.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;13&quot;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            chordType <span style="color: #339933;">=</span> ChordConstants.<span style="color: #660066;">THIRTEENTH</span><span style="color: #339933;">;</span>
            notation <span style="color: #339933;">=</span> notation.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>major <span style="color: #339933;">==</span> <span style="color: #CC0000;">1</span> <span style="color: #339933;">||</span> major <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                chordType <span style="color: #339933;">=</span> ChordConstants.<span style="color: #660066;">MAJOR_TRIAD</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                chordType <span style="color: #339933;">=</span> ChordConstants.<span style="color: #660066;">MINOR_TRIAD</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #003366; font-weight: bold;">var</span> notes <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">notesFromChordType</span><span style="color: #009900;">&#40;</span>chordType<span style="color: #339933;">,</span> root<span style="color: #339933;">,</span> octave<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// modify note set if needed</span>
        <span style="color: #003366; font-weight: bold;">var</span> modifier <span style="color: #339933;">=</span> notation<span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span> modifier.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">case</span> ChordConstants.<span style="color: #660066;">AUGMENTED</span><span style="color: #339933;">:</span>
                notes <span style="color: #339933;">=</span> augment<span style="color: #009900;">&#40;</span>notes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">case</span> ChordConstants.<span style="color: #660066;">DIMINISHED</span><span style="color: #339933;">:</span>
                <span style="color: #006600; font-style: italic;">// to do</span>
                <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">case</span> ChordConstants.<span style="color: #660066;">SUSTAIN</span><span style="color: #339933;">:</span>
                <span style="color: #003366; font-weight: bold;">var</span> param <span style="color: #339933;">=</span> int<span style="color: #009900;">&#40;</span>modifier.<span style="color: #660066;">charAt</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                notes <span style="color: #339933;">=</span> sustain<span style="color: #009900;">&#40;</span>notes<span style="color: #339933;">,</span> param<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">return</span> notes<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * get notes from chord types
     *
     * @param type
     * @param chord root
     * @return notes
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">notesFromChordType</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>type<span style="color: #339933;">,</span> root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span> type <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">case</span> ChordConstants.<span style="color: #660066;">SIXTH</span><span style="color: #339933;">:</span>
                 <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">sixth</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">case</span> ChordConstants.<span style="color: #660066;">MINOR_SIXTH</span><span style="color: #339933;">:</span>
                 <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">minorSixth</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">case</span> ChordConstants.<span style="color: #660066;">SEVENTH</span><span style="color: #339933;">:</span>
                 <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">seventh</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">case</span> ChordConstants.<span style="color: #660066;">MINOR_SEVENTH</span><span style="color: #339933;">:</span>
                 <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">minorSeventh</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">case</span> ChordConstants.<span style="color: #660066;">MAJOR_SEVENTH</span><span style="color: #339933;">:</span>
                 <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">majorSeventh</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">case</span> ChordConstants.<span style="color: #660066;">NINTH</span><span style="color: #339933;">:</span>
                 <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">ninth</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">case</span> ChordConstants.<span style="color: #660066;">MINOR_NINTH</span><span style="color: #339933;">:</span>
                 <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">minorNinth</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">case</span> ChordConstants.<span style="color: #660066;">MAJOR_NINTH</span><span style="color: #339933;">:</span>
                 <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">majorNinth</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">case</span> ChordConstants.<span style="color: #660066;">ELEVENTH</span><span style="color: #339933;">:</span>
                 <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">eleventh</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">case</span> ChordConstants.<span style="color: #660066;">THIRTEENTH</span><span style="color: #339933;">:</span>
                 <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">thirteenth</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">case</span> ChordConstants.<span style="color: #660066;">MAJOR_TRIAD</span><span style="color: #339933;">:</span>
                 <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">majorTriad</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">case</span> ChordConstants.<span style="color: #660066;">MINOR_TRIAD</span><span style="color: #339933;">:</span>
                 <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">minorTriad</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #003366; font-weight: bold;">default</span><span style="color: #339933;">:</span>
                 <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">majorTriad</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> rootOctave<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * build the chord given the parameters set in this class
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildChord</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>._notes <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> notations <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">notesFromChordNotation</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._notation<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>._rootOctave<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> c <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> c <span style="color: #339933;">&lt;</span> notations.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> c<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">this</span>._notes.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>notations<span style="color: #009900;">&#91;</span>c<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// do a build based on initial params</span>
    that.<span style="color: #660066;">buildChord</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Namespacing will probably come when I think of a name for this project!</p>
<p>Anyway, we have some code that creates chord structures!  Yay!  Now, what about sending this to the buffer for mixing.  The most basic example in audiolib.js is like so:</p>
<p>&nbsp;</p>

<div class="wp_code"><div class="pre"><pre class="javascript" style="font-family:'Times New Roman',Garamond, Times;"><span style="color: #003366; font-weight: bold;">function</span> audioCallback<span style="color: #009900;">&#40;</span>buffer<span style="color: #339933;">,</span> channelCount<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Fill the buffer with the oscillator output.</span>
    osc.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>buffer<span style="color: #339933;">,</span> channelCount<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This takes the Oscillator (or any audio generator for that matter) and generates some bytes to send to the audio buffer.  Audiolib had some examples of mixing, but I didn&#8217;t care for them so much, as they seemed a little overly complicated with whatever idea of &#8220;leads&#8221; they had &#8211; I was a bit confused. In the end, I came up with something that worked a little better for me, but is probably virtually the same concept:</p>

<div class="wp_code"><div class="pre"><pre class="javascript" style="font-family:'Times New Roman',Garamond, Times;"><span style="color: #003366; font-weight: bold;">function</span> audioCallback<span style="color: #009900;">&#40;</span>buffer<span style="color: #339933;">,</span> channelCount<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// get a list of generators from our keyboard controller object - it keeps tracks of keys pressed</span>
    <span style="color: #003366; font-weight: bold;">var</span> gens <span style="color: #339933;">=</span> ctrl.<span style="color: #660066;">pull</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> bl <span style="color: #339933;">=</span> buffer.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> gl <span style="color: #339933;">=</span> gens.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// loop through each sample in the buffer</span>
	<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>current<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> current<span style="color: #339933;">&lt;</span>bl<span style="color: #339933;">;</span> current<span style="color: #339933;">+=</span> channelCount<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		sample <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
                <span style="color: #006600; font-style: italic;">// here we're combining samples from our list of generators</span>
                <span style="color: #006600; font-style: italic;">// and combining down into one sample</span>
                <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>gl<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			gens<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">generate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			sample <span style="color: #339933;">+=</span> gens<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getMix</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">0.5</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// Here we write this sample to each channel we have (I'm just doing 2 channels)</span>
		<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>n<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> n<span style="color: #339933;">&lt;</span>channelCount<span style="color: #339933;">;</span> n<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			buffer<span style="color: #009900;">&#91;</span>current <span style="color: #339933;">+</span> n<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> sample<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>That&#8217;s pretty much it!  We&#8217;re constructing Oscillators from Chord notations, and then mixing the result for playback.</p>
<p>The last thing I wanted to try was an arpeggiator.  An arpeggiator takes a chord structure, and instead of playing all the notes at the same time, only plays one note at a time in sequence.</p>
<p>I extended the Audiolib Oscillator again, making an Arpeggiator plugin.  I needed to override the &#8220;generate&#8221; method.  Every time &#8220;generate&#8221; is called, I&#8217;d step through.  Once my step count reaches a threshold (the samplerate divided by some number), I&#8217;d change to the next frequency.</p>
<p>Here&#8217;s what I got:</p>

<div class="wp_code"><div class="pre"><pre class="javascript" style="font-family:'Times New Roman',Garamond, Times;">audioLib.<span style="color: #660066;">generators</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Arpeggiator'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>sampleRate<span style="color: #339933;">,</span> arpeggiatorRate<span style="color: #339933;">,</span> notes<span style="color: #339933;">,</span> octave<span style="color: #339933;">,</span> autoReverse<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// extend Oscillator</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> prop <span style="color: #000066; font-weight: bold;">in</span> audioLib.<span style="color: #660066;">generators</span>.<span style="color: #660066;">Oscillator</span>.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span>prop<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> audioLib.<span style="color: #660066;">generators</span>.<span style="color: #660066;">Oscillator</span>.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#91;</span>prop<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// do constructor routine for Note and Oscillator</span>
	<span style="color: #003366; font-weight: bold;">var</span>	that <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>autoReverse <span style="color: #339933;">===</span> undefined<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        that.<span style="color: #660066;">autoReverse</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        that.<span style="color: #660066;">autoReverse</span> <span style="color: #339933;">=</span> autoReverse<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    that.<span style="color: #660066;">buildFrequencies</span><span style="color: #009900;">&#40;</span>notes<span style="color: #339933;">,</span> octave<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	that.<span style="color: #660066;">waveTable</span>	<span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Float32Array<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	that.<span style="color: #660066;">sampleRate</span> <span style="color: #339933;">=</span> sampleRate<span style="color: #339933;">;</span>
	that.<span style="color: #660066;">waveShapes</span>	<span style="color: #339933;">=</span> that.<span style="color: #660066;">waveShapes</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    that.<span style="color: #660066;">arpStep</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    that.<span style="color: #660066;">arpIndex</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    that.<span style="color: #660066;">arpeggiatorRate</span> <span style="color: #339933;">=</span> arpeggiatorRate<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * override generate function
     */</span>
    that.<span style="color: #660066;">generate</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span>	self	<span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span>
			f	<span style="color: #339933;">=</span> <span style="color: #339933;">+</span>self.<span style="color: #660066;">frequency</span><span style="color: #339933;">,</span>
			pw	<span style="color: #339933;">=</span> self.<span style="color: #660066;">pulseWidth</span><span style="color: #339933;">,</span>
			p	<span style="color: #339933;">=</span> self.<span style="color: #660066;">phase</span><span style="color: #339933;">;</span>
		f <span style="color: #339933;">+=</span> f <span style="color: #339933;">*</span> self.<span style="color: #660066;">fm</span><span style="color: #339933;">;</span>
		self.<span style="color: #660066;">phase</span>	<span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>p <span style="color: #339933;">+</span> f <span style="color: #009966; font-style: italic;">/ self.sampleRate /</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
		p		<span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">phase</span> <span style="color: #339933;">+</span> self.<span style="color: #660066;">phaseOffset</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
		self._p		<span style="color: #339933;">=</span> p <span style="color: #339933;">&lt;</span> pw <span style="color: #339933;">?</span> p <span style="color: #009966; font-style: italic;">/ pw : (p-pw) /</span> <span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">-</span>pw<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        self.<span style="color: #660066;">arpStep</span> <span style="color: #339933;">++;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">arpStep</span> <span style="color: #339933;">&gt;</span> self.<span style="color: #660066;">sampleRate</span> <span style="color: #339933;">*</span> self.<span style="color: #660066;">arpeggiatorRate</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            self.<span style="color: #660066;">arpStep</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
            self.<span style="color: #660066;">arpIndex</span> <span style="color: #339933;">++;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">arpIndex</span> <span style="color: #339933;">&gt;=</span> self.<span style="color: #660066;">frequencies</span>.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                self.<span style="color: #660066;">arpIndex</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            self.<span style="color: #660066;">frequency</span> <span style="color: #339933;">=</span> self.<span style="color: #660066;">frequencies</span><span style="color: #009900;">&#91;</span>self.<span style="color: #660066;">arpIndex</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    buildFrequencies<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>notes<span style="color: #339933;">,</span> octave<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> self <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
        self.<span style="color: #660066;">frequencies</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>notes <span style="color: #000066; font-weight: bold;">instanceof</span> Array<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// work with either a chord structure...</span>
            notes <span style="color: #339933;">=</span> ChordFactory.<span style="color: #660066;">createNotations</span><span style="color: #009900;">&#40;</span>notes<span style="color: #339933;">,</span> octave<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> c <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> c <span style="color: #339933;">&lt;</span> notes.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> c<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// ...or array of notations</span>
            self.<span style="color: #660066;">frequencies</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span> Note.<span style="color: #660066;">getFrequencyForNotation</span><span style="color: #009900;">&#40;</span>notes<span style="color: #009900;">&#91;</span>c<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">autoReverse</span> <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> c <span style="color: #339933;">=</span> notes.<span style="color: #660066;">length</span><span style="color: #339933;">-</span><span style="color: #CC0000;">2</span><span style="color: #339933;">;</span> c <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> c<span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                self.<span style="color: #660066;">frequencies</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span> Note.<span style="color: #660066;">getFrequencyForNotation</span><span style="color: #009900;">&#40;</span>notes<span style="color: #009900;">&#91;</span>c<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        self.<span style="color: #660066;">frequency</span> <span style="color: #339933;">=</span> self.<span style="color: #660066;">frequencies</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Lotsa code here tonight!  I&#8217;d like to get started on some basic effects and envelopes next to make some non-boring tonal qualities to my playback.  At that point, I hope to have a nice little useable API I can put up somewhere &#8211; especially since I don&#8217;t have to put lotsa code up! </p>
]]></content:encoded>
			<wfw:commentRss>http://www.benfarrell.com/2011/12/13/chords-and-arpeggiators-with-audiolib-js/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making an Audiolib.js Plugin</title>
		<link>http://www.benfarrell.com/2011/12/05/making-an-audiolib-js-plugin/</link>
		<comments>http://www.benfarrell.com/2011/12/05/making-an-audiolib-js-plugin/#comments</comments>
		<pubDate>Mon, 05 Dec 2011 04:00:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[music]]></category>
		<category><![CDATA[ui]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.benfarrell.com/?p=568</guid>
		<description><![CDATA[I just started checking out Audiolib.js this weekend.  Audiolib is a Javascript project to help you synthesize sounds and tones in JS. Just recently, Google Chrome added the Web Audio API, while Firefox added the Audio Data API.  Both let you get low level with sound &#8211; you can add bytes to an audio buffer [...]]]></description>
			<content:encoded><![CDATA[<p>I just started checking out <a href="https://github.com/jussi-kalliokoski/audiolib.js/">Audiolib.js</a> this weekend.  Audiolib is a Javascript project to help you synthesize sounds and tones in JS.</p>
<p>Just recently, Google Chrome added the Web Audio API, while Firefox added the Audio Data API.  Both let you get low level with sound &#8211; you can add bytes to an audio buffer and manufacture sound in realtime.</p>
<p>The library is bundled with &#8220;sink.js&#8221; which handles the inconsistencies in the audio API between Firefox and Chrome to create a common API.  Once this is abstracted away, we can make some audio.</p>
<p>I&#8217;ve done the nitty gritty of writing bytes to the buffer in Flash, and from my limited playing Audiolib does a great job with this, and I&#8217;m ecstatic I don&#8217;t have to rewrite my Flash stuff!</p>
<p>Audiolib provides &#8220;generators&#8221; for you to work with.  A &#8220;generator&#8221; is something that makes a sound, and defines a common API for anything that makes a noise to write that sound to the audio buffer.</p>
<p>In the generators namespace/package, we have an Oscillator, White/Pink/Brown Noise, and a Sampler.  White noise is cool and all, but I jumped right to the Oscillator to make a real tone.  The Oscillator takes a sample rate and a frequency.</p>
<p>You can define it thusly:</p>

<div class="wp_code"><div class="pre"><pre class="javascript" style="font-family:'Times New Roman',Garamond, Times;">dev <span style="color: #339933;">=</span> audioLib.<span style="color: #660066;">AudioDevice</span><span style="color: #009900;">&#40;</span>audioCallback <span style="color: #009966; font-style: italic;">/* callback for the buffer fills */</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span> <span style="color: #009966; font-style: italic;">/* channelCount */</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
osc <span style="color: #339933;">=</span> audioLib.<span style="color: #660066;">Oscillator</span><span style="color: #009900;">&#40;</span>dev.<span style="color: #660066;">sampleRate</span> <span style="color: #009966; font-style: italic;">/* sampleRate */</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">440</span> <span style="color: #009966; font-style: italic;">/* frequency */</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So, first we defined the device to use &#8211; we told it what to use for the audio callback, and how many channels there are (we have 2 channels&#8230;left and right).</p>
<p>The audio callback is a pretty standard thing in the world of audio.  When the sound card is starting to run out of audio to play, it signals Javascript and says &#8220;Hey I need more audio! Fill me up with some bytes&#8221;.  With our &#8220;audioCallback&#8221; function, we say &#8220;No problem sound card!  I got your back!  When you run low, please call our audio callback method &#8211; this will fill your audio buffer up&#8221;</p>
<p>Here&#8217;s an example of the audio callback:</p>

<div class="wp_code"><div class="pre"><pre class="javascript" style="font-family:'Times New Roman',Garamond, Times;"><span style="color: #003366; font-weight: bold;">function</span> audioCallback<span style="color: #009900;">&#40;</span>buffer<span style="color: #339933;">,</span> channelCount<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  osc.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>buffer<span style="color: #339933;">,</span> channelCount<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>All that happens here, is that the buffer and channel count gets passed into the method, and we tell out Oscillator what this is, and this Oscillator generates the appropriate bytes and send them to the buffer.</p>
<p>But what is that generator&#8230;the Oscillator?  Well, it creates a sound at a certain frequency.  We pass in the frequency as we instantiate this &#8220;osc&#8221; object.  When the audioCallback fires, it pulls this frequency from the osc object and plays a specific tone!</p>
<p>This is awesome!  All the hard work is done for us &#8211; but one of the first things I wanted to tackle here was to make it easier for musicians to understand. </p>
<p>See, casual musicians don&#8217;t know that a middle &#8220;A&#8221; on a piano oscillates at 440hz &#8211; they just know that they hit a middle &#8220;A&#8221; to play the tone.  I want something that makes sense for casual musicians, so my first small task with audiolib is to override Oscillator to take a notation vs a frequency.</p>
<p>Audiolib provides a plugin spec, but it tripped me up a little to understand how a &#8220;Generator&#8221; worked.  See, they have a js/generation folder which contains the Oscillator.  It&#8217;s all quite readable and self-documented.</p>
<p>However, the &#8220;append&#8221; method was not found in the Oscillator class at all!  I was looking EVERYWHERE for it!  </p>
<p>Eventually I found it in the &#8220;wrapper-end.js&#8221; script that&#8217;s outside of all the folders. </p>
<p>Basically, you create a generator like Oscillator, and you write it to the audiolib.generators namespace.  By virtue of being in this namespace, the &#8220;wrapper-end&#8221; script comes along to wrap things up.  It takes all the things in the audiolib.generators namespace and adds the generator base functions.  Basically it makes all the generators extend the generator base class after the fact.</p>
<p>Kinda confusing and sneaky if you ask me!  Oh well.</p>
<p>Regardless, the plugins are well documented, so I copied the sample generator plugin:</p>

<div class="wp_code"><div class="pre"><pre class="javascript" style="font-family:'Times New Roman',Garamond, Times;">audioLib.<span style="color: #660066;">generators</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'SemiClock'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>sampleRate<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">sampleRate</span> <span style="color: #339933;">=</span> sampleRate<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
	prevSample<span style="color: #339933;">:</span> <span style="color: #CC0000;">0.0</span><span style="color: #339933;">,</span>
	generate<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>sample<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">phase</span> <span style="color: #339933;">=</span> <span style="color: #339933;">+</span> <span style="color: #339933;">!</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">phase</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
	getMix<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">phase</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
	phase<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Cool, so we&#8217;re naming a generator in the generator namespace, and defining some required methods, like &#8220;generate&#8221; and &#8220;getMix&#8221;.</p>
<p>Well, all I want to do is make a copy of all the objects in Oscillator and add a function and initialization routine to accept a musical notation and instantiate an oscillator with the correct frequency.</p>
<p>So here&#8217;s my take:</p>

<div class="wp_code"><div class="pre"><pre class="javascript" style="font-family:'Times New Roman',Garamond, Times;">audioLib.<span style="color: #660066;">generators</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Note'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>sampleRate<span style="color: #339933;">,</span> notation<span style="color: #339933;">,</span> octave<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// extend Oscillator</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> prop <span style="color: #000066; font-weight: bold;">in</span> audioLib.<span style="color: #660066;">generators</span>.<span style="color: #660066;">Oscillator</span>.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span>prop<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> audioLib.<span style="color: #660066;">generators</span>.<span style="color: #660066;">Oscillator</span>.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#91;</span>prop<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// do constructor routine for Note and Oscillator</span>
	<span style="color: #003366; font-weight: bold;">var</span>	self <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    self.<span style="color: #660066;">octave</span> <span style="color: #339933;">=</span> isNaN<span style="color: #009900;">&#40;</span>octave<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #CC0000;">4</span> <span style="color: #339933;">:</span> octave<span style="color: #339933;">;</span>
    self.<span style="color: #660066;">setNotation</span><span style="color: #009900;">&#40;</span>notation<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	self.<span style="color: #660066;">waveTable</span>	<span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Float32Array<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	self.<span style="color: #660066;">sampleRate</span> <span style="color: #339933;">=</span> sampleRate<span style="color: #339933;">;</span>
	self.<span style="color: #660066;">waveShapes</span>	<span style="color: #339933;">=</span> self.<span style="color: #660066;">waveShapes</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009966; font-style: italic;">/* incremental tones as sharp notation */</span>
    sharpNotations<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;A&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;A#&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;B&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;C&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;C#&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;D&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;D#&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;E&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;F&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;F#&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;G&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;G#&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
&nbsp;
    <span style="color: #009966; font-style: italic;">/* incremental tones as flat notation */</span>
    flatNotations<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;A&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Bb&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;B&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;C&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Db&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;D&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Eb&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;E&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;F&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Gb&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;G&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Ab&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * notation setter
     * @param notation (octave is optional)
     */</span>
    setNotation<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>nt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">notation</span> <span style="color: #339933;">=</span> nt<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// does notation include the octave?</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>isNaN<span style="color: #009900;">&#40;</span> parseInt<span style="color: #009900;">&#40;</span>nt.<span style="color: #660066;">charAt</span><span style="color: #009900;">&#40;</span>nt.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">octave</span> <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>nt.<span style="color: #660066;">charAt</span><span style="color: #009900;">&#40;</span>nt.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">notation</span> <span style="color: #339933;">=</span> nt.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> nt.<span style="color: #660066;">length</span><span style="color: #339933;">-</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">frequency</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>._getFrequencyForNotation<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">notation</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * turn a notation into a frequency
     * @param notation
     * @return frequency
     */</span>
    _getFrequencyForNotation<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>nt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> freq<span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> indx <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">sharpNotations</span>.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span>nt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>indx <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            indx <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">flatNotations</span>.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span>nt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>indx <span style="color: #339933;">!=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            indx <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">octave</span><span style="color: #339933;">-</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">sharpNotations</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
            freq <span style="color: #339933;">=</span> <span style="color: #CC0000;">440</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">pow</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> indx<span style="color: #339933;">/</span><span style="color: #CC0000;">12</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">return</span> freq<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Basically what I did here is extended the Oscillator class.  I looped through all the properties of the Oscillator.prototype object and copying it onto the new class.</p>
<p>I then, went in to the Oscillator generator and copied the small initialization routine.</p>
<p>Then I injected my own methods!  I replaced the frequency setting with a method to lookup the index of the musical notation in my preset arrays of notations. I also parsed out the octave (if it existed).</p>
<p>In the end, I have a &#8220;Note&#8221; class which is exactly the same as the Oscillator class &#8211; however, instead of passing in the frequency (ie 440hz), you pass in &#8220;A&#8221;, or &#8220;A4&#8243; for the 4th octave.</p>
<p>To change the notation of the oscillator/note, simply call the setNotation again!</p>
<p>I&#8217;m definitely looking forward to exploring Audiolib.js more.  It has a few concepts that I hadn&#8217;t gotten around to implementing in my own Flash framework yet, so I&#8217;m excited to see how it&#8217;s done.<br />
&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.benfarrell.com/2011/12/05/making-an-audiolib-js-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

