Skip to content

Commit

Permalink
new Audio Comp library
Browse files Browse the repository at this point in the history
for Guzdial-style sound samples fun
  • Loading branch information
jmoenig committed Nov 15, 2017
1 parent d620b92 commit cbe81c4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions history.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3792,3 +3792,4 @@ Fixes:
* Portuguese & Polish translation updates, thanks, Witek and Manuel!!
* escape xml attribute contents, thanks, Brian Broll!
* changed minimum stage width to 240
* new Audio Comp library for Guzdial-style sound samples fun
1 change: 1 addition & 0 deletions libraries/LIBRARIES
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ Eisenbergification.xml Provide getters and setters for all GUI-controlled global
bignumbers.xml Infinite precision integers, exact rationals, complex The full Scheme numeric tower. "USE BIGNUMS <True>" to enable.
animation_module.xml Animation glide, grow and rotate using easing functions.
pixel_module.xml Pixels manipulate costumes pixel-wise.
audioComp_module.xml Audio Comp analyze, manipulate and generate sound samples.
18 changes: 18 additions & 0 deletions libraries/audioComp_module.xml
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="%&apos;attribute&apos; of %&apos;sound&apos;" 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;&#xD;if (audioBuffer.numberOfChannels &gt; 1) {&#xD; result = new List();&#xD; for (i = 0; i &lt; audioBuffer.numberOfChannels; i += 1) {&#xD; result.add(new List(audioBuffer.getChannelData(i)));&#xD; }&#xD; return result;&#xD;}&#xD;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,&#xD; arrayBuffer, audioCtx;&#xD;if (sound.audioBuffer) {&#xD; return;&#xD;}&#xD;base64 = sound.audio.src.split(&apos;,&apos;)[1];&#xD;binaryString = window.atob(base64);&#xD;len = binaryString.length;&#xD;bytes = new Uint8Array(len);&#xD;for (i = 0; i &lt; len; i += 1) {&#xD; bytes[i] = binaryString.charCodeAt(i);&#xD;}&#xD;arrayBuffer = bytes.buffer;&#xD;Note.prototype.setupContext();&#xD;audioCtx = Note.prototype.audioContext;&#xD;audioCtx.decodeAudioData(arrayBuffer, function(buffer) {&#xD; sound.audioBuffer = buffer;&#xD;});</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) {&#xD;case &apos;sample rate&apos;:&#xD; return sound.audioBuffer.sampleRate;&#xD;case &apos;duration&apos;:&#xD; return sound.audioBuffer.duration;&#xD;case &apos;length&apos;:&#xD; return sound.audioBuffer.length;&#xD;case &apos;number of channels&apos;:&#xD; return sound.audioBuffer.numberOfChannels;&#xD;default:&#xD; return sound.audioBuffer;&#xD;}</l></block><list><block var="sound"/><block var="attribute"/></list></block></block></script></block-definition><block-definition s="turbo %&apos;action&apos;" 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 %&apos;samples&apos; at %&apos;sampleRate&apos; 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.&#xD;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;&#xD;Note.prototype.setupContext();&#xD;audioCtx = Note.prototype.audioContext;&#xD;channels = (samples.at(1) instanceof List) ? samples.length() : 1; &#xD;frameCount = (channels === 1) ? samples.length() : samples.at(1).length(); &#xD;arrayBuffer = audioCtx.createBuffer(channels, frameCount, rate);&#xD;if (!arrayBuffer.copyToChannel) {&#xD; arrayBuffer.copyToChannel = function (src, channel) {&#xD; var buffer = this.getChannelData(channel);&#xD; for (i = 0; i &lt; src.length; i += 1) {&#xD; buffer[i] = src[i];&#xD; }&#xD; }&#xD;}&#xD;if (channels === 1) {&#xD; arrayBuffer.copyToChannel(Float32Array.from(samples.asArray()), 0, 0);&#xD;} else {&#xD; for (i = 0; i &lt; channels; i += 1) {&#xD; arrayBuffer.copyToChannel(Float32Array.from(samples.at(i + 1).asArray()), i, 0);&#xD; }&#xD;}&#xD;source = audioCtx.createBufferSource();&#xD;source.buffer = arrayBuffer;&#xD;source.connect(audioCtx.destination);&#xD;source.start();&#xD;source.pause = source.stop;&#xD;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="%&apos;frequency&apos; Hz for %&apos;duration&apos; secs at %&apos;sample rate&apos; 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 %&apos;list&apos;" 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? %&apos;data&apos;" 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 %&apos;item&apos; of %&apos;data&apos; %&apos;action&apos;" 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 %&apos;radians&apos;" 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 %&apos;list&apos; such that %&apos;test&apos;" 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 %&apos;msg&apos;" 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 %&apos;fun&apos; over %&apos;list&apos;" 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 = [],&#xD; src = list.asArray(),&#xD; len = src.length,&#xD; i;&#xD;for (i = 0; i &lt; len; i += 1) {&#xD; result.push(invoke(fun, new List([src[i]])));&#xD;}&#xD;return new List(result);</l></block><list><block var="fun"/><block var="list"/></list></block></block></script></block-definition><block-definition s="sound named %&apos;name&apos;" 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 %&apos;sound&apos;" 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>

0 comments on commit cbe81c4

Please sign in to comment.