ON 20240202@2:26:47 PM at page: On a web page you were interested in at: http://techref.massmind.org/Techref/io/servo/wav.htm#45324.6019328704 James Newton[JMN-EFP-786] Code: <PRE><HTML> <HEAD> <!-- AP: Last modified: 21-Jan-2016 --> <TITLE>PWM</TITLE> </HEAD> <BODY> <P> <SCRIPT> <!-- var sampleRate = 96000; var samplesPerMilliSecond = sampleRate*0.001; var samplesPerCycle = samplesPerMilliSecond * 3; var bufferLength = samplesPerCycle; //buffer one cycle var context = new AudioContext(); //used to be webkitAudioContext(); var buffer = context.createBuffer(2, bufferLength, sampleRate); var data = buffer.getChannelData(0); var data2 = buffer.getChannelData(1); // Done: adjust samplesPerCycle to be an even multiple of the bufferLength var degrees = 0; function buildPWMWav(degrees, samplesPerMilliSecond, data) { var pwm = (degrees+90)/180*samplesPerMilliSecond+samplesPerMilliSecond; for (var i = 0; i < data.length; i++) { /* Prepare data */ //data[i] = (Math.random() - 0.5) * 2; // Noise //data[i] = (i>=samplesPerMilliSecond && i<pwm)? 2 : 0; //data[i] = (i%samplesPerCycle<pwm)? 2 : 0 data[i] = (i<pwm)? 2 : 0 }; } onPWMChange = function (degrees) { buildPWMWav(degrees, samplesPerMilliSecond, data); }; onPWMChange2 = function (degrees) { buildPWMWav(degrees, samplesPerMilliSecond, data2); }; onPWMChange(0); //start in the center. onPWMChange2(45); //start offcenter. var source = context.createBufferSource(); source.loop = true; // Make sure that sound will repeat over and over again source.buffer = buffer; // Assign our buffer to the source node buffer gainNode = context.createGain(); //connect the source to the gainNode, and the gainNode to the destination. source.connect(gainNode); gainNode.connect(context.destination); scale2log = function (x) { let x0 = 0, x1 = 1 let offset = 0.0125 let y0 = 0+offset, y1 = 1+offset let y1l = Math.log(y1) let y0l = Math.log(y0) let y = Math.exp((x * y0l - x * y1l + x0 * y1l - x1 * y0l) / (x0 - x1)) y = y-offset //remove the offset y = Math.round(y * 1000) / 1000 //round off //console.log("x",x,"y",y ,"y1l",y1l,"y0l",y0l) return y } onVolumeChange = function (vol) { context.resume(); gainNode.gain.value = scale2log(vol); //console.log(Math.floor(gainNode.gain.value * 100)); }; onVolumeChange(0); //set initial volumn to zero source.start(0); //Just start immediatly with zero volume. //Set volume to 0 to stop. If you use noteOff to stop, //you have to recreate the source. displayWave = function(data,canvas) { var ctx = canvas.getContext('2d'); var amp = Math.min(64*gainNode.gain.value,99)+10; canvas.width = data.length+10; ctx.fillStyle = 'white'; ctx.fillRect(0,0,canvas.width,canvas.height); ctx.strokeStyle = 'red'; ctx.beginPath(); ctx.moveTo(0,canvas.height); for (var i = 0; i < data.length; i++) { ctx.lineTo(i,canvas.height-data[i]*amp); }; ctx.moveTo(i-1,canvas.height); ctx.stroke(); } --> </SCRIPT> <H1> <A HREF="http://techref.massmind.org/Techref/language/html/HTML5webapps.htm">HTML5 web app</A> <A HREF="http://techref.massmind.org/Techref/io/pwm/index.htm">PWM</A> <A HREF="http://techref.massmind.org/Techref/io/servo/wav.htm">audio generator</A> for <A HREF="http://techref.massmind.org/Techref/io/servo/index.htm">Servo</A> <A HREF="http://techref.massmind.org/Techref/io/motors.htm">Motors</A> </H1> <P> Volume: <INPUT type="range" min=0 max=100 oninput=" onVolumeChange(parseInt(this.value, 10) / 100); displayWave(data,document.getElementById('scope')); displayWave(data2,document.getElementById('scope2')); " value=2> <INPUT type="button" value="On" onclick="onVolumeChange(1); displayWave(data,document.getElementById('scope')); displayWave(data2,document.getElementById('scope2')); "> <INPUT type="button" value="Off" onclick="onVolumeChange(0); displayWave(data,document.getElementById('scope')); displayWave(data2,document.getElementById('scope2')); "> <BR> Ch1 Degrees: -90 <INPUT type="range" min=-90 max=90 oninput=" onPWMChange(parseInt(this.value, 10)); document.getElementById('degree').innerHTML=this.value; displayWave(data,document.getElementById('scope')); " value=0>+90 <INPUT type="text" min=-90 max=90 size=3 onchange=" onPWMChange(parseInt(this.value, 10)); document.getElementById('degree').innerHTML=this.value; displayWave(data,document.getElementById('scope')); " value=0> <BR> Ch2 Degrees: -90 <INPUT type="range" min=-90 max=90 oninput=" onPWMChange2(parseInt(this.value, 10)); document.getElementById('degree2').innerHTML=this.value; displayWave(data2,document.getElementById('scope2')); " value=0>+90 <INPUT type="text" min=-90 max=90 size=3 onchange=" onPWMChange2(parseInt(this.value, 10)); document.getElementById('degree2').innerHTML=this.value; displayWave(data2,document.getElementById('scope2')); " value=0> <DIV id="degree"> 0 </DIV> <P> <canvas id="scope" width="200" height="100"></canvas> <DIV id="degree2"> 0 </DIV> <P></P> <canvas id="scope2" width="200" height="100"></canvas> <P></P> </BODY> </HTML></PRE> ON 20240202@2:28:39 PM at page: On a web page you were interested in at: http://techref.massmind.org/Techref/io/servo/wav.htm# James Newton[JMN-EFP-786] edited the page. Difference: http://techref.massmind.org/techref/diff.asp?url=\Techref\io\servo\wav.htm&version=1 ON 20240202@2:35:28 PM at page: On a web page you were interested in at: http://techref.massmind.org/Techref/io/servo/html4audiopwm.html# James Newton[JMN-EFP-786] edited the page. Difference: http://techref.massmind.org/techref/diff.asp?url=\Techref\io\servo\html4audiopwm.html&version=1
file: /Techref/new202402.txt, 6KB, , updated: 2024/2/2 14:35, local time: 2024/11/8 14:32,
18.118.152.100:LOG IN ©2024 PLEASE DON'T RIP! THIS SITE CLOSES OCT 28, 2024 SO LONG AND THANKS FOR ALL THE FISH!
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://www.piclist.com/techref/new202402.txt"> new202402</A> |
Did you find what you needed? |
PICList 2024 contributors:
o List host: MIT, Site host massmind.org, Top posters @none found - Page Editors: James Newton, David Cary, and YOU! * Roman Black of Black Robotics donates from sales of Linistep stepper controller kits. * Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters. * Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated! * Contributors: Richard Seriani, Sr. |
Welcome to www.piclist.com! |
.