-
Notifications
You must be signed in to change notification settings - Fork 748
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
for Guzdial-style sound samples fun
- Loading branch information
Showing
3 changed files
with
20 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<blocks app="Snap! 4.1, http://snap.berkeley.edu" version="1"><block-definition s="%'attribute' of %'sound'" type="reporter" category="sound"><header></header><code></code><inputs><input type="%s" readonly="true">duration<options>name | ||
duration | ||
length | ||
number of channels | ||
sample rate | ||
samples</options></input><input type="%obj"></input></inputs><script><block s="doIf"><block s="reportEquals"><block var="attribute"/><l>name</l></block><script><block s="doReport"><block s="evaluate"><block s="reportJSFunction"><list><l>sound</l></list><l>return sound.name;</l></block><list><block var="sound"/></list></block></block></script></block><block s="doIf"><block s="reportEquals"><block var="attribute"/><l>samples</l></block><script><block s="doReport"><block s="evaluate"><block s="reportJSFunction"><list><l>audioBuffer</l></list><l>var result, i;
if (audioBuffer.numberOfChannels > 1) {
 result = new List();
 for (i = 0; i < audioBuffer.numberOfChannels; i += 1) {
 result.add(new List(audioBuffer.getChannelData(i)));
 }
 return result;
}
return new List(audioBuffer.getChannelData(0));</l></block><list><custom-block s="%s of %obj"><l></l><block var="sound"/></custom-block></list></block></block></script></block><block s="doRun"><block s="reportJSFunction"><list><l>sound</l></list><l>var base64, binaryString, len, bytes, i,
 arrayBuffer, audioCtx;
