2011-08-27 5 views
0

이 AS3 코드에서 "2048"이 포함 된 500 개의 추적 출력이 필요합니다. 하지만 대신 "2048"을 포함하는 일부 줄만 얻습니다. 추적의 나머지 부분은 "0"을 출력하여 extract() 메서드가 아무런 데이터도 반환하지 않는다는 것을 나타냅니다.사운드 파일을 분석하기 위해 루프에서 Sound.extract를 사용하는 AS3

왜 이런 경우입니까? 각각의 위치에서 사운드의 2048 바이트를 추출하는 특정 단계로 사운드 파일을 반복하고 싶습니다. 초 flashplayer를 동결 한 번에 전체 사운드 파일을 추출하기 때문에 보다 작은 samplesInSound/2048입니다 단계 길이로 볼 수 있습니다 나를

이 문제에 대한 좋은 해결책이 아니다. 이 값이 더 가까울수록 "0"이있는 행이 2048 인 행에 비해 더 많이 인쇄됩니다.

var sound:Sound = new Sound(); 
sound.load(new URLRequest("soundfile.mp3")); 
sound.addEventListener(Event.COMPLETE, soundLoaded); 

function soundLoaded(e:Event){ 
    var samplesInSound:Number = sound.length*44.1; 
    var steps:int = 500; 
    var byteArray:ByteArray = new ByteArray(); 
    for(var i=0; i<samplesInSound; i += Math.floor(samplesInSound/steps)){ 
     var extracted = sound.extract(byteArray, 2048, i); 
     trace(i + ": " + extracted); 
    } 
} 

는 I는 사운드 파일의 압축 부분 (Actionscript 3 - Sound.extract method empties sound object data에서처럼)를 추출 후, 사운드 파일의 나머지 잘라낸 것을 생각했다 그래서 루프 다른 시용 :

for(var i=0; i<samplesInSound - steps * 2048; i += Math.floor(samplesInSound/steps) - 2048) 

하지만이 방법도 효과가 없었습니다.

누구든지이 문제를 해결할 수 있습니까?

답변

1

질문에 대한 평판이 부족하여 면책 조항과 함께이 답변을 작성하겠습니다. 문제가 해결되지 않으면 알려주십시오.

sound.extract에 대한 모든 호출은 sound.extract에 대한 이전 호출이 끝난 곳에서 추출하기 때문에 항상 매번 startPosition 인수를 사용할 필요가 없습니다.

더 중요한
sound.extract(byteArray, 2048) // advances 2048 samples into the sound 
sound.extract(byteArray, 2048) // we are 2048 samples in, grab another 2048 
sound.extract(byteArray, 2048) // we are 4096 samples in, grab another 2048, etc. 

, 당신은 순간에 그것을 가지고 방법이 실제로 중지하지 않는 플래시 플레이어가에 동결 : 그것은 당신이 이상한 결과를 얻는 이유, sound.extract에 대한 호출이 이미 소리를 통해 진행되었다입니다 어쨌든 하나의 루프에서 모든 것을하고 있기 때문에 긴 소리입니다. 루프에서 500 * 2048 (1,024,000) 단계를 잡는 것은 sound.extract (byteArray, 1024000)를 사용하여 한 번에 전체를 잡는 것보다 낫지 않습니다. 무엇보다 천천히 움직입니다. sound.extract를 호출 할 때 플래시 타임을 '호흡'해야합니다. 답변에 대한

class SoundLoader 
{ 
    public var sound:Sound; 
    public var byteArray:ByteArray; 
    public var samplesInSound:int; 

    public function SoundLoader(sound:Sound) 
    { 
     this.sound = sound; 
     samplesInSound = sound.length * 44.1; 
     byteArray = new ByteArray(); 
    } 

    // Returns "true" if the sound has finished extracting. 
    public function extract(samplesToExtract:int):Boolean 
    { 
     var extracted:int = sound.extract(byteArray, samplesToExtract); 

     return extracted < samplesToExtract; 
    } 
} 

function soundLoaded(e:Event) 
{ 
    soundLoader = new SoundLoader(sound); 

    // 2048 is very low, you'll need to experiment with how much you can load at once without Flash hiccupping. 

    if (soundLoader.extract(2048)) 
    { 
     // A very short sound! soundLoader.byteArray contains the extracted sound, ready to use. 
    } 
    else 
    { 
     // add a timer that calls soundLoader.extract(2048) again. 
     // keep repeating the timer until soundLoader.extract returns 'true' - then you can use soundLoader.byteArray. 
     // the timer allows Flash to do other things inbetween, which avoids it freezing up. 
    } 
} 
+0

감사 :

여기에 가능한 솔루션입니다. 불행히도 내 계획은 각 루프에서 추출한 데이터보다 큰 단계로 사운드를 반복하는 것입니다. 예를 들어, 다음과 같습니다 : 'sound.extract (byteArray, 2048, 0) // 사운드에 2048 샘플을 전달합니다. sound.extract (byteArray, 2048, 10000) // 우리는 10000 개의 샘플입니다. 다른 것을 잡아라 2048 sound.extract (byteArray, 2048, 20000) // 우리는 20000 개의 샘플을 가지고 있고, 2048 등을 붙잡는다. ' 내가 말한 얼어 붙은 문제에 대해 알고있다. 마지막 코드에서 프레임에 루프를 연결하거나 예제에서와 같이 타이머에 루프를 연결합니다. – user914964

관련 문제