如何使用JavaScript转置音乐和弦? [英] How do I transpose music chords using JavaScript?

查看:136
本文介绍了如何使用JavaScript转置音乐和弦?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何创建一个用于移调音乐和弦的javascript函数。

I was wondering how would one create a javascript function for transposing music chords.

由于我不希望每个人都成为这里的音乐家,我会尝试解释它在音乐理论中的作用。我希望我不要忘记一些事情。如果是的话,音乐家请更正我。

Since I don't expect everyone to be a musician here, I'll try to explain how it works in music theory. I hope I don't forget something. If yes, musicians, please, correct me.

1)简单的和弦

简单的和弦几乎是就像字母一样简单,它是这样的:

The simple chords are almost as simple as an alphabet and it goes like this:


C,C#,D,D#,E,F,F#,G,G# ,A,A#B

C, C#, D, D#, E, F, F#, G, G#, A, A# B

从B再次循环到C.因此,如果原始和弦是 E 我们想要转置+1,结果和弦是 F 。如果我们转置+4,结果和弦是 G#

From B it loops all over again to C. Therefore, If the original chord is E and we want to transpose +1, the resulting chord is F. If we transpose +4, the resulting chord is G#.

2)扩展和弦

它们几乎像简单的和弦一样工作,但包含更多的字符,在转置时可以安全地忽略它们。例如:

They work almost like the simple chords, but contain a few more characters, which can safely be ignored when transposing. For example:


Cmi,C#7,Dsus7,Emi,Fsus4,F#mi,G ...

Cmi, C#7, Dsus7, Emi, Fsus4, F#mi, G ...

再次,与简单的和弦一样,如果我们转置 Dsus7 + 3 = Fsus7

So again, as with the simple chords, if we transpose Dsus7 + 3 = Fsus7

3)非根低音音调

问题当低音演奏与和弦根音不同的音调时出现。这是以和弦后的斜线为标记,也需要调换。示例:

A problem arises when the bass plays a different tone than the chord root tone. This is marked by a slash after the chord and also needs to be transposed. Examples:


C / G,Dmi / A,F#sus7 / A#

C/G, Dmi/A, F#sus7/A#

与示例1和2一样,一切都是相同的,但斜杠后面的部分也需要转置,因此:

As with examples 1 and 2, everything is the same, but the part after the slash needs transpose too, therefore:

C / G + 5 = F / C

F#sus7 / A# + 1 = Gsus7 / B

我认为这应该是全部,除非我忘记了什么。

I think this should be all, unless I forgot something.

所以基本上,假设你有一个名为 chord的javascript变量和转置值转置。什么代码会转换和弦?

So basically, imagine you have a javascript variable called chord and the transpose value transpose. What code would transpose the chord?

示例:

var chord = 'F#sus7/C#';
var transpose = 3; // remember this value also may be negative, like "-4"
... code here ...
var result; // expected result = 'Asus7/E';


推荐答案

有点像这样的东西:

function transposeChord(chord, amount) {
  var scale = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];
  return chord.replace(/[CDEFGAB]#?/g,
                       function(match) {
                         var i = (scale.indexOf(match) + amount) % scale.length;
                         return scale[ i < 0 ? i + scale.length : i ];
                       });
}

alert(transposeChord("Dm7/G", 2)); // gives "Em7/A"
alert(transposeChord("Fmaj9#11", -23)); // gives "F#maj9#11"

注意我扔了F#maj9# 11示例只是为了让您更多地考虑有关构成有效和弦名称的内容:您可能会发现一个不跟随字母的#尖锐符号(在这种情况下它属于11) 。

Note that I threw in the "F#maj9#11" example just to give you more to think about with regard to what makes up a valid chord name: you may find a "#" sharp symbol that doesn't follow a letter (in this case it belongs to the "11").

显然,我的函数只能理解锐利,而不理解平面,并且不理解键,所以,例如, transposeChord(C / E,1)当它真的应该是C#/ E#时会给出C#/ F。

And, obviously, my function only understands sharps, not flats, and doesn't understand keys, so, e.g., transposeChord("C/E", 1) will give "C#/F" when it really should be "C#/E#".

这篇关于如何使用JavaScript转置音乐和弦?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