使用Web Audio API的音和弦检测算法

6
首先,我正在尝试实现这个和弦检测算法:http://www.music.mcgill.ca/~jason/mumt621/papers5/fujishima_1999.pdf 我最初实现了使用麦克风的算法,但它没有起作用。作为测试,我创建了三个振荡器来生成C和弦,但是该算法仍然无法工作。我认为我只应该看到C、E和G的更高数字,但我看到了所有音符的数字。我的算法实现有问题吗?还是我的N、fref或fs值有问题?
这是一段具有重要部分的代码片段:
// Set audio Context
window.AudioContext = window.AudioContext || window.webkitAudioContext;

var mediaStreamSource = null;
var analyser = null;
var N = 4096;//8192;//2048; // Samples of Sound
var bufferLen = null;
var buffer = null;
var PCP = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // Pitch Class Profiles
var fref = 261.63; // Reference frequency middle C (C4)
// fref = 65.4; // Reference frequency C2
// fref = 440.0; // Reference frequency A4
var audioContext = new AudioContext();
var fs = audioContext.sampleRate; // Retrieve sampling rate. Usually 48KHz
var useMicrophone = false;

navigator.mediaDevices.getUserMedia(constraints)
  .then(function(stream) {
    // Create an analyzer node to process the audio
    analyser = audioContext.createAnalyser();
    analyser.fftSize = N;
    bufferLen = N / 2;
    //bufferLen = analyser.frequencyBinCount;
    console.log( 'bufferLen = ' + bufferLen );
    buffer = new Float32Array(bufferLen);

    if ( useMicrophone ) {
      // Create an AudioNode from the stream.
      mediaStreamSource = audioContext.createMediaStreamSource(stream);
      // Connect it to the destination.
      mediaStreamSource.connect(analyser);
    }
    else {
      // As a test, feed a C chord directly into the analyzer
      // C4, E4, G4
      var freqs = [261.63, 329.63, 392.00];
      for( var i=0; i < freqs.length; i++) {
        var o = audioContext.createOscillator();
        var g = audioContext.createGain(); //Create Gain Node
        o.frequency.value = freqs[i];
        o.connect(g);
        g.gain.value = 0.25;
        g.connect( audioContext.destination );
        g.connect( analyser );
        o.start(0);
        //setTimeout(function(s) {s.stop(0)}, 1000, o);
      }
    }

    // Call algorithm every 50 ms
    setInterval(function() {
      pcpAlg();
    }, 50);
  })
  .catch(function(err) {
    console.log(err.name + ": " + err.message);
  });

function pcpAlg() {
  analyser.getFloatTimeDomainData(buffer);
  //analyser.getFloatFrequencyData( buffer );
  // Reset PCP
  PCP = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  // M(0)=-1 so we don't have to start at 0
  for (var l = 1; l < bufferLen; l++) { // l = 0,1,...,[(N/2) - 1]
    // Calculate M(l)
    var ML = Math.round(12 * Math.log2( (fs * (l / N) ) / fref ) ) % 12; //
    //console.log( ML );
    if (ML >= 0 && ML <= 11) {
      PCP[ML] += Math.pow( Math.abs( buffer[l] ), 2 );
    }
  }

  // Display Data on UI and also try to determine if the sound is a C or F chord
  displayAndCategorize();
}

如果您想尝试运行此代码,请查看我的完整CodePen。请注意,我将"useMicrophone"设置为false,因此它会发出C和弦声音: https://codepen.io/mapmaps/pen/ONQPpw


听起来是个很酷的项目,但你确定输入是正确的吗?你尝试过使用虚假的硬编码输入值来查看在那种情况下输出是否正确吗? - Christophe Roussy
你不想获取缓冲区中的浮点频率数据而不是时域数据吗?我还假设你想从fft结果中提取适当的bin以累积到PCP中。你目前只获取第一个时间值。 - Raymond Toy
1个回答

2
问题出在一篇1999年的论文中使用的算法上。你似乎正在使用FFT来检测幅度峰值,这是一种粗略的频谱频率估计器,而不是音乐音高检测/估计器。多声部和弦估计是更困难/复杂的任务。在这里查看有关多声部音乐提取最新算法的研究论文:http://www.music-ir.org/mirex/wiki/2015:MIREX2015_Results

我不确定是否有足够的知识将其标记为正确。我选择实现1999年论文中的算法,因为它似乎是我能够实现的,并且它声称是一个和弦检测算法。它还被引用在以下这两篇论文中:http://www.fim.uni-passau.de/fileadmin/files/lehrstuhl/sauer/geyer/BA_MA_Arbeiten/BA-HausnerChristoph-201409.pdf 和 https://ccrma.stanford.edu/~kglee/pubs/klee-ismir06.pdf 在1999年的论文中,有他的算法检测和弦的输出结果。也许他在论文中遗漏了一些东西? - user3220901

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接