00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "jdkmidi/world.h"
00026 #include "jdkmidi/showcontrol.h"
00027
00028 namespace jdkmidi
00029 {
00030
00031
00032 MIDIShowControlPacket::MIDIShowControlPacket()
00033 :
00034 QNumber(0),
00035 QList(0),
00036 QPath(0)
00037 {
00038 DeviceId=0;
00039 CommandFmt=0;
00040 Command=MIDI_SC_GO;
00041 HasTime=false;
00042 HasQNumber=false;
00043 HasQList=false;
00044 HasQPath=false;
00045 Hours=0;
00046 Minutes=0;
00047 Seconds=0;
00048 Frames=0;
00049 FractFrames=0;
00050 Val1=0;
00051 Val2=0;
00052 }
00053
00054 void MIDIShowControlPacket::ClearVariableStuff()
00055 {
00056 QNumber.Clear();
00057 QList.Clear();
00058 QPath.Clear();
00059
00060 Command=MIDI_SC_GO;
00061 HasTime=false;
00062 HasQNumber=false;
00063 HasQList=false;
00064 HasQPath=false;
00065 Hours=0;
00066 Minutes=0;
00067 Seconds=0;
00068 Frames=0;
00069 FractFrames=0;
00070 Val1=0;
00071 Val2=0;
00072 }
00073
00074
00075 bool MIDIShowControlPacket::ParseEntireSysEx( const MIDISystemExclusive *e )
00076 {
00077 bool f=true;
00078 int pos = 0;
00079
00080 if( e->GetData( pos++ )!=0xf0 )
00081 return false;
00082 if( e->GetData( pos++ )!=0x7f )
00083 return false;
00084
00085 ClearVariableStuff();
00086
00087 DeviceId=e->GetData( pos++ );
00088
00089 if( e->GetData(pos++)!=0x02 )
00090 return false;
00091
00092 CommandFmt = e->GetData( pos++ );
00093 Command = (MIDIShowCommand) e->GetData(pos++);
00094
00095 switch( Command )
00096 {
00097 case MIDI_SC_GO:
00098 case MIDI_SC_STOP:
00099 case MIDI_SC_RESUME:
00100 case MIDI_SC_LOAD:
00101 case MIDI_SC_GO_OFF:
00102 case MIDI_SC_GO_JAM:
00103 {
00104 f&=Parse3Param( e, &pos );
00105 }
00106 break;
00107 case MIDI_SC_TIMED_GO:
00108 {
00109 f&=ParseTime( e, &pos );
00110 f&=Parse3Param(e,&pos);
00111 }
00112 break;
00113 case MIDI_SC_SET:
00114 {
00115 f&=ParseSet(e,&pos);
00116 }
00117 break;
00118 case MIDI_SC_FIRE:
00119 {
00120 f&=ParseFire(e,&pos);
00121 }
00122 break;
00123 case MIDI_SC_ALL_OFF:
00124 case MIDI_SC_RESTORE:
00125 case MIDI_SC_RESET:
00126
00127 break;
00128
00129 case MIDI_SC_STANDBY_PLUS:
00130 case MIDI_SC_STANDBY_MINUS:
00131 case MIDI_SC_SEQUENCE_PLUS:
00132 case MIDI_SC_SEQUENCE_MINUS:
00133 case MIDI_SC_START_CLOCK:
00134 case MIDI_SC_STOP_CLOCK:
00135 case MIDI_SC_ZERO_CLOCK:
00136 case MIDI_SC_MTC_CHASE_ON:
00137 case MIDI_SC_MTC_CHASE_OFF:
00138 {
00139 if( e->GetData(pos)!=0xf7 && e->GetData(pos)!=0 )
00140 f&=ParseQList( e,&pos );
00141 }
00142 break;
00143 case MIDI_SC_SET_CLOCK:
00144 {
00145 f&=ParseTime(e,&pos);
00146 if( e->GetData(pos)!=0xf7 && e->GetData(pos)!=0 )
00147 f&=ParseQList(e,&pos);
00148 }
00149 break;
00150 case MIDI_SC_OPEN_Q_LIST:
00151 case MIDI_SC_CLOSE_Q_LIST:
00152 {
00153 f&=ParseQList(e,&pos);
00154 }
00155 break;
00156 case MIDI_SC_OPEN_Q_PATH:
00157 case MIDI_SC_CLOSE_Q_PATH:
00158 {
00159 f&=ParseQPath(e,&pos );
00160 }
00161 break;
00162 default:
00163 f=false;
00164 break;
00165 }
00166
00167 if( e->GetLength() < pos )
00168 {
00169 return false;
00170 }
00171
00172 return f;
00173 }
00174
00175 bool MIDIShowControlPacket::StoreToSysEx( MIDISystemExclusive *e ) const
00176 {
00177 bool f=true;
00178
00179 e->Clear();
00180 e->PutEXC();
00181 e->PutByte( 0x7f );
00182 e->PutByte( DeviceId );
00183 e->PutByte( 0x02 );
00184 e->PutByte( CommandFmt );
00185 e->PutByte( Command );
00186
00187 switch( Command )
00188 {
00189 case MIDI_SC_GO:
00190 case MIDI_SC_STOP:
00191 case MIDI_SC_RESUME:
00192 case MIDI_SC_LOAD:
00193 case MIDI_SC_GO_OFF:
00194 case MIDI_SC_GO_JAM:
00195 {
00196 f&=Store3Param( e );
00197 }
00198 break;
00199 case MIDI_SC_TIMED_GO:
00200 {
00201 f&=StoreTime( e );
00202 f&=Store3Param(e);
00203 }
00204 break;
00205 case MIDI_SC_SET:
00206 {
00207 f&=StoreSet(e);
00208 }
00209 break;
00210 case MIDI_SC_FIRE:
00211 {
00212 f&=StoreFire(e);
00213 }
00214 break;
00215 case MIDI_SC_ALL_OFF:
00216 case MIDI_SC_RESTORE:
00217 case MIDI_SC_RESET:
00218
00219 break;
00220
00221 case MIDI_SC_STANDBY_PLUS:
00222 case MIDI_SC_STANDBY_MINUS:
00223 case MIDI_SC_SEQUENCE_PLUS:
00224 case MIDI_SC_SEQUENCE_MINUS:
00225 case MIDI_SC_START_CLOCK:
00226 case MIDI_SC_STOP_CLOCK:
00227 case MIDI_SC_ZERO_CLOCK:
00228 case MIDI_SC_MTC_CHASE_ON:
00229 case MIDI_SC_MTC_CHASE_OFF:
00230 {
00231 if( HasQList )
00232 f&=StoreQList( e );
00233 }
00234 break;
00235 case MIDI_SC_SET_CLOCK:
00236 {
00237 f&=StoreTime(e );
00238 if( HasQList )
00239 f&=StoreQList(e);
00240 }
00241 break;
00242 case MIDI_SC_OPEN_Q_LIST:
00243 case MIDI_SC_CLOSE_Q_LIST:
00244 {
00245 f&=StoreQList(e);
00246 }
00247 break;
00248 case MIDI_SC_OPEN_Q_PATH:
00249 case MIDI_SC_CLOSE_Q_PATH:
00250 {
00251 f&=StoreQPath(e );
00252 }
00253 break;
00254 default:
00255 f=false;
00256 break;
00257 }
00258
00259 e->PutEOX();
00260
00261 if( e->IsFull() )
00262 return false;
00263
00264 return f;
00265 }
00266
00267
00268 bool MIDIShowControlPacket::StoreTime( MIDISystemExclusive *e ) const
00269 {
00270 if( HasTime )
00271 {
00272 e->PutByte( Hours );
00273 e->PutByte( Minutes );
00274 e->PutByte( Seconds );
00275 e->PutByte( Frames );
00276 e->PutByte( FractFrames );
00277 return true;
00278 }
00279 else
00280 {
00281 return false;
00282 }
00283 }
00284
00285 bool MIDIShowControlPacket::ParseTime( const MIDISystemExclusive *e, int *pos)
00286 {
00287
00288 Hours = e->GetData( (*pos)++ );
00289 Minutes = e->GetData( (*pos)++ );
00290 Seconds = e->GetData( (*pos)++ );
00291 Frames = e->GetData( (*pos)++ );
00292 FractFrames = e->GetData( (*pos)++ );
00293 return true;
00294 }
00295
00296
00297 bool MIDIShowControlPacket::Store3Param( MIDISystemExclusive *e ) const
00298 {
00299 bool f=true;
00300
00301 if( HasQNumber )
00302 {
00303 f&=StoreAsciiNum( e, GetQNumber() );
00304 if( HasQList )
00305 {
00306 e->PutByte( 0 );
00307 f&=StoreAsciiNum( e, GetQList() );
00308
00309 if( HasQPath )
00310 {
00311 e->PutByte( 0 );
00312 f&=StoreAsciiNum( e, GetQPath() );
00313 }
00314
00315 }
00316 }
00317 return f;
00318 }
00319
00320 bool MIDIShowControlPacket::Parse3Param(
00321 const MIDISystemExclusive *e,
00322 int *pos
00323 )
00324 {
00325 bool f=true;
00326 MIDICue v;
00327
00328 if( e->GetData( *pos) !=0xf7 )
00329 {
00330
00331
00332
00333
00334 f=ParseAsciiNum( e, pos, &v );
00335
00336 if( f )
00337 {
00338 HasQNumber=true;
00339 SetQNumber( v );
00340
00341 if( *pos < e->GetLength() )
00342 {
00343
00344
00345
00346
00347 f=ParseAsciiNum( e, pos, &v );
00348
00349 if( f )
00350 {
00351 HasQList=true;
00352 SetQList( v );
00353
00354 if( *pos < e->GetLength() )
00355 {
00356
00357
00358
00359
00360 f=ParseAsciiNum( e, pos, &v );
00361
00362 if( f )
00363 {
00364 HasQPath=true;
00365 SetQPath( v );
00366
00367 }
00368
00369 }
00370 }
00371
00372 }
00373 }
00374 }
00375
00376 return f;
00377 }
00378
00379
00380 bool MIDIShowControlPacket::StoreSet( MIDISystemExclusive *e ) const
00381 {
00382 bool f=true;
00383 e->PutByte( (uchar)(GetControlNum() & 0x7f) );
00384 e->PutByte( (uchar)((GetControlNum()>>7) & 0x7f) );
00385
00386 e->PutByte( (uchar)(GetControlVal() & 0x7f) );
00387 e->PutByte( (uchar)((GetControlVal()>>7) & 0x7f) );
00388 if( HasTime )
00389 {
00390 f=StoreTime( e );
00391 }
00392 return f;
00393 }
00394
00395 bool MIDIShowControlPacket::ParseSet( const MIDISystemExclusive *e,int *pos )
00396 {
00397 ulong v;
00398
00399 v=e->GetData( (*pos)++ );
00400 v+=(e->GetData( (*pos)++ )<<7);
00401 SetControlNum( v );
00402
00403 v=e->GetData( (*pos)++ );
00404 v+=(e->GetData( (*pos)++ )<<7);
00405 SetControlVal( v );
00406
00407 if( e->GetData(*pos)!=0xf7 )
00408 {
00409 return ParseTime( e, pos );
00410 }
00411 else
00412 {
00413 return true;
00414 }
00415
00416 }
00417
00418 bool MIDIShowControlPacket::StoreFire( MIDISystemExclusive *e ) const
00419 {
00420 e->PutByte( (uchar)(GetMacroNum()) );
00421 return true;
00422 }
00423
00424 bool MIDIShowControlPacket::ParseFire( const MIDISystemExclusive *e, int *pos )
00425 {
00426 int v;
00427
00428 v=e->GetData( (*pos)++ );
00429 SetMacroNum( v );
00430
00431 return true;
00432 }
00433
00434 bool MIDIShowControlPacket::StoreQPath( MIDISystemExclusive *e ) const
00435 {
00436 if( HasQPath )
00437 {
00438 return StoreAsciiNum( e, GetQPath() );
00439 }
00440 else
00441 {
00442 return false;
00443 }
00444 }
00445
00446 bool MIDIShowControlPacket::ParseQPath( const MIDISystemExclusive *e, int *pos )
00447 {
00448 bool f=true;
00449 MIDICue v;
00450
00451 if( e->GetData( *pos) !=0xf7 )
00452 {
00453
00454
00455
00456
00457 f=ParseAsciiNum( e, pos, &v );
00458
00459 SetQPath( v );
00460 HasQPath=true;
00461
00462 return f;
00463 }
00464 else
00465 {
00466 return false;
00467 }
00468
00469 }
00470
00471 bool MIDIShowControlPacket::StoreQList( MIDISystemExclusive *e ) const
00472 {
00473 if( HasQList )
00474 {
00475 return StoreAsciiNum( e, GetQList() );
00476 }
00477 else
00478 {
00479 return false;
00480 }
00481 }
00482
00483 bool MIDIShowControlPacket::ParseQList( const MIDISystemExclusive *e, int *pos )
00484 {
00485 bool f=true;
00486 MIDICue v;
00487
00488 if( e->GetData( *pos) !=0xf7 )
00489 {
00490
00491
00492
00493
00494 f=ParseAsciiNum( e, pos, &v );
00495
00496 SetQList( v );
00497 HasQList=true;
00498
00499 return f;
00500 }
00501 else
00502 {
00503 return false;
00504 }
00505 }
00506
00507 bool MIDIShowControlPacket::StoreAscii( MIDISystemExclusive *e, const char *str ) const
00508 {
00509 while( *str )
00510 {
00511 e->PutByte( *str++ );
00512 }
00513 return true;
00514 }
00515
00516
00517 bool MIDIShowControlPacket::StoreAsciiNum(
00518 MIDISystemExclusive *e,
00519 const MIDICue & num
00520 ) const
00521 {
00522 char buf[32];
00523 bool f=false;
00524
00525 *buf='\0';
00526
00527 switch( num.GetNumValues() )
00528 {
00529 default:
00530 case 0:
00531 break;
00532 case 1:
00533 sprintf( buf, "%ld", num.GetV1() );
00534 break;
00535 case 2:
00536 sprintf( buf, "%ld.%ld", num.GetV1(), num.GetV2() );
00537 break;
00538 case 3:
00539 sprintf( buf, "%ld.%ld.%ld", num.GetV1(), num.GetV2(), num.GetV3() );
00540 break;
00541 }
00542
00543 f=StoreAscii( e, buf );
00544 return f;
00545 }
00546
00547 bool MIDIShowControlPacket::ParseAsciiNum(
00548 const MIDISystemExclusive *e,
00549 int *pos,
00550 MIDICue *num )
00551 {
00552 ulong v;
00553 uchar c;
00554 bool f;
00555
00556
00557
00558 c= e->GetData( *pos );
00559
00560 if( c==0xf7 )
00561 {
00562
00563
00564 return false;
00565 }
00566
00567 f=ParseAsciiNum( e, pos, &v );
00568
00569 if( f )
00570 {
00571 num->SetNumValues(1);
00572 num->SetV1( v );
00573 }
00574
00575
00576
00577
00578
00579 c= e->GetData( *pos );
00580
00581 if( c=='.' )
00582 {
00583
00584
00585
00586
00587 (*pos)++;
00588
00589 f=ParseAsciiNum( e,pos,&v );
00590
00591 if( f )
00592 {
00593 num->SetNumValues(2);
00594 num->SetV2( v );
00595 }
00596
00597
00598
00599
00600
00601 c= e->GetData( *pos );
00602
00603 if( c=='.' )
00604 {
00605
00606
00607
00608
00609 (*pos)++;
00610
00611 f=ParseAsciiNum( e,pos,&v );
00612
00613 if( f )
00614 {
00615 num->SetNumValues(2);
00616 num->SetV3( v );
00617 }
00618
00619
00620
00621
00622
00623 c= e->GetData( *pos );
00624
00625 if( c=='.' )
00626 {
00627
00628
00629
00630
00631 while( *pos < e->GetLength() )
00632 {
00633 c=e->GetData( (*pos) );
00634
00635 if( c==0xf7 || c==0x00 )
00636 {
00637 break;
00638 }
00639 (*pos)++;
00640 }
00641 }
00642 }
00643 }
00644
00645 while( ++(*pos) < e->GetLength() )
00646 {
00647 if( e->GetData( *pos )!=0 )
00648 break;
00649 }
00650
00651 return f;
00652 }
00653
00654
00655 bool MIDIShowControlPacket::ParseAsciiNum(
00656 const MIDISystemExclusive *e,
00657 int *pos,
00658 ulong *num
00659 )
00660 {
00661 bool f=true;
00662 ulong v=0;
00663 uchar c=0;
00664
00665
00666
00667
00668
00669 while(*pos<e->GetLength())
00670 {
00671 c=e->GetData( (*pos) );
00672
00673 if( c>='0' && c<='9' )
00674 {
00675
00676
00677
00678
00679
00680 v*=10;
00681 v+=c-'0';
00682 }
00683 else if( c==0x00 || c==0xf7 || c=='.' )
00684 {
00685
00686
00687
00688
00689 *num=v;
00690
00691 return f;
00692 }
00693 (*pos)++;
00694 }
00695
00696 *num=v;
00697 return false;
00698 }
00699
00700
00701
00702 }