if (sound.audioBuffer) {
 return;
}
base64 = sound.audio.src.split(',')[1];
binaryString = window.atob(base64);
len = binaryString.length;
bytes = new Uint8Array(len);
for (i = 0; i < len; i += 1) {
 bytes[i] = binaryString.charCodeAt(i);
}
arrayBuffer = bytes.buffer;
Note.prototype.setupContext();
audioCtx = Note.prototype.audioContext;
audioCtx.decodeAudioData(arrayBuffer, function(buffer) {
 sound.audioBuffer = buffer;
});</l></block><list><block var="sound"/></list></block><block s="doWaitUntil"><block s="evaluate"><block s="reportJSFunction"><list><l>sound</l></list><l>return !isNil(sound.audioBuffer);</l></block><list><block var="sound"/></list></block></block><block s="doReport"><block s="evaluate"><block s="reportJSFunction"><list><l>sound</l><l>choice</l></list><l>switch (choice) {
case 'sample rate':
 return sound.audioBuffer.sampleRate;
case 'duration':
 return sound.audioBuffer.duration;
case 'length':
 return sound.audioBuffer.length;
case 'number of channels':
 return sound.audioBuffer.numberOfChannels;
default:
 return sound.audioBuffer;
}</l></block><list><block var="sound"/><block var="attribute"/></list></block></block></script></block-definition><block-definition s="turbo %'action'" type="command" category="other"><header></header><code></code><inputs><input type="%cs"></input></inputs><script><block s="doDeclareVariables"><list><l>begin</l></list></block><block s="doSetVar"><l>begin</l><block s="reportIsFastTracking"></block></block><block s="doSetFastTracking"><l><bool>true</bool></l></block><block s="doRun"><block var="action"/><list></list></block><block s="doSetFastTracking"><block var="begin"/></block></script></block-definition><block-definition s="play %'samples' at %'sampleRate' Hz" type="command" category="sound"><comment x="0" y="0" w="164" collapsed="false">play back a sound, a list of samples (single channel), or a list of lists (multiple channels) at the given sample rate.
for demo purposes this block also fast-plots the samples on the stage</comment><header></header><code></code><inputs><input type="%l"></input><input type="%n">44100<options>22.05 kHz=22050 | ||
44.1 kHz=44100 | ||
88.2 kHz=88200 | ||
96 kHz=96000</options></input></inputs><script><block s="doIf"><block s="reportIsA"><block var="samples"/><l><option>sound</option></l></block><script><block s="doSetVar"><l>samples</l><custom-block s="%s of %obj"><l>samples</l><block var="samples"/></custom-block></block></script></block><block s="doRun"><block s="reportJSFunction"><list><l>samples</l><l>rate</l></list><l>var audioCtx, channels, frameCount, arrayBuffer, i, source;
Note.prototype.setupContext();
audioCtx = Note.prototype.audioContext;
channels = (samples.at(1) instanceof List) ? samples.length() : 1; 
frameCount = (channels === 1) ? samples.length() : samples.at(1).length(); 
arrayBuffer = audioCtx.createBuffer(channels, frameCount, rate);
if (!arrayBuffer.copyToChannel) {
 arrayBuffer.copyToChannel = function (src, channel) {
 var buffer = this.getChannelData(channel);
 for (i = 0; i < src.length; i += 1) {
 buffer[i] = src[i];
 }
 }
}
if (channels === 1) {
 arrayBuffer.copyToChannel(Float32Array.from(samples.asArray()), 0, 0);
} else {
 for (i = 0; i < channels; i += 1) {
 arrayBuffer.copyToChannel(Float32Array.from(samples.at(i + 1).asArray()), i, 0);
 }
}
source = audioCtx.createBufferSource();
source.buffer = arrayBuffer;
source.connect(audioCtx.destination);
source.start();
source.pause = source.stop;
this.parentThatIsA(StageMorph).activeSounds.push(source);</l></block><list><block var="samples"/><block var="sampleRate"/></list></block><custom-block s="plot %obj"><block var="samples"/></custom-block></script></block-definition><block-definition s="%'frequency' Hz for %'duration' secs at %'sample rate' Hz sample rate" type="reporter" category="sound"><comment x="0" y="0" w="205" collapsed="false">generate a list of samples representing a sine wave at the given frequency, duration and sample rate</comment><header></header><code></code><inputs><input type="%n">440<options>a 55=55 | ||
a 110=110 | ||
a 220=220 | ||
a 440=440 | ||
a 880=880 | ||
a 1760=1760 | ||
a 3520=3520</options></input><input type="%n">1</input><input type="%n">44100<options>22.05 kHz=22050 | ||
44.1 kHz=44100 | ||
88.2 kHz=88200 | ||
96 kHz=96000</options></input></inputs><script><block s="doDeclareVariables"><list><l>output</l><l>sample period</l><l>duration in samples</l><l>time</l></list></block><block s="doSetVar"><l>sample period</l><block s="reportQuotient"><l>1</l><block var="sample rate"/></block></block><block s="doSetVar"><l>duration in samples</l><block s="reportMonadic"><l><option>ceiling</option></l><block s="reportProduct"><block var="duration"/><block var="sample rate"/></block></block></block><block s="doSetVar"><l>time</l><l>0</l></block><block s="doSetVar"><l>output</l><block s="reportNewList"><list></list></block></block><block s="doWarp"><script><block s="doRepeat"><block var="duration in samples"/><script><block s="doAddToList"><block s="reportMonadic"><l><option>sin</option></l><custom-block s="degrees %n"><block s="reportProduct"><l>2</l><block s="reportProduct"><custom-block s="PI"></custom-block><block s="reportProduct"><block var="frequency"/><block var="time"/></block></block></block></custom-block></block><block var="output"/></block><block s="doChangeVar"><l>time</l><block var="sample period"/></block></script></block></script></block><block s="doReport"><block var="output"/></block></script></block-definition><block-definition s="quick reverse %'list'" type="reporter" category="lists"><header></header><code></code><inputs><input type="%l"></input></inputs><script><block s="doReport"><block s="evaluate"><block s="reportJSFunction"><list><l>list</l></list><l>return new List(list.asArray().slice().reverse());</l></block><list><block var="list"/></list></block></block></script></block-definition><block-definition s="empty? %'data'" type="predicate" category="lists"><header></header><code></code><inputs><input type="%l"></input></inputs><script><block s="doReport"><block s="reportEquals"><block var="data"/><block s="reportNewList"><list></list></block></block></block></script></block-definition><block-definition s="for each %'item' of %'data' %'action'" type="command" category="lists"><header></header><code></code><inputs><input type="%upvar"></input><input type="%l"></input><input type="%cs"></input></inputs><script><block s="doUntil"><custom-block s="empty? %l"><block var="data"/></custom-block><script><block s="doSetVar"><l>item</l><block s="reportListItem"><l>1</l><block var="data"/></block></block><block s="doRun"><block var="action"/><list><block s="reportListItem"><l>1</l><block var="data"/></block></list></block><block s="doSetVar"><l>data</l><block s="reportCDR"><block var="data"/></block></block></script></block></script></block-definition><block-definition s="PI" type="reporter" category="operators"><header></header><code></code><inputs></inputs><script><block s="doReport"><l>3.141592653589793</l></block></script></block-definition><block-definition s="degrees %'radians'" type="reporter" category="operators"><header></header><code></code><inputs><input type="%n">0.5</input></inputs><script><block s="doReport"><block s="reportQuotient"><block s="reportProduct"><block var="radians"/><l>180</l></block><custom-block s="PI"></custom-block></block></block></script></block-definition><block-definition s="find first in %'list' such that %'test'" type="reporter" category="lists"><header></header><code></code><inputs><input type="%l"></input><input type="%predRing"></input></inputs><script><block s="doIf"><block s="reportEquals"><block var="list"/><block s="reportNewList"><list></list></block></block><script><custom-block s="error %s"><l>item not found in list</l></custom-block></script></block><block s="doIf"><block s="evaluate"><block var="test"/><list><block s="reportListItem"><l>1</l><block var="list"/></block></list></block><script><block s="doReport"><block s="reportListItem"><l>1</l><block var="list"/></block></block></script></block><block s="doReport"><custom-block s="find first in %l such that %predRing"><block s="reportCDR"><block var="list"/></block><block var="test"/></custom-block></block></script></block-definition><block-definition s="error %'msg'" type="command" category="control"><header></header><code></code><inputs><input type="%s"></input></inputs><script><block s="doRun"><block s="reportJSFunction"><list><l>msg</l></list><l>throw new Error(msg)</l></block><list><block var="msg"/></list></block></script></block-definition><block-definition s="quick map %'fun' over %'list'" type="reporter" category="lists"><header></header><code></code><inputs><input type="%repRing"></input><input type="%l"></input></inputs><script><block s="doReport"><block s="evaluate"><block s="reportJSFunction"><list><l>fun</l><l>list</l></list><l>var result = [],
 src = list.asArray(),
 len = src.length,
 i;
