turtlestitch/libraries/audioComp_module.xml

18 wiersze
20 KiB
XML

<blocks app="Snap! 4.2, http://snap.berkeley.edu" version="1"><block-definition s="%&apos;attribute&apos; of sound %&apos;sound&apos;" type="reporter" category="sound"><header></header><code></code><translations></translations><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 sound %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><translations></translations><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><translations></translations><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 sound %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 sound %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><translations></translations><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="for each %&apos;item&apos; of %&apos;data&apos; %&apos;action&apos;" type="command" category="lists"><header></header><code></code><translations></translations><inputs><input type="%upvar"></input><input type="%l"></input><input type="%cs"></input></inputs><script><block s="doUntil"><block s="reportEquals"><block var="data"/><block s="reportNewList"><list></list></block></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><translations></translations><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><translations></translations><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><translations></translations><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><translations></translations><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="sound named %&apos;name&apos;" type="reporter" category="sensing"><header></header><code></code><translations></translations><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 sound %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 %&apos;sound&apos;" type="command" category="pen"><comment 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><translations></translations><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 sound %obj"><l>samples</l><block var="sound"/></custom-block></block><block s="doSetVar"><l>step</l><block s="reportQuotient"><custom-block s="%s of sound %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><translations></translations><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><translations></translations><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><block-definition s="$circleSolid-1-255-0-0 record" type="reporter" category="sensing"><comment x="0" y="0" w="192" collapsed="false">records an audio snippet and reports it as a new sound, or zero if the user cancels</comment><header></header><code></code><translations></translations><inputs></inputs><script><block s="doDeclareVariables"><list><l>test</l><l>pic</l></list></block><block s="doSetVar"><l>test</l><block s="evaluate"><block s="reportJSFunction"><list></list><l>var soundRecorder,&#xD; result = false;&#xD;&#xD;soundRecorder = new SoundRecorderDialogMorph(&#xD; function (audio) {&#xD; if (audio) {&#xD; result = new Sound(audio, &apos;recording&apos;);&#xD; } else {&#xD; result = null;&#xD; this.destroy();&#xD; }&#xD; }&#xD;);&#xD;&#xD;soundRecorder.cancel = function () {&#xD; result = null;&#xD; this.destroy();&#xD;};&#xD;&#xD;soundRecorder.key = &apos;microphone&apos;;&#xD;soundRecorder.popUp(this.world());&#xD;return function () {return result; };</l></block><list></list></block></block><block s="doWaitUntil"><block s="evaluate"><block s="reifyScript"><script><block s="doSetVar"><l>pic</l><block s="evaluate"><block var="test"/><list></list></block></block><block s="doReport"><block s="reportNot"><block s="reportEquals"><block var="pic"/><block s="reportBoolean"><l><bool>false</bool></l></block></block></block></block></script><list></list></block><list></list></block></block><block s="doReport"><block var="pic"/></block></script></block-definition><block-definition s="loudness" type="reporter" category="sensing"><header></header><code></code><translations>ca:volum del so&#xD;es:volumen del sonido&#xD;</translations><inputs></inputs><script><block s="doIf"><block s="evaluate"><block s="reportJSFunction"><list></list><l>return typeof meter == &apos;undefined&apos;</l></block><list></list></block><script><block s="doRun"><block s="reportJSFunction"><list></list><l>navigator.getUserMedia = ( navigator.getUserMedia ||&#xD; navigator.webkitGetUserMedia ||&#xD; navigator.mozGetUserMedia ||&#xD; navigator.msGetUserMedia);&#xD;&#xD;var audioContext = new AudioContext();&#xD;&#xD;/*&#xD;The MIT License (MIT)&#xD;&#xD;Copyright (c) 2014 Chris Wilson&#xD;&#xD;Permission is hereby granted, free of charge, to any person obtaining a copy&#xD;of this software and associated documentation files (the "Software"), to deal&#xD;in the Software without restriction, including without limitation the rights&#xD;to use, copy, modify, merge, publish, distribute, sublicense, and/or sell&#xD;copies of the Software, and to permit persons to whom the Software is&#xD;furnished to do so, subject to the following conditions:&#xD;&#xD;The above copyright notice and this permission notice shall be included in all&#xD;copies or substantial portions of the Software.&#xD;&#xD;THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR&#xD;IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,&#xD;FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE&#xD;AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER&#xD;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,&#xD;OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE&#xD;SOFTWARE.&#xD;*/&#xD;&#xD;function createAudioMeter (audioContext,clipLevel,averaging,clipLag) {&#xD; var processor = audioContext.createScriptProcessor(512);&#xD; processor.onaudioprocess = volumeAudioProcess;&#xD; processor.clipping = false;&#xD; processor.lastClip = 0;&#xD; processor.volume = 0;&#xD; processor.clipLevel = clipLevel || 0.98;&#xD; processor.averaging = averaging || 0.95;&#xD; processor.clipLag = clipLag || 750;&#xD; processor.connect(audioContext.destination);&#xD; return processor;&#xD;};&#xD;&#xD;function volumeAudioProcess (event) {&#xD; var buf = event.inputBuffer.getChannelData(0),&#xD; bufLength = buf.length,&#xD; sum = 0,&#xD; x;&#xD; for (var i=0; i&lt;bufLength; i++) {&#xD; x = buf[i];&#xD; if (Math.abs(x)&gt;=this.clipLevel) {&#xD; this.clipping = true;&#xD; this.lastClip = window.performance.now();&#xD; }&#xD; sum += x * x;&#xD; }&#xD; var rms = Math.sqrt(sum / bufLength);&#xD; this.volume = Math.max(rms, this.volume*this.averaging);&#xD;};&#xD;&#xD;function gotStream (stream) {&#xD; mediaStreamSource = audioContext.createMediaStreamSource(stream);&#xD; window.meter = createAudioMeter(audioContext);&#xD; mediaStreamSource.connect(meter);&#xD;};&#xD;&#xD;navigator.getUserMedia(&#xD; {&#xD; "audio": {&#xD; "mandatory": {&#xD; "googEchoCancellation": "false",&#xD; "googAutoGainControl": "false",&#xD; "googNoiseSuppression": "false",&#xD; "googHighpassFilter": "false"&#xD; },&#xD; "optional": []&#xD; },&#xD; }, gotStream, nop);</l></block><list></list></block></script></block><block s="doWaitUntil"><block s="evaluate"><block s="reportJSFunction"><list></list><l>return typeof meter != &apos;undefined&apos;</l></block><list></list></block></block><block s="doReport"><block s="evaluate"><block s="reportJSFunction"><list></list><l>return Math.floor(meter.volume * 100);</l></block><list></list></block></block></script></block-definition></blocks>