Repository: urtzurd/html-audio
Branch: gh-pages
Commit: e1f733a4845e
Files: 6
Total size: 18.2 KB
Directory structure:
gitextract_iu8s7blx/
├── LICENSE
├── README.md
└── static/
├── css/
│ └── pitch-shifter.css
├── index.html
└── js/
├── buffer-loader.js
└── pitch-shifter.js
================================================
FILE CONTENTS
================================================
================================================
FILE: LICENSE
================================================
The MIT License (MIT)
Copyright (c) 2015 Urtzi Urdapilleta Roy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: README.md
================================================
Pitch shifter
=============
This is a very simple pith shifter using HTML5 Web Audio API. It's based in the principles of granular synthesis,
instead of FFT analysis and transformation, to keep things simple and fast. In order to use the microphone input, a
browser supporting the Media Stream API is needed.
There are three different audio graphic representations for fun, just drag the sliders and enjoy!

Live Demo
=========
A live demo can be found here: <a href="https://urtzurd.github.io/html-audio/static/">https://urtzurd.github.io/html-audio/static/</a>
Thanks
======
[DonKarlssonSan](http://github.com/DonKarlssonSan) for updating the project to the current version of the Web Audio API.
================================================
FILE: static/css/pitch-shifter.css
================================================
a:link {
font-weight: bold;
color: rgb(85, 85, 85);
}
a:visited {
font-weight: bold;
color: rgb(170, 170, 170);
}
body {
background-color: black;
text-align: justify;
}
em {
font-weight: bold;
color: rgb(85, 85, 85);
}
.container {
width: 512px;
border-radius: 5px;
background-color: white;
padding: 10px;
}
canvas {
padding-top: 10px;
width: 512px;
height: 240px;
}
.sliderContainer {
width: 100%;
padding-bottom: 30px;
}
.sliderLabel {
width: 128px;
float: left;
}
.sliderDisplay {
width: 104px;
float: left;
text-align: right;
}
#pitchRatioSlider, #overlapRatioSlider, #grainSizeSlider {
width: 272px;
float: left;
}
#audioVisualisationSlider {
margin-left: 144px;
width: 128px;
float: left;
}
#audioSourceSlider {
margin-left: 208px;
width: 64px;
float: left;
}
.warningContainer {
background-color: rgb(85, 0, 0);
color: white;
text-align: center;
padding: 10px;
width: 480px;
border-radius: 5px;
margin: auto;
}
================================================
FILE: static/index.html
================================================
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="css/pitch-shifter.css" />
<link rel="stylesheet" href="css/jquery-ui.min.css" />
<link rel="stylesheet" href="css/jquery.ui.slider.min.css" />
<script src="js/buffer-loader.js"></script>
<script src="js/pitch-shifter.js"></script>
<script src="js/jquery-1.9.1.min.js"></script>
<script src="js/jquery-ui.min.js"></script>
</head>
<body>
<div class="container">
<div class="sliderContainer">
<div class="sliderLabel">Pitch ratio</div>
<div id="pitchRatioSlider"></div>
<div id="pitchRatioDisplay" class="sliderDisplay"></div>
</div>
<div class="sliderContainer">
<div class="sliderLabel">Overlap ratio</div>
<div id="overlapRatioSlider"></div>
<div id="overlapRatioDisplay" class="sliderDisplay"></div>
</div>
<div class="sliderContainer">
<div class="sliderLabel">Grain size</div>
<div id="grainSizeSlider"></div>
<div id="grainSizeDisplay" class="sliderDisplay"></div>
</div>
<div class="sliderContainer">
<div class="sliderLabel">Visualisation</div>
<div id="audioVisualisationSlider"></div>
<div id="audioVisualisationDisplay" class="sliderDisplay"></div>
</div>
<div class="sliderContainer">
<div class="sliderLabel">Audio source</div>
<div id="audioSourceSlider"></div>
<div id="audioSourceDisplay" class="sliderDisplay"></div>
</div>
<canvas width="512px" height="240px"></canvas>
<p>This page needs a <a href="https://dvcs.w3.org/hg/audio/raw-file/tip/webaudio/specification.html">Web Audio API</a> enabled browser.
Here is an updated list with browser support: <a href="http://caniuse.com/#feat=audio-api">Can I use</a>.</p>
<p>
In order to use the microphone, you may have to enable a configuration flag:
<ul>
<li>Go to <em>chrome://flags</em></li>
<li>Enable the Web Audio Input option</li>
<li>Restart the browser</li>
</ul>
</p>
<div class="warningContainer">
<strong>Use the microphone with headphones or you may get a lot of feedback!</strong>
</div>
</div>
</body>
</html>
================================================
FILE: static/js/buffer-loader.js
================================================
function BufferLoader(context, urlList, callback) {
this.context = context;
this.urlList = urlList;
this.onload = callback;
this.bufferList = new Array();
this.loadCount = 0;
}
BufferLoader.prototype.loadBuffer = function (url, index) {
// Load buffer asynchronously
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.responseType = "arraybuffer";
var loader = this;
request.onload = function () {
// Asynchronously decode the audio file data in request.response
loader.context.decodeAudioData(
request.response,
function (buffer) {
if (!buffer) {
alert('error decoding file data: ' + url);
return;
}
loader.bufferList[index] = buffer;
if (++loader.loadCount == loader.urlList.length)
loader.onload(loader.bufferList);
},
function (error) {
console.error('decodeAudioData error', error);
}
);
}
request.onerror = function () {
alert('BufferLoader: XHR error');
}
request.send();
}
BufferLoader.prototype.load = function () {
for (var i = 0; i < this.urlList.length; ++i)
this.loadBuffer(this.urlList[i], i);
}
================================================
FILE: static/js/pitch-shifter.js
================================================
var pitchShifter = (function () {
var audioContext,
audioSources = [],
pitchShifterProcessor,
spectrumAudioAnalyser,
sonogramAudioAnalyser,
canvas,
canvasContext,
barGradient,
waveGradient;
var audioSourcesNames = ['MP3 file', 'Microphone'],
audioSourceIndex = 0,
audioVisualisationNames = ['Spectrum', 'Wave', 'Sonogram'],
audioVisualisationIndex = 0,
validGranSizes = [256, 512, 1024, 2048, 4096, 8192],
grainSize = validGranSizes[1],
pitchRatio = 1.0,
overlapRatio = 0.50,
spectrumFFTSize = 128,
spectrumSmoothing = 0.8,
sonogramFFTSize = 2048,
sonogramSmoothing = 0;
hannWindow = function (length) {
var window = new Float32Array(length);
for (var i = 0; i < length; i++) {
window[i] = 0.5 * (1 - Math.cos(2 * Math.PI * i / (length - 1)));
}
return window;
};
linearInterpolation = function (a, b, t) {
return a + (b - a) * t;
};
initAudio = function () {
if (!navigator.getUserMedia) {
alert('Your browser does not support the Media Stream API');
} else {
navigator.getUserMedia(
{audio: true, video: false},
function (stream) {
audioSources[1] = audioContext.createMediaStreamSource(stream);
},
function (error) {
alert('Unable to get the user media');
}
);
}
spectrumAudioAnalyser = audioContext.createAnalyser();
spectrumAudioAnalyser.fftSize = spectrumFFTSize;
spectrumAudioAnalyser.smoothingTimeConstant = spectrumSmoothing;
sonogramAudioAnalyser = audioContext.createAnalyser();
sonogramAudioAnalyser.fftSize = sonogramFFTSize;
sonogramAudioAnalyser.smoothingTimeConstant = sonogramSmoothing;
var bufferLoader = new BufferLoader(
audioContext, ['audio/voice.mp3'], function (bufferList) {
audioSources[0] = audioContext.createBufferSource();
audioSources[0].buffer = bufferList[0];
audioSources[0].loop = true;
audioSources[0].connect(pitchShifterProcessor);
audioSources[0].start(0);
}
);
bufferLoader.load();
};
initProcessor = function () {
if (pitchShifterProcessor) {
pitchShifterProcessor.disconnect();
}
if (audioContext.createScriptProcessor) {
pitchShifterProcessor = audioContext.createScriptProcessor(grainSize, 1, 1);
} else if (audioContext.createJavaScriptNode) {
pitchShifterProcessor = audioContext.createJavaScriptNode(grainSize, 1, 1);
}
pitchShifterProcessor.buffer = new Float32Array(grainSize * 2);
pitchShifterProcessor.grainWindow = hannWindow(grainSize);
pitchShifterProcessor.onaudioprocess = function (event) {
var inputData = event.inputBuffer.getChannelData(0);
var outputData = event.outputBuffer.getChannelData(0);
for (i = 0; i < inputData.length; i++) {
// Apply the window to the input buffer
inputData[i] *= this.grainWindow[i];
// Shift half of the buffer
this.buffer[i] = this.buffer[i + grainSize];
// Empty the buffer tail
this.buffer[i + grainSize] = 0.0;
}
// Calculate the pitch shifted grain re-sampling and looping the input
var grainData = new Float32Array(grainSize * 2);
for (var i = 0, j = 0.0;
i < grainSize;
i++, j += pitchRatio) {
var index = Math.floor(j) % grainSize;
var a = inputData[index];
var b = inputData[(index + 1) % grainSize];
grainData[i] += linearInterpolation(a, b, j % 1.0) * this.grainWindow[i];
}
// Copy the grain multiple times overlapping it
for (i = 0; i < grainSize; i += Math.round(grainSize * (1 - overlapRatio))) {
for (j = 0; j <= grainSize; j++) {
this.buffer[i + j] += grainData[j];
}
}
// Output the first half of the buffer
for (i = 0; i < grainSize; i++) {
outputData[i] = this.buffer[i];
}
};
pitchShifterProcessor.connect(spectrumAudioAnalyser);
pitchShifterProcessor.connect(sonogramAudioAnalyser);
pitchShifterProcessor.connect(audioContext.destination);
};
initSliders = function () {
$("#pitchRatioSlider").slider({
orientation: "horizontal",
min: 0.5,
max: 2,
step: 0.01,
range: 'min',
value: pitchRatio,
slide: function (event, ui) {
pitchRatio = ui.value;
$("#pitchRatioDisplay").text(pitchRatio);
}
});
$("#overlapRatioSlider").slider({
orientation: "horizontal",
min: 0,
max: 0.75,
step: 0.01,
range: 'min',
value: overlapRatio,
slide: function (event, ui) {
overlapRatio = ui.value;
$("#overlapRatioDisplay").text(overlapRatio);
}
});
$("#grainSizeSlider").slider({
orientation: "horizontal",
min: 0,
max: validGranSizes.length - 1,
step: 1,
range: 'min',
value: validGranSizes.indexOf(grainSize),
slide: function (event, ui) {
grainSize = validGranSizes[ui.value];
$("#grainSizeDisplay").text(grainSize);
initProcessor();
if (audioSources[audioSourceIndex]) {
audioSources[audioSourceIndex].connect(pitchShifterProcessor);
}
}
});
$("#audioVisualisationSlider").slider({
orientation: "horizontal",
min: 0,
max: audioVisualisationNames.length - 1,
step: 1,
value: audioVisualisationIndex,
slide: function (event, ui) {
audioVisualisationIndex = ui.value;
$("#audioVisualisationDisplay").text(audioVisualisationNames[audioVisualisationIndex]);
}
});
$("#audioSourceSlider").slider({
orientation: "horizontal",
min: 0,
max: audioSourcesNames.length - 1,
step: 1,
value: audioSourceIndex,
slide: function (event, ui) {
if (audioSources[audioSourceIndex]) {
audioSources[audioSourceIndex].disconnect();
}
audioSourceIndex = ui.value;
$("#audioSourceDisplay").text(audioSourcesNames[audioSourceIndex]);
if (audioSources[audioSourceIndex]) {
audioSources[audioSourceIndex].connect(pitchShifterProcessor);
}
}
});
$("#pitchRatioDisplay").text(pitchRatio);
$("#overlapRatioDisplay").text(overlapRatio);
$("#grainSizeDisplay").text(grainSize);
$("#audioVisualisationDisplay").text(audioVisualisationNames[audioVisualisationIndex]);
$("#audioSourceDisplay").text(audioSourcesNames[audioSourceIndex]);
};
initCanvas = function () {
canvas = document.querySelector('canvas');
canvasContext = canvas.getContext('2d');
barGradient = canvasContext.createLinearGradient(0, 0, 1, canvas.height - 1);
barGradient.addColorStop(0, '#550000');
barGradient.addColorStop(0.995, '#AA5555');
barGradient.addColorStop(1, '#555555');
waveGradient = canvasContext.createLinearGradient(canvas.width - 2, 0, canvas.width - 1, canvas.height - 1);
waveGradient.addColorStop(0, '#FFFFFF');
waveGradient.addColorStop(0.75, '#550000');
waveGradient.addColorStop(0.75, '#555555');
waveGradient.addColorStop(0.76, '#AA5555');
waveGradient.addColorStop(1, '#FFFFFF');
};
renderCanvas = function () {
switch (audioVisualisationIndex) {
case 0:
var frequencyData = new Uint8Array(spectrumAudioAnalyser.frequencyBinCount);
spectrumAudioAnalyser.getByteFrequencyData(frequencyData);
canvasContext.clearRect(0, 0, canvas.width, canvas.height);
canvasContext.fillStyle = barGradient;
var barWidth = canvas.width / frequencyData.length;
for (i = 0; i < frequencyData.length; i++) {
var magnitude = frequencyData[i];
canvasContext.fillRect(barWidth * i, canvas.height, barWidth - 1, -magnitude - 1);
}
break;
case 1:
var timeData = new Uint8Array(spectrumAudioAnalyser.frequencyBinCount);
spectrumAudioAnalyser.getByteTimeDomainData(timeData);
var amplitude = 0.0;
for (i = 0; i < timeData.length; i++) {
amplitude += timeData[i];
}
amplitude = Math.abs(amplitude / timeData.length - 128) * 5 + 1;
var previousImage = canvasContext.getImageData(1, 0, canvas.width - 1, canvas.height);
canvasContext.putImageData(previousImage, 0, 0);
var axisY = canvas.height * 3 / 4;
canvasContext.fillStyle = '#FFFFFF';
canvasContext.fillRect(canvas.width - 1, 0, 1, canvas.height);
canvasContext.fillStyle = waveGradient;
canvasContext.fillRect(canvas.width - 1, axisY, 1, -amplitude);
canvasContext.fillRect(canvas.width - 1, axisY, 1, amplitude / 2);
break;
case 2:
frequencyData = new Uint8Array(sonogramAudioAnalyser.frequencyBinCount);
sonogramAudioAnalyser.getByteFrequencyData(frequencyData);
previousImage = canvasContext.getImageData(1, 0, canvas.width - 1, canvas.height);
canvasContext.putImageData(previousImage, 0, 0);
var bandHeight = canvas.height / frequencyData.length;
for (var i = 0, y = canvas.height - 1;
i < frequencyData.length;
i++, y -= bandHeight) {
var color = frequencyData[i] << 16;
canvasContext.fillStyle = '#' + color.toString(16);
canvasContext.fillRect(canvas.width - 1, y, 1, -bandHeight);
}
break;
}
window.requestAnimFrame(renderCanvas);
};
return {
init: function () {
if ('AudioContext' in window) {
audioContext = new AudioContext();
} else {
alert('Your browser does not support the Web Audio API');
return;
}
initAudio();
initProcessor();
initSliders();
initCanvas();
window.requestAnimFrame(renderCanvas);
}
}
}());
window.requestAnimFrame = (function () {
return (window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function (callback) {
window.setTimeout(callback, 1000 / 60);
});
})();
window.addEventListener("DOMContentLoaded", pitchShifter.init, true);
gitextract_iu8s7blx/
├── LICENSE
├── README.md
└── static/
├── css/
│ └── pitch-shifter.css
├── index.html
└── js/
├── buffer-loader.js
└── pitch-shifter.js
SYMBOL INDEX (1 symbols across 1 files)
FILE: static/js/buffer-loader.js
function BufferLoader (line 1) | function BufferLoader(context, urlList, callback) {
Condensed preview — 6 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (20K chars).
[
{
"path": "LICENSE",
"chars": 1088,
"preview": "The MIT License (MIT)\n\nCopyright (c) 2015 Urtzi Urdapilleta Roy\n\nPermission is hereby granted, free of charge, to any pe"
},
{
"path": "README.md",
"chars": 825,
"preview": "Pitch shifter\n=============\n\nThis is a very simple pith shifter using HTML5 Web Audio API. It's based in the principles "
},
{
"path": "static/css/pitch-shifter.css",
"chars": 1078,
"preview": "a:link {\n font-weight: bold;\n color: rgb(85, 85, 85);\n}\n\na:visited {\n font-weight: bold;\n color: rgb(170, 17"
},
{
"path": "static/index.html",
"chars": 2444,
"preview": "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"utf-8\" />\n <link rel=\"stylesheet\" href=\"css/pitch-shifter."
},
{
"path": "static/js/buffer-loader.js",
"chars": 1353,
"preview": "function BufferLoader(context, urlList, callback) {\n\n this.context = context;\n this.urlList = urlList;\n this.on"
},
{
"path": "static/js/pitch-shifter.js",
"chars": 11826,
"preview": "var pitchShifter = (function () {\n\n var audioContext,\n audioSources = [],\n pitchShifterProcessor,\n "
}
]
About this extraction
This page contains the full source code of the urtzurd/html-audio GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 6 files (18.2 KB), approximately 4.4k tokens, and a symbol index with 1 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.