Permalink
Browse files

much easier on the ears

  • Loading branch information...
1 parent 250d8b9 commit 1cb5cd15591a6e8b831185a4dfb5d6ee6b891d6d @brandly committed Mar 8, 2014
Showing with 71 additions and 22 deletions.
  1. +17 −5 index.html
  2. +54 −17 scripts.js
View
22 index.html
@@ -6,10 +6,10 @@
<meta name="description" content="Play musical scales using the Web Audio API"/>
<meta name="author" content="Matthew Brandly">
- <link rel="shortcut icon" href="./src/img/favicon.png">
+ <link rel="shortcut icon" href="src/img/favicon.png">
<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Sansita+One">
- <link rel="stylesheet" type="text/css" href="./src/styles/css/main.css">
+ <link rel="stylesheet" type="text/css" href="src/styles/css/main.css">
<!-- Content type -->
<meta http-equiv="content-type" content="text/html;charset=utf-8">
@@ -21,9 +21,21 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- When you share on Facebook, choose this image
- <link rel="image_src" href="./src/img/share_logo.jpg">
+ <link rel="image_src" href="img/share_logo.jpg">
-->
+ <!-- analytics -->
+ <script>
+ (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
+ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
+ m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
+ })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
+
+ ga('create', 'UA-48129598-2', 'brandly.github.io');
+ ga('send', 'pageview');
+
+ </script>
+
</head>
<body>
@@ -95,11 +107,11 @@
</form>
<a href="https://github.com/brandly/scales">
- <img class="octocat" src="./src/img/GitHub-Mark-32px.png">
+ <img class="octocat" src="src/img/GitHub-Mark-32px.png">
</a>
</div>
- <script src="./scripts.js"></script>
+ <script src="scripts.js"></script>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
</body>
</html>
View
71 scripts.js
@@ -1,4 +1,4 @@
-// scales 2013-04-29
+// scales 2014-03-08
// Underscore.js 1.4.4
// ===================
@@ -1542,7 +1542,7 @@
})(window);
(function() {
- var Scales, activeNote, activeTimeout, context, getSelected, intitialOctave, keyboard, keyboardSettings, message, nodes, playButton, playScale, qwerty, scales,
+ var Scales, activeNote, activeTimeout, attack, context, filterTypes, getSelected, intitialOctave, keyboard, keyboardSettings, message, nodes, oscillatorTypes, playButton, playNote, playScale, qwerty, scales, sustain, volume,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
Scales = (function() {
@@ -1636,7 +1636,9 @@
})();
- if (typeof webkitAudioContext === 'undefined') {
+ window.audioContext = window.audioContext || window.webkitAudioContext;
+
+ if (!window.audioContext) {
message = '<h3>Sorry, your browser doesn\'t support the Web Audio API. ' + 'Try <a href="http://google.com/chrome">Chrome</a> instead!</h3>';
qwerty = document.getElementById('qwerty-hancock');
qwerty.insertAdjacentHTML('beforebegin', message);
@@ -1655,33 +1657,68 @@
keyboard = qwertyHancock(keyboardSettings);
- context = new webkitAudioContext();
+ context = new window.audioContext();
nodes = {};
- keyboard.keyDown(function(note, frequency) {
- var gainNode, oscillator;
+ oscillatorTypes = {
+ sine: 0,
+ square: 1,
+ sawtooth: 2,
+ triangle: 3
+ };
+
+ filterTypes = {
+ lowpass: 0,
+ highpass: 1,
+ bandpass: 2,
+ lowshelf: 3,
+ highshelf: 4,
+ peaking: 5,
+ notch: 6,
+ allpass: 7
+ };
+
+ volume = 0.4;
+
+ attack = 0.1;
+
+ sustain = 0.8;
+
+ playNote = function(context, frequency) {
+ var filter, gainNode, now, oscillator;
oscillator = context.createOscillator();
gainNode = context.createGainNode();
- oscillator.type = 1;
+ oscillator.type = oscillatorTypes.triangle;
oscillator.frequency.value = frequency;
- gainNode.gain.value = 0.3;
- oscillator.connect(gainNode);
- if (typeof oscillator.noteOn !== 'undefined') {
+ if (typeof oscillator.noteOn === "function") {
oscillator.noteOn(0);
}
+ now = context.currentTime;
+ gainNode.gain.cancelScheduledValues(now);
+ gainNode.gain.value = 0;
+ gainNode.gain.setTargetAtTime(0, now, .001);
+ gainNode.gain.linearRampToValueAtTime(volume, now + attack);
+ gainNode.gain.linearRampToValueAtTime(0, now + sustain);
+ filter = context.createBiquadFilter();
+ filter.type = filterTypes.lowpass;
+ filter.frequency.value = 900;
+ oscillator.connect(filter);
+ filter.connect(gainNode);
gainNode.connect(context.destination);
- return nodes[note] = oscillator;
- });
+ return oscillator;
+ };
- keyboard.keyUp(function(note, frequency) {
- if (typeof nodes[note].noteOff !== 'undefined') {
- nodes[note].noteOff0;
- }
- return nodes[note].disconnect();
+ keyboard.keyDown(function(note, frequency) {
+ var node;
+
+ node = playNote(context, frequency);
+ return nodes[note] = node;
});
+ keyboard.keyUp(function() {});
+
scales = new Scales(keyboardSettings.startNote, keyboardSettings.octaves);
activeNote = null;

0 comments on commit 1cb5cd1

Please sign in to comment.