Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package {
- import flash.display.*;
- import flash.net.*;
- import flash.events.*;
- import flash.utils.*;
- import flash.media.*;
- import flash.system.*;
- import HTools.Audio.DLS.*;
- import HTools.Audio.Midi.*;
- import HTools.Audio.MidiPlayer.Player;
- import HTools.Audio.Midi.Events.MidiEvent;
- import HTools.Audio.MidiPlayer.Instrument;
- import HTools.Audio.MidiPlayer.InstrumentRegion;
- import HTools.Audio.MidiPlayer.Sample;
- import HTools.Audio.WaveWriter;
- public class MidiPlayerTest extends Sprite {
- private var dlsLoader:URLLoader;
- private var dlsParser:DLSArchive;
- private var midiLoader:URLLoader;
- private var midiParser:MidiLoader;
- private var player:Player;
- private static const GMPATH:String="C:\\windows\\system32\\drivers\\gm.dls";
- private static const SONG:String="canyon.mid";
- private var outSound:Sound;
- private var outSoundChan:SoundChannel;
- private static const PUMP_LEN:uint=8000;
- public function MidiPlayerTest() {
- dlsLoader=new URLLoader();
- dlsLoader.dataFormat=URLLoaderDataFormat.BINARY;
- dlsLoader.addEventListener(Event.COMPLETE,dlsLoaded);
- dlsLoader.load(new URLRequest(GMPATH));
- midiLoader=new URLLoader();
- midiLoader.dataFormat=URLLoaderDataFormat.BINARY;
- midiLoader.addEventListener(Event.COMPLETE,midiLoaded);
- midiLoader.load(new URLRequest(SONG));
- }
- private function dlsLoaded(e:Event):void {
- const data:ByteArray=dlsLoader.data;
- trace("DLS loaded");
- const startTime:uint=getTimer();
- dlsParser=new DLSArchive();
- dlsParser.parse(data);
- trace("DLS Parsing took ",getTimer()-startTime," ms.");
- loaded();
- }
- private function midiLoaded(e:Event):void {
- const data:ByteArray=midiLoader.data;
- trace("MIDI loaded");
- const startTime:uint=getTimer();
- midiParser=new MidiLoader();
- midiParser.parse(data);
- trace("Loaded ",midiParser.tracks.length,"tracks");
- trace("Track 0 has",midiParser.tracks[0].length,"events");
- trace("MIDI Parsing took ",getTimer()-startTime," ms.");
- loaded();
- }
- private var loadCount:uint;
- private function loaded():void {
- if(++loadCount<2) return;
- var startTime:uint=getTimer();
- player=new Player();
- player.tickRate=midiParser.tickRate;
- trace("Player instantiation took ",getTimer()-startTime," ms.");
- startTime=getTimer();
- player.loadDLS(dlsParser);
- trace("Player DLS loading took ",getTimer()-startTime," ms.");
- player.requestMoreEvents=stuffEvents;
- /* List instruments
- for each(var inst:Instrument in player.instrumentLoader.instruments) {
- trace(inst.name,inst.instrumentId,inst.bankHigh,inst.bankLow);
- }*/
- //dumpEvents();
- //testSampleLoading();
- testExecuteEvents(false);
- //testExecuteEvents(true);
- //testPlayback();
- }
- private function dumpEvents():void {
- var track:Vector.<MidiEvent>=midiParser.tracks[0];
- for each(var evt:MidiEvent in track) {
- trace(evt);
- }
- }
- private function testSampleLoading():void {
- const inst:Instrument=player.instrumentLoader.instruments[0];
- const region:InstrumentRegion=inst.regions[0];
- const sample:Sample=region.sample;
- sample.loadSampleData();
- var writer:WaveWriter=new WaveWriter(sample.fmt.channels==2,32,sample.sampleRate);
- sample.sampleData.position=0;
- writer.addSamples(sample.sampleData);
- writer.finalize();
- var fr:FileReference=new FileReference();
- fr.save(writer.outBuffer,"sample.wav");
- }
- private function testExecuteEvents(saveData:Boolean):void {
- const startTime:uint=getTimer();
- const testLen:uint=100000;
- const ob:ByteArray=new ByteArray();
- ob.length=testLen*8;
- player.executeEvents(testLen,ob);
- ob.position=0;
- const takenTime:uint=getTimer()-startTime;
- const budget:uint=testLen/44100;
- const ratio:Number=takenTime/budget;
- trace("Running events took ",takenTime," ms.",(ratio*100).toFixed(2),"%");
- if(saveData) {
- var writer:WaveWriter=new WaveWriter(true,32,44100);
- ob.position=0;
- writer.addSamples(ob);
- writer.finalize();
- var fr:FileReference=new FileReference();
- fr.save(writer.outBuffer,"sample.wav");
- }
- }
- private function testPlayback():void {
- outSound=new Sound();
- outSound.addEventListener(SampleDataEvent.SAMPLE_DATA,pumpSamples);
- outSoundChan=outSound.play();
- }
- private function pumpSamples(e:SampleDataEvent):void {
- e.data.length=PUMP_LEN*4*2;
- e.data.position=0;
- player.executeEvents(PUMP_LEN,e.data);
- //e.data.position=0;
- }
- private function stuffEvents(events:Vector.<MidiEvent>):void {
- for each(var event:MidiEvent in midiParser.tracks[0]) {
- events.push(event);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement