1

使用 Web MIDI 和Teoria.JS我正在尝试构建一个基于 Web 的和弦控制器。

借助 teoria-chord-progression ,我找到了一种为音阶生成和弦的方法,然后获取它的 midi 代码。现在我想获得同一个和弦转位的midi音符。

到目前为止,我所做的是从原始 midi 音符中减去 12,用于第一次转位的第五个,然后为第二个转位的第三个和第五个,但我确信有更好的方法。

编辑:这里是我的代码,它只播放非倒置形式的和弦:

'use strict';

const teoria = require('teoria');
const chordProgression = require('teoria-chord-progression');
const Combokeys = require("combokeys");

const document = global.document;

const cSharpHMinor = teoria.scale('c#', 'harmonicminor');
const chords = chordProgression(cSharpHMinor, [1, 2, 3, 4, 5, 6, 7], 3).chords;
const combokeys = new Combokeys(document.documentElement);


global.navigator.requestMIDIAccess()
    .then((midiAccess) => {
        return Array.from(midiAccess.outputs.values());
    })
    .then((midiOutputs)=> {
        chords.forEach((chord, index) => {
            buildPadForChord(index + 1, chord, midiOutputs);
        });
    });


function createPad(index, chordName, listener) {
    let button = document.createElement('button');
    button.setAttribute('type', 'button');
    button.textContent = `${chordName} (${index})`;
    button.addEventListener('click', listener);

    let autorepeat = false;
    combokeys.bind(index.toString(), () => {
        if (!autorepeat) {
            autorepeat = true;
            listener();
        }
    }, 'keydown');
    combokeys.bind(index.toString(), () => {
        autorepeat = false;
    }, 'keyup');
    document.documentElement.appendChild(button);
}

function buildPadForChord(index, chord, midiOutputs) {

    let listener = () => {
        midiOutputs.forEach((midiOutput) => {
            chord.notes().forEach((note)=> {
                midiOutput.send([0x90, note.midi(), 127]);
                midiOutput.send([0x80, note.midi(), 127], global.performance.now() + 1000.0);
            });
        });
    };

    createPad(index, chord.name, listener);
}

4

1 回答 1

0

根据git 上的这个问题,目前 Teoria.js 中没有实现反转。在某一时刻,开发人员正在考虑添加它们,但到目前为止还没有。

我在自己的代码中的实现是这样的:

// Adds an "invert" function to the Teoria Chord class
teoria.Chord.prototype.invert = function(n){
  // Grabs the current chord voicing. Returns array of intervals.
  var voicing = this.voicing()
  // Reverse the array to work top-down when n is negative
  if(n < 0){
    voicing = voicing.reverse()
  }

  // Loop through each inversion
  var j = Math.abs(n);
  for(let i = 0; i < j; i++){
    // Find which interval we're modifying this loop.
    let index = i % voicing.length;
    if(n > 0){
        // Move the lowest note up a perfect 8th
        voicing[index] = voicing[index].add(teoria.interval('P8'))
      }else{
      // Move the highest note down a perfect 8th
      voicing[index] = voicing[index].add(teoria.interval('P-8'))
    }
  }
  // Change the array into a usable format
  var newVoicing = arrayToSimple(voicing)
  this.voicing(newVoicing)

  // Return the object for chaining
  return this;
}

arrayToSimple 函数:

function arrayToSimple(array){
    var newArray = []
    for(let item of array){
        newArray.push(item.toString())
    }
    return newArray
}

现在,您可以调用Chord.invert(n)任何和弦,n您的转位号码在哪里。如果n为负数,则和弦向下倒转,如果为正数,则和弦向上倒转。

请注意,上面的 invert() 函数需要一个[lowest note, ..., highest note]按顺序发声的和弦。因此, callChord.invert(1).invert(1)与 call 不同Chord.invert(2)。如果您需要此功能,您可以使用Chord.bass()和/或检测哪些音符最低Chord.notes(),并重新排序元素。不过,这可能是矫枉过正。

于 2017-05-08T02:52:21.843 回答