Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #38118

Re: Approach to find out parsed values that goes wrong?

Newsgroups comp.lang.javascript
Date 2020-01-22 22:41 -0800
References <974481a0-2ccc-43e4-aee3-5075ca956e5c@googlegroups.com> <75341974-1209-4fb5-aa61-c81443d32c5e@googlegroups.com> <68a08d9a-d78b-4fcf-b9a7-4c0f0ee7e324@googlegroups.com>
Message-ID <e4a52db6-038c-43ca-afab-e01dbe93290d@googlegroups.com> (permalink)
Subject Re: Approach to find out parsed values that goes wrong?
From Jonas Thörnvall <jonas.thornvall@gmail.com>

Show all headers | View raw


Den torsdag 23 januari 2020 kl. 07:10:09 UTC+1 skrev Jonas Thörnvall:
> Den torsdag 23 januari 2020 kl. 07:05:55 UTC+1 skrev Jonas Thörnvall:
> > Den torsdag 23 januari 2020 kl. 06:25:57 UTC+1 skrev Jonas Thörnvall:
> > > I am oarsing a file following a format, even made some what an editor for it.
> > > However sometimes the values not expected "bad parsed" at some instance.
> > > But it seem to screw up my listing of the values so i can not even see what the values it goes wrong is, although i see the "events/array element" missing.
> > > 
> > > Unfortunately for me what ever value that is there prevents me from sorting, the eventlist on time. And since there is several instruments pretty much layed out sequential aftereachother on same channel.
> > > 
> > > I noticed this because a guy had layed out left and right hand as separate "instruments". So basicly all things played with left hand comes after the ones played with right hand and since there seem to be NaN values in the event list the sort seem to bug out?
> > > 
> > > I really must say i do hate to parse midi even that the thing i parse from have neatly tied up the 70-style bit structure with labeled objects.
> > > I still bomb out try to reparse it for my sequenser.
> > > 
> > > Seeing that i miss event 3289,3290 in list do not help me that much.
> > > I somehow must find out what they contain.
> > 
> > Here is the order of blocks of different "message type".
> > There is things like a channel but does not really matter since a channel can hhave many blocks, but each block consist of these
> > 
> > 	parseCC(i);
> >         parsePitchBend(i);
> > 	parsePrograms(i);
> > 	parseNotes(i);
> >         //Here is where my code bugs out it seem to incorporate 2? extra event 
> >         //before next block processed "well the code says channel" but it is 
> >         //really block since a channel can have many block "that is really 
> >         //instruments 
> > 
> > My full code to decipher the fork misstake in next post.
> > 
> > Here is the fork where parsing goes wrong when there comes a new block 
> > 
> > 
> >   {
> >           "duration": 3.329729499999985,
> >           "durationTicks": 1800,
> >           "midi": 53,
> >           "name": "F3",
> >           "ticks": 263160,
> >           "time": 267.06666610833395,
> >           "velocity": 0.2755905511811024
> >         },
> >         {
> >           "duration": 3.082417666666572,
> >           "durationTicks": 1740,
> >           "midi": 61,
> >           "name": "C#4",
> >           "ticks": 263220,
> >           "time": 267.31397794166736,
> >           "velocity": 0.33070866141732286
> >         }
> > //Here two extra elements seem to be interpretated.
> >       ]
> >     },
> >     {
> >       "channel": 0,
> >       "controlChanges": {
> >         "7": [
> >           {
> >             "number": 7,
> >             "ticks": 0,
> >             "time": 0,
> >             "value": 0.7874015748031497
> >           }
> >         ],
> >         "10": [
> >           {
> >             "number": 10,
> >             "ticks": 0,
> >             "time": 0,
> >             "value": 0.5039370078740157
> >           }
> >         ],
> 
> // THIS IS THE MAIN FUNCTION FIRST CALLED WHEN PARSING A MIDI FILE STARTS
> function reparseMidi(){
> 	console.log("reparseMidi");
> 	document.getElementById("keystream").value="";
> 	track=[];
> 	makeTrack();
> 	document.getElementById("keystream").value=importedMidi;
> 	myChannels = importedMidi.split("channel");
> 	parseInfo();
> 	for (var i=1;i<myChannels.length;i++){
> 		//EXTRACT EACH CHANNEL
> 		console.log("****** NEW CHANNEL *********");
> 		channelStart=myChannels[i].indexOf(":");
> 		channelEnd=myChannels[i].indexOf(",");
> 		importChannel=myChannels[i].substring(channelStart+1,channelEnd);
> 		importChannel=parseInt(importChannel);
> 		activetrack=importChannel+1;
> 		document.getElementById("track").value=activetrack;
> 		console.log("ActiveTrack= "+activetrack);
> 		
> 		//There can be more "channel posts/objects" then one, on same channel
> 		alert("Number of parts="+myChannels.length+" This channel="+activetrack+" ChPart ="+myChannels[i]);
> 		parseCC(i);
> 		parsePitchBend(i);
> 		parsePrograms(i);
> 		parseNotes(i);
> 		track[activetrack].midiMess.sort(dynamicSort("time"));
> 	}	
> 	
> 	maxtrack=16;
> 	downMixALLEventList();
> 	getNoteTicks(activetrack);
> 	console.log("****** Finished import *********");
> 	//Set to track 1 and close menu
> 	document.getElementById("track").value=1;
> 	changeTrack();
> 	MA();
> }
> 
> 
> function parseInfo(){
> 	impPPQStart=myChannels[0].indexOf('"ppq": ')+7;
> 	impPPQEnd=myChannels[0].indexOf('"tempos":');
> 	impPPQ=myChannels[0].substring(impPPQStart,impPPQEnd);
> 	impPPQ=impPPQ.replace(",",""); 
> 	impPPQ=parseInt(impPPQ);
> 	document.getElementById("ppq").value=impPPQ;
> 	impBPMStart=myChannels[0].indexOf('"tempos": [');
>     impBPMEnd=myChannels[0].indexOf('"timeSignatures"');
> 	impBPM=myChannels[0].substring(impBPMStart,impBPMEnd);
> 	impBPM=impBPM.replace(",",""); 
> 	BPMS=impBPM.split("bpm");
> 	TICKS=impBPM.split("ticks");
> 	for (var i=1;i<BPMS.length;i++){
> 		BPMsplit(i);
> 		TICKsplit(i);
> 	}
> }
> 
> function TICKsplit(IN){
> 	TICKstart=TICKS[IN].indexOf('":')+2;
> 	TICKend=TICKS[IN].indexOf('}');
> 	TICKS[IN]=TICKS[IN].substring(TICKstart,TICKend);
> 	TICKS[IN]=TICKS[IN].replace(",",""); 
> 	//alert(TICKS[IN]);
> }
> 
> function BPMsplit(IN){
> 	BPMstart=BPMS[IN].indexOf(':"')+4;
> 	BPMend=BPMS[IN].indexOf('"ticks"');
> 	BPMS[IN]=BPMS[IN].substring(BPMstart,BPMend);
> 	BPMS[IN]=BPMS[IN].replace(",",""); 
> 	BPMS[IN]=parseFloat(BPMS[IN]);	
> 	document.getElementById("bpm").value=BPMS[IN];
> 	getFormNoteTime();
> 	//alert(BPMS[IN]);
> }
> 
> function parseCC(IC){
> 	impCCStart=myChannels[IC].indexOf('"tracks"');
>     impCCEnd=myChannels[IC].indexOf('"pitchBends"');
> 	impCC=myChannels[IC].substring(impCCStart,impCCEnd);
> 	//console.log("impCC"+impCC);
> 	CCpart=impCC.split("[");
> 	for (var i=1; i <CCpart.length;i++){
> 	//	console.log("CC="+CCpart[i]);
> 		CCsplit(i);
> 	}
> }
> 
> 
> function parsePitchBend(PB){
> 	
> 	
> }
> 
> function parsePrograms(IP){
> 	evtype = myChannels[IP].split("instrument");
> 	console.log("MidiChannel="+activetrack+" Nr of instruments="+evtype.length);
> 	for (var i=1; i <evtype.length;i++){
> 		impPRStart=evtype[i].indexOf('"number":')+9;
> 		impPREnd=evtype[i].indexOf('},');
> 		impPR=evtype[i].substring(impPRStart,impPREnd);
> 		impPR=impPR.trim();
> 		PRChan=191+activetrack;
> 		//alert("start"+impPR);
> 		activetrack=parseInt(activetrack);
> 		track[activetrack].midiMess.push({
> 			time: 0,
> 			data0:PRChan,
> 			data1:impPR,
> 		});
> 	}
> }
> 
> function parseNotes(IC){
> 	evtype = myChannels[IC].split("notes");
> 	impNote=evtype[1].split("},");
> 	for (var i=0; i <impNote.length;i++){
> 		noteSplit(i);
> 	}
> }
> 
> function CCsplit(IN){
> 	CCStartNumber=CCpart[IN].indexOf('"number":')+9;
>     CCEndNumber=CCpart[IN].indexOf('"ticks"');
> 	CCNumber=CCpart[IN].substring(CCStartNumber,CCEndNumber);
> 	CCNumber=CCNumber.replace(",",""); 
> 	CCNumber=parseInt(CCNumber);
> 		
> 	CCStartTick=CCpart[IN].indexOf('"ticks":')+9;
>     CCEndTick=CCpart[IN].indexOf('"time"');
> 	CCTick=CCpart[IN].substring(CCStartTick,CCEndTick);
> 	CCTick=CCTick.replace(",",""); 
> 	CCTick=parseInt(CCTick);
> 	//alert("CC tick "+CCTick); This should be used later to recalculate time at other BPM
> 	
> 	CCStartTime=CCpart[IN].indexOf('"time":')+7;
>     CCEndTime=CCpart[IN].indexOf('"value"');
> 	CCTime=CCpart[IN].substring(CCStartTime,CCEndTime);
> 	CCTime=CCTime.replace(",",""); 
> 	CCTime=parseFloat(CCTime*1000);
> 	
> 	CCStartValue=CCpart[IN].indexOf('"value":')+9;
> 	CCEndValue=CCpart[IN].indexOf('}');
> 	CCEValue=CCpart[IN].substring(CCStartValue,CCEndValue);
> 	CCEValue=CCEValue.replace(",",""); 
> 	CCEValue=Math.round(CCEValue*127);
> 		
> 	CCchan=176+importChannel;
> 	CCchan=parseInt(CCchan);
> 		
> 	//alert("CC time "+CCTime+" CC number "+CCNumber+" CC chanel "+CCchan+" CC value "+CCEValue);
> 	pushCC(IN);
> }
> 
> function pushCC(IN){
> 	if (CCNumber!=11 && CCNumber!=0 && CCNumber!=32 && CCNumber!=121 ){
> 		//track[activetrack].eventTicks.push(CCTick);
> 		track[activetrack].midiMess.push({
> 			time: CCTime,
> 			data0:CCchan,
> 			data1:CCNumber,
> 			data2:CCEValue
> 		});
> 	}
>  // console.log ("Event="+midiMess.length+" Time "+track[activetrack].midiMess[midiMess.length].time +" data0 "+track[activetrack].midiMess[midiMess.length].data0+" data1 "+track[activetrack].midiMess[midiMess.length].data1+" data2 "+track[activetrack].midiMess[midiMess.length].data2);  
> }
> 
> function noteSplit(IN){
> 	impStartTick=impNote[IN].indexOf('"ticks":')+9;
>     impEndTick=impNote[IN].indexOf('"time"');
> 	impTick=impNote[IN].substring(impStartTick,impEndTick);
> 	impTick=impTick.replace(",",""); 
> 	impTick=parseInt(impTick);
> 	//alert("imptick="+impTick);
> 	
> 	durStartTick=impNote[IN].indexOf('Ticks":')+7;
>     durEndTick=impNote[IN].indexOf('"midi');
> 	durTick=impNote[IN].substring(durStartTick,durEndTick);
> 	durTick=durTick.replace(",",""); 
> 	durTick=parseInt(durTick);
> 	
> 	endTick=impTick+durTick;
> 	//alert(endTick);
> 		
> 	impTimeStart=impNote[IN].indexOf('"time": ')+8;
> 	impTimeEnd=impNote[IN].indexOf('"velocity":');
> 	impTime=impNote[IN].substring(impTimeStart,impTimeEnd);
> 	impTime=impTime.replace(",",""); 
>     impTime=parseFloat(impTime*1000);
> 	
> 	impNoteStart=impNote[IN].indexOf('"midi": ')+8;
> 	impNoteEnd=impNote[IN].indexOf('"name":');
> 	impKey=impNote[IN].substring(impNoteStart,impNoteEnd);
> 	impKey=impKey.replace(",",""); 
> 	impKey=impKey.replace(".",""); 
> 	impKey=impKey.trim();
> 	impKey=parseInt(impKey);
> 	
> 	
> 	impDurationStart=impNote[IN].indexOf('"duration": ')+12;
> 	impDurationEnd=impNote[IN].indexOf('"durationTicks":');
> 	impDuration=impNote[IN].substring(impDurationStart,impDurationEnd);
> 	impDuration=impDuration.replace(",",""); 
> 	impDuration=parseFloat(impDuration*1000);
> 	
> 	impVelocityStart=impNote[IN].indexOf('"velocity": ')+12;
> 	impVelocityEnd=impNote[IN].indexOf("},"	);
> 	impVelocity=impNote[IN].substring(impVelocityStart,impNote[IN].length);
> 	impVelocity=Math.round(impVelocity*127);
> 		
> 	impOffTime=impTime+impDuration;
> 	NoteOffChan=128+importChannel;
> 	NoteOnChan=144+importChannel;
> 	pushNote(IN);
> 	
> }
> 
> function pushNote(IN){
> 	//if (activetrack==2){alert("channel 2");}
>  // track[activetrack].eventTicks.push(impTick);	
>   track[activetrack].midiMess.push({
>       time: impTime,
>       data0:NoteOnChan,
>       data1:impKey,
>       data2:impVelocity
>   });
>  // console.log ("Event="+IN+" Time "+track[activetrack].midiMess[IN].time +" data0 "+track[activetrack].midiMess[IN].data0+" data1 "+track[activetrack].midiMess[IN].data1+" data2 "+track[activetrack].midiMess[IN].data2);  
> // track[activetrack].eventTicks.push(endTick);
>  track[activetrack].midiMess.push({
>       time: impOffTime,
>       data0:NoteOffChan,
>       data1:impKey,
>   });
> // IN=IN+1;
> // console.log ("Event="+IN+" Time "+track[activetrack].midiMess[IN+1].time +" data0 "+track[activetrack].midiMess[IN+1].data0+" data1 "+track[activetrack].midiMess[IN+1].data1+" data2 "+track[activetrack].midiMess[IN+1].data2);  
> }

If you want to try yourself, how it fail.
https://midisequenser.000webhostapp.com/

It so weird it render real nice, because it use time to render and really do not care about missed notes however to be played the events have to be ordered. And since my parsing bug make it impossible to sort. It will only play right hand piano, but this is of course true for any song that hold more then one "instrument" on a channel. Well maybe even true for any song that holde more then one channel.

But the channels/tracks render perfectly och each track "always something". To play them up in correct order.....

Well you need to sort them which seem impossible.

Here is the midifile i used, probably should had found a smaller one. But it get the bug using only single channel with two instruments/hands.

http://www.piano-midi.de/midis/chopin/chpn_op66.mid

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Approach to find out parsed values that goes wrong? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-22 21:25 -0800
  Re: Approach to find out parsed values that goes wrong? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-22 22:05 -0800
    Re: Approach to find out parsed values that goes wrong? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-22 22:10 -0800
      Re: Approach to find out parsed values that goes wrong? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-22 22:41 -0800
        Re: Approach to find out parsed values that goes wrong? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-22 23:12 -0800
    Re: Approach to find out parsed values that goes wrong? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-22 22:27 -0800
  Re: Approach to find out parsed values that goes wrong? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-01-23 04:50 -0800
    Re: Approach to find out parsed values that goes wrong? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-23 06:12 -0800
    Re: Approach to find out parsed values that goes wrong? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-23 06:33 -0800
    Re: Approach to find out parsed values that goes wrong? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-23 06:55 -0800
      Re: Approach to find out parsed values that goes wrong? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-01-23 11:48 -0800
        Re: Approach to find out parsed values that goes wrong? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-23 14:54 -0800
          Re: Approach to find out parsed values that goes wrong? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-23 14:58 -0800
            Re: Approach to find out parsed values that goes wrong? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-23 15:00 -0800
              Re: Approach to find out parsed values that goes wrong? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-23 15:20 -0800
          Re: Approach to find out parsed values that goes wrong? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-01-23 17:59 -0800
            Re: Approach to find out parsed values that goes wrong? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-24 01:03 -0800
              Re: Approach to find out parsed values that goes wrong? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-01-24 05:38 -0800
                Re: Approach to find out parsed values that goes wrong? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-24 11:23 -0800
                Re: Approach to find out parsed values that goes wrong? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-01-24 17:12 -0800
                Re: Approach to find out parsed values that goes wrong? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-25 06:48 -0800
                Re: Approach to find out parsed values that goes wrong? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-26 11:22 -0800
                Re: Approach to find out parsed values that goes wrong? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-01-26 14:17 -0800
                Re: Approach to find out parsed values that goes wrong? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-26 22:38 -0800
                Re: Approach to find out parsed values that goes wrong? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-01-28 05:13 -0800
                Re: Approach to find out parsed values that goes wrong? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-28 17:35 -0800
                Re: Approach to find out parsed values that goes wrong? luserdroog <luser.droog@gmail.com> - 2020-01-24 21:32 -0800

csiph-web