2011-11-18 2 views
0

임포트 된 mp3 사운드 샘플을 처리하는 가상 피아노 키보드에서 작업 중입니다. 나는 this 질문을 가지고 해결책을 찾았지만 아래의 테스트 코드를 실행하면 사운드는 각 사운드 소스의 마지막 데이터 청크를 영원히 반복합니다. 분명히 추출 된 사운드 데이터의 끝 부분에서 멈추고 싶습니다. 추출 된 샘플 데이터에서도 동일한 결과가 발생합니다. 혼합 할 필요조차 없습니다. 동적으로 그려진 그래픽 파형을 추가하여 어떤 일이 발생했는지 시각적으로 확인할 수 있습니다. ByteArray 클래스를 처음 사용하는 것이므로이 문제를 해결하는 방법을 모르겠습니다.AS3 : 추출 된 sampledata 루프가 마지막 청크로 끝납니다.

import flash.media.Sound; 
import flash.utils.ByteArray; 
import flash.geom.Point; 
import flash.display.Sprite; 
import flash.net.URLRequest; 
import flash.events.MouseEvent; 

var t0:Sound = new Sound(new URLRequest("5_C.mp3")); //sound imported 
var t1:Sound = new Sound(new URLRequest("5_E.mp3")); //other sound imported 
//var t0:Sound = new C4(); //sound in library 
//var t1:Sound = new E4(); //other sound in library 
var t0p:Point = new Point(); 
var t1p:Point = new Point(); 
var t0a:ByteArray = new ByteArray(); 
var t1a:ByteArray = new ByteArray(); 
var mix:Sound = new Sound(); 

var spr:Sprite = new Sprite(); 
addChild(spr); 
spr.y = 10; 
var lx:Number = 0; 
var ly:Number = 0; 
spr.graphics.lineStyle(1,0,1,true); 

mix.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData); 

stage.addEventListener(MouseEvent.CLICK, mce); 
function mce(e:MouseEvent) { 
    mix.play(); 
} 

function onSampleData(e:SampleDataEvent):void { 

    t0a.position = 0; 
    t1a.position = 0; 

    t0.extract(t0a, 3072); 
    t1.extract(t1a, 3072); 

    t0a.position = 0; 
    t1a.position = 0; 

    for(var i:int = 0; i < 3072; i++) { 

     t0p.x = t0a.readFloat(); 
     t0p.y = t0a.readFloat(); 
     t1p.x = t1a.readFloat(); 
     t1p.y = t1a.readFloat(); 

     var tmp:Point = new Point(Math.max(Math.min(t0p.x + t1p.x, 1), -1), Math.max(Math.min(t0p.y + t1p.y, 1), -1)); 

     e.data.writeFloat(tmp.x); // left 
     e.data.writeFloat(tmp.y); // right 

     if (lx > 549) { 
      lx = 0; 
      ly += 10; 
      if (ly > 399) { 
       spr.graphics.clear(); 
       ly = 0; 
       spr.graphics.lineStyle(1,0,1,true); 
      } 
      spr.graphics.moveTo(lx, (tmp.x + tmp.y) * 4 + ly); 
     } else { 
      spr.graphics.lineTo(lx, (tmp.x + tmp.y) * 4 + ly); 
     } 
     lx += 0.05; 

    } 
} 

난 그냥 원시 데이터를 처리 경험이없는, 그래서 당신은 아무것도 나를 도울 수 있다면 정말 감사 할 것, 이것에 대한 매우 간단한 해결책이 있어야합니다.

답변

1

오케이, 나는 며칠 전에 해결책을 찾았습니다. 여기에 게시 할 시간이 없었습니다.

사운드의 ByteArray 샘플 길이를 계산하는 쉬운 방법은 samplesLength = sound.length * 44.1;입니다.

사운드는 길이가 초당 1000 개의 스테레오 샘플 쌍인 밀리 초 단위로 표시되고 플래시의 사운드 샘플링 해상도는 44100 Hz (초당 샘플 쌍)이기 때문에 44.1 샘플 짧은 밀리 초 동안 렌더링 된 쌍.

또한 샘플은 32 비트 부동 소수점 값이고 샘플 쌍은 64 비트 (8 바이트) 길이이므로 올바른 값은 samplesLength = soundExtract.length/8;입니다.