Skip to content

Commit

Permalink
Starting new audio lib
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjsmac committed Oct 31, 2015
1 parent 328419d commit 48d8809
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 857 deletions.
67 changes: 25 additions & 42 deletions js/runtime.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function(global){
'use strict';

// Dependencies: ctx, canvas, Event, runtime, sound, soundEffect,
// Dependencies: ctx, canvas, Event, runtime
// canvas/stage stuff
var _canvas, _ctx;
function canvas(){
Expand Down Expand Up @@ -821,49 +821,32 @@
sound: {

get: function(url){
return assets.sounds[url]; // already cached by sounds library
return console.log(url);
},
play: function(sound){
sound.play();
},
setLoop: function(sound, flag){
sound.loop = flag;
},
setVolume: function(sound, volume){
sound.volume = volume;
},
pause: function(sound){
sound.pause();
},
playFrom: function(sound, time){
sound.playFrom(time);
},
pan: function(sound, balance){
sound.pan = balance;
},
echo_DelayFeedbackFilter: function(sound, delay, feedback, filter){
sound.setEcho(delay, feedback, filter);
},
stopEcho: function(sound){
sound.echo = false;
},
reverb_DurationDecayReverse: function(sound, duration, decay, reverse){
sound.setReverb(duration, decay, reverse);
},
stopReverb: function(sound){
sound.reverb = false;

play: function(text){
T(text).bang().play();
},
effect: function(frequency, attack, decay, wait, echoDelay, echoFeedback, echoFilter, waveform, volume, balance, pitchBend, reverseBend, random, dissonance){
return {
play: function(){
soundEffect(
frequency, attack, decay, waveform,
volume, balance, wait,
pitchBend, reverseBend, random, dissonance,
[echoDelay, echoFeedback, echoFilter]
);
}
};

keys: function(wave, vol){
var synth = T("OscGen", {wave:wave, mul:vol}).play();

var keydict = T("ndict.key");
var midicps = T("midicps");
T("keyboard").on("keydown", function(e) {
var midi = keydict.at(e.keyCode);
if (midi) {
var freq = midicps.at(midi);
synth.noteOnWithFreq(freq, 100);
}
}).on("keyup", function(e) {
var midi = keydict.at(e.keyCode);
if (midi) {
synth.noteOff(midi, 100);
}
}).start();

alert("Play notes on the keyboard");
}
},

Expand Down
134 changes: 134 additions & 0 deletions lib/keyboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
(function(T) {
"use strict";

if (T.envtype !== "browser") {
return;
}

var fn = T.fn;
var instance = null;

function KeyboardListener(_args) {
if (instance) {
return instance;
}
instance = this;

T.Object.call(this, 1, _args);

fn.fixKR(this);
}
fn.extend(KeyboardListener);

var keyDown = {};
var shiftKey = false;
var ctrlKey = false;
var altKey = false;

var onkeydown = function(e) {
var _ = instance._;
var cell = instance.cells[0];
var value = e.keyCode * _.mul + _.add;

for (var i = 0, imax = cell.length; i < imax; ++i) {
cell[i] = value;
}
shiftKey = e.shiftKey;
ctrlKey = e.ctrlKey;
altKey = e.altKey;

if (!keyDown[e.keyCode]) {
keyDown[e.keyCode] = true;
instance._.emit("keydown", e);
}
};

var onkeyup = function(e) {
delete keyDown[e.keyCode];
instance._.emit("keyup", e);
};

var $ = KeyboardListener.prototype;

Object.defineProperties($, {
shiftKey: {
get: function() {
return shiftKey;
}
},
ctrlKey: {
get: function() {
return ctrlKey;
}
},
altKey: {
get: function() {
return altKey;
}
}
});

$.start = function() {
window.addEventListener("keydown", onkeydown, true);
window.addEventListener("keyup" , onkeyup , true);
return this;
};

$.stop = function() {
window.removeEventListener("keydown", onkeydown, true);
window.removeEventListener("keyup" , onkeyup , true);
return this;
};

$.play = $.pause = function() {
return this;
};

fn.register("keyboard", KeyboardListener);


var NDictKey = {
90 : 48, // Z -> C3
83 : 49, // S -> C+3
88 : 50, // X -> D3
68 : 51, // D -> D+3
67 : 52, // C -> E3
86 : 53, // V -> F3
71 : 54, // G -> F+3
66 : 55, // B -> G3
72 : 56, // H -> G+3
78 : 57, // N -> A3
74 : 58, // J -> A+3
77 : 59, // M -> B3
188: 60, // , -> C4
76 : 61, // L -> C+4
190: 62, // . -> D4
186: 63, // ; -> D+4

81 : 60, // Q -> C4
50 : 61, // 2 -> C+4
87 : 62, // W -> D4
51 : 63, // 3 -> D+4
69 : 64, // E -> E4
82 : 65, // R -> F4
53 : 66, // 5 -> F+4
84 : 67, // T -> G4
54 : 68, // 6 -> G+4
89 : 69, // Y -> A4
55 : 70, // 7 -> A+4
85 : 71, // U -> B4
73 : 72, // I -> C5
57 : 73, // 9 -> C#5
79 : 74, // O -> D5
48 : 75, // 0 -> D+5
80 : 76 // P -> E5
};

var NDictNode = fn.getClass("ndict");
fn.register("ndict.key", function(_args) {
var instance = new NDictNode(_args);
instance.dict = NDictKey;
return instance;
});

})(timbre);
113 changes: 113 additions & 0 deletions lib/ndict.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
(function(T) {
"use strict";

var fn = T.fn;

function NDictNode(_args) {
T.Object.call(this, 1, _args);

var _ = this._;
_.defaultValue = 0;
_.index = 0;
_.dict = {};
_.ar = false;
}
fn.extend(NDictNode);

var $ = NDictNode.prototype;

Object.defineProperties($, {
dict: {
set: function(value) {
if (typeof value === "object") {
this._.dict = value;
} else if (typeof value === "function") {
var dict = {};
for (var i = 0; i < 128; ++i) {
dict[i] = value(i);
}
this._.dict = dict;
}
},
get: function() {
return this._.dict;
}
},
defaultValue: {
set: function(value) {
if (typeof value === "number") {
this._.defaultValue = value;
}
},
get: function() {
return this._.defaultValue;
}
},
index: {
set: function(value) {
if (typeof value === "number") {
this._.index = value;
}
},
get: function() {
return this._.index;
}
}
});

$.at = function(index) {
var _ = this._;
return (_.dict[index|0] || _.defaultValue) * _.mul + _.add;
};

$.clear = function() {
this._.dict = {};
return this;
};

$.process = function(tickID) {
var cell = this.cells[0];
var _ = this._;

if (this.tickID !== tickID) {
this.tickID = tickID;

var len = this.nodes.length;
var index, value;
var dict = _.dict, defaultValue = _.defaultValue;
var mul = _.mul, add = _.add;
var i, imax = cell.length;

if (_.ar && len) {

fn.inputSignalAR(this);
for (i = 0; i < imax; ++i) {
index = cell[i];
if (index < 0) {
index = (index - 0.5)|0;
} else {
index = (index + 0.5)|0;
}
cell[i] = (dict[index] || defaultValue) * mul + add;
}
fn.outputSignalAR(this);
} else {
index = (this.nodes.length) ? fn.inputSignalKR(this) : _.index;
if (index < 0) {
index = (index - 0.5)|0;
} else {
index = (index + 0.5)|0;
}
value = (dict[index] || defaultValue) * mul + add;
for (i = 0; i < imax; ++i) {
cell[i] = value;
}
}
}

return this;
};

fn.register("ndict", NDictNode);

})(timbre);
Loading

0 comments on commit 48d8809

Please sign in to comment.