在Actionscript computeSpectrum和节拍检测中苦苦挣扎

时间:2014-12-16 20:00:33

标签: actionscript-3 beat-detection

我试图理解从computeSpectrum方法返回给我的值。我想用较低的频率选择一个轨道的低音鼓。我从byteArray返回的数字毫无意义。例如,它表示当有明显的声音播放时该值为0。我在这里错过了什么...我知道节拍检测并不容易,并且已经查看过这个主题的大部分帖子......只是返回给我的数字毫无意义,有人可以解释它们我?提前谢谢。

我的代码:

import flash.display.Graphics;
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.net.URLRequest;
import flash.display.MovieClip;

var snd: Sound = new Sound();
var req: URLRequest = new URLRequest("mySong.mp3");
snd.load(req);

var channel: SoundChannel;
channel = snd.play();
addEventListener(Event.ENTER_FRAME, onEnterFrame);
snd.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);

const CHANNEL_LENGTH: int = 256;
const BUFFER_LENGTH: int = 512;

var bytes:ByteArray = new ByteArray();

function onEnterFrame(event: Event): void 
{

  SoundMixer.computeSpectrum(bytes, true, 0);


  for (var i:int = 0; i < CHANNEL_LENGTH; i++) // channel_length = 256
  {


      var sampleValue:Number = bytes.readFloat() * 200;
      var byteArrayIndex = bytes.position;
      trace(byteArrayIndex, sampleValue);

  }
}

function onPlaybackComplete(e:Event):void
{
    removeEventListener(Event.ENTER_FRAME, onEnterFrame);
}

1 个答案:

答案 0 :(得分:0)

很高兴再次见到你,我想这次我更了解你的想法。

如果你要检查是否存在击败&#39;在歌曲的特定时间; 我可以建议这个方法参考:

import flash.events.Event;

var snd: Sound = new Sound();
var req: URLRequest = new URLRequest("mySong.mp3");
snd.load(req);

var channel: SoundChannel;
channel = snd.play();
addEventListener(Event.ENTER_FRAME, onEnterFrame);
snd.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);


// I want to specify which region of byteData is passing to my check function.
// this two value help me to extract the sample I need.
var checkLength:int = 20; 
var checkStart:int = 0; 
var theshold:Number = 0.5; // How big the value should I determine it is the 'beat'.

const CHANNEL_LENGTH: int = 256;
const BUFFER_LENGTH: int = 512;

var myArray:Array;
var bytes:ByteArray = new ByteArray();

function onEnterFrame(event: Event): void 
{

    SoundMixer.computeSpectrum(bytes, true, 0);

    myArray = [];
   for (var i:int = checkStart; i < checkLength; i+=8) // extract the sample
   {
     var sampleValue:Number = bytes.readFloat();
       myArray.push(sampleValue);
   }
   if( CheckBeat( myArray ))
   {
       trace("here maybe a beat!!", getTimer());
   }
}

function CheckBeat(valueArray:Array):Boolean  // check the whether 'beat' exist
{
    var meanValue:Number = valueArray[0];
    for(var i:int = 1; i < valueArray.length; i++)
    {
        meanValue = (meanValue + valueArray[i])/2;
    }
    return meanValue > theshold;
}

function onPlaybackComplete(e:Event):void
{
    removeEventListener(Event.ENTER_FRAME, onEnterFrame);
}

但这种方法只是一种估算,而且根本不够准确。