Javascript 在Teoria.js中,转换和弦的标准方法是什么

Javascript 在Teoria.js中,转换和弦的标准方法是什么,javascript,music-notation,Javascript,Music Notation,使用Web MIDI,我正在尝试构建一个基于Web的和弦控制器 我找到了一种为音阶生成和弦的方法,这多亏了它,然后得到了它的midi代码。现在,我想为同一和弦的倒转得到midi音符 到目前为止,我所做的是从原始midi音符中减去12作为第一个倒装,然后是第三个倒装,第五个倒装,但我相信有更好的方法 编辑:这里是我的代码,它只以非倒转形式播放和弦: “严格使用”; const teoria=require('teoria'); const chordProgression=require('te

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

我找到了一种为音阶生成和弦的方法,这多亏了它,然后得到了它的midi代码。现在,我想为同一和弦的倒转得到midi音符

到目前为止,我所做的是从原始midi音符中减去12作为第一个倒装,然后是第三个倒装,第五个倒装,但我相信有更好的方法

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

“严格使用”;
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=和弦推进(cSharpHMinor,[1,2,3,4,5,6,7,3])。和弦;
const combokeys=新的combokeys(document.documentElement);
global.navigator.requestMIDIAccess()
。然后((MIDI访问)=>{
返回Array.from(midiAccess.outputs.values());
})
。然后((MIDI输出)=>{
和弦。forEach((和弦,索引)=>{
buildPadForChord(索引+1,和弦,MIDI输出);
});
});
函数createPad(索引、chordName、侦听器){
let button=document.createElement('button');
setAttribute('type','button');
button.textContent=`${chordName}(${index})`;
addEventListener(“单击”,侦听器);
让autorepeat=false;
combokeys.bind(index.toString(),()=>{
if(!autorepeat){
自动回复=真;
监听器();
}
}"键控",;
combokeys.bind(index.toString(),()=>{
自动回复=错误;
}"keyup",;
document.documentElement.appendChild(按钮);
}
函数buildPadForChord(索引、和弦、MIDI输出){
让侦听器=()=>{
midiOutput.forEach((midiOutput)=>{
chord.notes().forEach((note)=>{
发送([0x90,note.midi(),127]);
midiOutput.send([0x80,note.midi(),127],global.performance.now()+1000.0);
});
});
};
createPad(索引、chord.name、侦听器);
}
根据,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;
}
现在,您可以在任何和弦上调用
Chord.invert(n)
,其中
n
是您的倒装号码。如果
n
为负,和弦将向下倒转,而如果为正,和弦将向上倒转

请注意,上面的invert()函数需要一个和弦,其发声顺序为
[最低音符,…,最高音符]
。因此,调用
Chord.invert(1).invert(1)
将与调用
Chord.invert(2)
不同。如果您需要此功能,可以使用
Chord.bass()
和/或
Chord.notes()
检测哪些音符最低,并对元素重新排序。不过,这可能有点过头了。

根据,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;
}
现在,您可以在任何和弦上调用
Chord.invert(n)
,其中
n
是您的倒装号码。如果
n
为负,和弦将向下倒转,而如果为正,和弦将向上倒转


请注意,上面的invert()函数需要一个和弦,其发声顺序为
[最低音符,…,最高音符]
。因此,调用
Chord.invert(1).invert(1)
将与调用
Chord.invert(2)
不同。如果您需要此功能,可以使用
Chord.bass()
和/或
Chord.notes()
检测哪些音符最低,并对元素重新排序。不过,这可能太过分了。

你能给我们看看你所做工作的源代码吗?@OrtomalaLokni,我刚刚添加了代码段如果这是node.js,你应该添加node.jstag@chiliNUT它使用CommonJS语法作为包依赖项(并使用browserify构建它)但是它只能在浏览器中运行,因为它使用的是Web MIDI APICan。你可以向我们展示你所做工作的源代码吗?@OrtomalaLokni,我刚刚添加了代码段。如果这是node.js,你应该添加node.jstag@chiliNUT它使用CommonJS语法作为包依赖项(并使用browserify构建它)但它只能在浏览器中运行,因为它使用的是Web MIDI API