Snippets

markdiary 楽器.me 移調スライダー

Created by markdiary last modified
// ==UserScript==
// @name        楽器me移調スライダー
// @namespace   labs.markdiary.com
// @include     http://gakufu.gakki.me/m/*
// @version     1
// @grant       none
// ==/UserScript==

(function () {
  var chords = [
    'C',
    'D♭',
    'D',
    'E♭',
    'E',
    'F',
    'F#',
    'G',
    'A♭',
    'A',
    'B♭',
    'B'
  ];
  var chord_sharp = [
    'C',
    'C#',
    'D',
    'D#',
    'E',
    'F',
    'F#',
    'G',
    'G#',
    'A',
    'A#',
    'B'
  ];
  var chord_flat = [
    'C',
    'D♭',
    'D',
    'E♭',
    'E',
    'F',
    'G♭',
    'G',
    'A♭',
    'A',
    'B♭',
    'B'
  ];
  var keyChanger = document.getElementById('chg_key');
  if (!keyChanger) return;
  var sliderUnit = document.createElement('input');
  sliderUnit.id = 'chg_chord_slider';
  sliderUnit.type = 'range';
  sliderUnit.setAttribute('min', '-6');
  sliderUnit.setAttribute('max', '5');
  sliderUnit.setAttribute('step', '1');
  sliderUnit.value = '0';
  keyChanger.appendChild(sliderUnit);
  var showValue = document.createElement('input');
  showValue.id = 'sliderValue';
  showValue.type = 'text';
  showValue.size = '2';
  showValue.value = '0';
  keyChanger.appendChild(showValue);
  setTimeout(function () {
    var elm = document.getElementById('chg_chord_slider');
    elm.addEventListener('change', function (e) {
      var newValue = e.target.value;
      var val = document.getElementById('sliderValue');
      val.value = newValue;
      transepose(Number(newValue));
    }, false);
  }, 300);
  function transepose(offset) {
    var meChord = document.querySelectorAll('a[onclick] u.blue');
    var f = Array.prototype;
    f.forEach.call(meChord, function (chord) {
      var chordTemp = chord.firstChild.nodeValue;
      var baseKey = chordTemp.match(/([CDEFGAB]{1}[♭#]?)/g);
      for (var i = 0; i < baseKey.length; i++) {
 //       console.log(baseKey + ' : ' + baseKey[i] + '=>' + trans_(baseKey[i], offset));
        chord.firstChild.nodeValue = chord.firstChild.nodeValue.replace(baseKey[i], trans_(baseKey[i], offset));
      }
    });
  }
  // TRANSPOSE CHORD

  function trans_(baseKey, offset) {
    var res = '';
    var index = get_index(baseKey);
    res += baseKey.replace(baseKey, chords[get_key(index + Number(offset))]);
    return res;
  }
  function get_index(key) {
    var res;
    if (key.indexOf('#') > 0) {
      res = chord_sharp.indexOf(key);
    } else if (key.indexOf('♭') > 0) {
      res = chord_flat.indexOf(key);
    } else {
      res = chords.indexOf(key);
    }
    return res;
  }
  function get_key(key) {
    if (key < 0) {
      key = key + 12;
    } else if (key > 11) {
      key = key - 12;
    }
    return key;
  }
})();

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.