如何通过了解歌曲的和弦顺序以编程方式找到歌曲的键? 我问过一些人他们如何确定一首歌的关键,他们都说他们这样做是靠耳朵'或者通过“试错法”#39;通过判断一个和弦是否能够解决一首歌...对于一般情况下可能很好的音乐家来说,但作为一个程序员,我找不到答案。
所以我开始寻找音乐相关的库,看看是否还有其他人为此编写过算法。但是,虽然我发现了一个名为“音调”的大型图书馆。在GitHub上:https://danigb.github.io/tonal/api/index.html我无法找到一个接受一组和弦并返回键的方法。
我选择的语言是JavaScript(NodeJs),但我不一定要寻找JavaScript答案。伪代码或可以在没有太多麻烦的情况下翻译成代码的解释就完全没问题了。
正如你们有些人提到的那样,歌曲中的键可以改变。我不确定是否可以足够可靠地检测到密钥更改。所以,现在我们只是说,我正在寻找一种能够很好地逼近给定和弦序列的算法的算法。
...
在查看五度音程后,我想我找到了一种模式,可以找到属于每个键的所有和弦。我为此写了一个函数getChordsFromKey(key)
。通过针对每个键检查和弦序列的和弦,我可以创建一个数组,其中包含键与给定和弦序列匹配的可能性概率:calculateKeyProbabilities(chordSequence)
。然后我添加了另一个函数estimateKey(chordSequence)
,它获取具有最高概率得分的键,然后检查和弦序列的最后一个和弦是否是其中之一。如果是这种情况,则返回仅包含该和弦的数组,否则返回具有最高概率得分的所有和弦的数组。
这样做很好,但它仍然没有为很多歌曲找到正确的键或者返回具有相同概率的多个键。主要问题是像A5, Asus2, A+, A°, A7sus4, Am7b5, Aadd9, Adim, C/G
等和弦不在五分之一圈内的和弦。事实上,例如,键C
包含与键Am
完全相同的和弦,G
与Em
相同,等等...
这是我的代码:
'use strict'
const normalizeMap = {
"Cb":"B", "Db":"C#", "Eb":"D#", "Fb":"E", "Gb":"F#", "Ab":"G#", "Bb":"A#", "E#":"F", "B#":"C",
"Cbm":"Bm","Dbm":"C#m","Eb":"D#m","Fbm":"Em","Gb":"F#m","Ab":"G#m","Bbm":"A#m","E#m":"Fm","B#m":"Cm"
}
const circleOfFifths = {
majors: ['C', 'G', 'D', 'A', 'E', 'B', 'F#', 'C#', 'G#','D#','A#','F'],
minors: ['Am','Em','Bm','F#m','C#m','G#m','D#m','A#m','Fm','Cm','Gm','Dm']
}
function estimateKey(chordSequence) {
let keyProbabilities = calculateKeyProbabilities(chordSequence)
let maxProbability = Math.max(...Object.keys(keyProbabilities).map(k=>keyProbabilities[k]))
let mostLikelyKeys = Object.keys(keyProbabilities).filter(k=>keyProbabilities[k]===maxProbability)
let lastChord = chordSequence[chordSequence.length-1]
if (mostLikelyKeys.includes(lastChord))
mostLikelyKeys = [lastChord]
return mostLikelyKeys
}
function calculateKeyProbabilities(chordSequence) {
const usedChords = [ ...new Set(chordSequence) ] // filter out duplicates
let keyProbabilities = []
const keyList = circleOfFifths.majors.concat(circleOfFifths.minors)
keyList.forEach(key=>{
const chords = getChordsFromKey(key)
let matchCount = 0
//usedChords.forEach(usedChord=>{
// if (chords.includes(usedChord))
// matchCount++
//})
chords.forEach(chord=>{
if (usedChords.includes(chord))
matchCount++
})
keyProbabilities[key] = matchCount / usedChords.length
})
return keyProbabilities
}
function getChordsFromKey(key) {
key = normalizeMap[key] || key
const keyPos = circleOfFifths.majors.includes(key) ? circleOfFifths.majors.indexOf(key) : circleOfFifths.minors.indexOf(key)
let chordPositions = [keyPos, keyPos-1, keyPos+1]
// since it's the CIRCLE of fifths we have to remap the positions if they are outside of the array
chordPositions = chordPositions.map(pos=>{
if (pos > 11)
return pos-12
else if (pos < 0)
return pos+12
else
return pos
})
let chords = []
chordPositions.forEach(pos=>{
chords.push(circleOfFifths.majors[pos])
chords.push(circleOfFifths.minors[pos])
})
return chords
}
// TEST
//console.log(getChordsFromKey('C'))
const chordSequence = ['Em','G','D','C','Em','G','D','Am','Em','G','D','C','Am','Bm','C','Am','Bm','C','Em','C','D','Em','Em','C','D','Em','Em','C','D','Em','Em','C','D','Am','Am','Em','C','D','Em','Em','C','D','Em','Em','C','D','Em','Em','C','D','Em','Em','C','D','Em','Em','C','D','Em','Em','C','D','Em','Em','C','D','Em']
const key = estimateKey(chordSequence)
console.log('Example chord sequence:',JSON.stringify(chordSequence))
console.log('Estimated key:',JSON.stringify(key)) // Output: [ 'Em' ]
&#13;
答案 0 :(得分:12)
特定键的歌曲中的和弦主要是 键的成员。我想通过将所列出的和弦中的主要偶然事件与键的关键签名进行比较,你可以在统计上得到一个很好的近似值(如果有足够的数据)。
请参阅https://en.wikipedia.org/wiki/Circle_of_fifths
当然,任何按键中的一首歌都可能/将会出现意外而不是按键音阶,所以很可能是一个统计近似值。但是在几个条形图中,如果你将偶然事件加起来并过滤掉除了最常出现的事件之外的所有事件,你可以匹配一个关键签名。
附录:正如Jonas w正确指出的那样,你可以获得签名,但你不太可能确定它是主键还是小键。
答案 1 :(得分:10)
这是我想出的。现代JS仍然是新的,所以对于凌乱和使用map()的不好道歉。
我环顾了色调库的内部,它有一个函数scales.detect(),但它并不好,因为它需要每个音符存在。相反,我用它作为灵感,并将进展扁平化为一个简单的音符列表,并在所有转置中将其作为所有可能音阶的子集进行检查。
const _ = require('lodash');
const chord = require('tonal-chord');
const note = require('tonal-note');
const pcset = require('tonal-pcset');
const dictionary = require('tonal-dictionary');
const SCALES = require('tonal-scale/scales.json');
const dict = dictionary.dictionary(SCALES, function (str) { return str.split(' '); });
//dict is a dictionary of scales defined as intervals
//notes is a string of tonal notes eg 'c d eb'
//onlyMajorMinor if true restricts to the most common scales as the tonal dict has many rare ones
function keyDetect(dict, notes, onlyMajorMinor) {
//create an array of pairs of chromas (see tonal docs) and scale names
var chromaArray = dict.keys(false).map(function(e) { return [pcset.chroma(dict.get(e)), e]; });
//filter only Major/Minor if requested
if (onlyMajorMinor) { chromaArray = chromaArray.filter(function (e) { return e[1] === 'major' || e[1] === 'harmonic minor'; }); }
//sets is an array of pitch classes transposed into every possibility with equivalent intervals
var sets = pcset.modes(notes, false);
//this block, for each scale, checks if any of 'sets' is a subset of any scale
return chromaArray.reduce(function(acc, keyChroma) {
sets.map(function(set, i) {
if (pcset.isSubset(keyChroma[0], set)) {
//the midi bit is a bit of a hack, i couldnt find how to turn an int from 0-11 into the repective note name. so i used the midi number where 60 is middle c
//since the index corresponds to the transposition from 0-11 where c=0, it gives the tonic note of the key
acc.push(note.pc(note.fromMidi(60+i)) + ' ' + keyChroma[1]);
}
});
return acc;
}, []);
}
const p1 = [ chord.get('m','Bb'), chord.get('m', 'C'), chord.get('M', 'Eb') ];
const p2 = [ chord.get('M','F#'), chord.get('dim', 'B#'), chord.get('M', 'G#') ];
const p3 = [ chord.get('M','C'), chord.get('M','F') ];
const progressions = [ p1, p2, p3 ];
//turn the progression into a flat string of notes seperated by spaces
const notes = progressions.map(function(e) { return _.chain(e).flatten().uniq().value(); });
const possibleKeys = notes.map(function(e) { return keyDetect(dict, e, true); });
console.log(possibleKeys);
//[ [ 'Ab major' ], [ 'Db major' ], [ 'C major', 'F major' ] ]
一些缺点:
- 没有给你想要的和谐音符。在p2中,更正确的响应是C#major,但这可以通过以某种方式检查原始进程来修复
- 不会处理&#39;装饰品&#39;流行歌曲中可能出现的和弦之外的和弦,例如。 CMaj7 FMaj7 GMaj7而不是C F G.不确定这是多么常见,我认为不是太多。
答案 2 :(得分:8)
给出一系列这样的音调:
var tones = ["G","Fis","D"];
我们首先可以生成一组独特的音调:
tones = [...new Set(tones)];
然后我们可以检查#和bs的外观:
var sharps = ["C","G","D","A","E","H","Fis"][["Fis","Cis","Gis","Dis","Ais","Eis"].filter(tone=>tones.includes(tone)).length];
然后对bs执行相同操作并获得结果:
var key = sharps === "C" ? bs:sharps;
但是,您仍然不知道它的主要或次要,并且许多组件不关心上层规则(并且更改了中间的键)... < / p>
答案 3 :(得分:7)
对于每个支持&#34;您可能也能够使用键保持结构。 scale,其值为一个与该音阶匹配的和弦数组。
鉴于和弦进行,您可以根据您的结构制作一个键列表。
通过多次匹配,您可以尝试做出有根据的猜测。例如,添加其他&#34; weight&#34;任何与根音符相匹配的音阶。
答案 4 :(得分:5)
您可以使用螺旋阵列,这是一种由Elaine Chew创建的色调3D模型,它具有关键检测算法。
Chuan,Ching-Hua和Elaine Chew。 “Polyphonic audio key finding using the spiral array CEG algorithm”。多媒体与博览会,2005年.ICME 2005. IEEE国际会议。 IEEE,2005。
我最近的张力模型(.jar file here中提供)也可以输出基于螺旋阵列的关键(除了张力测量)。它可以将musicXML文件或文本文件作为输入,只需获取作品中每个“时间窗口”的音高名称列表。
Herremans D.,Chew E .. 2016. Tension ribbons: Quantifying and visualising tonal tension。第二届音乐符号和代表技术国际会议(TENOR)。 2:8-18
答案 5 :(得分:4)
一种方法是找到正在播放的所有音符,并与不同音阶的签名进行比较,看看哪个是最佳匹配。
通常,比例签名非常独特。自然小音阶将具有与主音阶相同的音符(对于所有模式都是如此),但通常当我们说小音阶时,我们指的是具有特定特征的谐波小音阶。
因此,将和弦中的音符与不同音阶进行比较应该会给你一个很好的估计。你可以通过在不同的音符上添加一些重量来改进(例如最多出现的音符,或者第一个和最后一个和弦,每个和弦的音调等)。
这似乎能够准确地处理大多数基本情况:
'use strict'
const allnotes = [
"C", "C#", "D", "Eb", "E", "F", "F#", "G", "Ab", "A", "Bb", "B"
]
// you define the scales you want to validate for, with name and intervals
const scales = [{
name: 'major',
int: [2, 4, 5, 7, 9, 11]
}, {
name: 'minor',
int: [2, 3, 5, 7, 8, 11]
}];
// you define which chord you accept. This is easily extensible,
// only limitation is you need to have a unique regexp, so
// there's not confusion.
const chordsDef = {
major: {
intervals: [4, 7],
reg: /^[A-G]$|[A-G](?=[#b])/
},
minor: {
intervals: [3, 7],
reg: /^[A-G][#b]?[m]/
},
dom7: {
intervals: [4, 7, 10],
reg: /^[A-G][#b]?[7]/
}
}
var notesArray = [];
// just a helper function to handle looping all notes array
function convertIndex(index) {
return index < 12 ? index : index - 12;
}
// here you find the type of chord from your
// chord string, based on each regexp signature
function getNotesFromChords(chordString) {
var curChord, noteIndex;
for (let chord in chordsDef) {
if (chordsDef[chord].reg.test(chordString)) {
var chordType = chordsDef[chord];
break;
}
}
noteIndex = allnotes.indexOf(chordString.match(/^[A-G][#b]?/)[0]);
addNotesFromChord(notesArray, noteIndex, chordType)
}
// then you add the notes from the chord to your array
// this is based on the interval signature of each chord.
// By adding definitions to chordsDef, you can handle as
// many chords as you want, as long as they have a unique regexp signature
function addNotesFromChord(arr, noteIndex, chordType) {
if (notesArray.indexOf(allnotes[convertIndex(noteIndex)]) == -1) {
notesArray.push(allnotes[convertIndex(noteIndex)])
}
chordType.intervals.forEach(function(int) {
if (notesArray.indexOf(allnotes[noteIndex + int]) == -1) {
notesArray.push(allnotes[convertIndex(noteIndex + int)])
}
});
}
// once your array is populated you check each scale
// and match the notes in your array to each,
// giving scores depending on the number of matches.
// This one doesn't penalize for notes in the array that are
// not in the scale, this could maybe improve a bit.
// Also there's no weight, no a note appearing only once
// will have the same weight as a note that is recurrent.
// This could easily be tweaked to get more accuracy.
function compareScalesAndNotes(notesArray) {
var bestGuess = [{
score: 0
}];
allnotes.forEach(function(note, i) {
scales.forEach(function(scale) {
var score = 0;
score += notesArray.indexOf(note) != -1 ? 1 : 0;
scale.int.forEach(function(noteInt) {
// console.log(allnotes[convertIndex(noteInt + i)], scale)
score += notesArray.indexOf(allnotes[convertIndex(noteInt + i)]) != -1 ? 1 : 0;
});
// you always keep the highest score (or scores)
if (bestGuess[0].score < score) {
bestGuess = [{
score: score,
key: note,
type: scale.name
}];
} else if (bestGuess[0].score == score) {
bestGuess.push({
score: score,
key: note,
type: scale.name
})
}
})
})
return bestGuess;
}
document.getElementById('showguess').addEventListener('click', function(e) {
notesArray = [];
var chords = document.getElementById('chodseq').value.replace(/ /g,'').replace(/["']/g,'').split(',');
chords.forEach(function(chord) {
getNotesFromChords(chord)
});
var guesses = compareScalesAndNotes(notesArray);
var alertText = "Probable key is:";
guesses.forEach(function(guess, i) {
alertText += (i > 0 ? " or " : " ") + guess.key + ' ' + guess.type;
});
alert(alertText)
})
<input type="text" id="chodseq" />
<button id="showguess">
Click to guess the key
</button>
对于你的例子,它给出G大调,那是因为有一个和声小音阶,没有D大调或Bm和弦。
您可以尝试简单的:C,F,G或Eb,Fm,Gm
或有些事故:C,D7,G7(这个会给你2个猜测,因为有一个真正的模糊,没有提供更多的信息,它可能是两者)
一个事故但准确的:C,Dm,G,A
答案 6 :(得分:1)
如果您不反对切换语言,则Python中的music21(我的库,免责声明)会这样做:
from music21 import stream, harmony
chordSymbols = ['Cm', 'Dsus2', 'E-/C', 'G7', 'Fm', 'Cm']
s = stream.Stream()
for cs in chordSymbols:
s.append(harmony.ChordSymbol(cs))
s.analyze('key')
返回:<music21.key.Key of c minor>
系统将知道C#专业版和Db专业版之间的区别。它具有完整的和弦名称词汇,因此“ Dsus2”之类的内容不会引起混淆。唯一可能会吸引新人的是,公寓的单位都标有减号,所以用“ E- / C”代替“ Eb / C”
答案 7 :(得分:0)
有一个在线免费工具(MazMazika Songs Chord Analyzer),可以非常快速地分析和检测任何歌曲的和弦。您可以通过文件上传 (MP3/WAV) 或粘贴 YouTube / SoundCloud 链接来处理歌曲。处理完文件后,您可以一边播放一边实时看到所有和弦在播放,还有一个包含所有和弦的表格,每个和弦都分配了一个时间位置和一个编号ID,您可以点击直接转到相应的和弦和它的时间位置。