[
  {
    "path": "LICENSE",
    "content": "The MIT License (MIT)\n\nCopyright (c) 2015 Urtzi Urdapilleta Roy\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "Pitch shifter\n=============\n\nThis is a very simple pith shifter using HTML5 Web Audio API. It's based in the principles of granular synthesis,\ninstead of FFT analysis and transformation, to keep things simple and fast. In order to use the microphone input, a\nbrowser supporting the Media Stream API is needed.\n\nThere are three different audio graphic representations for fun, just drag the sliders and enjoy!\n\n![Waveform screenshot](http://urtzurd.github.io/html-audio/static/img/screenshot.png \"Screenshot of the waveform display\")\n\nLive Demo\n=========\nA live demo can be found here: <a href=\"https://urtzurd.github.io/html-audio/static/\">https://urtzurd.github.io/html-audio/static/</a>\n\nThanks\n======\n[DonKarlssonSan](http://github.com/DonKarlssonSan) for updating the project to the current version of the Web Audio API.\n"
  },
  {
    "path": "static/css/pitch-shifter.css",
    "content": "a:link {\n    font-weight: bold;\n    color: rgb(85, 85, 85);\n}\n\na:visited {\n    font-weight: bold;\n    color: rgb(170, 170, 170);\n}\n\nbody {\n    background-color: black;\n    text-align: justify;\n}\n\nem {\n    font-weight: bold;\n    color: rgb(85, 85, 85);\n}\n\n.container {\n    width: 512px;\n    border-radius: 5px;\n    background-color: white;\n    padding: 10px;\n}\n\ncanvas {\n    padding-top: 10px;\n    width: 512px;\n    height: 240px;\n}\n\n.sliderContainer {\n    width: 100%;\n    padding-bottom: 30px;\n}\n\n.sliderLabel {\n    width: 128px;\n    float: left;\n}\n\n.sliderDisplay {\n    width: 104px;\n    float: left;\n    text-align: right;\n}\n\n#pitchRatioSlider, #overlapRatioSlider, #grainSizeSlider {\n    width: 272px;\n    float: left;\n}\n\n#audioVisualisationSlider {\n    margin-left: 144px;\n    width: 128px;\n    float: left;\n}\n\n#audioSourceSlider {\n    margin-left: 208px;\n    width: 64px;\n    float: left;\n}\n\n.warningContainer {\n    background-color: rgb(85, 0, 0);\n    color: white;\n    text-align: center;\n    padding: 10px;\n    width: 480px;\n    border-radius: 5px;\n    margin: auto;\n}\n"
  },
  {
    "path": "static/index.html",
    "content": "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n    <meta charset=\"utf-8\" />\n    <link rel=\"stylesheet\" href=\"css/pitch-shifter.css\" />\n    <link rel=\"stylesheet\" href=\"css/jquery-ui.min.css\" />\n    <link rel=\"stylesheet\" href=\"css/jquery.ui.slider.min.css\" />\n    <script src=\"js/buffer-loader.js\"></script>\n    <script src=\"js/pitch-shifter.js\"></script>\n    <script src=\"js/jquery-1.9.1.min.js\"></script>\n    <script src=\"js/jquery-ui.min.js\"></script>\n</head>\n<body>\n    <div class=\"container\">\n\n        <div class=\"sliderContainer\">\n            <div class=\"sliderLabel\">Pitch ratio</div>\n            <div id=\"pitchRatioSlider\"></div>\n            <div id=\"pitchRatioDisplay\" class=\"sliderDisplay\"></div>\n        </div>\n\n        <div class=\"sliderContainer\">\n            <div class=\"sliderLabel\">Overlap ratio</div>\n            <div id=\"overlapRatioSlider\"></div>\n            <div id=\"overlapRatioDisplay\" class=\"sliderDisplay\"></div>\n        </div>\n\n        <div class=\"sliderContainer\">\n            <div class=\"sliderLabel\">Grain size</div>\n            <div id=\"grainSizeSlider\"></div>\n            <div id=\"grainSizeDisplay\" class=\"sliderDisplay\"></div>\n        </div>\n\n        <div class=\"sliderContainer\">\n            <div class=\"sliderLabel\">Visualisation</div>\n            <div id=\"audioVisualisationSlider\"></div>\n            <div id=\"audioVisualisationDisplay\" class=\"sliderDisplay\"></div>\n        </div>\n\n        <div class=\"sliderContainer\">\n            <div class=\"sliderLabel\">Audio source</div>\n            <div id=\"audioSourceSlider\"></div>\n            <div id=\"audioSourceDisplay\" class=\"sliderDisplay\"></div>\n        </div>\n\n        <canvas width=\"512px\" height=\"240px\"></canvas>\n\n        <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. \n        Here is an updated list with browser support: <a href=\"http://caniuse.com/#feat=audio-api\">Can I use</a>.</p>\n        <p>\n            In order to use the microphone, you may have to enable a configuration flag:\n            <ul>\n                <li>Go to <em>chrome://flags</em></li>\n                <li>Enable the Web Audio Input option</li>\n                <li>Restart the browser</li>\n            </ul>\n        </p>\n\n        <div class=\"warningContainer\">\n            <strong>Use the microphone with headphones or you may get a lot of feedback!</strong>\n        </div>\n\n    </div>\n</body>\n</html>\n"
  },
  {
    "path": "static/js/buffer-loader.js",
    "content": "function BufferLoader(context, urlList, callback) {\n\n    this.context = context;\n    this.urlList = urlList;\n    this.onload = callback;\n    this.bufferList = new Array();\n    this.loadCount = 0;\n}\n\nBufferLoader.prototype.loadBuffer = function (url, index) {\n\n    // Load buffer asynchronously\n    var request = new XMLHttpRequest();\n    request.open(\"GET\", url, true);\n    request.responseType = \"arraybuffer\";\n\n    var loader = this;\n    request.onload = function () {\n\n        // Asynchronously decode the audio file data in request.response\n        loader.context.decodeAudioData(\n\n            request.response,\n\n            function (buffer) {\n\n                if (!buffer) {\n                    alert('error decoding file data: ' + url);\n                    return;\n                }\n\n                loader.bufferList[index] = buffer;\n                if (++loader.loadCount == loader.urlList.length)\n                    loader.onload(loader.bufferList);\n            },\n\n            function (error) {\n                console.error('decodeAudioData error', error);\n            }\n        );\n    }\n\n    request.onerror = function () {\n        alert('BufferLoader: XHR error');\n    }\n\n    request.send();\n}\n\nBufferLoader.prototype.load = function () {\n\n    for (var i = 0; i < this.urlList.length; ++i)\n        this.loadBuffer(this.urlList[i], i);\n}\n"
  },
  {
    "path": "static/js/pitch-shifter.js",
    "content": "var pitchShifter = (function () {\n\n    var audioContext,\n        audioSources = [],\n        pitchShifterProcessor,\n        spectrumAudioAnalyser,\n        sonogramAudioAnalyser,\n        canvas,\n        canvasContext,\n        barGradient,\n        waveGradient;\n\n    var audioSourcesNames = ['MP3 file', 'Microphone'],\n        audioSourceIndex = 0,\n        audioVisualisationNames = ['Spectrum', 'Wave', 'Sonogram'],\n        audioVisualisationIndex = 0,\n        validGranSizes = [256, 512, 1024, 2048, 4096, 8192],\n        grainSize = validGranSizes[1],\n        pitchRatio = 1.0,\n        overlapRatio = 0.50,\n        spectrumFFTSize = 128,\n        spectrumSmoothing = 0.8,\n        sonogramFFTSize = 2048,\n        sonogramSmoothing = 0;\n\n    hannWindow = function (length) {\n\n        var window = new Float32Array(length);\n        for (var i = 0; i < length; i++) {\n            window[i] = 0.5 * (1 - Math.cos(2 * Math.PI * i / (length - 1)));\n        }\n        return window;\n    };\n\n    linearInterpolation = function (a, b, t) {\n        return a + (b - a) * t;\n    };\n\n    initAudio = function () {\n\n        if (!navigator.getUserMedia) {\n\n            alert('Your browser does not support the Media Stream API');\n\n        } else {\n\n            navigator.getUserMedia(\n\n                {audio: true, video: false},\n\n                function (stream) {\n                    audioSources[1] = audioContext.createMediaStreamSource(stream);\n                },\n\n                function (error) {\n                    alert('Unable to get the user media');\n                }\n            );\n        }\n\n        spectrumAudioAnalyser = audioContext.createAnalyser();\n        spectrumAudioAnalyser.fftSize = spectrumFFTSize;\n        spectrumAudioAnalyser.smoothingTimeConstant = spectrumSmoothing;\n\n        sonogramAudioAnalyser = audioContext.createAnalyser();\n        sonogramAudioAnalyser.fftSize = sonogramFFTSize;\n        sonogramAudioAnalyser.smoothingTimeConstant = sonogramSmoothing;\n\n        var bufferLoader = new BufferLoader(\n            audioContext, ['audio/voice.mp3'], function (bufferList) {\n\n                audioSources[0] = audioContext.createBufferSource();\n                audioSources[0].buffer = bufferList[0];\n                audioSources[0].loop = true;\n                audioSources[0].connect(pitchShifterProcessor);\n                audioSources[0].start(0);\n            }\n        );\n\n        bufferLoader.load();\n    };\n\n    initProcessor = function () {\n\n        if (pitchShifterProcessor) {\n            pitchShifterProcessor.disconnect();\n        }\n\n        if (audioContext.createScriptProcessor) {\n            pitchShifterProcessor = audioContext.createScriptProcessor(grainSize, 1, 1);\n        } else if (audioContext.createJavaScriptNode) {\n            pitchShifterProcessor = audioContext.createJavaScriptNode(grainSize, 1, 1);\n        }\n\n        pitchShifterProcessor.buffer = new Float32Array(grainSize * 2);\n        pitchShifterProcessor.grainWindow = hannWindow(grainSize);\n        pitchShifterProcessor.onaudioprocess = function (event) {\n\n            var inputData = event.inputBuffer.getChannelData(0);\n            var outputData = event.outputBuffer.getChannelData(0);\n\n            for (i = 0; i < inputData.length; i++) {\n\n                // Apply the window to the input buffer\n                inputData[i] *= this.grainWindow[i];\n\n                // Shift half of the buffer\n                this.buffer[i] = this.buffer[i + grainSize];\n\n                // Empty the buffer tail\n                this.buffer[i + grainSize] = 0.0;\n            }\n\n            // Calculate the pitch shifted grain re-sampling and looping the input\n            var grainData = new Float32Array(grainSize * 2);\n            for (var i = 0, j = 0.0;\n                 i < grainSize;\n                 i++, j += pitchRatio) {\n\n                var index = Math.floor(j) % grainSize;\n                var a = inputData[index];\n                var b = inputData[(index + 1) % grainSize];\n                grainData[i] += linearInterpolation(a, b, j % 1.0) * this.grainWindow[i];\n            }\n\n            // Copy the grain multiple times overlapping it\n            for (i = 0; i < grainSize; i += Math.round(grainSize * (1 - overlapRatio))) {\n                for (j = 0; j <= grainSize; j++) {\n                    this.buffer[i + j] += grainData[j];\n                }\n            }\n\n            // Output the first half of the buffer\n            for (i = 0; i < grainSize; i++) {\n                outputData[i] = this.buffer[i];\n            }\n        };\n\n        pitchShifterProcessor.connect(spectrumAudioAnalyser);\n        pitchShifterProcessor.connect(sonogramAudioAnalyser);\n        pitchShifterProcessor.connect(audioContext.destination);\n    };\n\n    initSliders = function () {\n\n        $(\"#pitchRatioSlider\").slider({\n            orientation: \"horizontal\",\n            min: 0.5,\n            max: 2,\n            step: 0.01,\n            range: 'min',\n            value: pitchRatio,\n            slide: function (event, ui) {\n\n                pitchRatio = ui.value;\n                $(\"#pitchRatioDisplay\").text(pitchRatio);\n            }\n        });\n\n        $(\"#overlapRatioSlider\").slider({\n            orientation: \"horizontal\",\n            min: 0,\n            max: 0.75,\n            step: 0.01,\n            range: 'min',\n            value: overlapRatio,\n            slide: function (event, ui) {\n\n                overlapRatio = ui.value;\n                $(\"#overlapRatioDisplay\").text(overlapRatio);\n            }\n        });\n\n        $(\"#grainSizeSlider\").slider({\n            orientation: \"horizontal\",\n            min: 0,\n            max: validGranSizes.length - 1,\n            step: 1,\n            range: 'min',\n            value: validGranSizes.indexOf(grainSize),\n            slide: function (event, ui) {\n\n                grainSize = validGranSizes[ui.value];\n                $(\"#grainSizeDisplay\").text(grainSize);\n\n                initProcessor();\n\n                if (audioSources[audioSourceIndex]) {\n                    audioSources[audioSourceIndex].connect(pitchShifterProcessor);\n                }\n            }\n        });\n\n        $(\"#audioVisualisationSlider\").slider({\n            orientation: \"horizontal\",\n            min: 0,\n            max: audioVisualisationNames.length - 1,\n            step: 1,\n            value: audioVisualisationIndex,\n            slide: function (event, ui) {\n\n                audioVisualisationIndex = ui.value;\n                $(\"#audioVisualisationDisplay\").text(audioVisualisationNames[audioVisualisationIndex]);\n            }\n        });\n\n        $(\"#audioSourceSlider\").slider({\n            orientation: \"horizontal\",\n            min: 0,\n            max: audioSourcesNames.length - 1,\n            step: 1,\n            value: audioSourceIndex,\n            slide: function (event, ui) {\n\n                if (audioSources[audioSourceIndex]) {\n                    audioSources[audioSourceIndex].disconnect();\n                }\n\n                audioSourceIndex = ui.value;\n                $(\"#audioSourceDisplay\").text(audioSourcesNames[audioSourceIndex]);\n\n                if (audioSources[audioSourceIndex]) {\n                    audioSources[audioSourceIndex].connect(pitchShifterProcessor);\n                }\n            }\n        });\n\n        $(\"#pitchRatioDisplay\").text(pitchRatio);\n        $(\"#overlapRatioDisplay\").text(overlapRatio);\n        $(\"#grainSizeDisplay\").text(grainSize);\n        $(\"#audioVisualisationDisplay\").text(audioVisualisationNames[audioVisualisationIndex]);\n        $(\"#audioSourceDisplay\").text(audioSourcesNames[audioSourceIndex]);\n    };\n\n    initCanvas = function () {\n\n        canvas = document.querySelector('canvas');\n        canvasContext = canvas.getContext('2d');\n\n        barGradient = canvasContext.createLinearGradient(0, 0, 1, canvas.height - 1);\n        barGradient.addColorStop(0, '#550000');\n        barGradient.addColorStop(0.995, '#AA5555');\n        barGradient.addColorStop(1, '#555555');\n\n        waveGradient = canvasContext.createLinearGradient(canvas.width - 2, 0, canvas.width - 1, canvas.height - 1);\n        waveGradient.addColorStop(0, '#FFFFFF');\n        waveGradient.addColorStop(0.75, '#550000');\n        waveGradient.addColorStop(0.75, '#555555');\n        waveGradient.addColorStop(0.76, '#AA5555');\n        waveGradient.addColorStop(1, '#FFFFFF');\n    };\n\n    renderCanvas = function () {\n\n        switch (audioVisualisationIndex) {\n\n            case 0:\n\n                var frequencyData = new Uint8Array(spectrumAudioAnalyser.frequencyBinCount);\n                spectrumAudioAnalyser.getByteFrequencyData(frequencyData);\n\n                canvasContext.clearRect(0, 0, canvas.width, canvas.height);\n                canvasContext.fillStyle = barGradient;\n\n                var barWidth = canvas.width / frequencyData.length;\n                for (i = 0; i < frequencyData.length; i++) {\n                    var magnitude = frequencyData[i];\n                    canvasContext.fillRect(barWidth * i, canvas.height, barWidth - 1, -magnitude - 1);\n                }\n\n                break;\n\n            case 1:\n\n                var timeData = new Uint8Array(spectrumAudioAnalyser.frequencyBinCount);\n                spectrumAudioAnalyser.getByteTimeDomainData(timeData);\n                var amplitude = 0.0;\n                for (i = 0; i < timeData.length; i++) {\n                    amplitude += timeData[i];\n                }\n                amplitude = Math.abs(amplitude / timeData.length - 128) * 5 + 1;\n\n                var previousImage = canvasContext.getImageData(1, 0, canvas.width - 1, canvas.height);\n                canvasContext.putImageData(previousImage, 0, 0);\n\n                var axisY = canvas.height * 3 / 4;\n                canvasContext.fillStyle = '#FFFFFF';\n                canvasContext.fillRect(canvas.width - 1, 0, 1, canvas.height);\n                canvasContext.fillStyle = waveGradient;\n                canvasContext.fillRect(canvas.width - 1, axisY, 1, -amplitude);\n                canvasContext.fillRect(canvas.width - 1, axisY, 1, amplitude / 2);\n\n                break;\n\n            case 2:\n\n                frequencyData = new Uint8Array(sonogramAudioAnalyser.frequencyBinCount);\n                sonogramAudioAnalyser.getByteFrequencyData(frequencyData);\n\n                previousImage = canvasContext.getImageData(1, 0, canvas.width - 1, canvas.height);\n                canvasContext.putImageData(previousImage, 0, 0);\n\n                var bandHeight = canvas.height / frequencyData.length;\n                for (var i = 0, y = canvas.height - 1;\n                     i < frequencyData.length;\n                     i++, y -= bandHeight) {\n\n                    var color = frequencyData[i] << 16;\n                    canvasContext.fillStyle = '#' + color.toString(16);\n                    canvasContext.fillRect(canvas.width - 1, y, 1, -bandHeight);\n                }\n\n                break;\n        }\n\n        window.requestAnimFrame(renderCanvas);\n    };\n\n    return {\n\n        init: function () {\n\n            if ('AudioContext' in window) {\n                audioContext = new AudioContext();\n            } else {\n                alert('Your browser does not support the Web Audio API');\n                return;\n            }\n\n            initAudio();\n            initProcessor();\n            initSliders();\n            initCanvas();\n\n            window.requestAnimFrame(renderCanvas);\n        }\n    }\n\n}());\n\nwindow.requestAnimFrame = (function () {\n\n    return (window.requestAnimationFrame ||\n            window.webkitRequestAnimationFrame ||\n            window.mozRequestAnimationFrame ||\n            function (callback) {\n                window.setTimeout(callback, 1000 / 60);\n            });\n})();\n\nwindow.addEventListener(\"DOMContentLoaded\", pitchShifter.init, true);\n"
  }
]