【问题标题】:Chord Detection Algorithm with the Web Audio API [closed]使用 Web Audio API 的和弦检测算法 [关闭]
【发布时间】:2016-08-05 10:49:46
【问题描述】:

首先我尝试实现这个和弦检测算法: 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();
}

如果您想尝试自己运行它,这是我的完整代码笔。警告我已将 useMicrophone 设置为 false,因此它会发出 c 和弦声音: https://codepen.io/mapmaps/pen/ONQPpw

【问题讨论】:

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

标签: javascript algorithm web-audio-api audio-processing audio


【解决方案1】:

问题在于 1999 年论文中的算法。您似乎将 FFT 用于幅度峰值,这是一个粗略的频谱频率估计器,而不是音乐音高检测器/估计器。复音和弦估计是一项更加困难/复杂的任务。看这里关于和弦音乐提取的最新算法的研究论文:http://www.music-ir.org/mirex/wiki/2015:MIREX2015_Results

【讨论】:

猜你喜欢
  • 2011-05-19
  • 1970-01-01
  • 2015-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多