Skip to content

Commit

Permalink
Merge pull request #1289 from amaclean199/new_sound_lib
Browse files Browse the repository at this point in the history
Chord block and custom sound effect block
  • Loading branch information
alexjsmac committed Dec 7, 2015
2 parents 427025f + 33687a7 commit 61b42ba
Show file tree
Hide file tree
Showing 6 changed files with 308 additions and 26 deletions.
139 changes: 139 additions & 0 deletions example/twinkling_song.wb

Large diffs are not rendered by default.

Binary file added images/png/star.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ function handleExampleButton(evt){
handleExample('Simple Bounce', 'bounce'),
handleExample('Simple Move', 'simple_move'),
handleExample('Simple Pong', 'simple_pong'),
handleExample("Twinkling Song", 'twinkling_song'),
handleExample('Draw from Array','array_draw'),
handleExample('Follow pointer', 'follow_pointer')
]
Expand Down
84 changes: 69 additions & 15 deletions js/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,7 @@
var audio = T("audio", {load:file});
return audio;
},
playNote: function(note, octave, beats){
addNote: function(note, octave, beats){
switch(note){
case "A":
note = "a";
Expand Down Expand Up @@ -1083,35 +1083,91 @@
var newNote = note + length;
song += newNote;
},
playChord: function(){
var args = [].slice.call(arguments)
var oscenv = args[0];
var freqs = []
for (var i = 1; i < args.length; i++) {
var freq;
switch(args[i]){
case "A":
freq = 55.000;
break;
case "A#/Bb":
freq = 58.270;
break;
case "B":
freq = 61.735;
break;
case "C":
freq = 65.406;
break;
case "C#/Db":
freq = 69.296;
break;
case "D":
freq = 73.416;
break;
case "D#/Eb":
freq = 77.782;
break;
case "E":
freq = 82.407;
break;
case "F":
freq = 87.307;
break;
case "F#/Gb":
freq = 92.499;
break;
case "G":
freq = 97.999;
break;
case "G#/Ab":
freq = 103.826;
break;
}
var octave = args[++i];
parseInt(octave);
freq = freq * Math.pow(2, octave-1);
freqs.push(freq);
}
T("interval", {interval:"L4", timeout:"L4"}, function() {
for (var i = 0; i < freqs.length; i++) {
oscenv.noteOnWithFreq(freqs[i], 64);
}
}).on("ended", function() {
this.stop();
}).set({buddies:oscenv}).start();
},
playAudio: function(audio){
audio.play();
if (audio){
audio.play();
}
},
playInstrument: function(sound){
console.log(song);
playNotes: function(sound){
T("mml", {mml:song}, sound).on("ended", function() {
sound.pause();
this.stop();
}).start();
song = "o4 l4 V12 ";
},
mml: function(sound, mml){
playMML: function(sound, mml){
var gen = T("OscGen", {wave:sound, env:{type:"perc"}, mul:0.25}).play();
T("mml", {mml:mml}, gen).on("ended", function() {
gen.pause();
this.stop();
}).start();
},
tempo: function(tempo){
tempoChange: function(tempo){
song += ("t" + tempo + " ");
},
pause: function(sound){
if (assets.sounds[sound.name]){
assets.sounds[sound.name].pause();
pauseAudio: function(audio){
if (audio){
audio.pause();
}
},
keys: function(wave, vol){
var synth = T("OscGen", {wave:wave, mul:vol}).play();

keys: function(synth){
var keydict = T("ndict.key");
var midicps = T("midicps");
T("keyboard").on("keydown", function(e) {
Expand All @@ -1126,10 +1182,8 @@
synth.noteOff(midi, 100);
}
}).start();

alert("Play notes on the keyboard");
},
effect: function(effect){
soundEffect: function(effect){
switch(effect) {
case "laser":
var table = [1760, [110, "200ms"]];
Expand Down
32 changes: 21 additions & 11 deletions playground.html
Original file line number Diff line number Diff line change
Expand Up @@ -228,40 +228,50 @@ <h1>Waterbear Playground</h1>
</header>
<p>Play and manipulate existing sound files or synthesize new sounds.</p>
<wb-expression type="sound" ns="sound" fn="get">
<wb-value type="list" allow="literal" options="sin, saw, tri, pulse, fami" value="sin">instrument</wb-value>
<wb-value>synth</wb-value>
<wb-value type="list" allow="literal" options="sin, saw, tri, pulse, fami" value="sin">wave</wb-value>
<wb-value type="number" value="10">attack</wb-value>
<wb-value type="number" value="300">release</wb-value>
</wb-expression>
<wb-expression type="audio" ns="sound" fn="getAudio">
<wb-value type="text" value="/sounds/pianoA.wav">audio</wb-value>
</wb-expression>
<wb-step ns="sound" fn="playNote">
<wb-step ns="sound" fn="addNote">
<wb-value type="list" allow="literal" options="C,C#/Db,D,D#/Eb,E,F,F#/Gb,G,G#/Ab,A,A#/Bb,B,Rest" value="C">note</wb-value>
<wb-value type="list" allow="literal" options="1,2,3,4,5,6,7,8" value="4">octave</wb-value>
<wb-value type="list" allow="literal" options="1/32,1/16,1/8,1/4,1/2,1" value="1/4">beats</wb-value>
</wb-step>
<wb-step ns="sound" fn="playChord">
<wb-value>play chord</wb-value>
<wb-value type="sound">synth</wb-value>
<wb-row>
<wb-value type="list" allow="literal" options="C,C#/Db,D,D#/Eb,E,F,F#/Gb,G,G#/Ab,A,A#/Bb,B,Rest" value="C">note</wb-value>
<wb-value type="list" allow="literal" options="1,2,3,4,5,6,7,8" value="4">octave</wb-value>
<button class="remove-item">-</button>
<button class="add-item">+</button>
</wb-row>
</wb-step>
<wb-step ns="sound" fn="playAudio">
<wb-value type="audio">play audio</wb-value>
</wb-step>
<wb-step ns="sound" fn="playInstrument">
<wb-value type="sound">play instrument</wb-value>
<wb-step ns="sound" fn="playNotes">
<wb-value type="sound">play notes</wb-value>
</wb-step>
<wb-step ns="sound" fn="mml">
<wb-value type="sound">instrument</wb-value>
<wb-step ns="sound" fn="playMML">
<wb-value type="sound">synth</wb-value>
<wb-value type="text">mml</wb-value>
</wb-step>
<wb-step ns="sound" fn="tempo">
<wb-step ns="sound" fn="tempoChange">
<wb-value type="number" value="120">set tempo</wb-value>
</wb-step>
<wb-step ns="sound" fn="pause">
<wb-step ns="sound" fn="pauseAudio">
<wb-value type="audio">pause</wb-value>
</wb-step>
<wb-step ns="sound" fn="keys">
<wb-value>keys</wb-value>
<wb-value type="list" allow="literal" options="sin, saw, tri, pulse, fami" value="sin">wave</wb-value>
<wb-value type="number" value="0.25">vol</wb-value>
<wb-value type="sound">synth</wb-value>
</wb-step>
<wb-step ns="sound" fn="effect">
<wb-step ns="sound" fn="soundEffect">
<wb-value type="list" allow="literal" options="laser, alarm" value="laser">sound effect</wb-value>
</wb-step>
</wb-accordion>
Expand Down
78 changes: 78 additions & 0 deletions test/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,84 @@ QUnit.test('check for collision, no colliding circle and polygon', function(asse
});

/* TODO: sound */
QUnit.module('sound');
QUnit.test('get', function(assert){
var sound = runtime.sound;
var synth = sound.get('tri', '10', '300');
assert.ok(synth);
});
QUnit.test('addNote', function(assert){
var sound = runtime.sound;
var notes = ['C','C#/Db','D','D#/Eb','E','F','F#/Gb','G','G#/Ab','A','A#/Bb','B','Rest'];
notes.forEach(function (note, index) {
sound.addNote(note, '4', '1/4');
});
var octaves = ['1','2','3','4','5','6','7','8'];
octaves.forEach(function (octave, index) {
sound.addNote('C', octave, '1/16');
})
var beats = ['1/32','1/16','1/8','1/4','1/2','1'];
beats.forEach(function (length, index) {
sound.addNote('C', '3', length);
})
assert.ok(sound);
});
QUnit.test('playChord', function(assert){
var sound = runtime.sound;
var synth = sound.get('tri', '10', '300');
sound.playChord(synth, 'A', '4', 'A#/Bb', '4', 'B', '4', 'C', '4', 'C#/Db', '4', 'D', '4', 'D#/Eb', '4', 'E', '4', 'F', '4', 'F#/Gb', '4', 'G', '4', 'G#/Ab', '4');
assert.ok(sound);
});
QUnit.test('playNotes', function(assert){
var sound = runtime.sound;
var synth = sound.get('tri', '10', '300');
sound.playNotes(synth);
assert.ok(sound);
});
QUnit.test('playMML', function(assert){
var sound = runtime.sound;
var synth = sound.get('tri', '10', '300');
var mml = "abcdefg";
sound.playMML(synth, mml);
assert.ok(sound);
});
QUnit.test('tempoChange', function(assert){
var sound = runtime.sound;
sound.tempoChange('200');
assert.ok(sound);
});
QUnit.test('keys', function(assert){
var sound = runtime.sound;
var synth = sound.get('tri', '10', '300');
sound.keys(synth);
assert.ok(sound);
});
QUnit.test('soundEffect', function(assert){
var sound = runtime.sound;
sound.soundEffect('laser');
sound.soundEffect('alarm');
assert.ok(sound);
});
QUnit.test('getAudio', function(assert){
var sound = runtime.sound;
var audio = sound.getAudio('/sounds/pianoA.wav');
assert.ok(audio);
});
QUnit.test('playAudio', function(assert){
var sound = runtime.sound;
var audio = sound.getAudio('/sounds/pianoA.wav');
sound.playAudio(audio);
sound.playAudio();
assert.ok(sound);
});
QUnit.test('pauseAudio', function(assert){
var sound = runtime.sound;
var audio = sound.getAudio('/sounds/pianoA.wav');
sound.playAudio(audio);
sound.pauseAudio(audio);
sound.pauseAudio();
assert.ok(sound);
});
/* TODO: arrays */
QUnit.module('arrays');
QUnit.test('create', function(assert){
Expand Down

0 comments on commit 61b42ba

Please sign in to comment.