我试图理解从computeSpectrum方法中返回给我的值。我想用较低的频率挑选出曲目的低音鼓。我从字节数组中得到的数字毫无意义。例如,当明显有声音播放时,它说值为 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);
}
很高兴再次见到你,我想这次我更理解你的心思了。
如果您要检查歌曲的特定时间是否存在"节拍";我可能会建议这种方法供参考:
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);
}
但这种方法只是一个估计,根本不足够准确。