for (i = 0; i < len; i += 1) {
 result.push(invoke(fun, new List([src[i]])));
}
return new List(result);</l></block><list><block var="fun"/><block var="list"/></list></block></block></script></block-definition><block-definition s="sound named %'name'" type="reporter" category="sensing"><header></header><code></code><inputs><input type="%s"></input></inputs><script><block s="doReport"><custom-block s="find first in %l such that %predRing"><block s="reportGet"><l><option>sounds</option></l></block><block s="reifyPredicate"><autolambda><block s="reportEquals"><custom-block s="%s of %obj"><l>name</l><block var="sound"/></custom-block><block var="name"/></block></autolambda><list><l>sound</l></list></block></custom-block></block></script></block-definition><block-definition s="plot %'sound'" type="command" category="pen"><comment x="0" y="0" w="164" collapsed="false">quickly plot the samples of a sound, a list of samples (single channel), or a list of lists (multiple channels) to the stage at a lower resolution.</comment><header></header><code></code><inputs><input type="%obj"></input></inputs><script><block s="doDeclareVariables"><list><l>samples</l><l>step</l><l>offset</l><l>i</l><l>stage height</l></list></block><block s="doSetVar"><l>stage height</l><custom-block s="stage height"></custom-block></block><block s="doIfElse"><block s="reportIsA"><block var="sound"/><l><option>sound</option></l></block><script><block s="doSetVar"><l>samples</l><custom-block s="%s of %obj"><l>samples</l><block var="sound"/></custom-block></block><block s="doSetVar"><l>step</l><block s="reportQuotient"><custom-block s="%s of %obj"><l>length</l><block var="sound"/></custom-block><block s="reportProduct"><custom-block s="stage width"></custom-block><l>2</l></block></block></block></script><script><block s="doSetVar"><l>samples</l><block var="sound"/></block></script></block><block s="clear"></block><block s="up"></block><block s="doIfElse"><block s="reportIsA"><block s="reportListItem"><l>1</l><block var="samples"/></block><l><option>list</option></l></block><script><block s="doSetVar"><l>step</l><block s="reportQuotient"><block s="reportListLength"><block s="reportListItem"><l>1</l><block var="samples"/></block></block><block s="reportProduct"><custom-block s="stage width"></custom-block><l>2</l></block></block></block><block s="doSetVar"><l>offset</l><block s="reportQuotient"><block var="stage height"/><l>4</l></block></block><custom-block s="for each %upvar of %l %cs"><l>channel</l><block var="samples"/><script><block s="doSetVar"><l>i</l><l>0</l></block><block s="gotoXY"><block s="reportQuotient"><custom-block s="stage width"></custom-block><l>-2</l></block><block var="offset"/></block><block s="down"></block><block s="setSize"><l>1</l></block><custom-block s="turbo %cs"><script><block s="doRepeat"><block s="reportProduct"><custom-block s="stage width"></custom-block><l>2</l></block><script><block s="doChangeVar"><l>i</l><block var="step"/></block><block s="gotoXY"><block s="reportSum"><block s="xPosition"></block><l>0.5</l></block><block s="reportSum"><block s="reportProduct"><block s="reportListItem"><block s="reportMonadic"><l><option>floor</option></l><block var="i"/></block><block var="channel"/></block><block s="reportQuotient"><block var="stage height"/><l>6.4</l></block></block><block var="offset"/></block></block></script></block></script></custom-block><block s="up"></block><block s="doChangeVar"><l>offset</l><block s="reportQuotient"><custom-block s="stage height"></custom-block><l>-2</l></block></block></script></custom-block></script><script><block s="doSetVar"><l>step</l><block s="reportQuotient"><block s="reportListLength"><block var="samples"/></block><block s="reportProduct"><custom-block s="stage width"></custom-block><l>2</l></block></block></block><block s="gotoXY"><block s="reportQuotient"><custom-block s="stage width"></custom-block><l>-2</l></block><l>0</l></block><block s="down"></block><block s="setSize"><l>1</l></block><custom-block s="turbo %cs"><script><block s="doRepeat"><block s="reportProduct"><custom-block s="stage width"></custom-block><l>2</l></block><script><block s="doChangeVar"><l>i</l><block var="step"/></block><block s="gotoXY"><block s="reportSum"><block s="xPosition"></block><l>0.5</l></block><block s="reportProduct"><block s="reportListItem"><block s="reportMonadic"><l><option>floor</option></l><block var="i"/></block><block var="samples"/></block><block s="reportQuotient"><block var="stage height"/><l>3.2</l></block></block></block></script></block></script></custom-block><block s="up"></block></script></block></script></block-definition><block-definition s="stage width" type="reporter" category="sensing"><header></header><code></code><inputs></inputs><script><block s="doReport"><block s="evaluate"><block s="reportJSFunction"><list></list><l>return StageMorph.prototype.dimensions.x;</l></block><list></list></block></block></script></block-definition><block-definition s="stage height" type="reporter" category="sensing"><header></header><code></code><inputs></inputs><script><block s="doReport"><block s="evaluate"><block s="reportJSFunction"><list></list><l>return StageMorph.prototype.dimensions.y;</l></block><list></list></block></block></script></block-definition></blocks> |