please dont rip this site

NEW202402.TXT

 

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>&lt;HTML&gt;
&lt;HEAD&gt;
  &lt;!-- AP: Last modified: 21-Jan-2016 --&gt;
  &lt;TITLE&gt;PWM&lt;/TITLE&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;P&gt;
&lt;SCRIPT&gt;
&lt;!-- 
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 &lt; data.length; i++) {
/* Prepare data */
//data[i] = (Math.random() - 0.5) * 2; // Noise
//data[i] = (i&gt;=samplesPerMilliSecond &amp;&amp; i&lt;pwm)? 2 : 0;
//data[i] = (i%samplesPerCycle&lt;pwm)? 2 : 0
data[i] = (i&lt;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(&quot;x&quot;,x,&quot;y&quot;,y ,&quot;y1l&quot;,y1l,&quot;y0l&quot;,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 &lt; data.length; i++) {
	  ctx.lineTo(i,canvas.height-data[i]*amp);
  };
  ctx.moveTo(i-1,canvas.height);
  ctx.stroke();
}




--&gt; &lt;/SCRIPT&gt;
&lt;H1&gt;
  &lt;A HREF=&quot;http://techref.massmind.org/Techref/language/html/HTML5webapps.htm">HTML5 web app&lt;/A&gt;
  &lt;A HREF=&quot;http://techref.massmind.org/Techref/io/pwm/index.htm">PWM</A> 
  &lt;A HREF=&quot;http://techref.massmind.org/Techref/io/servo/wav.htm">audio generator&lt;/A&gt;
  for &lt;A HREF=&quot;http://techref.massmind.org/Techref/io/servo/index.htm">Servo</A> 
  &lt;A HREF=&quot;http://techref.massmind.org/Techref/io/motors.htm">Motors</A>
&lt;/H1&gt;
&lt;P&gt;
Volume:
&lt;INPUT type=&quot;range&quot; min=0 max=100 oninput=&quot;
	onVolumeChange(parseInt(this.value, 10) / 100);
	displayWave(data,document.getElementById('scope'));
	displayWave(data2,document.getElementById('scope2'));
	&quot; value=2&gt;
&lt;INPUT type=&quot;button&quot; value=&quot;On&quot; onclick=&quot;onVolumeChange(1);
	displayWave(data,document.getElementById('scope'));
	displayWave(data2,document.getElementById('scope2'));
	&quot;&gt;
&lt;INPUT type=&quot;button&quot; value=&quot;Off&quot; onclick=&quot;onVolumeChange(0);
	displayWave(data,document.getElementById('scope'));
	displayWave(data2,document.getElementById('scope2'));
	&quot;&gt; 
&lt;BR&gt;
Ch1 Degrees: -90
&lt;INPUT type=&quot;range&quot; min=-90 max=90 oninput=&quot;
	onPWMChange(parseInt(this.value, 10));
	document.getElementById('degree').innerHTML=this.value;
	displayWave(data,document.getElementById('scope'));
	&quot; value=0&gt;+90
&lt;INPUT type=&quot;text&quot; min=-90 max=90 size=3 onchange=&quot;
	onPWMChange(parseInt(this.value, 10));
	document.getElementById('degree').innerHTML=this.value;
	displayWave(data,document.getElementById('scope'));
	&quot; value=0&gt; &lt;BR&gt;
Ch2 Degrees: -90
&lt;INPUT type=&quot;range&quot; min=-90 max=90 oninput=&quot;
	onPWMChange2(parseInt(this.value, 10));
	document.getElementById('degree2').innerHTML=this.value;
	displayWave(data2,document.getElementById('scope2'));
	&quot; value=0&gt;+90
&lt;INPUT type=&quot;text&quot; min=-90 max=90 size=3 onchange=&quot;
	onPWMChange2(parseInt(this.value, 10));
	document.getElementById('degree2').innerHTML=this.value;
	displayWave(data2,document.getElementById('scope2'));
	&quot; value=0&gt;
&lt;DIV id=&quot;degree&quot;&gt;
  0
&lt;/DIV&gt;
&lt;P&gt;
&lt;canvas id=&quot;scope&quot; width=&quot;200&quot; height=&quot;100&quot;&gt;&lt;/canvas&gt;
&lt;DIV id=&quot;degree2&quot;&gt;
  0
&lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;canvas id=&quot;scope2&quot; width=&quot;200&quot; height=&quot;100&quot;&gt;&lt;/canvas&gt;
&lt;P&gt;&lt;/P&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;</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 15:35, local time: 2024/7/26 20:49,
TOP NEW HELP FIND: 
3.145.92.29:LOG IN

 ©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?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://ecomorder.com/techref/new202402.txt"> new202402</A>

Did you find what you needed?

 

Welcome to ecomorder.com!

 

Welcome to ecomorder.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .