@@ -404,7 +404,7 @@ var WebTones;
404
404
( function ( WebTones ) {
405
405
var StaffString = /** @class */ ( function ( ) {
406
406
function StaffString ( ) {
407
- this . SymbolRe = / ^ ( \| ) | ( \$ ) | ( [ # @ ] ? [ a - g ] \d \/ \d { 1 , 2 } ) $ / ;
407
+ this . SymbolRe = / ( ^ [ | $ ] $ ) | ( ^ [ # @ ] ? [ a - g ] \d \/ \d { 1 , 2 } $ ) / ;
408
408
}
409
409
StaffString . prototype . setCarret = function ( carret ) {
410
410
this . carret = carret ;
@@ -422,21 +422,14 @@ var WebTones;
422
422
for ( var s = 0 ; s < subNotes . length ; s ++ ) {
423
423
totalChars += s > 0 ? 1 : 0 ;
424
424
if ( subNotes [ s ] . length > 0 ) {
425
- if ( this . SymbolRe . test ( subNotes [ s ] ) ) {
426
- var symbol = this . createSymbol ( subNotes [ s ] ) ;
427
- symbol . chordFirst = n == 0 && s == 0 ;
428
- symbol . chordLast = n == notes . length - 1 && s == subNotes . length - 1 ;
429
- symbol . seqFirst = s == 0 ;
430
- symbol . seqLast = s == subNotes . length - 1 ;
431
- symbol . posBegin = totalChars ;
432
- symbol . posEnd = totalChars + subNotes [ s ] . length ;
433
- this . symbols . push ( symbol ) ;
434
- totalChars = symbol . posEnd ;
435
- }
436
- else {
437
- console . warn ( "Unknown symbol: " + subNotes [ s ] ) ;
438
- totalChars = totalChars + subNotes [ s ] . length ;
439
- }
425
+ var symbol = this . createSymbol ( subNotes [ s ] ) ;
426
+ symbol . chordFirst = n == 0 && s == 0 ;
427
+ symbol . chordLast = n == notes . length - 1 && s == subNotes . length - 1 ;
428
+ symbol . seqFirst = s == 0 ;
429
+ symbol . seqLast = s == subNotes . length - 1 ;
430
+ symbol . posBegin = totalChars ;
431
+ symbol . posEnd = totalChars = totalChars + subNotes [ s ] . length ;
432
+ this . symbols . push ( symbol ) ;
440
433
}
441
434
}
442
435
}
@@ -445,9 +438,14 @@ var WebTones;
445
438
} ;
446
439
StaffString . prototype . createSymbol = function ( noteName ) {
447
440
var symbol = new StaffSymbol ( ) ;
448
- var parts = noteName . trim ( ) . split ( '/' ) ;
449
- symbol . noteName = parts [ 0 ] ;
450
- symbol . noteDiv = parts . length == 2 ? new Number ( parts [ 1 ] ) . valueOf ( ) : 1 ;
441
+ if ( noteName . match ( this . SymbolRe ) ) {
442
+ var parts = noteName . split ( '/' ) ;
443
+ symbol . noteName = parts [ 0 ] ;
444
+ symbol . noteDiv = parts . length == 2 ? new Number ( parts [ 1 ] ) . valueOf ( ) : 1 ;
445
+ }
446
+ else {
447
+ symbol . error = true ;
448
+ }
451
449
return symbol ;
452
450
} ;
453
451
return StaffString ;
@@ -495,6 +493,7 @@ var WebTones;
495
493
_this . CodeC = 'c' . charCodeAt ( 0 ) ;
496
494
_this . Code4 = '4' . charCodeAt ( 0 ) ;
497
495
_this . LineColor = 'darkgray' ;
496
+ _this . ErrorColor = 'red' ;
498
497
_this . WallColor = 'black' ;
499
498
_this . NoteColor = 'black' ;
500
499
_this . SelectionColor = 'red' ;
@@ -536,7 +535,11 @@ var WebTones;
536
535
if ( this . notesDrawn == 0 )
537
536
this . drawStaff ( ) ;
538
537
var symbol = this . symbols [ index ] ;
539
- if ( symbol . noteName == '|' || symbol . noteName == '$' ) {
538
+ if ( symbol . error ) {
539
+ this . drawError ( ) ;
540
+ return index ;
541
+ }
542
+ else if ( symbol . noteName == '|' || symbol . noteName == '$' ) {
540
543
this . drawWall ( ) ;
541
544
return index ;
542
545
}
@@ -561,6 +564,15 @@ var WebTones;
561
564
this . drawLine ( this . LineColor , 0 , y2 , width , y2 ) ;
562
565
}
563
566
} ;
567
+ StaffStringPainter . prototype . drawError = function ( ) {
568
+ var x = this . getCurrentX ( ) ;
569
+ var y1 = this . getNoteY ( 3 ) ;
570
+ var y2 = this . getNoteY ( - 5 ) ;
571
+ this . context . beginPath ( ) ;
572
+ this . drawLine ( this . ErrorColor , x , y1 , x , y2 ) ;
573
+ this . context . stroke ( ) ;
574
+ this . notesDrawn += 0.5 ;
575
+ } ;
564
576
StaffStringPainter . prototype . drawWall = function ( ) {
565
577
var x = this . getCurrentX ( ) ;
566
578
var y1 = this . getNoteY ( 4 ) ;
@@ -596,24 +608,30 @@ var WebTones;
596
608
} ;
597
609
StaffStringPainter . prototype . drawNote = function ( index , flagDir ) {
598
610
var symbol = this . symbols [ index ] ;
599
- var noteX = this . getCurrentX ( ) ;
600
- var line = this . getNoteLine ( symbol . noteName ) ;
601
- if ( line != null ) {
602
- var noteY = this . getNoteY ( line ) ;
603
- if ( symbol . noteName . indexOf ( '#' ) > - 1 )
604
- this . drawNoteSharp ( noteX , noteY ) ;
605
- else if ( symbol . noteName . indexOf ( '@' ) > - 1 )
606
- this . drawNoteFlat ( noteX , noteY ) ;
607
- noteX = this . getCurrentX ( ) ;
608
- if ( this . carret >= symbol . posBegin && this . carret <= symbol . posEnd )
609
- this . drawNoteSelection ( noteX , noteY ) ;
610
- this . drawMiniLines ( line , noteX ) ;
611
- this . drawElipse ( noteX , noteY , symbol . noteDiv > 2 ) ;
612
- this . drawFlagPost ( line , symbol . noteDiv , flagDir , noteX , noteY ) ;
613
- this . updateWidth ( noteX ) ;
614
- this . updateHeight ( noteY ) ;
615
- }
616
- return noteX ;
611
+ if ( ! symbol . error ) {
612
+ var noteX = this . getCurrentX ( ) ;
613
+ var line = this . getNoteLine ( symbol . noteName ) ;
614
+ if ( line != null ) {
615
+ var noteY = this . getNoteY ( line ) ;
616
+ if ( symbol . noteName . indexOf ( '#' ) > - 1 )
617
+ this . drawNoteMark ( '♯' , noteX , noteY ) ;
618
+ else if ( symbol . noteName . indexOf ( '@' ) > - 1 )
619
+ this . drawNoteMark ( '♭' , noteX , noteY ) ;
620
+ noteX = this . getCurrentX ( ) ;
621
+ if ( this . carret >= symbol . posBegin && this . carret <= symbol . posEnd )
622
+ this . drawNoteSelection ( noteX , noteY ) ;
623
+ this . drawMiniLines ( line , noteX ) ;
624
+ this . drawElipse ( noteX , noteY , symbol . noteDiv > 2 ) ;
625
+ this . drawFlagPost ( line , symbol . noteDiv , flagDir , noteX , noteY ) ;
626
+ this . updateWidth ( noteX ) ;
627
+ this . updateHeight ( noteY ) ;
628
+ }
629
+ return noteX ;
630
+ }
631
+ else {
632
+ this . drawError ( ) ;
633
+ return this . getCurrentX ( ) ;
634
+ }
617
635
} ;
618
636
StaffStringPainter . prototype . drawMiniLine = function ( line , x ) {
619
637
var y = this . getNoteY ( line ) ;
@@ -623,14 +641,10 @@ var WebTones;
623
641
this . context . strokeStyle = this . NoteColor ;
624
642
this . context . stroke ( ) ;
625
643
} ;
626
- StaffStringPainter . prototype . drawNoteSharp = function ( noteX , noteY ) {
644
+ StaffStringPainter . prototype . drawNoteMark = function ( mark , noteX , noteY ) {
627
645
this . context . font = this . noteFont ;
628
- this . context . fillText ( "♯" , noteX , noteY + this . noteWidth2 ) ;
629
- this . notesDrawn += 1.0 ;
630
- } ;
631
- StaffStringPainter . prototype . drawNoteFlat = function ( noteX , noteY ) {
632
- this . context . font = this . noteFont ;
633
- this . context . strokeText ( "♭" , noteX , noteY + this . noteWidth2 ) ;
646
+ this . context . fillStyle = this . NoteColor ;
647
+ this . context . fillText ( mark , noteX , noteY + this . noteWidth2 ) ;
634
648
this . notesDrawn += 1.0 ;
635
649
} ;
636
650
StaffStringPainter . prototype . drawMiniLines = function ( line , noteX ) {
@@ -689,11 +703,12 @@ var WebTones;
689
703
var dashes = this . getNoteDashes ( symbol1 . noteDiv ) ;
690
704
var line1 = this . getNoteLine ( symbol1 . noteName ) ;
691
705
var line2 = this . getNoteLine ( symbol2 . noteName ) ;
692
- if ( line1 && line2 ) {
706
+ if ( line1 != null && line2 != null ) {
693
707
var fx1 = this . getFlagX ( noteX1 , flagDir ) ;
694
708
var fy1 = this . getFlagY ( line1 , flagDir ) ;
695
709
var fx2 = this . getFlagX ( noteX2 , flagDir ) ;
696
710
var fy2 = this . getFlagY ( line2 , flagDir ) ;
711
+ this . context . strokeStyle = this . NoteColor ;
697
712
for ( var i = 0 ; i < dashes ; i ++ ) {
698
713
this . context . beginPath ( ) ;
699
714
this . context . moveTo ( fx1 , fy1 + i * flagDir * this . noteWidth2 ) ;
@@ -725,7 +740,9 @@ var WebTones;
725
740
} ;
726
741
StaffStringPainter . prototype . findSeqLast = function ( index ) {
727
742
for ( var i = index + 1 ; i < this . symbols . length ; i ++ )
728
- if ( this . symbols [ i ] . seqLast )
743
+ if ( this . symbols [ i ] . error || this . symbols [ index ] . noteDiv != this . symbols [ i ] . noteDiv )
744
+ return index != i - 1 ? i - 1 : - 1 ;
745
+ else if ( this . symbols [ i ] . seqLast )
729
746
return i ;
730
747
return - 1 ;
731
748
} ;
@@ -828,9 +845,13 @@ var WebTones;
828
845
}
829
846
} ;
830
847
StaffStringPlayer . prototype . processNote = function ( symbol ) {
831
- // todo implement tempo
832
- var durationSec = 3 / symbol . noteDiv ;
833
- this . timeSec += this . instrument . playNote ( this . timeSec , symbol . noteName , durationSec ) ;
848
+ if ( ! symbol . error ) {
849
+ // todo implement tempo
850
+ var durationSec = 3 / symbol . noteDiv ;
851
+ this . timeSec += this . instrument . playNote ( this . timeSec , symbol . noteName , durationSec ) ;
852
+ }
853
+ else
854
+ this . timeSec += this . instrument . playNote ( this . timeSec , 'a2' , 0.5 ) ;
834
855
} ;
835
856
return StaffStringPlayer ;
836
857
} ( WebTones . StaffString ) ) ;
0 commit comments