グライド (AudioParam)

WAVE TYPE
GLIDE TYPE
(function() {

    var onDOMContentLoaded = function() {

        window.AudioContext = window.AudioContext || window.webkitAudioContext;

        try {
            // Create the instance of AudioContext
            var context = new AudioContext();
        } catch (error) {
            window.alert(error.message + ' : Please use Chrome or Safari.');
            return;
        }

        // Create the instance of OscillatorNode
        var oscillator = context.createOscillator();

        // Parameters for the instance of OscillatorNode
        var type      = oscillator.type;
        var detune    = oscillator.detune.value;

        // for legacy browsers
        context.createGain = context.createGain || context.createGainNode;

        // Create the instance of GainNode
        var gain = context.createGain();

        // Flag for starting or stopping sound
        var isStop = true;

        // Parameters for Glide
        var startFrequency = -1;  // Abnormal value
        var time           = document.getElementById('range-glide').valueAsNumber;
        var glideType      = document.getElementById('form-glide-type').elements['radio-glide-type'][0].value;

        /*
         * Event Listener
         */

        // Start or Stop sound
        var PIANO_88S = [
                        'A-4', 'A-4h', 'B-4',
                        'C-3', 'C-3h', 'D-3', 'D-3h', 'E-3', 'F-3', 'F-3h', 'G-3', 'G-3h', 'A-3', 'A-3h', 'B-3',
                        'C-2', 'C-2h', 'D-2', 'D-2h', 'E-2', 'F-2', 'F-2h', 'G-2', 'G-2h', 'A-2', 'A-2h', 'B-2',
                        'C-1', 'C-1h', 'D-1', 'D-1h', 'E-1', 'F-1', 'F-1h', 'G-1', 'G-1h', 'A-1', 'A-1h', 'B-1',
                        'C',   'Ch',   'D',   'Dh',   'E',   'F',   'Fh',   'G',   'Gh',   'A',   'Ah',   'B',
                        'C1',  'C1h',  'D1',  'D1h',  'E1',  'F1',  'F1h',  'G1',  'G1h',  'A1',  'A1h',  'B1',
                        'C2',  'C2h',  'D2',  'D2h',  'E2',  'F2',  'F2h',  'G2',  'G2h',  'A2',  'A2h',  'B2',
                        'C3',  'C3h',  'D3',  'D3h',  'E3',  'F3',  'F3h',  'G3',  'G3h',  'A3',  'A3h',  'B3',
                        'C4'
                        ];

        var pianoKeys = Array.prototype.slice.call(document.querySelectorAll('#piano ul li'), 0);

        var convertIndex = function(index) {
            //
            // The 12 equal temparement
            //
            // Min -> A 27.5Hz, Max -> C 4186 Hz
            //
            // Example :
            //    A * 1.059463 -> A# (half up)
            //
            var FREQUENCY_RATIO = Math.pow(2, (1 / 12));  // about 1.059463;
            var MIN_A           = 27.5;

            return (MIN_A * Math.pow(FREQUENCY_RATIO, index));
        };

        pianoKeys.forEach(function(element, index, array) {
            // Start sound
            element.addEventListener(EventWrapper.START, function(event) {
                if (!isStop) {
                    oscillator.stop(0);
                }

                var pianoIndex   = PIANO_88S.indexOf(this.id);
                var endFrequency = convertIndex(pianoIndex);

                // Create the instance of OscillatorNode
                oscillator = context.createOscillator();

                // for legacy browsers
                oscillator.start = oscillator.start || oscillator.noteOn;
                oscillator.stop  = oscillator.stop  || oscillator.noteOff;

                // OscillatorNode (Input) -> GainNode (Volume) -> AudioDestinationNode (Output)
                oscillator.connect(gain);
                gain.connect(context.destination);

                // Set parameters
                oscillator.type         = type;
                oscillator.detune.value = detune;

                // for scheduling
                var t0 = context.currentTime;
                var t1 = t0 + time;

                // Scheduling for Glide
                oscillator.frequency.cancelScheduledValues(t0);

                // The 1st sound ?
                if (startFrequency === -1) {
                    oscillator.frequency.setValueAtTime(endFrequency, t0);
                } else {
                    oscillator.frequency.setValueAtTime(startFrequency, t0);
                }

                switch (glideType) {
                    case 'linear' :
                        oscillator.frequency.linearRampToValueAtTime(endFrequency, t1);
                        break;
                    case 'exponential' :
                        oscillator.frequency.exponentialRampToValueAtTime(endFrequency, t1);
                        break;
                    default :
                        break;
                }

                // Start sound at t0
                oscillator.start(t0);

                // Update
                startFrequency = endFrequency;

                isStop = false;
                this.classList.remove('key-off');
                this.classList.add('key-on');
            }, false);

            // Stop sound
            element.addEventListener(EventWrapper.END, function() {
                if (isStop) {
                    return;
                }

                oscillator.stop(0);

                isStop = true;
                this.classList.remove('key-on');
                this.classList.add('key-off');
            }, false);
        });

        // Control Volume
        document.getElementById('range-volume').addEventListener('input', function() {
            var min = gain.gain.minValue || 0;
            var max = gain.gain.maxValue || 1;

            if ((this.valueAsNumber >= min) && (this.valueAsNumber <= max)) {
                gain.gain.value = this.valueAsNumber;
                document.getElementById('output-volume').textContent = this.value;
            }
        }, false);

        // Select type
        document.getElementById('form-wave-type').addEventListener('change', function() {
            for (var i = 0, len = this.elements['radio-wave-type'].length; i < len; i++) {
                if (this.elements['radio-wave-type'][i].checked) {
                    oscillator.type = type = (typeof oscillator.type === 'string') ? this.elements['radio-wave-type'][i].value : i;
                    break;
                }
            }
        }, false);

        // Control detune
        document.getElementById('range-detune').addEventListener('input', function() {
            var min = oscillator.detune.minValue || -4800;
            var max = oscillator.detune.maxValue ||  4800;

            if ((this.valueAsNumber >= min) && (this.valueAsNumber <= max)) {
                oscillator.detune.value = detune = this.valueAsNumber;
                document.getElementById('output-detune').textContent = this.value;
            }
        }, false);

        // Control Glide Time
        document.getElementById('range-glide').addEventListener('input', function() {
            if (this.valueAsNumber >= 0) {
                time = this.valueAsNumber / 5;
                document.getElementById('output-glide').textContent = this.value;
            }
        }, false);
    };

    if ((document.readyState === 'interactive') || (document.readyState === 'complete')) {
        onDOMContentLoaded();
    } else {
        document.addEventListener('DOMContentLoaded', onDOMContentLoaded, true);
    }

})();
function EventWrapper(){
}

(function(){
    var click = '';
    var start = '';
    var move  = '';
    var end   = '';

    // Touch Panel ?
    if (/iPhone|iPad|iPod|Android/.test(navigator.userAgent)) {
        click = 'click';
        start = 'touchstart';
        move  = 'touchmove';
        end   = 'touchend';
    } else {
        click = 'click';
        start = 'mousedown';
        move  = 'mousemove';
        end   = 'mouseup';
    }

    EventWrapper.CLICK = click;
    EventWrapper.START = start;
    EventWrapper.MOVE  = move;
    EventWrapper.END   = end;
})();