!RunImage 140 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
REM >!Maestro.!RunImage

REM Copyright 2012 Castle Technology Ltd
REM
REM Licensed under the Apache License, Version 2.0 (the "License");
REM you may not use this file except in compliance with the License.
REM You may obtain a copy of the License at
REM
REM     http://www.apache.org/licenses/LICENSE-2.0
REM
REM Unless required by applicable law or agreed to in writing, software
REM distributed under the License is distributed on an "AS IS" BASIS,
REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
REM See the License for the specific language governing permissions and
REM limitations under the License.
REM

REM (c) Acorn Computers 1988

Task_h%=0

REM TRACE TO "serial:"

ROOL's avatar
ROOL committed
24 25 26
DIM message_buffer% 256, err_block% 204
Messages_h% = FNopen_messagefile("<Maestro$Dir>.Messages")
maestro$ = FNmessage_lookup(Messages_h%, "Maestro")
27

28 29
SPACE%=HIMEM-END :REM check enough space available. Nice friendly way to exit...
IF SPACE%<100000 STOP
30

31
ON ERROR PROCerror:END
ROOL's avatar
ROOL committed
32

33 34 35
INITIALISED%=FNinitialise
OSCLI ("Set Maestro$Running Yes")
ON ERROR PROCerror
36

37
REM get any filename passed as an argument
38 39 40 41 42 43
SYS "OS_GetEnv" TO EnvStr$
IF INSTR(EnvStr$," -quit ") THEN
  I%=INSTR(EnvStr$,"""")
  I%=INSTR(EnvStr$,"""",I%+1)
  REPEATI%+=1:UNTILMID$(EnvStr$,I%,1)<>" "
  f$=MID$(EnvStr$,I%)
44 45 46 47
  IF f$ <> "" THEN
    IF FNvalid_number_of_gates (f$) THEN
      PROCload_music(f$)
    ELSE
ROOL's avatar
ROOL committed
48
      ret_code% = FNCheckOK (FNmessage_lookupN(Messages_h%, "TooBig", f$, "", "", ""), 1)
49 50
    ENDIF
  ENDIF
51 52 53
ENDIF

REPEAT
ROOL's avatar
ROOL committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  IF PLAYING% THEN PROCCheckQ
  SYS "Wimp_Poll",(NOT(PLAYING% OR SCORING% OR AwaitingAck% OR SCROLLING%) AND 1),Block% TO reason%
  REM Recheck since poll might have stayed away for a while
  IF PLAYING% THEN PROCCheckQ 
  CASE reason% OF
    WHEN 0:REM Null Event
      IF AwaitingAck% THEN
        IF FNCheckOKTag("BadData",3) THEN
          AwaitingAck%=FALSE
          CHANGED%=wasCHANGED%
        ENDIF
      ENDIF
      IF SCORING% THEN PROCsymbol_pointer
      IF SCROLLING% THEN PROCCheckScroll
    WHEN 1:REM Redraw Window
69
      PROCredraw_window_request
ROOL's avatar
ROOL committed
70 71 72 73 74 75
    WHEN 2:REM Open Window      
      PROCOpenWindow(Block%)
    WHEN 3:REM Close Window
      PROCCloseWindow(Block%!0)
    WHEN 4:REM Pointer Leaving Window
      IF Block%!0=ScoreWind_h% THEN
76 77 78 79
        PROCrelease
        wasSCORING%=SCORING%
        SCORING%=FALSE
      ENDIF
ROOL's avatar
ROOL committed
80 81 82 83 84 85 86
    WHEN 5:REM Pointer Entering Window
      IF Block%!0=ScoreWind_h% THEN
        SCORING%=(wasSCORING% AND NOT stopSCORING%)=TRUE
      ENDIF
    WHEN 6:REM Mouse Click
      PROCmouse_button_click(Block%!12,Block%!16,Block%!8,Block%!0,Block%!4)
    WHEN 7:REM User Drag Box
87
      PROCUserDragBox
ROOL's avatar
ROOL committed
88 89 90 91 92 93 94
    WHEN 8:REM Key Pressed
      PROCKeyPressed(Block%!0,Block%!24)
    WHEN 9:REM Menu Selection
      PROCMenuSelect(Block%!0,Block%!4,Block%!8)
    WHEN 10:REM Scroll Request
      PROCScrollReq(Block%!32,Block%!36)
    WHEN 17,18:REM User Message
95 96 97 98 99 100 101 102 103 104 105 106 107
      PROCreceive
  ENDCASE
UNTIL FALSE
END



DEF PROCCheckQ
  B1%=B2%: B2%=BEAT
  IFB2%<B1% PROCplay_bar
ENDPROC

DEF PROCreceive
ROOL's avatar
ROOL committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
LOCAL task%,ref%
LOCAL f$,wx%,wy%
task%=Block%!4
ref%=Block%!8
REM Ignore messages from this task
IF task%=Task_h% THEN ENDPROC
REM Process Messages
CASE Block%!16 OF
  WHEN 0:REM Quit
    PROCterminate
  WHEN 1:REM DataSave
    IF NOT SAVING% THEN
      doing_scrap_load%=TRUE
      Block%!0=64
      Block%!12=ref%  
      Block%!16=2                   :REM DataSaveAck
      Block%!36=-1                  :REM Unsafe data
      Block%!40=&AF1                :REM Filetype
      $(Block%+44)="<Wimp$Scrap>"   :REM Path
      ?(Block%+44+13)=0
      SYS "Wimp_SendMessage",17,Block%,task%
    ENDIF
  WHEN 8:REM PreQuit
    IF CHANGED% THEN
      IF (Block%!20 AND %1)=0 THEN
        shutdown%=TRUE              :REM Flag for restarting quit
134
      ELSE
ROOL's avatar
ROOL committed
135
        shutdown%=FALSE
136
      ENDIF
ROOL's avatar
ROOL committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
      REM Acknowledge PreQuit message
      Block%!0=20
      Block%!12=ref%
      Block%!16=0
      quit_sender%=task%
      SYS "Wimp_SendMessage",19,Block%,task%
      PROCOpenDiscardCancelBox
    ELSE
      PROCterminate
    ENDIF
  WHEN 2:REM DataSaveAck
    IF SAVING% THEN
      wasCHANGED%=CHANGED%
      PROCsave_music(FNGetStr(Block%+44))
      SAVING%=FALSE
      REM Send DataLoad message
      Block%!0=256
      Block%!12=my_ref%
      Block%!16=3                   :REM DataLoad
      Block%!36=-1                  :REM Unsafe data
      SYS "Wimp_SendMessage",17,Block%,task%
      AwaitingAck%=TRUE
      SYS "Wimp_CreateMenu",,-1
    ENDIF
  WHEN 3:REM DataLoad
    REM Check filetype
    f$=FNGetStr(Block%+44)
    IF FNvalid_file_type(f$) THEN   
      PROCDataLoadAck(f$,ref%,task%)   
    ELSE
      ret_code%=FNCheckOK(FNmessage_lookupN(Messages_h%,"NotMusic",f$,"","",""),1)
    ENDIF
  WHEN 4:REM DataLoadAck
    AwaitingAck%=FALSE
  WHEN 5:REM DataOpen
    f$=FNGetStr(Block%+44)
    IF Block%!40=MusicFileType% THEN PROCDataLoadAck(f$,ref%,task%)
  WHEN 10:REM SaveDesktop
    desktop_save_handle%=Block%!20
    SYS "OS_ReadVarVal","Maestro$Dir",Block%,256,0,0 TO ,,len%
    Block%?len%=13
    BPUT #desktop_save_handle%,"Run "+$Block%
  WHEN 11:REM DeviceClaim
    IF Block%!20=7 THEN
      f$=FNmessage_lookup(Messages_h%,"SSIU")
      Block%!0=(32+LEN(f$)) AND NOT(3)
      Block%!12=0
      Block%!16=12                  :REM DeviceInUse
      $(Block%+28)=f$+CHR$(0)       :REM Text
      SYS "Wimp_SendMessage",17,Block%,Block%!4
    ENDIF
  WHEN &502:REM HelpRequest
    PROCHelp(Block%!32,Block%!36,ref%,task%)
  WHEN &400C0:REM Menu Warning
    wx%=Block%!24
    wy%=Block%!28 
    CASE Block%!20 OF
      WHEN Bar_h%:       
        PROCgoto_init
        SYS "Wimp_CreateSubMenu",,Bar_h%,wx%,wy%
      WHEN Print_h%:
        PROCprint_init
        SYS "Wimp_CreateSubMenu",,Print_h%,wx%,wy%
    ENDCASE
  WHEN &400C1:REM ModeChange
    PROCgetmodeinfo(FALSE)
ENDCASE
204 205
ENDPROC

ROOL's avatar
ROOL committed
206 207 208 209 210 211 212 213
DEF PROCDataLoadAck (f$,ref%,task%)
IF FNvalid_number_of_gates(f$) THEN
  IF CHANGED% THEN
    load_pending%=TRUE
    pending_filename$=f$
    PROCOpenDiscardCancelBox
  ELSE
    PROCload_music(f$)
214
  ENDIF
ROOL's avatar
ROOL committed
215 216 217 218 219 220 221 222 223
  REM Acknowledge message
  Block%!0=256
  Block%!12=ref%
  Block%!16=4    :REM DataLoadAck
  SYS "Wimp_SendMessage",17,Block%,task% 
  IF doing_scrap_load% THEN doing_scrap_load%=FALSE
ELSE
  ret_code%=FNCheckOK(FNmessage_lookupN(Messages_h%,"TooBig",f$,"","",""),1)
ENDIF
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
ENDPROC

DEF FNvalid_number_of_gates (filename$)

  LOCAL fhandle%,M$,loop%,ret%,gates%, bomb%
  ret%=TRUE
  M$=""
  bomb%=FALSE
REM  SYS "OS_Find", 79, filename$ TO fhandle%
  fhandle% = OPENIN (filename$)
  FOR loop% = 1 TO 8
    IF NOT EOF#fhandle% THEN M$ += CHR$(BGET#fhandle%) ELSE CLOSE#fhandle%:=TRUE : REM This is an invalid file, load_music picks it up
  NEXT loop%
  IF M$ = "Maestro"+CHR$(10) THEN
    IF BGET#fhandle% = 2 THEN
      REPEAT
        CASE BGET#fhandle% OF
          WHEN 1
            INPUT#fhandle%, gates%
            IF gates% > Max_Gate% THEN
              ret% = FALSE
            ENDIF
            bomb%=TRUE : REM Stop here, we've got what we want

          WHEN 2
            FOR loop%=1 TO 3
              INPUT#fhandle%,gates%
            NEXT loop%

          WHEN 3
            FOR loop%=1 TO 17
              INPUT#fhandle%,gates%
            NEXT loop%

          WHEN 4, 5
            FOR loop%=1 TO 9
              INPUT#fhandle%,gates%
            NEXT loop%

          WHEN 6
            FOR loop%=1 TO 2
              INPUT#fhandle%,gates%
            NEXT loop%

          WHEN 7
            INPUT#fhandle%, M$

          WHEN 8
            FOR loop%=1 TO 8
              INPUT#fhandle%,M$
            NEXT loop%

          WHEN 9
            FOR loop%=1 TO 8
              INPUT#fhandle%,gates%
            NEXT loop%

          OTHERWISE : REM !!
        ENDCASE
      UNTIL bomb% OR EOF#fhandle%
    ENDIF
  ENDIF
  CLOSE#fhandle%
=ret%



DEF FNvalid_file_type (filename$)

  LOCAL laddr%, type%, ret_code%

  IF filename$ <> "" THEN
296
    SYS"OS_File",5,filename$ TO type%,,laddr%
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
  ENDIF

  CASE type% OF
    WHEN 1
      IF ((laddr%>>20)AND&FFF)=&FFF THEN
        IF (laddr%>>8 AND &FFF) = MusicFileType% THEN
          =TRUE
        ELSE
          =FALSE
        ENDIF
      ENDIF
    WHEN 2
      =FALSE
  ENDCASE

=FALSE

ROOL's avatar
ROOL committed
314 315 316 317 318 319 320 321
DEF PROCHelp (window%,icon%,ref%,task%)
LOCAL text$
CASE window% OF
  WHEN -2:
    text$=FNmessage_lookup(Messages_h%,"IconHelp")
  WHEN ScoreWind_h%:
    IF SCORING% AND SCRIBE%(drawn%) THEN
      text$=FNmessage_lookup(Messages_h%,"ScoreHelp0")
322
    ELSE
ROOL's avatar
ROOL committed
323
      text$=FNmessage_lookup(Messages_h%,"ScoreHelp1")
324
    ENDIF
ROOL's avatar
ROOL committed
325 326 327 328 329 330 331
  WHEN NotesPane_h%:
    IF icon%>0 AND icon%<17 THEN
      text$=FNmessage_lookup(Messages_h%,"NH"+STR$icon%)
      text$=FNmessage_lookupN(Messages_h%,"SelectObject",text$,"","","")
    ENDIF
  WHEN SharpsPane_h%:
    CASE icon% OF
Chris Wraight's avatar
Chris Wraight committed
332
      WHEN 2,3,4,5,6,7,8,9,10,11,12,17,19,23:
ROOL's avatar
ROOL committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
        text$=FNmessage_lookup(Messages_h%,"AH"+STR$icon%)
        text$=FNmessage_lookupN(Messages_h%,"SelectObject",text$,"","","")
      WHEN 14,16,18,20:
        text$=FNmessage_lookup(Messages_h%,"AH"+STR$icon%)
      WHEN 13:REM Barlines
        text$=FNmessage_lookup(Messages_h%,"B"+STR$(BarlineType%))
        text$=FNmessage_lookupN(Messages_h%,"SelectObject",text$,"","","")
      WHEN 15:REM Clefs
        text$=FNmessage_lookup(Messages_h%,"C"+STR$(ClefType%))
        text$=FNmessage_lookupN(Messages_h%,"SelectObject",text$,"","","")
    ENDCASE 
  WHEN InstrWind_h%:
    CASE icon% OF
      WHEN 20,21,22,23,24,25,26,27:REM Instruments popup
        text$=FNmessage_lookup(Messages_h%,"IH0")
      WHEN 36,37,38,39,40,41,42,43:REM Volumes (down)
        text$=FNmessage_lookup(Messages_h%,"IH1")
      WHEN 44,45,46,47,48,49,50,51:REM Stereos
        text$=FNmessage_lookup(Messages_h%,"IH2")
      WHEN 62,63,64:text$=FNmessage_lookup(Messages_h%,"IH"+STR$icon%)
      OTHERWISE:text$=FNmessage_lookup(Messages_h%,"IH")
    ENDCASE
  WHEN StaveWind_h%:
    CASE icon% OF
      WHEN 2,3,4,5:text$=FNmessage_lookup(Messages_h%,"SH1")
      WHEN 6,7,8:text$=FNmessage_lookup(Messages_h%,"SH"+STR$icon%)
      OTHERWISE:text$=FNmessage_lookup(Messages_h%,"SH")
    ENDCASE
  WHEN Save_h%:
    CASE icon% OF
      WHEN 0,1,2:text$=FNmessage_lookup(Messages_h%,"SVH"+STR$icon%)
      OTHERWISE:text$=FNmessage_lookup(Messages_h%,"SVH")
    ENDCASE 
  WHEN QuitQuery_h%:
    CASE icon% OF
      WHEN 0,2:text$=FNmessage_lookup(Messages_h%,"QH"+STR$icon%)
      OTHERWISE:text$=FNmessage_lookup(Messages_h%,"QH")
    ENDCASE
  WHEN ClearQuery_h%:
    CASE icon% OF
      WHEN 0,2,3:text$=FNmessage_lookup(Messages_h%,"CH"+STR$icon%)
      OTHERWISE:text$=FNmessage_lookup(Messages_h%,"CH")
     ENDCASE
  WHEN Print_h%:
    CASE icon% OF
      WHEN 1,2:text$=FNmessage_lookup(Messages_h%,"PH"+STR$icon%)
      OTHERWISE:text$=FNmessage_lookup(Messages_h%,"PH")
    ENDCASE
  WHEN TimeSig_h%:
    CASE icon% OF
      WHEN 2,3,4,5,6:text$=FNmessage_lookup(Messages_h%,"TS"+STR$icon%)
      OTHERWISE:text$=FNmessage_lookup(Messages_h%,"TS")
    ENDCASE 
  OTHERWISE:
    SYS "Wimp_GetMenuState",1,Block%,window%,icon%
    CASE CurrentMenu% OF
      WHEN MainMenu%:
        IF Block%!0>-1 THEN
          text$=STR$(Block%!0)
          IF Block%!4>-1 THEN
            text$=text$+"."+STR$(Block%!4)
            IF Block%!8>-1 THEN text$=text$+".0"
395
          ENDIF
ROOL's avatar
ROOL committed
396 397 398 399 400 401 402 403
        ENDIF
        IF text$<>"" THEN text$=FNmessage_lookup(Messages_h%,"MH"+text$)
      WHEN IconMenu%:
        IF Block%!0>-1 THEN
          text$=FNmessage_lookup(Messages_h%,"ICH"+STR$(Block%!0))
        ENDIF
      OTHERWISE:text$=""
    ENDCASE
404
ENDCASE
ROOL's avatar
ROOL committed
405 406 407 408 409 410 411 412
REM If HelpText found, send HelpReply message
IF text$<>"" THEN
  Block%!0=(((20+LEN(text$)+1) DIV 4)*4)+4
  Block%!12=ref%
  Block%!16=&503
  $(Block%+20)=text$+CHR$(0)
  SYS "Wimp_SendMessage",17,Block%,task%
ENDIF
413 414 415 416
ENDPROC

DEF PROCredraw_window_request
LOCAL R%
ROOL's avatar
ROOL committed
417
SYS "Wimp_RedrawWindow",,Window%+handle% TO R%
418 419 420
WHILE R%
  IF Window%!handle% = ScoreWind_h% PROCdraw_staves:IF PLAYING% PROCCheckQ
REM try to avoid interruptions in the music
ROOL's avatar
ROOL committed
421
  SYS "Wimp_GetRectangle",,Window%+handle% TO R%
422 423 424
  ENDWHILE
ENDPROC

ROOL's avatar
ROOL committed
425 426 427 428 429 430 431
DEF PROCmouse_button_click (window%,icon%,button%,mx%,my%)
IF (window%<>QuitQuery_h%) AND shutdown% THEN shutdown%=FALSE
IF button%=2 THEN
  PROCStopScoring
  IF window%=-2 THEN
    SYS "Wimp_CreateMenu",,IconMenu%,mx%-64,96+44*3
    CurrentMenu%=IconMenu%
432
  ELSE
ROOL's avatar
ROOL committed
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
    IF window%=ScoreWind_h% THEN
      SYS "Wimp_CreateMenu",,MainMenu%,mx%-64,my%
      CurrentMenu%=MainMenu% 
    ENDIF
  ENDIF
ELSE
  CASE window% OF
    WHEN -2:REM Iconbar click
      IF FirstOpen% THEN
        PROCscore_init
      ELSE
        Block%!0=ScoreWind_h%
        SYS "Wimp_GetWindowState",,Block%
        Block%!28=-1
        PROCOpenWindow(Block%)
        ScoreClosed%=FALSE
        IF PLAYING% THEN PROCCheckScroll
      ENDIF      
    WHEN QuitQuery_h%:
      REM This window is used in a couple of circumstances:
      REM 1. when we want to quit
      REM 2. when we're about to overwrite a bit of music with a new piece
      IF icon%=2 THEN
456
        IF load_pending% THEN
ROOL's avatar
ROOL committed
457
          PROCload_music(pending_filename$)
458 459
        ELSE
          IF shutdown% THEN
ROOL's avatar
ROOL committed
460 461 462 463
            SYS "Wimp_GetCaretPosition",,Block%
            REM Restart PreQuit, send CTRL-SHIFT-F12
            Block%!24=&1FC                 
            SYS "Wimp_SendMessage",8,Block%,quit_sender%
464 465 466
          ENDIF
          PROCterminate
        ENDIF
ROOL's avatar
ROOL committed
467
        SYS "Wimp_CreateMenu",,-1
468
      ELSE
ROOL's avatar
ROOL committed
469 470 471 472 473
        REM We've decided not to quit
        IF shutdown% THEN shutdown%=FALSE
        REM Cancel any impending load 
        IF load_pending% THEN load_pending%=FALSE 
        SYS "Wimp_CreateMenu",,-1
474
      ENDIF
ROOL's avatar
ROOL committed
475 476 477
    WHEN ClearQuery_h%:
      CASE icon% OF
        WHEN 2:REM Discard
478
          PROCClearAllMusic
ROOL's avatar
ROOL committed
479 480 481
          Block%!0=ScoreWind_h%
          SYS "Wimp_GetWindowState",,Block%
          Block%!20=0
482
          LHBAR%=0
ROOL's avatar
ROOL committed
483 484 485 486 487 488
          SYS "Wimp_OpenWindow",,Block%
          SYS "Wimp_CreateMenu",,-1
        WHEN 0:REM Save
          SYS "Wimp_CreateMenu",,Save_h%,mx%,my%
        WHEN 3:REM Cancel
          SYS "Wimp_CreateMenu",,-1
489
      ENDCASE
ROOL's avatar
ROOL committed
490 491 492 493 494 495 496 497 498
    WHEN Print_h%:
      PROCprint_click(icon%,button%)
    WHEN NotesPane_h%,SharpsPane_h%,ScoreWind_h%:
      PROCscore_click(window%,icon%,button%)
    WHEN InstrWind_h%:
      PROCinstruments_click(icon%,mx%,button%)
    WHEN StaveWind_h%:
      PROCstaves_click(icon%,button%)
    WHEN Save_h%:
499
      PROCStopScoring
ROOL's avatar
ROOL committed
500 501 502 503 504 505 506 507 508
      IF icon%=0 THEN
        PROCsave_music(FNwimp_geticontext(Save_h%,1))
        PROCCloseWindow(window%)
        SYS "Wimp_CreateMenu",,-1
      ENDIF
      IF icon%=2 THEN
        LOCAL x%,y%                          : REM dragging icon
        Window%!handle%=window%
        SYS "Wimp_GetWindowState",,Window%+handle%
509 510 511
        ysize%=Window%!y1%-Window%!y0%
        x%=Window%!x0%
        y%=Window%!y0%
ROOL's avatar
ROOL committed
512 513 514
        !Window%=window%
        Window%!4=icon%
        SYS "Wimp_GetIconState",,Window% : REM returns icon box in right place for drag box
515 516 517 518
        Window%!8  += x%
        Window%!12 += y% + ysize%
        Window%!16 += x%
        Window%!20 += y% + ysize%
ROOL's avatar
ROOL committed
519
    REM get size idan appropriate part of block: parent box=screen boundary
520 521 522 523 524 525 526
        Window%!24 = 0
        Window%!28 = 0
        Window%!32 = S_Width%
        Window%!36 = S_Height%
        !Window%=0
        Window%!4=5: REM fixed size drag box
        SAVING%=TRUE
ROOL's avatar
ROOL committed
527 528 529
        SYS "OS_Byte", 161, &1C TO ,,icon%
        IF icon% AND 2 THEN
           SYS "XDragASprite_Start", &C5, 1, "file_af1", Window% + 8 
530
        ELSE
ROOL's avatar
ROOL committed
531
           SYS "Wimp_DragBox",,Window%
532 533
        ENDIF
      ENDIF
ROOL's avatar
ROOL committed
534 535 536 537 538 539 540 541 542 543 544
    WHEN TimeSig_h%:
      PROCtimesig_click(icon%,button%)
    WHEN Clef_h%:
      CASE icon% OF
        WHEN 0:REM Treble clef
          PROCwimp_seticonval(SharpsPane_h%,15,"Sstreble")
          ClefType%=icon%  
        WHEN 1:REM Bass clef
          PROCwimp_seticonval(SharpsPane_h%,15,"Ssbass")
          ClefType%=icon% 
      ENDCASE
ROOL's avatar
ROOL committed
545
      IF FNwimp_geticonstate(SharpsPane_h%,15,2) THEN PROCattach_clef
ROOL's avatar
ROOL committed
546 547 548 549 550 551 552 553 554 555
      IF button%=4 THEN PROCCloseWindow(Clef_h%)      
    WHEN Barline_h%:
      CASE icon% OF
        WHEN 0:REM Single barline
          PROCwimp_seticonval(SharpsPane_h%,13,"Ssbarline")
          BarlineType%=icon%
        WHEN 1:REM Double barline
          PROCwimp_seticonval(SharpsPane_h%,13,"Ssdbar")
          BarlineType%=icon% 
      ENDCASE
ROOL's avatar
ROOL committed
556
      IF FNwimp_geticonstate(SharpsPane_h%,13,2) THEN PROCattach_barline
ROOL's avatar
ROOL committed
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
      IF button%=4 THEN PROCCloseWindow(Barline_h%)      
  ENDCASE
ENDIF
ENDPROC  

REM // Instruments //

DEF PROCinstruments_init (x%,y%)
LOCAL i%,c%,text$
REM Create Voices menu
PROCCheckInstalledVoices
FOR i%=0 TO NVoices%-1
  PROCwimp_setmenutext(VoiceMenu%,i%,LEFT$(Voice$(i%+1),31))  
NEXT
VoiceMenu%!(28+(24*(NVoices%-1)))=128
REM Set volume/stereo fields
FOR c%=0 TO 7
  text$=FNmessage_lookup(Messages_h%,"V"+STR$(Volumes%(c%)))
  PROCwimp_seticontext(InstrWind_h%,c%+28,LEFT$(text$,3))
  PROCinstruments_slider(c%+44,Stereo_Position%(c%))
NEXT
ROOL's avatar
ROOL committed
578
PROCinstruments_fill(OutputType%)
ROOL's avatar
ROOL committed
579 580 581 582
PROCwimp_opendialogue(InstrWind_h%,ScoreWind_h%,1)
SYS "Wimp_SetCaretPosition",InstrWind_h%,-1
ENDPROC

ROOL's avatar
ROOL committed
583
DEF PROCinstruments_fill (type%)
ROOL's avatar
ROOL committed
584 585
LOCAL text$,c%
REM Set voice/MIDI channel fields
ROOL's avatar
ROOL committed
586
text$="OT"+STR$(type%)
ROOL's avatar
ROOL committed
587 588
PROCwimp_seticontext(InstrWind_h%,61,FNmessage_lookup(Messages_h%,text$))
FOR c%=0 TO 7
ROOL's avatar
ROOL committed
589
  CASE type% OF
ROOL's avatar
ROOL committed
590 591 592 593 594 595 596
    WHEN 0:REM Internal sound system
      text$=LEFT$(Voice$(Instrument%(c%,1)),20)
    WHEN 1:REM MIDI device
      text$=FNmessage_lookupN(Messages_h%,"MIDIvoice",STR$(MIDIChannel%(c%)),"","","")
  ENDCASE
  PROCwimp_seticontext(InstrWind_h%,c%+12,text$)
NEXT
ROOL's avatar
ROOL committed
597 598 599
REM Set Output Menu options
PROCwimp_setmenustate(OutputMenu%,1,22,NOT MIDIpresent%)
PROCwimp_radiotick(OutputMenu%,type%)
ROOL's avatar
ROOL committed
600 601 602 603 604 605 606 607 608 609
ENDPROC

DEF PROCinstruments_click (icon%,mx%,button%)
LOCAL c%,v%,text$,inc%,wx%,ix%
PROCStopScoring
PROCChangedScore
REM Voices
IF icon%>19 AND icon%<28 THEN
  REM Set tick in VoiceMenu/ChannelMenu
  PopUpIcon%=icon%-8
ROOL's avatar
ROOL committed
610 611
  CASE FNwimp_getmenustate(OutputMenu%,0,0) OF
    WHEN TRUE:REM Internal sound system
ROOL's avatar
ROOL committed
612 613 614 615 616 617 618
      text$=FNwimp_geticontext(InstrWind_h%,icon%-8)
      FOR i%=0 TO NVoices%-1
        IF FNwimp_getmenutext(VoiceMenu%,i%)=text$ THEN
          PROCwimp_radiotick(VoiceMenu%,i%)
        ENDIF
      NEXT
      CurrentMenu%=FNwimp_popup(InstrWind_h%,VoiceMenu%,icon%)
ROOL's avatar
ROOL committed
619 620 621 622 623
    WHEN FALSE:REM MIDI
      text$=FNwimp_geticontext(InstrWind_h%,icon%-8)
      text$=RIGHT$(text$,2)
      IF LEFT$(text$,1)=" " THEN text$=RIGHT$(text$,1)
      PROCwimp_radiotick(ChannelMenu%,VAL(text$)-1)
ROOL's avatar
ROOL committed
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
      CurrentMenu%=FNwimp_popup(InstrWind_h%,ChannelMenu%,icon%)
  ENDCASE  
ENDIF
REM Volumes
IF icon%>35 AND icon%<44 THEN
  REM Set tick in VolumeMenu
  v%=FNinstruments_getvol(FNwimp_geticontext(InstrWind_h%,icon%-8))
  PROCwimp_radiotick(VolumeMenu%,v%)
  PopUpIcon%=icon%-8   
  CurrentMenu%=FNwimp_popup(InstrWind_h%,VolumeMenu%,icon%)
ENDIF
REM Stereo positions
IF icon%>43 AND icon%<52 THEN
  Block%!0=InstrWind_h%
  SYS "Wimp_GetWindowState",,Block%
  wx%=Block%!4
  Block%!4=44
  SYS "Wimp_GetIconState",,Block%
  ix%=Block%!8
  PROCinstruments_slider(icon%,(mx%-wx%-ix%) DIV 24)
ENDIF
REM Output PopUp
IF icon%=62 THEN
  CurrentMenu%=FNwimp_popup(InstrWind_h%,OutputMenu%,icon%)
ENDIF
REM Cancel button
IF icon%=63 AND button%=4 THEN PROCwimp_closedialogue(InstrWind_h%,ScoreWind_h%)
REM Set button
IF icon%=64 THEN
  FOR c%=0 TO 7
    REM Voices/MIDI channels
    text$=FNwimp_geticontext(InstrWind_h%,c%+12)
ROOL's avatar
ROOL committed
656 657
    CASE FNwimp_getmenustate(OutputMenu%,0,0) OF
      WHEN TRUE:REM Internal sound system
ROOL's avatar
ROOL committed
658 659 660 661
        FOR i%=1 TO NVoices%
          IF text$=Voice$(i%) THEN v%=i%
        NEXT
        PROCAttachVoice(c%,v%)
ROOL's avatar
ROOL committed
662 663
        OutputType%=0
      WHEN FALSE:REM MIDI device
ROOL's avatar
ROOL committed
664 665 666
        text$=RIGHT$(text$,2)
        IF LEFT$(text$,1)=" " THEN text$=RIGHT$(text$,1)
        MIDIChannel%(c%)=VAL(text$)
ROOL's avatar
ROOL committed
667
        OutputType%=1
668
    ENDCASE
ROOL's avatar
ROOL committed
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
    REM Set Volumes
    text$=FNwimp_geticontext(InstrWind_h%,c%+28)
    Volumes%(c%)=FNinstruments_getvol(text$)
    REM Set Stereo positions
    Block%!4=c%+44
    SYS "Wimp_GetIconState",,Block%
    ix%=Block%!8
    Block%!4=c%+52
    SYS "Wimp_GetIconState",,Block%
    mx%=Block%!8+12
    Stereo_Position%(c%)=(mx%-ix%) DIV 24
    SYS "Sound_Stereo",c%+1,Stereo%(Stereo_Position%(c%))
  NEXT 
  IF button%=4 THEN PROCwimp_closedialogue(InstrWind_h%,ScoreWind_h%)
ENDIF
ENDPROC

DEF FNinstruments_keypress (key%)
LOCAL used%
used%=TRUE
CASE key% OF
  WHEN &1B:REM Escape
    PROCinstruments_click(63,0,4)  
  WHEN &0D:REM Enter
    PROCinstruments_click(64,0,4) 
  WHEN &1A2:REM Ctrl-F2
    PROCinstruments_click(63,0,4)
  OTHERWISE:used%=FALSE
ENDCASE
=used%

DEF FNinstruments_getvol (s$)
LOCAL i%,v%
FOR i%=0 TO 7
  IF s$=FNmessage_lookup(Messages_h%,"V"+STR$(i%)) THEN v%=i%
NEXT
=v%

DEF PROCinstruments_slider (icon%,pos%)
REM We want to work on pre-RISC OS 3.5, so can't use Wimp_ResizeIcon
REM to implement an 8-point slider. Delete and re-create icons instead
LOCAL i%,x0%,x1%,y0%,y1%
Block%!0=InstrWind_h%
Block%!4=icon%
SYS "Wimp_GetIconState",,Block%
x0%=Block%!8  
y0%=Block%!12
x1%=Block%!16
y1%=Block%!20
Block%!4=icon%+8
SYS "Wimp_GetIconState",,Block%
SYS "Wimp_DeleteIcon",,Block%
FOR i%=8 TO 40 STEP 4
  Block%!i%=Block%!(i%+4)
NEXT
Block%!4=x0%+4+(pos%*22)
Block%!12=x0%+4+(pos%*22)+24
SYS "Wimp_CreateIcon",icon%+8,Block%
SYS "Wimp_ForceRedraw",Block%!0,x0%,y0%,x1%,y1%
728 729
ENDPROC

ROOL's avatar
ROOL committed
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
REM // Staves //

DEF PROCstaves_init (x%,y%)
LOCAL i%
FOR i%=2 TO 5
  PROCwimp_seticonstate(StaveWind_h%,i%,21,FALSE)
NEXT
PROCwimp_seticonstate(StaveWind_h%,2+STAVE%,21,TRUE)
IF PERC% THEN
  PROCwimp_seticonstate(StaveWind_h%,6,21,TRUE)
ELSE
  PROCwimp_seticonstate(StaveWind_h%,6,21,FALSE)
ENDIF
PROCwimp_opendialogue(StaveWind_h%,ScoreWind_h%,1)
SYS "Wimp_SetCaretPosition",StaveWind_h%,-1
ENDPROC

DEF PROCstaves_click (icon%,button%)
CASE icon% OF
  WHEN 7:REM Cancel
    IF button%=4 THEN PROCwimp_closedialogue(StaveWind_h%,ScoreWind_h%)
  WHEN 8:REM Set
    FOR i%=2 TO 5
      IF FNwimp_geticonstate(StaveWind_h%,i%,21) THEN
        STAVE%=i%-2
      ENDIF
    NEXT
    IF FNwimp_geticonstate(StaveWind_h%,6,21) THEN
      PERC%=1
    ELSE
      PERC%=0
    ENDIF
    SYS "Hourglass_On"
    PROCposition_staves
    PROCSetExtent(S_Width%)
    PROCscore_init
    PROCSetDefaultChannels
    PROCstart_music
    PROCrescore(0)
    SYS "Hourglass_Off"   
    PROCChangedScore
    IF button%=4 THEN PROCwimp_closedialogue(StaveWind_h%,ScoreWind_h%)
ENDCASE
ENDPROC

DEF FNstaves_keypress (key%)
LOCAL used%
used%=TRUE
CASE key% OF
  WHEN &1B:REM Escape
    PROCstaves_click(7,4)  
  WHEN &0D:REM Enter
    PROCstaves_click(8,4) 
  WHEN &1A2:REM Ctrl-F2
    PROCstaves_click(7,4) 
  OTHERWISE:used%=FALSE
ENDCASE
=used%

REM // TimeSig //

DEF PROCtimesig_init (icon%)
LOCAL n%,b%
n%=TIME_SIG%(0)+1
b%=2^(TIME_SIG%(1)-1)
PROCwimp_seticontext(TimeSig_h%,0,STR$(n%))
PROCwimp_seticontext(TimeSig_h%,1,STR$(b%))
CurrentMenu%=FNwimp_popup(SharpsPane_h%,TimeSig_h%,icon%)
ENDPROC

DEF PROCtimesig_click (icon%,button%)
REM Time signature held in TIME_SIG%() as (n+1) / 2^(d-1)
REM To edit, we have to convert to the following range:
REM n = 2-16; b = 2,4,8,16
LOCAL n%,b%,v%
n%=VAL(FNwimp_geticontext(TimeSig_h%,0))-1
b%=SQR(VAL(FNwimp_geticontext(TimeSig_h%,1))+1)+1 
CASE icon% OF
  WHEN 2:REM Decrement n
    IF button%=4 AND n%>1 THEN
      v%=1+(n%+13) MOD 15
      PROCwimp_seticontext(TimeSig_h%,0,STR$(v%+1))
    ENDIF
    IF button%=1 AND n%<15 THEN
      v%=1+(n%+15) MOD 15
      PROCwimp_seticontext(TimeSig_h%,0,STR$(v%+1))
    ENDIF
  WHEN 3:REM Increment n
    IF button%=4 AND n%<15 THEN
      v%=1+(n%+15) MOD 15
      PROCwimp_seticontext(TimeSig_h%,0,STR$(v%+1))
    ENDIF
    IF button%=1 AND n%>1 THEN
      v%=1+(n%+13) MOD 15
      PROCwimp_seticontext(TimeSig_h%,0,STR$(v%+1))
    ENDIF   
  WHEN 4:REM Decrement b
    IF button%=4 AND b%>2 THEN
      v%=(b%+1) MOD 4+2
      PROCwimp_seticontext(TimeSig_h%,1,STR$(2^(v%-1)))
    ENDIF
    IF button%=1 AND b%<5 THEN
      v%=(b%-1) MOD 4+2
      PROCwimp_seticontext(TimeSig_h%,1,STR$(2^(v%-1)))
    ENDIF
  WHEN 5:REM Increment b
    IF button%=4 AND b%<5 THEN
      v%=(b%-1) MOD 4+2
      PROCwimp_seticontext(TimeSig_h%,1,STR$(2^(v%-1)))
    ENDIF
    IF button%=1 AND b%>2 THEN
      v%=(b%+1) MOD 4+2
      PROCwimp_seticontext(TimeSig_h%,1,STR$(2^(v%-1)))
    ENDIF   
  WHEN 6:REM Select a new time sig
    PROCSetTimeSig(n%,b%)
    IF button%=4 THEN PROCCloseWindow(TimeSig_h%)      
ENDCASE
ENDPROC

DEF PROCattach_clef
CASE ClefType% OF
  WHEN 0:REM Treble
    PROCattach(clef%,%100)
  WHEN 1:REM Bass
    PROCattach(clef%+3,%100)
ENDCASE   
ENDPROC  

DEF PROCattach_barline
CASE BarlineType% OF
  WHEN 0:REM Single
    PROCattach(bar%,%10010000)
  WHEN 1:REM Double
    PROCattach(bar%+1,%10010000)
ENDCASE   
ENDPROC 

REM // Tool icons //

DEF PROCtoolicon_click (window%,icon%)
PROCrelease
wasSCORING%=TRUE
stopSCORING%=FALSE
PROCtoolicon_clear
PROCwimp_seticonstate(window%,icon%,2,TRUE)
PROCwimp_seticonstate(window%,icon%,5,TRUE)
SelW%=window%
SelI%=icon%
ENDPROC

DEF PROCtoolicon_clear
IF SelI%>-1 THEN
  PROCwimp_seticonstate(SelW%,SelI%,2,FALSE)
  PROCwimp_seticonstate(SelW%,SelI%,5,FALSE)
ENDIF
ENDPROC

REM // Voice handling //

890 891 892 893 894
DEF PROCCheckInstalledVoices
    LOCAL NewVoice$, NewNVoices%, changedVoices%

    changedVoices%=FALSE

ROOL's avatar
ROOL committed
895
    SYS "Sound_InstallVoice" TO I$,NewNVoices%:NewNVoices% -= 1
896 897 898 899 900 901 902 903

    IF FNassert(NVoices%>0,"NoVoices") STOP

    IF NewNVoices% <> NVoices% THEN
        changedVoices% = TRUE
        NVoices% = NewNVoices%
    ENDIF

ROOL's avatar
ROOL committed
904
    IF NVoices%>MAX_VOICES% THEN NVoices%=MAX_VOICES%
905 906

    FOR R% = 1 TO NVoices%
ROOL's avatar
ROOL committed
907 908 909 910
        SYS "Sound_InstallVoice",2,R% TO ,,,NewVoice$
        IF NewVoice$<>Voice$(R%) THEN
            changedVoices%=TRUE
            Voice$(R%)=NewVoice$
911 912 913 914 915
        ENDIF
    NEXT : REM find if instruments have changed

    IF changedVoices% THEN
        FOR R% = 0 TO 7
ROOL's avatar
ROOL committed
916
            SYS "Sound_AttachVoice",R% + 1,0 TO L%,S%
917 918 919
            IF S% < 1 OR S% > NVoices% THEN
                S%=1:REM Make sure a voice is attached to all channels
            ENDIF
ROOL's avatar
ROOL committed
920
            SYS "Sound_AttachVoice",L%,S%
921 922 923 924 925 926 927
            Instrument%(R%,0) = S_C%(R%)+1 : REM Instrument stave
            Instrument%(R%,1) = S%:REM Instrument voice
        NEXT : REM Get details of each channel
        PROCSetDefaultChannels
    ENDIF
ENDPROC

ROOL's avatar
ROOL committed
928 929 930 931 932 933 934 935 936 937
DEF PROCAttachVoice (c%,slot%)
LOCAL v%
PROCCheckInstalledVoices
REM Find channel's previous voice
SYS "Sound_AttachVoice",c%+1 TO ,v%
REM Attach new voice
IF v%=0 THEN v%=1
IF slot%=0 THEN slot%=v%
SYS "Sound_AttachVoice",c%+1,slot%
Instrument%(c%,1)=slot%
938 939 940
ENDPROC

DEF PROCUserDragBox
ROOL's avatar
ROOL committed
941 942
LOCAL filename$
filename$=FNwimp_geticontext(Save_h%,1)
943
IF SAVING% THEN
ROOL's avatar
ROOL committed
944 945 946 947 948 949 950 951 952 953 954 955 956 957
  SYS "Wimp_GetPointerInfo",,Block%
  Block%!32=Block%!4
  Block%!28=Block%!0
  Block%!24=Block%!16
  Block%!20=Block%!12 :REM this is the destination window handle
  Block%!16=1         :REM DataSave
  Block%!12=0
  Block%!36=-1        :REM don't know file size / unsafe?
  Block%!40=MusicFileType%
  $(Block%+44)=FNGetLeafName(filename$)+CHR$(0)
  !Block%=64
  SYS "Wimp_SendMessage",17,Block%,Block%!20,Block%!24
  my_ref%=Block%!8
  SYS "DragASprite_Stop"
958 959 960
ENDIF
ENDPROC

ROOL's avatar
ROOL committed
961 962 963 964 965 966 967
DEF PROCMenuSelect (sel1%,sel2%,sel3%)
LOCAL n%,SoundEnable%,mx%,my%,button%,text$,allowadjust%
SYS "Wimp_GetPointerInfo",,Block%
mx%=Block%!0
my%=Block%!4
button%=Block%!8
allowadjust%=TRUE
968
CASE CurrentMenu% OF
ROOL's avatar
ROOL committed
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
  WHEN MainMenu%:
    CASE sel1% OF
      WHEN 0:REM File menu
        CASE sel2% OF
          WHEN 1:REM Save file
            SYS "Wimp_CreateMenu",,Save_h%,mx%,my%
            allowadjust%=FALSE 
          WHEN 2:REM Print file
            PROCprint_init
            SYS "Wimp_CreateMenu",,Print_h%,mx%,my%
            allowadjust%=FALSE          
        ENDCASE   
      WHEN 1:REM Edit menu
        CASE sel2% OF
          WHEN 0:REM Go to bar
            PROCgoto_init
            SYS "Wimp_CreateMenu",,Bar_h%,mx%,my%
            allowadjust%=FALSE
          WHEN 1:REM Clear file
            IF CHANGED% THEN
              PROCOpenSaveDiscardCancelBox
            ELSE
              PROCClearAllMusic
              Block%!0=ScoreWind_h%
              SYS "Wimp_GetWindowState",,Block%
              Block%!20=0
              LHBAR%=0
              SYS "Wimp_OpenWindow",,Block%
            ENDIF
            allowadjust%=FALSE
        ENDCASE 
      WHEN 2:REM Score menu
        CASE sel2% OF
          WHEN 0:REM REM Staves
            PROCstaves_init(mx%,my%)
            allowadjust%=FALSE 
          WHEN 1:REM Instruments
            PROCinstruments_init(mx%,my%)
            allowadjust%=FALSE 
          WHEN 2:REM Volume menu
            IF sel3%>=0 THEN
              PROCSetVolume(sel3%)
              PROCChangedScore
            ENDIF
          WHEN 3:REM Tempo menu
            IF sel3%>=0 THEN
              PROCSetTempo(sel3%)
              PROCChangedScore
            ENDIF
        ENDCASE
      WHEN 3:REM Play
        SYS "Sound_Enable",0 TO SoundEnable%:REM dont pretend to be able to play if the sound system is disabled
1021
        IF SoundEnable%=2 THEN
ROOL's avatar
ROOL committed
1022 1023 1024 1025
          SCORING%=FALSE
          PLAYING%=NOT PLAYING%
          SCROLLING%=PLAYING%
          IF PLAYING% THEN PROCplay_start ELSE PROCplay_stop
1026
        ELSE
ROOL's avatar
ROOL committed
1027
          n%=FNCheckOKTag("NoSound",1)
1028 1029
        ENDIF
    ENDCASE
ROOL's avatar
ROOL committed
1030 1031 1032 1033 1034 1035
  WHEN IconMenu%:
    CASE sel1% OF
      WHEN 1:REM Help
        OSCLI("Filer_Run <Maestro$Dir>.!Help")
      WHEN 2:REM Quit
        IF CHANGED% THEN PROCOpenDiscardCancelBox ELSE PROCterminate
1036
    ENDCASE
ROOL's avatar
ROOL committed
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
  WHEN KeyMenu%:
    REM Key signature
    PROCSetKeySig(sel1%,sel2%)
  WHEN VoiceMenu%:
    text$=Voice$(sel1%+1)     
    PROCwimp_seticontext(InstrWind_h%,PopUpIcon%,text$)
    PROCwimp_radiotick(VoiceMenu%,sel1%)
  WHEN ChannelMenu%:
    text$=FNmessage_lookupN(Messages_h%,"MIDIvoice",STR$(sel1%+1),"","","")
    PROCwimp_seticontext(InstrWind_h%,PopUpIcon%,text$)
    PROCwimp_radiotick(ChannelMenu%,sel1%)
  WHEN VolumeMenu%:
    REM This is when open over the Instruments dialogue as a pop-up
    text$=FNmessage_lookup(Messages_h%,"V"+STR$(sel1%))
    PROCwimp_seticontext(InstrWind_h%,PopUpIcon%,text$)
    PROCwimp_radiotick(VolumeMenu%,sel1%)
  WHEN OutputMenu%:
ROOL's avatar
ROOL committed
1054 1055 1056
    IF FNwimp_getmenustate(OutputMenu%,sel1%,0)=FALSE THEN
      PROCinstruments_fill(sel1%)
    ENDIF   
1057
ENDCASE
ROOL's avatar
ROOL committed
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
IF allowadjust% AND button%=1 THEN SYS "Wimp_CreateMenu",,CurrentMenu%
ENDPROC

DEF PROCKeyPressed (window%,key%)
LOCAL used%
used%=TRUE
CASE window% OF
  WHEN Save_h%:
    CASE key% OF
      WHEN &0D:REM Enter
        PROCsave_music(FNwimp_geticontext(Save_h%,1))
        PROCCloseWindow(window%)
        SYS "Wimp_CreateMenu",,-1
      OTHERWISE:used%=FALSE
    ENDCASE
  WHEN Bar_h%:
    used%=FNgoto_keypress(key%)
  WHEN StaveWind_h%:
    used%=FNstaves_keypress(key%)
  WHEN ScoreWind_h%:
    used%=FNscore_keypress(key%)
  WHEN InstrWind_h%:
    used%=FNinstruments_keypress(key%)
  OTHERWISE:used%=FALSE
ENDCASE
IF used%=FALSE THEN SYS "Wimp_ProcessKey",key%   
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
ENDPROC

DEF PROCScrollReq(x_scroll%, y_scroll%)
IF Window%!handle%=ScoreWind_h% THEN
  IF PLAYING%AND(x_scroll%<>0) ENDPROC
  CASE ABS(x_scroll%) OF
    WHEN 1:Window%!scx%+=x_scroll%*4*Pgap%
    WHEN 2:Window%!scx%+=(x_scroll%/2)*(Window%!x1%-Window%!x0%)
    ENDCASE
  CASE ABS(y_scroll%) OF
    WHEN 1:Window%!scy%+=y_scroll%*C_Height%
    WHEN 2:Window%!scy%+=(y_scroll%/2)*(Window%!y1%-Window%!y0%)
    ENDCASE
ROOL's avatar
ROOL committed
1097
  SYS "Wimp_OpenWindow",,Window%+handle%
1098
  ScoreClosed%=FALSE
ROOL's avatar
ROOL committed
1099 1100
  LHBAR%=FNFindBar(Window%!scx%) :REM the bar number at the left of the window
ENDIF
1101 1102
ENDPROC

ROOL's avatar
ROOL committed
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
DEF PROCOpenWindow (b%)
LOCAL x0%,x1%,y0%,y1%,behind%,ox%
IF b%!0=ScoreWind_h% THEN
  IF PLAYING% THEN b%!20=OldX%
  SYS "Wimp_OpenWindow",,b%
  SYS "Wimp_GetWindowState",,b%
  x0%=b%!4
  y0%=b%!8
  x1%=b%!12
  y1%=b%!16
  ox%=b%!20
  behind%=b%!28 
  REM Open Sharps Pane
  Block%!0=SharpsPane_h%
  Block%!4=x0%
  Block%!8=y0%
  Block%!12=x1%
  Block%!16=y0%+PaneHeight%
  Block%!20=0
  Block%!24=0
  Block%!28=behind%
  SYS "Wimp_OpenWindow",,Block%
  REM Open Notes Pane
  Block%!0=NotesPane_h%
  Block%!4=x0%
  Block%!8=y1%-PaneHeight%
  Block%!12=x1%
  Block%!16=y1%  
  Block%!20=0
  Block%!24=0
  Block%!28=SharpsPane_h%
  SYS "Wimp_OpenWindow",,Block% 
  IF PLAYING% THEN PROCCheckScroll
  PROCrelease
  LHBAR%=FNFindBar(ox%)
1138
ELSE
ROOL's avatar
ROOL committed
1139
  SYS "Wimp_OpenWindow",,b%
1140 1141 1142
ENDIF
ENDPROC

ROOL's avatar
ROOL committed
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
DEF PROCUpdateTitle (title$)
LOCAL top%
Block%!0=ScoreWind_h%
SYS "Wimp_GetWindowInfo",,Block% OR 1
$(Block%!76)=title$+CHR$(0)
SYS "Wimp_GetWindowState",,Block%
top%=Block%!16
SYS "Wimp_GetWindowOutline",,Block%
SYS "Wimp_ForceRedraw",-1,Block%!4,top%,Block%!12,Block%!16
ENDPROC

DEF PROCCloseWindow (handle%)
Block%!0=handle%
SYS "Wimp_CloseWindow",,Block%
IF handle%=ScoreWind_h% THEN
  Block%!0=NotesPane_h%
  SYS "Wimp_CloseWindow",,Block%
  Block%!0=SharpsPane_h%
  SYS "Wimp_CloseWindow",,Block%
  ScoreClosed%=TRUE
ENDIF
1164 1165 1166
ENDPROC

DEF PROCsymbol_pointer
ROOL's avatar
ROOL committed
1167 1168
SYS "Wimp_GetPointerInfo",,Block%
IF Block%!12=ScoreWind_h% THEN PROCscribe(Block%!0,Block%!4)
1169 1170 1171 1172 1173
ENDPROC

DEF PROCStopScoring
stopSCORING%=TRUE
PROCrelease
ROOL's avatar
ROOL committed
1174
PROCtoolicon_clear
1175 1176 1177 1178 1179
ENDPROC

DEF PROCChangedScore
IF NOT CHANGED% THEN
  CHANGED%=TRUE
ROOL's avatar
ROOL committed
1180
  PROCwimp_seticontext(FileInfo_h%,2,FNmessage_lookup(Messages_h%,"Yes"))
ROOL's avatar
ROOL committed
1181
  PROCUpdateTitle(FileName$+" *")
1182 1183 1184
ENDIF
ENDPROC

ROOL's avatar
ROOL committed
1185 1186 1187 1188 1189 1190 1191
DEF PROCSetExtent (width%)
Score_Width%=width%
Block%!0=0
Block%!4=-Score_Height%-PaneHeight%
Block%!8=Score_Width%
Block%!12=PaneHeight%
SYS "Wimp_SetExtent",ScoreWind_h%,Block%
1192 1193
ENDPROC

ROOL's avatar
ROOL committed
1194 1195
DEF FNGetLeafName(name$)
LOCAL ch$,n%
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
IF ( (INSTR(name$,".")=0) AND (INSTR(name$,":")=0) ) THEN=name$
n%=LEN(name$)
REM scan string to find leaf name of file
REPEAT
  ch$= MID$(name$, n%, 1)
  n%-=1
  UNTIL (n%<=0 OR ch$="." OR ch$=":")
IF n%>0 THEN
    =RIGHT$(name$, LEN(name$)-n%-1)
ELSE
    = name$
ENDIF
= ""

DEF FNGetStr(s%) : REM get string
LOCAL n$
WHILE?s%:n$+=CHR$?s%:s%+=1:ENDWHILE
=n$

DEF PROCSetDefaultChannels
ROOL's avatar
ROOL committed
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
LOCAL s%,text$
REM Only 1 percussion channel permitted
REM Channel assignment changes if staves change
FOR s%=0 TO 7
  S_C%(s%)=Stave_Channels%(STAVE%,s%)
NEXT
REM Steal channel for percussion lines
IF PERC% THEN S_C%(7)=STAVE%+1
REM Instrument allocation
FOR s%=0 TO 7
  Instrument%(s%,0)=S_C%(s%)+1
  REM Set up stave names in instrument window
  text$="S"+STR$(Instrument%(s%,0)+(STAVE%-3)*((Instrument%(s%,0)-1)>STAVE%))
  PROCwimp_seticontext(InstrWind_h%,s%+4,FNmessage_lookup(Messages_h%,text$)) 
  PROCAttachVoice(s%,0)
NEXT
REM Steal 1 channel for percussion
IF PERC% THEN
  text$=FNmessage_lookup(Messages_h%,"S5")
  PROCwimp_seticontext(InstrWind_h%,11,text$)
ENDIF
1237 1238
ENDPROC

ROOL's avatar
ROOL committed
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
DEF FNGetFileInfo (file$)
LOCAL type%,laddr%,eaddr%,len%,attr%
IF file$<>"" AND file$<>FNmessage_lookup(Messages_h%,"Untitled") THEN
  SYS "OS_File",5,file$ TO type%,,laddr%,eaddr%,len%,attr%
ENDIF
IF file$="" OR file$=FNmessage_lookup(Messages_h%,"Untitled") OR ((type%=1) AND (attr% AND 1) AND (len%>8))=0 THEN
  PROCwimp_seticontext(FileInfo_h%,1,FNmessage_lookup(Messages_h%,"Untitled"))
  PROCset_type_string(MusicFileType%)
  PROCwimp_seticontext(FileInfo_h%,4,"")
  ?ftime%=3
  SYS "OS_Word",14,ftime%
  SYS "Territory_ConvertStandardDateAndTime",-1,ftime%,Block%,255
  PROCwimp_seticontext(FileInfo_h%,5,FNGetStr(Block%))
  =FALSE
ELSE
  PROCwimp_seticontext(FileInfo_h%,1,file$)
  PROCwimp_seticontext(FileInfo_h%,4,STR$(len%))
  IF ((laddr%>>20) AND &FFF)=&FFF THEN
    IF (laddr%>>8 AND &FFF)=MusicFileType% THEN
      PROCset_type_string(MusicFileType%)
    ELSE
      PROCset_type_string(laddr%>>8 AND &FFF)
1261
    ENDIF
ROOL's avatar
ROOL committed
1262 1263 1264 1265 1266 1267 1268
    ftime%?4=laddr% AND &FF
    ftime%?3=eaddr%>>24 AND &FF
    ftime%?2=eaddr%>>16 AND &FF
    ftime%?1=eaddr%>>8 AND &FF
    ftime%?0=eaddr% AND &FF
    SYS "OS_ConvertStandardDateAndTime",ftime%,Block%,255
    PROCwimp_seticontext(FileInfo_h%,5,FNGetStr(Block%))
1269
  ENDIF
ROOL's avatar
ROOL committed
1270
ENDIF
1271 1272
=TRUE

ROOL's avatar
ROOL committed
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
DEF PROCset_type_string (type%)
LOCAL s$,r2%,r3%,space$
s$=""
SYS "OS_FSControl",18,,type% TO ,,r2%,r3%
s$=CHR$(r2% AND &FF)+CHR$((r2%>>8) AND &FF)
s$+=CHR$((r2%>>16) AND &FF)+CHR$((r2%>>24) AND &FF)
s$+=CHR$(r3% AND &FF)+CHR$((r3%>>8) AND &FF)
s$+=CHR$((r3%>>16) AND &FF)+CHR$((r3%>>24) AND &FF)
space$=STRING$(8-LEN(s$)," ")
PROCwimp_seticontext(FileInfo_h%,3,s$+space$+"("+STR$~type%+")")
ENDPROC  

1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
DEF PROCload_music(FF$)
LOCAL F%,M$,ZZ%
LOCAL ERROR
T%=0
SCORING%=FALSE
IF PLAYING% PROCplay_stop
SCROLLING%=FALSE
IF LENFF$=0 THEN VDU7 : T%=FNCheckOKTag("BadName",1) : ENDPROC
SYS "Hourglass_On"
FILE%=OPENINFF$
REM SYS "OS_Find", 79, FF$ TO FILE%

ROOL's avatar
ROOL committed
1297
ON ERROR LOCAL VDU7:OSCLI("FX 229,1"):PROCClearAllMusic:SYS "Hourglass_Off":T%=FNCheckOK(FNmessage_lookupN(Messages_h%, "IntErr", REPORT$, STR$(ERL), "", ""),1):ENDPROC
1298 1299 1300

M$=""
FORR%=1TO7
ROOL's avatar
ROOL committed
1301
  IF NOT EOF#FILE% THEN M$+=CHR$BGET#FILE% ELSE SYS "Hourglass_Off":T%=FNCheckOK(FNmessage_lookup(Messages_h%,"BadFile"),1): ENDPROC
1302 1303 1304 1305 1306 1307 1308 1309
NEXT
OSCLI("FX 229,0") :REM enable escape key
B%=BGET#FILE%
IFM$="Maestro" THEN
  IF NOT FNGetFileInfo(FF$) THEN VDU7 :OSCLI("FX 229,1") : SYS "Hourglass_Off" :T%=FNCheckOKTag("BadFile",1) : ENDPROC
  T%=TRUE
  NBars%=0
  CASE BGET#FILE% OF
ROOL's avatar
ROOL committed
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
    WHEN 0:T%=FALSE
    WHEN 1
      PROClTempo:PROClInstruments:PROClStaves:PROClMusic
    OTHERWISE REM File id version 2 and above
      A%=FALSE
      REPEAT
        REM Used to be an ON BGET#FILE% statement,
        REM changed to allow exiting if music is too big.
        CASE BGET#FILE% OF
          WHEN 1:PROClMusic
          WHEN 2:PROClStaves
          WHEN 3:PROClInstruments
          WHEN 4:PROClVolumes
          WHEN 5:PROClStereos
          WHEN 6:PROClTempo            
          OTHERWISE:A%=TRUE
        ENDCASE
      UNTIL EOF#FILE% OR A%
1328
    ENDCASE
ROOL's avatar
ROOL committed
1329 1330 1331
  CLOSE#FILE%

  FILE%=FALSE
1332
  OSCLI("FX 229,1")
ROOL's avatar
ROOL committed
1333 1334
  PROCwimp_seticontext(FileInfo_h%,1,FNmessage_lookup(Messages_h%,FF$))
  PROCwimp_seticontext(FileInfo_h%,2,FNmessage_lookup(Messages_h%,"No"))
ROOL's avatar
ROOL committed
1335 1336 1337 1338 1339 1340 1341 1342 1343
  IF LEFT$(FF$,12)="<Wimp$Scrap>" THEN
    FF$=FNmessage_lookup(Messages_h%,"Untitled") 
    PROCwimp_seticontext(Save_h%,1,FNmessage_lookup(Messages_h%,"MusicFile"))
  ELSE
    PROCwimp_seticontext(Save_h%,1,FF$)
  ENDIF
  T%=FNGetFileInfo(FF$)
  PROCUpdateTitle(FF$)
  FileName$=FF$
1344 1345 1346 1347
  SYS "Hourglass_Off"
  SYS "Hourglass_On"
  IF T% THEN
    PROCposition_staves
ROOL's avatar
ROOL committed
1348
    PROCSetExtent(S_Width%)
1349 1350 1351
    PROCstart_music
    PROCset_score(0)
    PROCSetupBarStarts(0)
ROOL's avatar
ROOL committed
1352 1353
    PROCscore_init
    PROCSetDefaultChannels
ROOL's avatar
ROOL committed
1354
    PROCscore_update(0,-Score_Height%,Score_Width%,0)
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
    PROCStopScoring
    CHANGED%=FALSE
  ENDIF
ELSE
  OSCLI("FX 229,1")
  T%=FALSE
ENDIF
IF NOT T% THEN ZZ%=FNCheckOKTag("InvMusic",1) : CLOSE#FILE%
load_pending% = FALSE
SYS "Hourglass_Off"
ENDPROC

DEF PROClMusic
LOCALC%,B%,ret%
INPUT#FILE%,GATE%
GATE%+=MUSIC%
FORC%=0TO7
INPUT#FILE%,FINE%(C%):FINE%(C%)+=MUSIC%(C%): REM Sets up start and end pointers for data
NEXT
B%=MUSIC%:WHILEB%<GATE%:?B%=BGET#FILE%:B%+=1:ENDWHILE
FORC%=0TO7
    B%=MUSIC%(C%)
    WHILEB%<FINE%(C%)
        ?B%=BGET#FILE%
        B%+=1
    ENDWHILE
NEXT
PP%=MUSIC%:P%()=MUSIC%()
ENDPROC

DEF PROClStaves
STAVE%=BGET#FILE%
PERC%=BGET#FILE%
ENDPROC

DEF PROClInstruments
ROOL's avatar
ROOL committed
1391 1392 1393 1394 1395 1396 1397 1398 1399
LOCAL c%,chan%,v%
FOR c%=0 TO 7
  REM Get channel
  chan%=BGET#FILE%
  REM Write voice numbers allocated to each channel
  v%=BGET#FILE% MOD (NVoices%+1)
  IF v%=0 THEN v%=1
  SYS "Sound_AttachVoice",chan%+1,v%
  PROCAttachVoice(chan%,0)
1400 1401 1402 1403
NEXT
ENDPROC

DEF PROClVolumes
ROOL's avatar
ROOL committed
1404 1405 1406 1407 1408
LOCAL c%
FOR c%=0 TO 7
  Volumes%(c%)=BGET#FILE%
  IF Volumes%(c%)>7 THEN Volumes%(c%)=7
  IF Volumes%(c%)<0 THEN Volumes%(c%)=0
1409 1410 1411 1412
NEXT
ENDPROC

DEF PROClStereos
ROOL's avatar
ROOL committed
1413 1414 1415 1416
LOCAL c%
FOR c%=0 TO 7
  Stereo_Position%(c%)=BGET#FILE%
  SYS "Sound_Stereo",c%+1,Stereo%(Stereo_Position%(c%))
1417 1418 1419 1420
NEXT
ENDPROC

DEF PROClTempo
ROOL's avatar
ROOL committed
1421 1422 1423 1424 1425 1426
LOCAL t%
t%=BGET#FILE%
PROCSetTempo(t%)
ENDPROC

DEF PROCsave_init (x%,y%)
ROOL's avatar
ROOL committed
1427 1428 1429
LOCAL f$
f$=FNwimp_geticontext(Save_h%,1)
IF (INSTR(f$,".")=0 AND INSTR(f$,":")=0) THEN
ROOL's avatar
ROOL committed
1430 1431
  SYS "Wimp_CreateMenu",,Save_h%,x%,y%
ELSE
ROOL's avatar
ROOL committed
1432
  PROCsave_music(f$)
ROOL's avatar
ROOL committed
1433
ENDIF            
1434 1435
ENDPROC

ROOL's avatar
ROOL committed
1436 1437
DEF PROCsave_music (FF$)
LOCAL n%,tmp%,laddr%,eaddr%
1438 1439 1440
LOCAL ERROR
T%=0
REM simple check for pathname rather than local name
ROOL's avatar
ROOL committed
1441 1442
IF (INSTR(FF$,".")=0 AND INSTR(FF$,":")=0 AND INSTR(FF$,"<")=0) THEN
  n%=FNCheckOKTag("ToSave",1)
1443 1444 1445
  ENDPROC
ENDIF
SYS "Hourglass_On"
ROOL's avatar
ROOL committed
1446 1447 1448 1449
ON ERROR LOCAL OSCLI("FX 229,1"):SYS "Hourglass_Off":T%=FNCheckOK(REPORT$,1):ENDPROC
OSCLI("FX 229,0"):REM enable escape key
SYS "OS_File",5,FF$ TO ,,laddr%,eaddr%
IF CHANGED% OR (((laddr%>>20) AND &FFF)<>&FFF) THEN
1450 1451
  REM file changed or wasn't timestamped
  REM get current time
ROOL's avatar
ROOL committed
1452 1453 1454
  Block%?0=3:SYS "OS_Word",&0E,Block%
  laddr%=Block%?4
  eaddr%=Block%!0
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
  ENDIF
REM force music file type, and preserve timestamp
laddr%=(laddr% AND &FF) OR (&FFF<<20) OR (MusicFileType%<<8)
REM I don't know what the length will be, so use zero
SYS "OS_File", &07, FF$, laddr%, eaddr%, 0, 0
REM OPENUP, error if directory or not found
SYS "OS_Find", &CC, FF$ TO FILE%
REM timestamp is automatically updated on 1st byte write
BPUT#FILE%,"Maestro"
BPUT#FILE%,2
PROCsMusic
PROCsStaves
PROCsInstruments
PROCsVolumes
PROCsStereos
PROCsTempo
CLOSE#FILE%:FILE%=FALSE
ROOL's avatar
ROOL committed
1472
IF (NOT CHANGED%) AND (((laddr%>>20) AND &FFF)=&FFF) THEN
1473 1474
  REM file not changed and was timestamped
  REM preserve original timestamp
1475 1476
  SYS "OS_File",2,FF$,laddr% :REM re-stamp with old stamp
  SYS "OS_File",3,FF$,,eaddr% :REM nb eaddr% in r3
1477 1478 1479 1480 1481 1482
ENDIF
OSCLI("FX 229,1")
REM Twiddle the name we keep so that it doesn't ever get set to <Wimp$Scrap>
REM Also, if a scrap transfer is done, the document should still be marked as
REM having been altered - a scrap transfer isn't safe.
IF LEFT$(FF$, 12) = "<Wimp$Scrap>" THEN
ROOL's avatar
ROOL committed
1483
  F$ = LEFT$(FileName$) : REM Preserve old title
1484 1485 1486 1487 1488
  IF CHANGED% THEN
    PROCUpdateTitle(LEFT$(F$) + "*") : REM Will already have space on end
  ENDIF
ELSE
  CHANGED%=FALSE
ROOL's avatar
ROOL committed
1489
  PROCwimp_seticontext(FileInfo_h%,2,FNmessage_lookup(Messages_h%,"No"))
1490 1491
  F%=FNGetFileInfo(FF$)
  PROCUpdateTitle(FF$)
ROOL's avatar
ROOL committed
1492
  PROCwimp_seticontext(Save_h%,1,FF$+CHR$(0))
ROOL's avatar
ROOL committed
1493
  FileName$=FF$
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
ENDIF
SYS "Hourglass_Off"
ENDPROC

DEF PROCsMusic
LOCALC%,B%
BPUT#FILE%,1
PRINT#FILE%,GATE%-MUSIC%
FORC%=0TO7
PRINT#FILE%,FINE%(C%)-MUSIC%(C%)
NEXT
B%=MUSIC%:WHILEB%<GATE%:BPUT#FILE%,?B%:B%+=1:ENDWHILE
FORC%=0TO7
B%=MUSIC%(C%):WHILEB%<FINE%(C%):BPUT#FILE%,?B%:B%+=1:ENDWHILE
NEXT
ENDPROC

DEF PROCsStaves
BPUT#FILE%,2
BPUT#FILE%,STAVE%
BPUT#FILE%,PERC%
ENDPROC

DEF PROCsInstruments
LOCALC%
BPUT#FILE%,3
FORC%=0TO7
BPUT#FILE%,C%
BPUT#FILE%,Instrument%(C%,1)
NEXT
ENDPROC

DEF PROCsVolumes
LOCALC%
BPUT#FILE%,4
FORC%=0TO7
BPUT#FILE%,Volumes%(C%)
NEXT
ENDPROC

DEF PROCsStereos
LOCALC%
BPUT#FILE%,5
FORC%=0TO7
BPUT#FILE%,Stereo_Position%(C%)
NEXT
ENDPROC

DEF PROCsTempo
BPUT#FILE%,6
BPUT#FILE%,Tempo%
ENDPROC

Chris Wraight's avatar
Chris Wraight committed
1547 1548 1549 1550 1551
DEF PROCsprite(s%,sx%,sy%)
REM Plot sprite S%(s%) at sx%,sy%
sx%=sx%-x%(s%)
sy%=sy%-y%(s%)
SYS "OS_SpriteOp",SprPlot%,SprBlk%,S%(s%),sx%,sy%,8,factors%,pixtrans%
1552 1553
ENDPROC

ROOL's avatar
ROOL committed
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
DEF PROCfloat (s%,sx%,sy%)
LOCAL more%,width%,height%,n%,b%
REM Get size of attribute for redraw rectangle
CASE s% OF
  WHEN key%:REM Key signature
    IF KEY_SIG%(1) THEN
      n%=X%(accidental%+2+KEY_SIG%(0))+x%(accidental%+2+KEY_SIG%(0))
      width%=KEY_SIG%(1)*n%
    ELSE
      width%=X%(key%)
    ENDIF
    height%=72
  WHEN time%:REM Time signature
    n%=TIME_SIG%(0)+1
    b%=2^(TIME_SIG%(1)-1)
    width%=X%(time%+n%)
    IF width%<X%(time%+b%) THEN width%=X%(time%+b%)
    height%=Stave_Height%
  OTHERWISE:REM Any other attribute
    width%=X%(s%)
    height%=Y%(s%)
ENDCASE
Block%!0=ScoreWind_h%
Block%!12=sx%+width%+2
sx%-=x%(s%)
Block%!4=sx%
Block%!16=sy%+height%
sy%-=y%(s%)
Block%!8=sy%
SYS "Wimp_UpdateWindow",,Block% TO more%
sx%+=Block%!4-Block%!20
sy%+=Block%!16-Block%!24
ROOL's avatar
ROOL committed
1586 1587
WHILE more%
  CASE s% OF
ROOL's avatar
ROOL committed
1588 1589
    WHEN key%:PROCfloat_key_sig(sx%,sy%+y%(s%))
    WHEN time%:PROCfloat_time_sig(sx%,sy%,width%)
ROOL's avatar
ROOL committed
1590
    OTHERWISE:
ROOL's avatar
ROOL committed
1591
      SYS "OS_SpriteOp",SprPlot%,SprBlk%,S%(s%),sx%,sy%,11,factors%,fpixtrans%
ROOL's avatar
ROOL committed
1592
  ENDCASE
ROOL's avatar
ROOL committed
1593
  SYS "Wimp_GetRectangle",,Block% TO more%
ROOL's avatar
ROOL committed
1594
ENDWHILE
1595 1596
ENDPROC

ROOL's avatar
ROOL committed
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
DEF PROCfloat_key_sig (kx%,ky%)
LOCAL i%,a%,c%,w%,m%
IF KEY_SIG%(1) THEN
  c%=SCRIBE%(sclef%)
  a%=KEY_SIG%(0)
  m%=accidental%+2+a%
  w%=x%(m%)+X%(m%)
  ky%-=y%(m%)
  FOR i%=0 TO KEY_SIG%(1)-1
    SYS "OS_SpriteOp",SprPlot%,SprBlk%,S%(m%),kx%,ky%+Li%*Key_Y%(c%,a%,i%),11,factors%,fpixtrans%
    kx%+=w%
  NEXT
1609
ELSE
ROOL's avatar
ROOL committed
1610
  SYS "OS_SpriteOp",SprPlot%,SprBlk%,S%(key%),kx%,ky%-y%(key%),11,factors%,fpixtrans%
1611 1612 1613
ENDIF
ENDPROC

ROOL's avatar
ROOL committed
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623
DEF PROCfloat_time_sig (tx%,ty%,width%)
LOCAL sx%,w%,n%,b%
n%=TIME_SIG%(0)+1
w%=X%(time%+n%)
IF w%<width% THEN sx%=tx%+12 ELSE sx%=tx%
SYS "OS_SpriteOp",SprPlot%,SprBlk%,S%(time%+n%),sx%,ty%+36,11,factors%,fpixtrans%
b%=2^(TIME_SIG%(1)-1)
w%=X%(time%+b%)
IF w%<width% THEN sx%=tx%+12 ELSE sx%=tx%
SYS "OS_SpriteOp",SprPlot%,SprBlk%,S%(time%+b%),sx%,ty%+4,11,factors%,fpixtrans%
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
ENDPROC

REM *********************************************************************** REM
REM                                                                         REM
REM         M U S I C   T R A N S C R I P T I O N   R O U T I N E S         REM
REM                                                                         REM
REM *********************************************************************** REM
::
REM   PROCEDURE: start_music
REM
REM DESCRIPTION: Initialise to start of music
:
DEF PROCstart_music
LOCAL n%
BAR%=0 :REM current bar
GP%=MUSIC%:REM Set current gate pointer to start of displayed music
N%()=MUSIC%()
CLEF%()=0:REM Start with base clefs (NOT BASS!)
SIG%(0)=%01100111:REM (Default time sig of 4/4 time)
SIG%(1)=%00000010:REM (Default of C Major) First displayed signatures attribute byte
PX%=0
PROCPutBarInfo(0)
ENDPROC

REM   PROCEDURE: note_type(Channel C%, Type T%)

DEF PROCnote_type(C%,T%)
Chris Wraight's avatar
Chris Wraight committed
1651
N%(C%)?1=N%(C%)?1 AND &1F OR T%<<5
1652 1653 1654
ENDPROC

DEF PROCnote_dots(C%,D%)
Chris Wraight's avatar
Chris Wraight committed
1655
N%(C%)?1=N%(C%)?1 AND &E7 OR D%<<3
1656 1657 1658
ENDPROC

DEF PROCnote_accidental(C%,A%)
Chris Wraight's avatar
Chris Wraight committed
1659
N%(C%)?1=N%(C%)?1 AND &F8 OR A%
1660 1661
ENDPROC

ROOL's avatar
ROOL committed
1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674
DEF PROCnote_line (c%,l%,type%)
REM Sets the y pos for Notes/Rests
REM Note range is +/-15, Rest range is +/-3
REM For Notes, these are converted to 1-31
REM For Rests, they're converted to and 1-3 below centre line,
REM 5-7 above centre line, and 0 on centre line (to preserve
REM compatibility with older versions where Rests were always centred).
IF type%>=rest% THEN
  IF l%=0 THEN l%=-4
  N%(c%)?1=(N%(c%)?1 AND &F8) OR l%+4
ELSE
  ?N%(c%)=(?N%(c%) AND &7) OR (l%+16)<<3
ENDIF
1675 1676
ENDPROC

Chris Wraight's avatar
Chris Wraight committed
1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
DEF PROCnote_tie (c%,type%)
REM Add/remove a tie from a note
CASE type% OF
  WHEN 0:REM Remove tie
    ?N%(c%)=?N%(c%) AND NOT (2<<1)
  WHEN 1:REM Add an over-note tie
    ?N%(c%)=?N%(c%) AND NOT (1<<1)
    ?N%(c%)=?N%(c%) OR (1<<2)
  WHEN 2:REM Add an under-note tie
    ?N%(c%)=?N%(c%) OR (3<<1)
ENDCASE
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
ENDPROC

DEF PROCnote_stem(C%,D%)
?N%(C%)=?N%(C%)AND&FE OR(D%<>0)AND%1
ENDPROC

DEF PROCnote_clear(C%)
?N%(C%)=0:N%(C%)?1=0
ENDPROC

DEF PROCtime_sig(N%,B%)
?GP%=0:GP%?1=Time%ORN%<<1ORB%<<5
ENDPROC

ROOL's avatar
ROOL committed
1702 1703 1704
DEF PROCkey_sig (A%,N%)
GP%?0=0
GP%?1=Key%ORA%<<2ORN%<<3
1705 1706
ENDPROC

ROOL's avatar
ROOL committed
1707 1708 1709
DEF PROCclef (S%,C%)
GP%?0=0
GP%?1=Clef% OR C%<<3 OR S%<<6
1710 1711 1712
ENDPROC

DEF PROCbar
ROOL's avatar
ROOL committed
1713 1714
GP%?0=0
GP%?1=Bar% OR BarlineType%<<6
1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
ENDPROC

DEF PROCinsert_gate(W%)
LOCALG%
IFGP%<GATE% THEN
FORG%=GATE%-W%TOGP%STEP-4:G%!W%=!G%:NEXT:REM Shift up by W% from insertion
G%+=3:REM Byte before last copied
WHILEG%>=GP%:G%?W%=?G%:G%+=TRUE:ENDWHILE:REM Clear up in odd word
ENDIF
GATE%+=W%:REM Insert gate of size W% into gate queue
EP%+=W%:REM This assumes gates will only be inserted below EP%
?GP%=0:GP%?(W%-1)=0:REM Zeroise inserted gate
ENDPROC

DEF PROCinsert_note(C%)
LOCALN%
IFN%(C%)<FINE%(C%) THEN
FORN%=FINE%(C%)-2TON%(C%)STEP-4:N%!2=!N%:NEXT:REM Shift up from insertion
N%+=3:REM Last copied word
WHILEN%>=N%(C%):N%?2=?N%:N%+=TRUE:ENDWHILE:REM Clear up in odd word
ENDIF
FINE%(C%)+=2:REM Insert a note word into this queue
?GP%=?GP%OR%1<<C%:REM Mark in gate
PROCnote_clear(C%)
ENDPROC
::
DEF PROCdelete_gate(W%)
LOCALG%
GATE%-=W%
IFGP%<GATE% FORG%=GP%TOGATE%STEP4:!G%=G%!W%:NEXT
EP%-=W%:REM This assumes gates will only be deleted below EP%
ENDPROC
::
DEF PROCdelete_note(C%)
LOCALN%
FINE%(C%)-=2
IFN%(C%)<FINE%(C%) FORN%=N%(C%)TOFINE%(C%)STEP4:!N%=N%!2:NEXT
?GP%=?GP%ANDNOT(%1<<C%)
ENDPROC

ROOL's avatar
ROOL committed
1755 1756 1757 1758
DEF FNallocate_channel (s%)
LOCAL channel%,c%
channel%=-1
c%=7
1759
REPEAT
ROOL's avatar
ROOL committed
1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
  WHILE c%>=0 AND (?GP% AND 1<<c%)
    c%-=1
  ENDWHILE
  IF c%>=0 THEN
    IF S_C%(c%)=s% THEN channel%=c%
  ENDIF
  c%-=1
UNTIL c%<0
=channel%

DEF PROCarrange_stave (S%)
LOCAL C%,B%,bar%
B%=TRUE
C%=TRUE
IF ?GP% THEN
ELSE
  REPEAT
    GP%+=2
  UNTIL ?GP% OR GP%>=GATE%
ENDIF
WHILE B% AND GP%<GATE%
  C%=C% OR FNsort_gate
  PROCskip_notes(?GP%)
  GP%+=1
  IF ?GP% THEN
  ELSE
    B%=C%
    C%=FALSE
    REPEAT
      GP%+=2
    UNTIL ?GP% OR GP%>=GATE%
  ENDIF
1792
ENDWHILE
ROOL's avatar
ROOL committed
1793
PROCSetupBarStarts(BAR%)
1794 1795 1796 1797 1798 1799 1800
ENDPROC

DEF FNsort_gate
LOCALC%,NC%,NN%,G%,g%,pg%,d%,shortest%,Gchanged%
shortest%=255
G%=?GP%
g%=FNprevious_gate(GP%)
ROOL's avatar
ROOL committed
1801
pg%=FNpreceding_gate(GP%):REM note-gate immediately before
1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
NC%=-1
NN%=-1
FORC%=0TO7
IFS_C%(C%)=S% THEN
NC%+=1:n%(NC%)=C%:IFG%AND%1<<C% NN%+=1:C%(NN%)=C%
IFpg%AND%1<<C% d%=N%(C%)?-1>>3EOR%11100:IFd%<shortest% shortest%=d%
ENDIF
NEXT
shortest%=shortest%EOR%11100
IFg%ANDNC%>0ANDNN%>=0 PROCsort
=Gchanged%

DEF PROCsort
ROOL's avatar
ROOL committed
1815
LOCAL N%,M%
1816
c%()=-1
ROOL's avatar
ROOL committed
1817 1818 1819 1820 1821 1822
FOR M%=0 TO NC%
  FOR N%=0 TO NN%
    IF FNsame_pitch(n%(M%),C%(N%)) THEN
      c%(N%)=n%(M%)
    ENDIF
  NEXT
1823
NEXT
ROOL's avatar
ROOL committed
1824 1825
FOR N%=0 TO NN%
  IF c%(N%)<0 THEN c%(N%)=FNbest
1826
NEXT
ROOL's avatar
ROOL committed
1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837
FOR N%=0 TO NN%
  IF C%(N%)=c%(N%) THEN
  ELSE
    Gchanged%=TRUE
    M%=FNin(c%(N%),C%())
    IF M%>N% THEN
      PROCswap_notes(N%,M%)
    ELSE
      PROCmove_note(N%)
    ENDIF
  ENDIF
1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863
NEXT
ENDPROC

DEF PROCswap_notes(N%,M%)
LOCALs%,d%
s%=C%(N%):d%=c%(N%)
SWAPC%(N%),C%(M%)
SWAP?N%(s%),?N%(d%)
SWAPN%(s%)?1,N%(d%)?1
ENDPROC

DEF PROCmove_note(N%)
LOCALs%,d%
s%=C%(N%):d%=c%(N%)
PROCinsert_note(d%)
?N%(d%)=?N%(s%)
N%(d%)?1=N%(s%)?1
PROCdelete_note(s%)
ENDPROC

DEF FNbest
LOCALN%,C%
LOCAL short%,free%,rest%,any%,tied%
FOR N%=NC%TO0STEP-1
C%=n%(N%)
IFFNin(C%,c%())<0 THEN
ROOL's avatar
ROOL committed
1864 1865 1866 1867
  IFN%(C%)?-2AND4 THEN
    tied%=C%+1
  ELSE
    IFpg%AND%1<<C% THEN
ROOL's avatar
ROOL committed
1868
      IF(N%(C%)?-1>>3)=shortest% short%=C%+1
ROOL's avatar
ROOL committed
1869 1870 1871 1872 1873
    ELSE
      free%=C%+1
    ENDIF
    any%=C%+1
  ENDIF
1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
ENDIF
NEXT
IFshort% C%=short% ELSEIFfree% C%=free% ELSEIFrest% C%=rest% ELSEIFany% C%=any% ELSEC%=tied%
=C%-1

DEF FNin(U%,U%())
LOCALI%:I%=NN%
WHILEI%ANDU%<>U%(I%):I%-=1:ENDWHILE
=I%+(U%<>U%(I%))

ROOL's avatar
ROOL committed
1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
DEF FNsame_pitch(c%,C%)
LOCAL R%,r%
R%=(?N%(C%) AND &F8)>>3
IF R%=0 THEN
  R%=N%(C%)?1 AND &7
  IF R%=0 THEN R%=16 ELSE R%=R%*2
ENDIF
r%=(N%(c%)?-2 AND &F8)>>3
IF r%=0 THEN
  r%=N%(c%)?-1 AND &7
  IF r%=0 THEN r%=16 ELSE r%=r%*2
ENDIF
=N%(c%)-2>=MUSIC%(c%) AND (g% OR (N%(c%)?-2 AND 4)=4) AND %1<<c% AND (R% EOR r%)=FALSE AND (FALSE<>R% EOR r%=FALSE)
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944

DEF FNpreceding_gate(gp%)
LOCALC%,gm%
FORC%=0TO7:IFS_C%(C%)=S% gm%=gm%OR%1<<C%
NEXT
REPEAT
gp%-=1
UNTILgm%AND?gp%ORgp%<MUSIC%+2ORgp%?-1=FALSE
=?gp%ANDgp%>MUSIC%+1ANDgp%?-1<>FALSE

DEF FNprevious_gate(gp%)
LOCALC%,gm%
FORC%=0TO7:IFS_C%(C%)=S% gm%=gm%OR%1<<C%
NEXT
REPEAT
gp%-=1
WHILEgp%>MUSIC%ANDgp%?-1=FALSE:gp%-=2:ENDWHILE
UNTILgm%AND?gp%ORgp%<MUSIC%+2
=?gp%ANDgp%>MUSIC%+1

REM *********************************************************************** REM
REM                                                                         REM
REM           M U S I C   T Y P E S E T T I N G   R O U T I N E S           REM
REM                                                                         REM
REM *********************************************************************** REM
::
REM   PROCEDURE: set_score(Starting position PX%)
REM
REM DESCRIPTION: Typeset music score from current bar to end of music/screen
REM              No drawing is done as only the positions are calculated.
REM
REM     EFFECTS: Stores gate X positions & types in PX%() & PTYPE%()
:
DEF PROCset_score(PX%)
WHILE GP%<GATE%
IF?GP% PROCset_notes(?GP%):GP%+=1 ELSEPROCset_attribute(GP%?1):GP%+=2
ENDWHILE:REM Until edge of screen or end of music
EP%=GP%:REM Last GP%, pointer to first undrawn gate
EX%=PX%:REM Last PX%, index to last position
PX%(PX%+1)=PX%(PX%)+PW%(PX%)+Pgap%:REM Appendation position after last symbol
IF PX%(PX%+1)>S_Width% PROCSetExtent(PX%(PX%+1)+100*Hi%) ELSE PROCSetExtent(S_Width%)
PXn%(NBars%)=PX%
PTYPE%(PX%+1)=Note%:REM Note type by default
ENDPROC
::
REM NOTE: The key signature is the only attribute that must be kept track of in order to ensure correct setting.
REM       This is because a naturalising signature consists of the same number of naturals as accidentals in the previous key signature
:
ROOL's avatar
ROOL committed
1945 1946 1947 1948
DEF PROCset_attribute (a%)
LOCAL type%,subtype%,n%,b%,w%
IF a% AND PTYPE%(PX%)+TRUE ELSE IF a% AND PTYPE%(PX%) ENDPROC
type%=%1:IF a% AND %1 ELSE REPEAT type%=type%<<1:UNTIL a% AND type%
1949 1950
PX%(PX%+1)=PX%(PX%)+PW%(PX%)+Pgap%
PX%+=1
ROOL's avatar
ROOL committed
1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981
PTYPE%(PX%)=type%
CASE type% OF
  WHEN Time%:REM Time signature
    n%=(a%>>1 AND 15)+1
    b%=%1<<(a%>>5)-1
    w%=X%(time%+n%)
    IF X%(time%+b%)>w% THEN w%=X%(time%+b%)
    PW%(PX%)=x%(time%+2)+w%
  WHEN Key%:REM Key signature
    IF a% AND 56 THEN
      REM New Key Signature has accidentals
      subtype%=accidental%+(a%>>2 AND %1)+2
      SIG%(1)=a%
    ELSE
      REM New Key Signature has no accidentals
      subtype%=accidental%+1
      SWAP a%,SIG%(1)
      IF a% AND 56 THEN
        REM Width: naturalising Key Signature
      ELSE
        REM Width: empty Key Signature
        a%=8
        subtype%+=1
      ENDIF
    ENDIF
    PW%(PX%)=(a%>>3 AND 7)*(x%(subtype%)+X%(subtype%))
  WHEN Clef%:REM Clef
    PW%(PX%)=x%(clef%+3)+X%(clef%+3)
  WHEN Bar%:REM Barline
    subtype%=a%>>6
    PW%(PX%)=x%(bar%+subtype%)+X%(bar%+subtype%)
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001
ENDCASE
ENDPROC

DEF PROCset_notes(G%)
LOCALlx0%,lx1%,ly0%,ly1%
LOCALC%,P%,R%,s%:REM Channel, Note prefix & remainder widths, Sprite
C%=-1
REPEAT
REPEATC%-=TRUE:UNTILG%AND%1<<C%
PROCbound_note(!N%(C%))
IFlx0%>P% P%=lx0%:REM Maximum prefix
IFlx1%>R% R%=lx1%:REM Maximum remainder
N%(C%)+=2:REM Pull from note queue
UNTIL(2<<C%)>G%:REM Until no more notes (1 bits)
PX%(PX%+1)=PX%(PX%)+PW%(PX%)+Pgap%+P%:REM Next position is previous position + width+gap+prefix of next note
PX%+=1:REM next note position index
PW%(PX%)=R%:REM Width of new note
PTYPE%(PX%)=Note%:REM Note type
ENDPROC

ROOL's avatar
ROOL committed
2002 2003 2004 2005 2006 2007 2008 2009 2010 2011
DEF PROCbound_note (L%)
LOCAL H%,S%,s%
H%=L%>>8 AND &FF
IF (L% AND &F8) THEN
  REM Note
  S%=H%>>5 OR L%<<3 AND &8
ELSE
  REM Rest
  S%=rest% OR H%>>5
ENDIF
2012 2013 2014 2015
lx0%=x%(S%):REM Prefix width
lx1%=X%(S%):REM Suffix width
ly0%=y%(S%):REM Decender height
ly1%=Y%(S%):REM Ascender height
ROOL's avatar
ROOL committed
2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028
REM If a Note, and has accidentals, add width
IF (H% AND &7)>0 AND (L% AND &F8)>0 THEN
  s%=accidental% OR (H% AND &7)
  lx0%+=x%(s%):REM Add accidental width
  IF y%(s%)>ly0% THEN ly0%=y%(s%):REM Lower ?
  IF Y%(s%)>ly1% THEN ly1%=Y%(s%):REM Higher ?
ENDIF
REM If Note or Rest has dots, add width
IF (H% AND &18) THEN
  s%=dot%+(H%>>3 AND &3)
  lx1%=x%(S%)+X%(s%)
  REM Dot adds suffix and may be lower
  IF y%(s%)>ly0% THEN ly0%=y%(s%)
2029 2030
ENDIF
ENDPROC
ROOL's avatar
ROOL committed
2031

Chris Wraight's avatar
Chris Wraight committed
2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061
DEF FNbound_tie (pos%)
REM Scans ahead to check that a valid second note exists
REM to tie to. If the scan passes more than one barline, or
REM hits the end of the score, return 0. Otherwise, return
REM the x position of the gate that contains the note.
LOCAL gp%,px%,bar%,sline%,xpos%,bline%
REM Get first note's staveline
sline%=(?N%(C%) AND &F8)>>3
REM Store current score and bar pointers
PROCsavp
bar%=BAR%
REM Scan for a note on the same channel and staveline
WHILE GP%<GATE%+1 AND bline%<2
  PROCskip_gate
  pos%+=1
  REM Check for barline
  IF GP%?0=0 AND GP%?1=&20 THEN bline%+=1
  REM Check for note
  IF (?GP% AND (1<<C%)) AND bline%<2 THEN
    IF ((?N%(C%) AND &F8)>>3)=sline% THEN
      xpos%=pos%
      GP%=GATE%+1
    ENDIF
  ENDIF
ENDWHILE
REM Restore score and bar pointers
PROCrstp
BAR%=bar%
=xpos%

2062
DEF PROCposition_staves
ROOL's avatar
ROOL committed
2063
LOCAL y%,s%
2064
Score_Height%=(PERC%+1+3*(STAVE%+1)+1)*Stave_Height%
ROOL's avatar
ROOL committed
2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075
y%=-Score_Height%-Stave_Height% DIV 2
IF PERC% THEN
  FOR s%=PERC% TO 1 STEP -1
    y%+=Stave_Height%
    Y_STAVE%(STAVE%+s%)=y%
  NEXT
ENDIF
FOR s%=STAVE% TO 0 STEP -1
  y%+=3*Stave_Height%
  Y_STAVE%(s%)=y%
NEXT
2076 2077
ENDPROC

ROOL's avatar
ROOL committed
2078 2079 2080 2081 2082 2083 2084 2085 2086 2087
REM // Score //

DEF PROCscore_init
LOCAL wy%
REM Ensure visible height matches height of workarea
Block%!0=ScoreWind_h%
SYS "Wimp_GetWindowInfo",,(Block% OR 1)
wy%=ABS(Block%!48)+PaneHeight%
Block%!8=Block%!16-wy%
PROCOpenWindow(Block%)
2088 2089
LHBAR%=0
ScoreClosed%=FALSE
ROOL's avatar
ROOL committed
2090 2091 2092 2093 2094 2095 2096 2097 2098 2099
FirstOpen%=FALSE
SYS "Wimp_SetCaretPosition",ScoreWind_h%,-1
ENDPROC

DEF PROCscore_click (window%,icon%,button%)
CASE window% OF
  WHEN NotesPane_h%:
    IF icon%>0 THEN
      PROCtoolicon_click(NotesPane_h%,icon%)
      IF icon%<9 THEN PROCattach(note%+icon%-1,%111000)
ROOL's avatar
ROOL committed
2100
      IF icon%>8 THEN PROCattach(rest%+icon%-9,%111000)
ROOL's avatar
ROOL committed
2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120
    ENDIF
  WHEN SharpsPane_h%:
    IF icon%>1 THEN
      CASE icon% OF
        WHEN 14,16,18,20:
        OTHERWISE:
          IF icon%=21 OR icon%=22 THEN icon%=19
          PROCtoolicon_click(SharpsPane_h%,icon%)
      ENDCASE      
      IF icon%<9 THEN PROCattach(accidental%+icon%-1,%1100000)
      IF icon%>8 AND icon%<12 THEN PROCattach(dot%+icon%-8,%1101000)
      IF icon%=12 THEN PROCattach(tie%,%1101000)
      IF icon%=13 THEN PROCattach_barline
      IF icon%=14 THEN CurrentMenu%=FNwimp_popup(SharpsPane_h%,Barline_h%,icon%)
      IF icon%=15 THEN PROCattach_clef
      IF icon%=16 THEN CurrentMenu%=FNwimp_popup(SharpsPane_h%,Clef_h%,icon%)
      IF icon%=17 THEN PROCattach(key%,%10)    :REM key signature
      IF icon%=18 THEN CurrentMenu%=FNwimp_popup(SharpsPane_h%,KeyMenu%,icon%)
      IF icon%=19 THEN PROCattach(time%,%1)    :REM time signature
      IF icon%=20 THEN PROCtimesig_init(icon%)
Chris Wraight's avatar
Chris Wraight committed
2121
      IF icon%=23 THEN PROCattach(tie%+1,%1101000)
ROOL's avatar
ROOL committed
2122 2123 2124 2125 2126 2127 2128
    ENDIF
  WHEN ScoreWind_h%:
    IF button%=4 THEN
      IF SCORING% AND SCRIBE%(drawn%) THEN PROCput_down
    ENDIF
    SYS "Wimp_SetCaretPosition",ScoreWind_h%,-1
ENDCASE
2129 2130
ENDPROC

ROOL's avatar
ROOL committed
2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177
DEF FNscore_keypress (key%)
LOCAL used%,x%,y%,mx%,my%
used%=TRUE
SYS "Wimp_GetPointerInfo",,Block%
mx%=Block%!0
my%=Block%!4
CASE key% OF
  WHEN &181:REM F1 (Help)
    OSCLI("Filer_Run <Maestro$Dir>.!Help")
  WHEN &183:REM F3 (Save)
    Block%!0=Save_h%
    SYS "Wimp_GetWindowState",,Block%
    x%=mx%-(Block%!12-Block%!4) DIV 2
    y%=my%+(Block%!16-Block%!8) DIV 2
    SYS "Wimp_CreateMenu",,Save_h%,x%,y%
  WHEN &185:REM F5 (Goto)
    Block%!0=Bar_h%
    SYS "Wimp_GetWindowState",,Block%
    x%=mx%-(Block%!12-Block%!4) DIV 2
    y%=my%+(Block%!16-Block%!8) DIV 2
    PROCgoto_init
    SYS "Wimp_CreateMenu",,Bar_h%,x%,y%
  WHEN &013:REM ^S (Staves)
    Block%!0=StaveWind_h%
    SYS "Wimp_GetWindowState",,Block%
    x%=mx%-(Block%!12-Block%!4) DIV 2
    y%=my%+(Block%!16-Block%!8) DIV 2
    PROCstaves_init(x%,y%) 
  WHEN &009:REM ^I (Instruments)
    Block%!0=InstrWind_h%
    SYS "Wimp_GetWindowState",,Block%
    x%=mx%-(Block%!12-Block%!4) DIV 2
    y%=my%+(Block%!16-Block%!8) DIV 2
    PROCinstruments_init(x%,y%)
  WHEN &180:REM Print
    Block%!0=Print_h%
    SYS "Wimp_GetWindowState",,Block%
    x%=mx%-(Block%!12-Block%!4) DIV 2
    y%=my%+(Block%!16-Block%!8) DIV 2
    PROCprint_init
    SYS "Wimp_CreateMenu",,Print_h%,x%,y%
  WHEN &1A2:REM Ctrl-F2
    PROCCloseWindow(ScoreWind_h%)
  OTHERWISE:used%=FALSE
ENDCASE
=used%

ROOL's avatar
ROOL committed
2178 2179 2180 2181 2182 2183 2184 2185 2186
DEF PROCscore_update (x0%,y0%,x1%,y1%)
LOCAL more%
Block%!0=ScoreWind_h%
Block%!4=x0%
Block%!8=y0%
Block%!12=x1%
Block%!16=y1%
SYS "Wimp_UpdateWindow",,Block% TO more%
WHILE more%
ROOL's avatar
ROOL committed
2187 2188
  CLG
  PROCdraw_staves
ROOL's avatar
ROOL committed
2189
  SYS "Wimp_GetRectangle",,Block% TO more%
ROOL's avatar
ROOL committed
2190 2191 2192
ENDWHILE
ENDPROC

ROOL's avatar
ROOL committed
2193 2194 2195 2196 2197 2198 2199 2200
REM // Goto bar //

DEF PROCgoto_init
LOCAL bar%
Block%!0=ScoreWind_h%
SYS "Wimp_GetWindowState",,Block%
bar%=FNFindBar(Block%!20)
IF bar%<1 THEN bar%=1
Chris Wraight's avatar
Chris Wraight committed
2201
PROCwimp_seticontext(Bar_h%,0,STR$(bar%))
ROOL's avatar
ROOL committed
2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229
PROCwimp_seticontext(Bar_h%,2,STR$(NBars%-1))
ENDPROC

DEF FNgoto_keypress (key%)
LOCAL used%
used%=TRUE
CASE key% OF
  WHEN &0D:REM Enter
    BAR%=VAL(FNwimp_geticontext(Bar_h%,0))
    IF BAR%>NBars% THEN
      BAR%=NBars%
    ELSE
      IF BAR%<0 THEN BAR%=0
    ENDIF
    Block%!0=ScoreWind_h%
    SYS "Wimp_GetWindowState",,Block%
    Block%!20=PX%(PXn%(BAR%))+4
    REM Set scroll to requested bar number
    SYS "Wimp_OpenWindow",,Block%
    LHBAR%=FNFindBar(Block%!20)
    PROCCloseWindow(Bar_h%)
    SYS "Wimp_CreateMenu",,-1
  WHEN &1A2:REM Ctrl-F2
    PROCCloseWindow(Bar_h%)
  OTHERWISE:used%=FALSE
ENDCASE
=used%

ROOL's avatar
ROOL committed
2230
REM // Draw //
2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243

REM   PROCEDURE: draw_staves
REM
REM DESCRIPTION: Draw current stave structure
REM              1 stave for 1 voice
REM              2 staves for keyboard
REM              3 staves for 1 voice and keyboard
REM              4 staves for 4 voice chorus
:
DEF PROCdraw_staves
LOCAL Y%,T%,B%,S%,L%:REM Y%,T%,B%=Position & Y bounds of each stave, S%=Stave index, L%=Line position
LOCAL x%,y%,lx1%:REM Virtual coordinates of bottom left & top right of score window
LOCAL c%,t%,b%:REM Left edge of clip window and Y bounds
ROOL's avatar
ROOL committed
2244 2245 2246 2247 2248 2249 2250 2251
y%=Block%!16-Block%!24
x%=Block%!4-Block%!20
lx1%=x%+Score_Width%
IF lx1%>Block%!36 THEN lx1%=Block%!36
c%=Block%!28
IF c%<x%+Hi% THEN c%=x%+Hi%
b%=Block%!32
t%=Block%!40
2252
B%=Score_Width%
ROOL's avatar
ROOL committed
2253 2254 2255 2256
T%=Block%!36-x%
IF T%<B% THEN B%=T%:REM Right bound
T%=Block%!28-x%
IF T%<0 THEN T%=0:REM Left bound
ROOL's avatar
ROOL committed
2257
PROCdraw_score(x%,y%,T%,B%):REM Draw symbols on stave
2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274
IFPERC% THEN
FOR S%=STAVE%+1TOSTAVE%+PERC%
Y%=y%+Y_STAVE%(S%)
IFb%<=Y%ANDt%>=Y% LINEc%,Y%,lx1%,Y%
NEXT
ENDIF
FOR S%=0 TO STAVE%
Y%=y%+Y_STAVE%(S%):T%=Y%+Stave_Height%DIV2:B%=T%-Stave_Height%
IFb%<=T%ANDt%>=B% THEN
LINEc%,Y%,lx1%,Y%:REM Plot centre bar line
FORL%=Li%*2TOL%*2STEPL%
  LINEc%,Y%+L%,lx1%,Y%+L%:REM Plot upper line
  LINEc%,Y%-L%,lx1%,Y%-L%:REM Plot lower line
NEXT
ENDIF
NEXT
ENDPROC
ROOL's avatar
ROOL committed
2275

2276 2277 2278 2279
REM   PROCEDURE: draw_score(Based X%,Y%, PX bounds A%,B%)
REM
REM DESCRIPTION: Write current section of music score that appears between A%
REM              and B%
Chris Wraight's avatar
Chris Wraight committed
2280

2281
DEF PROCdraw_score(X%,Y%,A%,B%)
Chris Wraight's avatar
Chris Wraight committed
2282 2283 2284
LOCAL PX%
REM Must be subtle about redrawing last note (or other item) of
REM score to prevent picking up rubbish data after end
2285 2286
IF A%>PX%(PXn%(NBars%)) THEN
  IF A%>PX%(PXn%(NBars%))+2*Pgap% ENDPROC ELSE A%=PX%(PXn%(NBars%))
Chris Wraight's avatar
Chris Wraight committed
2287 2288 2289 2290
ENDIF
REM Ensure bar numbers are always completely updated,
REM but don't lose first note draw
BAR%=0
2291
IF NBars%>2 THEN
Chris Wraight's avatar
Chris Wraight committed
2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306
  REM Move to the first bar to draw from
  WHILE PX%(PXn%(BAR%+2))<A% AND BAR%<=NBars%-1
    BAR%+=1
  ENDWHILE
  IF BAR%>NBars% THEN ENDPROC
  REM Move back two bars to ensure any note ties are picked up,
  REM then get bar pointers and update clipping window
  IF BAR%>2 THEN BAR%-=2
  PROCGetBarInfo(BAR%)
  IF PX%<A% THEN A%=PX%
  IF GP%>=GATE% THEN ENDPROC
  REM Skip music until A%
  WHILE PX%(PX%+3)<A% AND PX%<EX%
    PROCskip_gate
  ENDWHILE
2307
ELSE
Chris Wraight's avatar
Chris Wraight committed
2308
  PROCstart_music
2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326
ENDIF
WHILEPX%(PX%)<=B%ANDGP%<GATE%
IF?GP% PROCdraw_notes(?GP%):GP%+=1 ELSEPROCdraw_attribute(GP%?1):GP%+=2:IF PLAYING% PROCCheckQ
ENDWHILE
ENDPROC

REM   PROCEDURE: draw_notes(Gate G%)
REM
REM DESCRIPTION: Get notes from the indicated channel queues and draw the
REM              particular types of note in the correct places on the stave.
REM              One may be a rest, may have accidentals preceding it, may have
REM              dots following it, may be tied
REM
REM ASSUMPTIONS: Gate is non-zero
REM
REM Join=?N%(C%)AND2
:
DEF PROCdraw_notes(G%)
2327
LOCAL C%,x%,y%,s%,l%,ly%,NC0%,NC1%,lasty%,checkstagger%
2328 2329 2330 2331 2332
PX%+=1:REM Move on to next position
x%=X%+PX%(PX%):REM Calculate gate column X (convert from work- to screen-coord)
checkstagger%=FALSE
C%=-1
REPEAT
2333 2334 2335 2336
  REPEAT C%-=TRUE:UNTIL G% AND %1<<C%
  NC0%=?N%(C%) : NC1%=N%(C%)?1 :REM fast local vars
  y%=Y%+Y_STAVE%(S_C%(C%))
  IF NC0% AND &F8 THEN
ROOL's avatar
ROOL committed
2337
    REM Draw note
2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353
    l%=(NC0%>>3)-16
    ly%=y%
    y%+=Li%*l%
    s%=NC1%>>5 OR NC0%<<3 AND 8
    IF checkstagger% THEN
      REM stagger next note if too close to one below
      REM stagger fails sometimes if channels of 2 adjacent notes
      REM are not in draw order?
      IF ABS(lasty%-y%)<2*Li% PROCsprite(s%,x%+Pgap%,y%):checkstagger%=FALSE ELSE PROCsprite(s%,x%,y%)
    ELSE
      PROCsprite(s%,x%,y%):checkstagger%=TRUE
    ENDIF
    IF ABSl%>5 PROCsprite(ledger%+l%DIV2,x%,ly%):REM Ledger lines
    lasty%=y%
    IF NC1% AND 7 PROCsprite(accidental% OR NC1% AND 7,x%-x%(s%),y%)
  ELSE
ROOL's avatar
ROOL committed
2354
    REM Draw rest
2355
    s%=rest% OR NC1%>>5
Chris Wraight's avatar
Chris Wraight committed
2356
    y%+=(Li%*2)*FNrest_getline(NC1%)
2357 2358 2359
    PROCsprite(s%,x%,y%)
  ENDIF
  IF NC1% AND 24 PROCsprite(dot%+(NC1%>>3AND3),x%+x%(s%),y%)
Chris Wraight's avatar
Chris Wraight committed
2360 2361
  REM Draw a tie
  IF (NC0% AND &4) THEN PROCdraw_tie(tie%+((NC0% AND &2)>>1),x%,y%)
2362
  N%(C%)+=2:REM Pull from note queue
2363 2364
UNTIL(2<<C%)>G%:REM Until no more notes (1 bits)
ENDPROC
ROOL's avatar
ROOL committed
2365

Chris Wraight's avatar
Chris Wraight committed
2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385
DEF PROCdraw_tie (s%,sx%,sy%)
REM Plot a scaled tie sprite
LOCAL span%,swidth%
LOCAL oldx0%,oldx1%,scale,scale%
sx%=sx%-x%(s%)
sy%=sy%-y%(s%)
swidth%=x%(s%)+X%(s%)
span%=X%+PX%(FNbound_tie(PX%))-sx%+Pgap%
oldx0%=factors%!0
oldx1%=factors%!8
scale=span%/swidth%
IF scale<0.6 THEN scale=0.6
scale%=scale*10
factors%!0=oldx0%*scale%
factors%!8=oldx1%*10
SYS "OS_SpriteOp",SprPlot%,SprBlk%,S%(s%),sx%,sy%,8,factors%,pixtrans%
factors%!0=oldx0%
factors%!8=oldx1%
ENDPROC

2386 2387 2388 2389 2390
DEF PROCdraw_attribute(A%)
LOCALx%,N%
N%=TRUE:REPEATN%-=TRUE:UNTILA%AND%1<<N%
IFPTYPE%(PX%)EOR%1<<N% PX%+=1
x%=X%+PX%(PX%)
ROOL's avatar
ROOL committed
2391
ONN%+1 PROCdraw_time_sig(A%,x%),PROCdraw_key_sig(A%),PROCdraw_clef(A%),PROCdraw_slur(A%),PROCdraw_octave(A%),PROCdraw_barline(A%)
2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447
ENDPROC

DEF PROCPutBarInfo(bar%)
REM setup the various pointers to arrays at the start of this bar
FOR n%=0 TO 7
 BPn%(n%,bar%)=N%(n%)
 NEXT
PXn%(bar%)=PX%
GPn%(bar%)=GP%
FOR n%=0 TO Max_Stave%
 CLEFn%(n%,bar%)=CLEF%(n%)
 NEXT
SIGn%(0,bar%)=SIG%(0)
SIGn%(1,bar%)=SIG%(1)
ENDPROC

DEF PROCGetBarInfo(bar%)
REM get the values of the data at the start of this bar
FOR n%=0 TO 7
 N%(n%)=BPn%(n%,bar%)
 NEXT
PX%=PXn%(bar%)
GP%=GPn%(bar%)
FOR n%=0 TO Max_Stave%
 CLEF%(n%)=CLEFn%(n%,bar%)
 NEXT
SIG%(0)=SIGn%(0,bar%)
SIG%(1)=SIGn%(1,bar%)
ENDPROC

DEF PROCSetupBarStarts(bar%)
REM find pointers to various bits at the starts of all bars through
REM the music. Start at bar%
REM this is just to speed up redraw, avoiding all the note-skipping
LOCAL last%
BAR%=bar%
IF bar%>0 PROCGetBarInfo(bar%) ELSE PROCstart_music
last%=BAR%
PROCPutBarInfo(BAR%)
WHILE GP%<GATE%
 IF?GP% THEN
  PROCskip_notes(?GP%):GP%+=1
 ELSE
  PROCskip_attribute(GP%?1):GP%+=2
  IF BAR%<>last% last%=BAR% : PROCPutBarInfo(BAR%)
 ENDIF
 ENDWHILE
BAR%+=1
PROCPutBarInfo(BAR%)
NBars%=BAR%
ENDPROC

DEF PROCskip_gate
IF?GP% PROCskip_notes(?GP%):GP%+=1 ELSEPROCskip_attribute(GP%?1):GP%+=2
ENDPROC

Chris Wraight's avatar
Chris Wraight committed
2448 2449
DEF PROCskip_notes(g%)
LOCAL c%
2450
PX%+=1
Chris Wraight's avatar
Chris Wraight committed
2451
c%=-1
2452 2453
REPEAT
  REPEAT
Chris Wraight's avatar
Chris Wraight committed
2454 2455 2456 2457
    c%+=1
  UNTIL (g% AND %1<<c%)
  N%(c%)+=2
UNTIL (2<<c%)>g%
2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486
ENDPROC

DEF PROCskip_attribute(A%)
LOCALT%
T%=%1:IFA%AND%1 ELSEREPEATT%=T%<<%1:UNTILA%ANDT%
CASET%OF
WHENTime%,Key%:SIG%(T%-1)=A%
WHENClef%:CLEF%(A%>>6)=A%>>3AND3
WHENBar%:BAR%+=1
ENDCASE
IFT%EORPTYPE%(PX%) PX%+=1
ENDPROC

DEF PROCback_gate
IFGP%?-2 GP%-=1:PROCback_notes(?GP%) ELSEGP%-=2:PROCback_attribute(GP%?1)
ENDPROC

DEF PROCback_notes(G%)
PX%-=1
LOCALC%:C%=TRUE
REPEATREPEATC%-=TRUE:UNTILG%AND%1<<C%:N%(C%)-=2:UNTIL(2<<C%)>G%
ENDPROC

DEF PROCback_attribute(A%)
IFA%=Bar% BAR%-=1
IFA%ANDPTYPE%(PX%+1)+TRUE ELSEIFA%ANDPTYPE%(PX%+1) ENDPROC
PX%-=1
ENDPROC

ROOL's avatar
ROOL committed
2487 2488
DEF PROCdraw_time_sig (a%,x%)
LOCAL i%,n%,b%,nx%,bx%
2489
SIG%(0)=a%
ROOL's avatar
ROOL committed
2490 2491 2492 2493 2494 2495
n%=(a%>>1 AND 15)+1
b%=%1<<(a%>>5)-1
IF X%(time%+n%)+x%(time%+n%)<PW%(PX%) THEN nx%=x%+12 ELSE nx%=x%
nx%+=x%(time%+n%)
IF X%(time%+b%)+x%(time%+b%)<PW%(PX%) THEN bx%=x%+12 ELSE bx%=x%
bx%+=x%(time%+b%)
2496
FOR i%=0 TO STAVE%+PERC%
ROOL's avatar
ROOL committed
2497 2498
  PROCsprite(time%+n%,nx%,Y%+Y_STAVE%(i%))
  PROCsprite(time%+b%,bx%,Y%+Y_STAVE%(i%)-32)
2499
NEXT
2500
ENDPROC  
2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532

DEF PROCdraw_key_sig(A%)
LOCALS%,C%,N%,a%,W%
IFA%AND56 SIG%(1)=A% ELSESWAPA%,SIG%(1):a%=accidental%+1:REM Change to C Major uses naturals in old key sig positions
N%=(A%>>3AND7)-1
IFN%>=0 THEN
A%=A%>>2AND%1:REM 0-Sharp or 1-flat signature
IFa% ELSEa%=accidental%+2+A%
W%=x%(a%)+X%(a%)
x%+=x%(a%)
FOR C%=0 TO N%
FOR S%=0TOSTAVE%
PROCsprite(a%,x%,Y%+Y_STAVE%(S%)+Li%*Key_Y%(CLEF%(S%),A%,C%))
NEXT
x%+=W%
NEXT
ENDIF
ENDPROC

DEF PROCdraw_clef(A%)
LOCALS%
S%=A%>>6
CLEF%(S%)=A%>>3AND3
IFS%<=STAVE% PROCsprite(clef%+CLEF%(S%),x%,Y%+Y_STAVE%(S%))
ENDPROC

DEF PROCdraw_slur(A%)
ENDPROC

DEF PROCdraw_octave(A%)
ENDPROC

ROOL's avatar
ROOL committed
2533 2534
DEF PROCdraw_barline (a%)
LOCAL s%,type%,by%,bx%
ROOL's avatar
ROOL committed
2535 2536
REM Draw barline sprite
type%=a%>>6
2537
BAR%+=1
ROOL's avatar
ROOL committed
2538 2539
FOR s%=0 TO STAVE%+PERC%
  PROCsprite(bar%+type%,x%,Y%+Y_STAVE%(s%))
2540
NEXT
ROOL's avatar
ROOL committed
2541 2542
REM Print bar number
IF BAR% MOD 5=0 THEN
ROOL's avatar
ROOL committed
2543
  MOVE x%,Y%+Y_STAVE%(0)+Stave_Height%+20*Vi%
2544
  PRINT STR$(BAR%)
ROOL's avatar
ROOL committed
2545 2546
ENDIF
REM Connect keyboard staves
ROOL's avatar
ROOL committed
2547
bx%=x%-x%(bar%+type%)
ROOL's avatar
ROOL committed
2548
IF (STAVE%+1) AND 2 THEN
ROOL's avatar
ROOL committed
2549 2550 2551
  MOVE bx%,Y%+Y_STAVE%(STAVE%)+Stave_Height% DIV 2
  by%=Y_STAVE%(STAVE%-1)-Y_STAVE%(STAVE%)-Stave_Height%
  DRAW BY 0,by%
ROOL's avatar
ROOL committed
2552
  IF type%=1 THEN
ROOL's avatar
ROOL committed
2553
    RECTANGLE FILL bx%+6,Y%+Y_STAVE%(STAVE%)+Stave_Height% DIV 2,5,by% 
2554 2555 2556 2557
  ENDIF
ENDIF
ENDPROC

ROOL's avatar
ROOL committed
2558 2559
REM // Put //

2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571
DEF PROCput_down
LOCALC%,PX%,X%,Y%,S%,s%
SYS "Hourglass_On"
GP%=SCRIBE%(sgp%)
FORC%=0TO7:N%(C%)=SCRIBE%(C%):NEXT
PX%=SCRIBE%(posx%)
X%=SCRIBE%(sx%)
Y%=SCRIBE%(sy%)
s%=SCRIBE%(sprite%)
S%=SCRIBE%(stave%)
C%=SCRIBE%(sc%)
PROCrelease:REM undraw sprite being dragged
2572 2573 2574
IF s%<accidental% THEN PROCput_note
IF s%>=accidental% AND s%<clef% THEN PROCput_accidental
IF s%>=clef% AND s%<=clef%+3 THEN PROCput_clef
ROOL's avatar
ROOL committed
2575
IF s%>dot% AND s%<=dot%+3 THEN PROCput_dot
2576 2577 2578 2579
IF s%>=bar% AND s%<=bar%+3 THEN PROCput_bar
IF s%>=tie% AND s%<=tie%+1 THEN PROCput_tie
IF s%=time% THEN PROCput_time
IF s%=key% THEN PROCput_key
2580 2581 2582 2583 2584 2585 2586 2587 2588
GP%=SCRIBE%(sgp%)
FORC%=0TO7:N%(C%)=SCRIBE%(C%):NEXT
PROCarrange_stave(SCRIBE%(stave%))
IF PX%(EX%)+200*Hi%>S_Width% PROCSetExtent(PX%(EX%)+200*Hi%)
PROCChangedScore
SYS "Hourglass_Off"
ENDPROC

DEF PROCput_note
ROOL's avatar
ROOL committed
2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614
LOCAL end%,type%
IF s%<rest% THEN type%=note% ELSE type%=rest%
IF X%=PX%(PX%+1) THEN
  REM Insert at end of score or over a current note
  end%=(GP%=EP%)
  IF end% THEN
    PROCinsert_gate(1)
  ELSE
    C%=FNput_conflict(S%,SCRIBE%(line%),type%)
  ENDIF
  IF end% OR C%=-1 THEN
    C%=FNallocate_channel(S%)
    IF C%>=0 THEN
      PROCinsert_note(C%)
      PROCnote_type(C%,s% AND 7)
      PROCnote_line(C%,SCRIBE%(line%),s%)
      IF type%=note% THEN PROCnote_stem(C%,s% AND 8)
      s%=!N%(C%)
      IF end% THEN PROCset_score(PX%):X%=PX%(PX%+1)         
      PROCupdate_note(S%,s%,X%,Y%)
    ENDIF
  ELSE
    s%=!N%(C%)
    REM Only notes delete notes/rests delete rests
    IF ((?N%(C%) AND &F8) AND type%=note%) OR ((?N%(C%) AND &F8)=0 AND type%=rest%) THEN
      PROCdelete_note(C%)
Chris Wraight's avatar
Chris Wraight committed
2615 2616 2617 2618 2619 2620 2621
      REM Check if previous Note has a tie; if so, remove it
      IF N%(C%)>MUSIC%(C%) THEN
        N%(C%)-=2
        IF (?N%(C%) AND &4) THEN PROCnote_tie(C%,0)
        N%(C%)+=2
      ENDIF
      REM If no Notes left in chord, delete the Gate
ROOL's avatar
ROOL committed
2622 2623 2624 2625 2626 2627 2628 2629 2630 2631
      IF ?GP% THEN
        PROCupdate_note(S%,s%,X%,Y%)
      ELSE
        PROCdelete_gate(1)
        IF GP%?-2=0 THEN
          WHILE ?GP%=0 AND GP%<EP%
            PROCdelete_gate(2)
          ENDWHILE
        ENDIF
      ENDIF
Chris Wraight's avatar
Chris Wraight committed
2632
      PROCrescore(0)
ROOL's avatar
ROOL committed
2633 2634
    ENDIF
  ENDIF
2635
ELSE
ROOL's avatar
ROOL committed
2636 2637 2638 2639 2640 2641 2642 2643 2644
  REM Insert between notes
  IF X%>PX%(PX%+1) THEN PROCskip_gate
  PROCinsert_gate(1)
  C%=FNallocate_channel(S%)
  PROCinsert_note(C%)
  PROCnote_type(C%,s% AND 7)
  PROCnote_line(C%,SCRIBE%(line%),s%)
  IF type%=note% THEN PROCnote_stem(C%,s% AND 8)
  PROCrescore(PX%)
2645 2646 2647
ENDIF
ENDPROC

ROOL's avatar
ROOL committed
2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673
DEF FNput_conflict (s%,l%,type%)
REM Check if the placed note/rest is over an existing note/rest
REM Returns channel number (0-7) if conflict found, otherwise -1
LOCAL c%,pline%,conflict%
REM Convert the line value of the placed note/rest to 1-32
IF type%=rest% THEN l%=l%*2
l%+=16
REM Check against converted line values of already placed notes/rests
c%=7
REPEAT
  IF (?GP% AND 1<<c%) AND S_C%(c%)=s% THEN
   IF (?N%(c%) AND &F8) THEN
      REM Already-placed object is a note
      pline%=(?N%(c%)>>3)
    ELSE
      REM Already-placed object is a rest
      pline%=(N%(c%)?1 AND &7)
      IF pline%=0 THEN pline%=4
      pline%=(pline%-4)*2+16
    ENDIF
  ENDIF
  conflict%=(pline%=l%)
  c%-=1
UNTIL conflict% OR c%<0
=c%-(conflict%<>0)

2674
DEF PROCput_tie
Chris Wraight's avatar
Chris Wraight committed
2675 2676 2677 2678 2679 2680
LOCAL c%,tx%
IF (?N%(C%) AND &4) THEN
  REM Remove a tie from the note and redraw
  PROCnote_tie(C%,0)
  tx%=FNbound_tie(SCRIBE%(posx%)+1)
  PROCscore_update(X%,Y%-26,X%+PX%(tx%),Y%+28)
2681
ELSE
Chris Wraight's avatar
Chris Wraight committed
2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702
  REM Check that first note isn't a rest
  IF (?N%(C%) AND &F8)=0 THEN ENDPROC
  REM Place a tie on the first note
  PROCnote_tie(C%,SCRIBE%(sprite%)-(tie%-1))
  REM Arrange channel distribution on the stave so that the next note
  REM appearing on the same stave line (if present) is also the next
  REM note in the Note queue for that channel
  PROCskip_notes(?GP%):GP%+=1
  PROCarrange_stave(S%)
  REM Check that there's a second note on the same staveline to tie to.
  REM If there is, mark the area to redraw; if not, remove the tie.
  GP%=SCRIBE%(sgp%)
  FOR c%=0 TO 7
    N%(c%)=SCRIBE%(c%)
  NEXT
  tx%=FNbound_tie(SCRIBE%(posx%)+1)
  IF tx% THEN
    PROCscore_update(X%,Y%-26,X%+PX%(tx%)+70,Y%+28)
  ELSE
    PROCnote_tie(C%,0)
  ENDIF
2703 2704 2705 2706 2707 2708 2709 2710 2711
ENDIF
ENDPROC

DEF PROCput_accidental
LOCALA%,a%
a%=N%(C%)?1AND7
A%=s%AND7
IFA%=a% A%=0
PROCnote_accidental(C%,A%)
ROOL's avatar
ROOL committed
2712
IFA%*a% PROCscore_update(X%-34,Y%-24,X%+16,Y%+52) ELSEPROCrescore(PX%)
2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723
ENDPROC

DEF PROCput_dot
LOCALD%,d%
d%=N%(C%)?1>>3AND3
D%=s%AND3
IFD%=d% D%=0
PROCnote_dots(C%,D%)
IFD%*d% THEN
s%=N%(C%)?1>>5OR?N%(C%)<<3AND8:IF?N%(C%)AND&F8 ELSEs%=s%ORrest%
X%+=x%(s%)
ROOL's avatar
ROOL committed
2724
PROCscore_update(X%+24,Y%-8,X%+50,Y%)
2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760
ELSE
PROCrescore(PX%)
ENDIF
ENDPROC

DEF PROCput_clef
LOCALc%
c%=s%AND3
IFGP%=EP%OR?GP%OR(GP%?1AND%111)<>Clef% THEN
PROCinsert_gate(2)
PROCclef(S%,c%)
ELSE
WHILE ?GP%=0AND(GP%?1AND%111)=Clef%AND(GP%?1>>6)<>S%ANDGP%<EP%
PROCskip_gate
ENDWHILE
IFGP%<EP%AND?GP%=0AND(GP%?1AND%111)=Clef%AND(GP%?1>>6)=S% THEN
IF(GP%?1>>3AND3)<>c% PROCclef(S%,c%) ELSEPROCdelete_gate(2)
ELSE
PROCinsert_gate(2)
PROCclef(S%,c%)
ENDIF
ENDIF
PROCrescore(PX%)
ENDPROC

DEF PROCput_key
IFGP%=EP%OR?GP%OR(GP%?1AND%11)<>Key% THEN
PROCinsert_gate(2)
PROCkey_sig(KEY_SIG%(0),KEY_SIG%(1))
ELSE
IFKEY_SIG%(1)>0AND(GP%?1>>2AND%1)<>KEY_SIG%(0)OR(GP%?1>>3)<>KEY_SIG%(1) PROCkey_sig(KEY_SIG%(0),KEY_SIG%(1)) ELSEPROCdelete_gate(2)
ENDIF
PROCrescore(PX%)
ENDPROC

DEF PROCput_time
ROOL's avatar
ROOL committed
2761 2762 2763 2764 2765
IF GP%=EP% OR ?GP% OR (GP%?1AND%1)<>Time% THEN
  REM Insert a new Time Signature
  PROCinsert_gate(2)
  PROCtime_sig(TIME_SIG%(0),TIME_SIG%(1))
  PROCrescore(PX%)
2766
ELSE
ROOL's avatar
ROOL committed
2767 2768 2769 2770 2771 2772 2773 2774 2775
  IF (GP%?1>>1AND15)<>TIME_SIG%(0) OR (GP%?1>>5)<>TIME_SIG%(1) THEN
    REM Overwrite new values on an existing Time Signature
    PROCtime_sig(TIME_SIG%(0),TIME_SIG%(1))
    PROCrescore(PX%)
  ELSE
    REM Delete the Time Signature
    PROCdelete_gate(2)
    PROCrescore(PX%)
  ENDIF
2776 2777 2778 2779
ENDIF
ENDPROC

DEF PROCput_bar
ROOL's avatar
ROOL committed
2780 2781
IF (?GP% OR (GP%?1 AND %111111)<>Bar%) AND X%>PX%(PX%+1) AND GP%<EP% THEN
  PROCskip_gate
2782
ELSE
ROOL's avatar
ROOL committed
2783
  IF GP%?-2=0 AND (GP%?-1 AND %111111)=Bar% THEN PROCback_gate
2784
ENDIF
ROOL's avatar
ROOL committed
2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800
IF GP%>MUSIC% THEN
  IF GP%<EP% AND ?GP%=0 AND (GP%?1 AND %111111)=Bar% THEN
    PROCdelete_gate(2)
    NBars%-=1
    WHILE ?GP%=0 AND GP%<EP%
      PROCdelete_gate(2)
    ENDWHILE
    PROCrescore(PX%)
  ELSE
    IF GP%?-2 THEN
      PROCinsert_gate(2)
      PROCbar
      NBars%+=1
      PROCrescore(PX%)
    ENDIF
  ENDIF
2801 2802 2803 2804 2805 2806 2807
ENDIF
ENDPROC

DEF PROCrescore(px%)
IFpx% ELSEPROCstart_music:REM Bar position assumed set, but reset if px%=0
PROCset_score(px%)
IF px%=0 PROCSetupBarStarts(0) ELSE PROCSetupBarStarts(FNFindBar(px%))
ROOL's avatar
ROOL committed
2808
PROCscore_update(PX%(px%),-Score_Height%,Score_Width%,0)
2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819
ENDPROC

DEF PROCupdate_note(S%,N%,X%,Y%)
LOCALlx0%,lx1%,ly0%,ly1%
PROCbound_note(N%):REM Get minimum bounding rectangle of note
IFABS(Y%-Y_STAVE%(S%))>Li%*5 THEN
IFx%(dot%)>lx0% lx0%=x%(dot%)
IFX%(dot%)>lx1% lx1%=X%(dot%):REM Extend to ledger extent
ENDIF
ly1%+=Y%:Y%-=ly0%:ly0%=Y_STAVE%(S%):REM Stave position
IFly1%<ly0%-Li%*5 ly1%=ly0%-Li%*5 ELSEIFY%>ly0%+Li%*5 Y%=ly0%+Li%*5:REM If ledger lines will be drawn increase vertical update space
ROOL's avatar
ROOL committed
2820
PROCscore_update(X%-lx0%,Y%,X%+lx1%+Pgap%,ly1%):REM Update symbol space
2821 2822 2823 2824 2825 2826 2827 2828 2829 2830
ENDPROC

DEF PROCattach(s%,V%)
SCRIBE%(sx%)=TRUE:REM New position (Maintain Y for orientation maintainance)
SCRIBE%(drawn%)=FALSE
IFs%<rest%ANDSCRIBE%(sprite%)<rest% s%=s%AND7ORSCRIBE%(sprite%)AND8:REM Maintain note orientation
SCRIBE%(sprite%)=s%
SCRIBE%(valid%)=V%
SCORING%=TRUE
ENDPROC
ROOL's avatar
ROOL committed
2831

2832
DEF PROCrelease
ROOL's avatar
ROOL committed
2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843
IF SCORING% AND SCRIBE%(drawn%) THEN
  REM Undraw sprite
  PROCfloat(SCRIBE%(sprite%),SCRIBE%(sx%),SCRIBE%(sy%))
  IF SCRIBE%(sprite%)<rest% THEN
    IF ABSSCRIBE%(line%)>5 THEN
      REM Undraw ledger lines
      PROCfloat(ledger%+SCRIBE%(line%)DIV2,SCRIBE%(sx%),Y_STAVE%(SCRIBE%(stave%)))
    ENDIF
  ENDIF
  SCRIBE%(sx%)=TRUE:REM Continue drawing of symbol
  SCRIBE%(drawn%)=FALSE
2844 2845
ENDIF
ENDPROC
ROOL's avatar
ROOL committed
2846

2847 2848 2849
REM   PROCEDURE: scribe(At X%,Y%)
REM
REM DESCRIPTION: Draw current music symbol at position over score window
ROOL's avatar
ROOL committed
2850

2851
DEF PROCscribe(X%,Y%)
ROOL's avatar
ROOL committed
2852 2853 2854 2855 2856
LOCAL S%,L%,C%,A%
Block%!0=ScoreWind_h%
SYS "Wimp_GetWindowState",,Block%
X%-=Block%!4-Block%!20
Y%-=Block%!16-Block%!24
2857
PROCproximate(SCRIBE%(valid%))
ROOL's avatar
ROOL committed
2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897
IF X%<>SCRIBE%(sx%) OR Y%<>SCRIBE%(sy%) THEN
  A%=TRUE
  IF (SCRIBE%(valid%) AND &70)=&60 THEN
    A%=FALSE
    IF C%>=0 THEN A%=SCRIBE%(valid%) AND &8 OR S%<=STAVE% AND ?N%(C%) AND &F8
  ENDIF
  IF A% THEN
    IF SCRIBE%(drawn%) THEN
      PROCfloat(SCRIBE%(sprite%),SCRIBE%(sx%),SCRIBE%(sy%))
      IF SCRIBE%(sprite%)<rest% THEN
        IF ABS(SCRIBE%(line%))>5 THEN
          PROCfloat(ledger%+SCRIBE%(line%) DIV 2,SCRIBE%(sx%),Y_STAVE%(SCRIBE%(stave%)))
        ENDIF
      ENDIF
    ELSE
      SCRIBE%(drawn%)=TRUE
    ENDIF
    IF SCRIBE%(sprite%)<rest% THEN
      IF ABS(L%)>5 THEN PROCfloat(ledger%+L% DIV 2,X%,Y_STAVE%(S%))
      IF Y%<>SCRIBE%(sy%) THEN
        SCRIBE%(sprite%)=SCRIBE%(sprite%) AND 7 OR 8 AND Y%>SCRIBE%(sy%)
      ENDIF
    ENDIF
    SCRIBE%(sx%)=X%
    SCRIBE%(sy%)=Y%
    SCRIBE%(stave%)=S%
    IF S%<=Max_Stave% THEN
      SCRIBE%(sclef%)=CLEF%(S%)
    ELSE
      SCRIBE%(sclef%)=CLEF%(Max_Stave%)
    ENDIF  
    SCRIBE%(line%)=L%
    SCRIBE%(posx%)=PX%
    SCRIBE%(sgp%)=GP%
    SCRIBE%(sc%)=C%
    FOR C%=0 TO 7
      SCRIBE%(C%)=N%(C%)
    NEXT
    PROCfloat(SCRIBE%(sprite%),X%,Y%)
  ENDIF
2898 2899
ENDIF
ENDPROC
ROOL's avatar
ROOL committed
2900 2901

REM PROCEDURE:   proximate(Positions valid mask V%)
2902 2903 2904 2905 2906 2907 2908 2909 2910
REM
REM DESCRIPTION: Will set X%,Y% to be the nearest valid position to the point
REM              X%,Y% supplied (all in stave coordinates) and return the
REM              associated position index PX%.
REM
REM              A mask is supplied to indicate the valid positions:
REM              V% bit  0 -> Valid Time signature positions
REM                      1 -> Valid Key signature positions
REM                      2 -> Valid Clef positions
ROOL's avatar
ROOL committed
2911
REM                      3 -> At note/rest positions
2912 2913 2914 2915 2916
REM                      4 -> In between notes
REM                      5 -> To nearest stave line (else nearest stave)
REM                      6 -> To nearest symbol present
REM                      7 -> On bar line if near
REM
ROOL's avatar
ROOL committed
2917 2918 2919 2920 2921 2922 2923
REM SETS:        S%        Stave
REM              L%        Line(-15..15)
REM              C%        Channel of note
REM              GP%,N%()  Gate and Note pointers
REM              PX%       PX%() index
REM              X%,Y%     Position

2924
DEF PROCproximate(V%):REM (V%, var X%,Y%) = (S%,L%,PX%)
ROOL's avatar
ROOL committed
2925 2926 2927
LOCAL d%,D%:REM Distance previous&current
LOCAL px%,x%:REM Previous PX%, Between note X position
LOCAL gp%:REM Previous gate pointer, key sig
ROOL's avatar
ROOL committed
2928
LOCAL T%:REM Type mask
2929
IF NBars%>4 THEN BAR%=LHBAR% ELSE BAR%=0
ROOL's avatar
ROOL committed
2930 2931
REM Start from start of page
PROCGetBarInfo(BAR%)
2932
D%=4*S_Width%:REM A suitably excessive distance
ROOL's avatar
ROOL committed
2933 2934 2935 2936 2937 2938 2939 2940
IF (V% AND &7) THEN
  REM Set positions for Time Signatures, Key Signatures and Clefs
  REM Pointer points to centre of object  
  X%-=X%(clef%)>>1
  REM Valid Clef/Key/Time: Work out types to ignore after bars 
  T%=-V%<<1 AND &6
  REM Repeat until distance increases or no more positions
  REPEAT
Chris Wraight's avatar
Chris Wraight committed
2941 2942
    REM Save score pointers and distance
    PROCsavp:d%=D%
ROOL's avatar
ROOL committed
2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965
    REM Move to next bar PX%
    REPEAT
      PROCskip_gate
    UNTIL PTYPE%(PX%) AND Bar% OR GP%=EP%
    IF PTYPE%(PX%) AND Bar% THEN
      REM Skipping any T% types
      WHILE PTYPE%(PX%+1) AND T% AND GP%<EP%
        REM Skip all clefs
        REPEAT
          PROCskip_gate
        UNTIL GP%=EP% OR GP%?1 AND %111 EOR %100
      ENDWHILE
      REM Calculate distance to this position
      D%=ABS(X%-PX%(PX%+1))
    ELSE
      REM No more bars
      D%=2*S_Width%
    ENDIF
  UNTIL D%>d% OR GP%=EP%
  REM Previous position may be closer; if so, reset
  IF d%<D% THEN PROCrstp
  REM Set as position
  X%=PX%(PX%+1)
2966
ELSE
ROOL's avatar
ROOL committed
2967 2968 2969 2970 2971
  REM Set x positions for Notes/Rests
  REM Pointer points to centre of note
  X%-=X%(2)>>1
  REM Repeat until distance increases or no more notes
  REPEAT
Chris Wraight's avatar
Chris Wraight committed
2972 2973
    REM Save score pointers and distance
    PROCsavp:d%=D%
ROOL's avatar
ROOL committed
2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012
    REM Move to next symbol
    PROCskip_gate
    REM Skip any attribute types
    WHILE PTYPE%(PX%+1) AND GP%<EP%
      PROCskip_gate
    ENDWHILE
    REM Calculate distance to this note position
    D%=ABS(X%-PX%(PX%+1))
  UNTIL D%>d%ORGP%=EP%
  REM Previous position may be closer (or the last valid note 
  REM position for an accidental/dot to appear); if so, reset 
  IF d%<D% OR GP%=EP% AND (V% AND &40) THEN PROCrstp
  REM If mask setting: To Nearest Stave Line
  IF (V% AND &10) AND GP%<EP% THEN
    REM Valid between notes (if before last position)
    IF X%<PX%(PX%+1) OR X%>PX%(EX%) THEN
      REM Before current: between previous (also if after last note)
      IF PTYPE%(PX%) THEN
        x%=PX%(PX%)+PW%(PX%)-X%(2)
      ELSE
        x%=PX%(PX%)
      ENDIF
      x%=x%+PX%(PX%+1)>>1
      IF (V% AND &80) THEN x%+=8:IF PTYPE%(PX%)=Bar% THEN x%=PX%(PX%)
    ELSE
      REM After current: between next
      x%=PX%(PX%+1)+PX%(PX%+2)>>1
      IF (V% AND &80) THEN x%+=8:IF PTYPE%(PX%+2)=Bar% THEN x%=PX%(PX%+2)
    ENDIF
    REM If mask setting: At Notes, find closest position
    IF (V% AND &8) AND ABS(PX%(PX%+1)-X%)<ABS(x%-X%) THEN
      X%=PX%(PX%+1)
    ELSE
      X%=x%
    ENDIF
  ELSE
    REM Only At Notes: find closest position
    X%=PX%(PX%+1)
  ENDIF
3013
ENDIF
ROOL's avatar
ROOL committed
3014 3015 3016 3017
C%=-1         :REM Initially no channel
L%=0          :REM Initially centre line
res%=1        :REM Resolution setting for Rest placement
IF (V% AND &40) AND ?GP%>0 THEN
Chris Wraight's avatar
Chris Wraight committed
3018
  REM Set y position of Accidentals, Dots and Ties
ROOL's avatar
ROOL committed
3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033
  REM Find nearest symbol at gate
  LOCAL G%,c%:REM Gate mask copy, Previous channel counter
  c%=C%
  d%=2*S_Height%:REM A suitably excessive distance
  G%=?GP%
  REPEAT
    REPEAT
      C%+=1
    UNTIL G% AND %1<<C%
    D%=Y%-Y_STAVE%(S_C%(C%))
    IF (?N%(C%) AND &F8) THEN
      REM Note
      D%=ABS(D%-Li%*((?N%(C%)>>3)-16))
    ELSE
      REM Rest
Chris Wraight's avatar
Chris Wraight committed
3034
      D%=ABS(D%-Li%*FNrest_getline(N%(C%)?1))
ROOL's avatar
ROOL committed
3035 3036 3037 3038 3039 3040 3041 3042 3043 3044
    ENDIF
    IF D%<d% THEN d%=D%:c%=C%
  UNTIL (2<<C%)>G%
  C%=c%:REM Nearest note
  S%=S_C%(C%)
  IF (?N%(C%) AND &F8) THEN
    REM Note
    L%=(?N%(C%)>>3)-16
  ELSE
    REM Rest
Chris Wraight's avatar
Chris Wraight committed
3045
    L%=FNrest_getline(N%(C%)?1)
ROOL's avatar
ROOL committed
3046 3047
    res%=2
  ENDIF
3048
ELSE
ROOL's avatar
ROOL committed
3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090
  REM Set y positions of other objects
  REM First find the correct stave
  LOCAL MS%:REM Highest stave number
  MS%=STAVE%
  REM If object is Clef or Key Signature,
  REM do not include percussion stave
  IF (V% AND &6) THEN
  ELSE
    MS%+=PERC%
  ENDIF
  S%=-1:REM Before first stave
  D%=2*S_Height%:REM A suitably excessive distance
  REM Repeat until distance increases or no more staves
  REPEAT
    d%=D%:REM Copy previous distance
    S%+=1:REM Next stave
    D%=ABS(Y%-Y_STAVE%(S%)):REM Distance between point and current stave
  UNTIL D%>d% OR S%=MS%
  S%+=d%<D%:REM Previous stave was closest unless last&current stave is nearer
  REM Now set y position within stave, if a Note/Rest
  REM Ledger lines take priority over percussion lines
  IF (V% AND &20) THEN
    IF S%=STAVE%+1 THEN S%+=Y%>=Y_STAVE%(STAVE%)-Li%*16
    IF S%<=STAVE% THEN
      IF SCRIBE%(sprite%)>=rest% THEN
        REM Rest
        REM Y% is L% steps away from centre stave, step=Li%*2
        L%=(Y%-Y_STAVE%(S%))/(Li%*2)+8.75
        L%-=8
        res%=2
        REM No further than 3 lines away
        IF ABS(L%)>3 THEN L%=3*SGN(L%)
      ELSE
        REM Note
        REM Y% is L% steps away from centre stave, step=Li%
        L%=(Y%-Y_STAVE%(S%))/Li%+16.75
        L%-=16
        REM No further than 15 lines away
        IF ABS(L%)>15 THEN L%=15*SGN(L%)
      ENDIF
    ENDIF
  ENDIF
3091
ENDIF
ROOL's avatar
ROOL committed
3092 3093
REM Set Y% to centre line of stave, +/- offset, * resolution
Y%=Y_STAVE%(S%)+L%*(Li%*res%)
3094 3095
ENDPROC

Chris Wraight's avatar
Chris Wraight committed
3096 3097 3098 3099 3100 3101
DEF FNrest_getline (n%)
LOCAL line%
line%=(n% AND &7)
IF line%>0 THEN line%-=4
=line%

ROOL's avatar
ROOL committed
3102 3103 3104 3105 3106 3107 3108
DEF PROCsavp
n%()=N%()
px%=PX%
gp%=GP%
clef%()=CLEF%()
sig%()=SIG%()
ENDPROC
3109

ROOL's avatar
ROOL committed
3110 3111 3112 3113 3114 3115 3116
DEF PROCrstp
N%()=n%()
PX%=px%
GP%=gp%
CLEF%()=clef%()
SIG%()=sig%()
ENDPROC
3117

ROOL's avatar
ROOL committed
3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147
DEF PROCSetTempo (t%)
Tempo%=t%
SYS "Sound_QTempo",Tempo%(t%)*128*4096 DIV 6000
PROCwimp_radiotick(TempoMenu%,t%)
ENDPROC

DEF PROCSetVolume (v%)
SYS "Sound_Volume",Volume%(v%)
PROCwimp_radiotick(VolumeMenu%,v%)
ENDPROC

DEF PROCSetKeySig (m%,k%)
LOCAL n%
KEY_SIG%(0)=(k%>=7)+1
KEY_SIG%(1)=ABS(k%-7)    
IF KEY_SIG%(1) THEN
  n%=accidental%+2+KEY_SIG%(0)
  X%(key%)=(x%(n%)+X%(n%))*KEY_SIG%(1)
ELSE
  X%(key%)=x%(accidental%+2)+X%(accidental%+2)
ENDIF
PROCwimp_radiotick(KeyMenu%,m%)
CASE m% OF
  WHEN 0:REM Major menu      
    PROCwimp_radiotick(MajorMenu%,k%)
    PROCwimp_radiotick(MinorMenu%,-1)
  WHEN 1:REM Minor menu
    PROCwimp_radiotick(MajorMenu%,-1)
    PROCwimp_radiotick(MinorMenu%,k%)
ENDCASE
ROOL's avatar
ROOL committed
3148
IF FNwimp_geticonstate(SharpsPane_h%,17,2) THEN PROCattach(key%,%10)
ROOL's avatar
ROOL committed
3149 3150 3151 3152 3153 3154 3155
ENDPROC  

DEF PROCSetTimeSig (n%,b%)
TIME_SIG%(0)=n%
TIME_SIG%(1)=b%
PROCwimp_seticonval(SharpsPane_h%,21,"S"+STR$(TIME_SIG%(0)+1))
PROCwimp_seticonval(SharpsPane_h%,22,"S"+STR$(2^(TIME_SIG%(1)-1)))
ROOL's avatar
ROOL committed
3156
IF FNwimp_geticonstate(SharpsPane_h%,19,2) THEN PROCattach(time%,%1)
3157 3158 3159 3160 3161 3162 3163 3164 3165 3166
ENDPROC

REM given x position, return number of bar
DEF FNFindBar(xpos%)
LOCAL bar%
bar%=0
WHILE PX%(PXn%(bar%))<xpos% AND bar%<=NBars%:bar%+=1:ENDWHILE
IF bar%>0 bar%-=1
=bar%

ROOL's avatar
ROOL committed
3167 3168
REM // Play //

3169 3170
DEF PROCplay_start
LOCALC%,n%
ROOL's avatar
ROOL committed
3171
SYS "Sound_Configure",8
3172 3173
REM set play start bar PBAR% to start of currently displayed window (scx%)
Window%!handle%=ScoreWind_h%
ROOL's avatar
ROOL committed
3174
SYS "Wimp_GetWindowState",,Window%+handle%
3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189
PBAR%=FNFindBar(Window%!scx%)
PP%=GPn%(PBAR%)
FOR n%=0 TO 7
 P%(n%)=BPn%(n%,PBAR%)
 NEXT
FORC%=0TO3
PCLEF%(C%)=Clef%(CLEFn%(C%,PBAR%))
NEXT
PROCplay_key_sig(SIGn%(1,PBAR%))
PLAYING%=TRUE
SCROLLING%=TRUE
Beats%=((SIGn%(0,PBAR%)>>1AND&F)+1)*Length%(SIGn%(0,PBAR%)>>3AND%11100)
Q%()=Beats%
TIE%=&FF
B2%=&10000
ROOL's avatar
ROOL committed
3190 3191
SYS "Sound_QBeat",Beats%
SYS "Sound_QSchedule",Beats%,Sch% OR Sound_QTempo%,Tempo%(Tempo%)*128*4096DIV6000
3192
C%=Beats%/50*&1000:IFC%>&7FFF C%=&7FFF
ROOL's avatar
ROOL committed
3193
SYS "Sound_QTempo",C%
3194 3195
MIDI_OFF%(0,0) = -1
MIDI_OFF%(1,0) = -1
ROOL's avatar
ROOL committed
3196 3197
PROCwimp_setmenustate(MainMenu%,3,0,TRUE)
PROCwimp_setmenustate(EditMenu%,0,22,TRUE)
3198 3199 3200 3201 3202 3203
ENDPROC

REM flush sound queue and ensure note_offs are sent to midi channels
DEFPROCplay_stop
LOCAL note%, n%
PLAYING%=FALSE
ROOL's avatar
ROOL committed
3204 3205 3206
SYS "Sound_QInit"
REM Handle MIDI
IF OutputType%=1 THEN
3207 3208 3209 3210 3211 3212 3213 3214 3215 3216
 FOR n%=0 TO 1
  note%=0
  WHILE (MIDI_OFF%(n%,note%) > 0)
   SYS MIDI_TxCommand%, MIDI_OFF%(n%,note%)
   note%+=1
   ENDWHILE
  NEXT
 REM reset end markers
 MIDI_OFF%(0,0) = -1
 MIDI_OFF%(1,0) = -1
ROOL's avatar
ROOL committed
3217 3218 3219
ENDIF
PROCwimp_setmenustate(MainMenu%,3,0,FALSE)
PROCwimp_setmenustate(EditMenu%,0,22,FALSE)
3220 3221 3222 3223 3224 3225 3226 3227 3228
ENDPROC

REM This should give a reasonable approximation to a smooth scroll
DEF PROCCheckScroll
LOCAL PosX%, WindowWidth%, BarPos%, LastScroll%,ThisScroll%
IF ScoreClosed% THEN
 IF NOT PLAYING% SCROLLING%=FALSE
 ENDPROC
 ENDIF
ROOL's avatar
ROOL committed
3229
IF PBAR%<=2 ENDPROC
3230 3231
IF PLAYING% PROCCheckQ
IF PLAYING% SBAR%=PBAR%-2 ELSE B1%=B2%:B2%=BEAT:IFB2%<B1%:SBAR%+=1
ROOL's avatar
ROOL committed
3232 3233 3234 3235 3236
IF SBAR%>=NBars% OR SBAR%>PBAR%+2 SCROLLING%=FALSE:PROCplay_stop:ENDPROC
Block%!0=ScoreWind_h%
SYS "Wimp_GetWindowState",,Block%
WindowWidth%=Block%!12-Block%!4
LastScroll%=Block%!20
3237 3238 3239 3240 3241 3242 3243 3244 3245
PosX%=PX%(PXn%(SBAR%))
BarPos%=(PX%(PXn%(SBAR%+1))-PosX%)*BEAT DIV Beats%
IF BarPos%*2<Pgap% ENDPROC :REM if small amount into bar can scroll to wrong bar (sync with play)
PosX%+=BarPos%
ThisScroll% = PosX%-WindowWidth%DIV2
IF ThisScroll%<=0 ENDPROC
IF ThisScroll%<>LastScroll% THEN
 REM auto-scroll
 REM divide scroll into small bits to make it appear smooth
ROOL's avatar
ROOL committed
3246 3247 3248 3249 3250
 Block%!20=(LastScroll%+ThisScroll%)/2
 SYS "Wimp_OpenWindow",,Block%
 Block%!20=ThisScroll%
 OldX%=ThisScroll%
 SYS "Wimp_OpenWindow",,Block%
3251 3252
 LHBAR%=FNFindBar(ThisScroll%)-1 :REM the bar number at the left of the window
 IF PLAYING%=FALSE THEN
ROOL's avatar
ROOL committed
3253 3254
   REM stop scrolling at end of music
   IF Block%!20<=LastScroll% THEN SCROLLING%=FALSE   
3255
 ENDIF
ROOL's avatar
ROOL committed
3256
ENDIF
3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270
ENDPROC

DEF PROCplay_bar
LOCALC%,L%,I%,D%,S%,Q%,T%,B%,A%:REM Vars for play_bar & play_notes
LOCAL f
MIDI_Notenum%=0
Q%()=Beats%:REM Reset stave queue counters for next bar
B%=PBAR%
Accidental%()=0:REM No incidental accidentals initially
First%=TRUE
WHILEB%=PBAR%ANDPP%<GATE%
IF?PP% PROCplay_notes(?PP%):PP%+=1 ELSEPROCplay_attribute(PP%?1):PP%+=2
First%=FALSE
ENDWHILE
ROOL's avatar
ROOL committed
3271 3272 3273 3274 3275
IFPP%>=GATE% THEN
  PLAYING%=FALSE:SBAR%+=1
  PROCwimp_setmenustate(MainMenu%,3,0,FALSE)
  PROCwimp_setmenustate(EditMenu%,0,22,FALSE)
ENDIF
3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301
ENDPROC

REM Notes for each gate start playing at Q%; the latest Q%() of the staves on
REM which notes are to play. This is because all notes in a gate play at the
REM same time and cannot start before the shortest of the notes preceding them
REM have finished.
REM Q% is the time at which the notes in the gate are played
REM Q%(S%) is the time at the end of the shortest note played on stave S%.
REM QI%(S%) is the current shortest note beats interval on stave S%

DEF PROCplay_notes(G%)
LOCAL note%, packed%, Qoff%,xtraVol,tempo
tempo = 5 * 128 * Tempo%(Tempo%) / 6000
Q%=FALSE:C%=TRUE
REPEATREPEATC%-=TRUE:UNTILG%AND%1<<C%
IFQ%(S_C%(C%))>Q% Q%=Q%(S_C%(C%))
UNTIL(2<<C%)>G%
QI%()=&10000:C%=TRUE
REPEAT
  IF First% xtraVol=1.01 ELSE xtraVol=1.0 :REM a little dynamics. stress 1st beat in bar very slightly
  REPEATC%-=TRUE:UNTILG%AND%1<<C%
  IF INT(xtraVol*Volume%(Volumes%(C%)))>&7F xtraVol=1.0  :REM ensure no overflow
  T%=?P%(C%):D%=P%(C%)?1:I%=D%>>3:S%=S_C%(C%):L%=T%>>3:A%=0
  IFL%ANDS%<=STAVE% THEN
    IFD%AND7 Accidental%(S%,L%)=D%AND7
    A%=Accidental%(S%,L%):L%+=PCLEF%(S%):IFA% ELSEA%=Key%(L%MOD7):REM If no accidental then revert to key signature
ROOL's avatar
ROOL committed
3302
  ENDIF
3303 3304 3305
  IFTIE%AND%1<<C% THEN
    D%=Duration%(Tempo%)?I%:IFT%AND4 TIE%=TIE%ANDNOT(%1<<C%):T%=P%(C%)+1:REPEATT%+=2:D%+=Duration%(Tempo%)?(?T%>>3):UNTILT%>FINE%(C%)OR4ANDNOTT%?TRUE:IFD%>254 D%=254
    IFL% THEN
ROOL's avatar
ROOL committed
3306 3307 3308
      REM Internal sound system
      IF OutputType%=0 THEN
        SOUNDC%+1,INT(xtraVol*Volume%(Volumes%(C%)))OR&100,Line(L%)+Aoff(A%),D%,Q%
3309
      ENDIF
ROOL's avatar
ROOL committed
3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323
      REM MIDI
      IF OutputType%=1 THEN
        note% = 12 + INT(((Line(L%)+Aoff(A%))/&1000) * 12)
        packed% = (MIDIChannel%(C%)-1) OR (note%<<8) OR INT(xtraVol*(Volume%(Volumes%(C%)))<<16)
        MIDI_OFF%(PBAR%MOD2,MIDI_Notenum%) = M_NoteOff% OR packed%
        MIDI_Notenum% += 1
        MIDI_OFF%(PBAR%MOD2,MIDI_Notenum%) = -1 :REM end marker
        REM D% = no. of 5 centisecs
        Qoff% = Q% + D% * tempo
        IF Qoff% > Q% + 15 Qoff% -= 10  :REM ensure note is off after start and before next note start
        SYS "Sound_QSchedule",Q%,    Sch% OR MIDI_TxCommand%, M_NoteOn%  OR packed%
        SYS "Sound_QSchedule",Qoff%, Sch% OR MIDI_TxCommand%, M_NoteOff% OR packed%
      ENDIF
    ENDIF
3324 3325
  ELSE
    IF4ANDNOTT% TIE%=TIE%OR%1<<C%
ROOL's avatar
ROOL committed
3326
  ENDIF
3327
  P%(C%)+=2:IFLength%(I%)<QI%(S%) QI%(S%)=Length%(I%):Q%(S%)=Q%+QI%(S%)
ROOL's avatar
ROOL committed
3328
UNTIL(2<<C%)>G%
3329 3330 3331 3332
ENDPROC

DEF PROCplay_attribute(A%)
C%=TRUE:REPEATC%-=TRUE:UNTILA%AND%1<<C%
ROOL's avatar
ROOL committed
3333
ONC%+1 PROCplay_time_sig(A%),PROCplay_key_sig(A%),PROCplay_clef(A%),PROCplay_slur(A%),PROCplay_octave(A%),PROCplay_barline(A%)
3334 3335 3336 3337
ENDPROC

DEF PROCplay_time_sig(A%)
A%=((A%>>1AND&F)+1)*Length%(A%>>3AND%11100)
ROOL's avatar
ROOL committed
3338
SYS "Sound_QSchedule",Beats%,Sch% OR Sound_QBeat%,A%
3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356
Beats%=A%
ENDPROC

DEF PROCplay_key_sig(A%)
LOCALN%:A%=A%>>2
FORN%=0TO6:Key%(N%)=Key_Sig%(A%,N%):NEXT
ENDPROC

DEF PROCplay_clef(A%)
PCLEF%(A%>>6)=Clef%(A%>>3AND3)
ENDPROC

DEF PROCplay_slur(A%)
ENDPROC

DEF PROCplay_octave(A%)
ENDPROC

ROOL's avatar
ROOL committed
3357
DEF PROCplay_barline(A%)
3358 3359 3360 3361 3362 3363 3364 3365 3366 3367
PBAR%+=1
ENDPROC

DEF FNinitialise
LOCAL SoundEnable%
PROCEnumerateSWIs
PROCinitialise_miscellany
PROCinitialise_screen
PROCinitialise_sprites
PROCinitialise_music
ROOL's avatar
ROOL committed
3368
PROCinitialise_wimp
3369
PROCinitialise_menu
ROOL's avatar
ROOL committed
3370
SoundEnable%=FNinitialise_sound
3371
IF SoundEnable%=1 IF NOT FNCheckOKTag("NoSoundQuit", 3) PROCterminate
ROOL's avatar
ROOL committed
3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382
REM Set up the defaults for an empty score: Cmaj, 4/4, FF, Moderato
PROCClearAllMusic
PROCSetKeySig(0,7)
PROCSetTimeSig(3,3)
PROCSetVolume(6)
PROCSetTempo(8)
REM Set Toolbar defaults
ClefType%=0     :REM Treble clef
BarlineType%=0  :REM Single barline
REM Set up channels
PROCSetDefaultChannels
3383 3384 3385
=TRUE

DEF PROCEnumerateSWIs
ROOL's avatar
ROOL committed
3386 3387 3388
Sch%=&0F000000 : REM SWI number mask for QSchedule SWI
Sound_QTempo%=&401C5
Sound_QBeat%=&401C6
3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405
LOCAL M%
LOCAL ERROR
ON ERROR LOCAL MIDIpresent%=FALSE : ENDPROC
SYS "OS_SWINumberFromString",0,"MIDI_SoundEnable" TO M%
M_NoteOff% = &80
M_NoteOn%  = &90
MIDI_SoundEnable%        = M%
MIDI_SetTxChannel%       = M% + 2
MIDI_SetTxActiveSensing% = M% + 3
MIDI_TxCommand%          = M% + 10
MIDI_TxLocalControl%     = M% + 15
MIDI_TxOmniModeOff%      = M% + 17
MIDI_TxOmniModeOn%       = M% + 18
MIDI_TxMonoModeOn%       = M% + 19
MIDI_TxPolyModeOn%       = M% + 20
MIDI_TxProgramChange%    = M% + 21
MIDI_TxSystemReset%      = M% + 30
ROOL's avatar
ROOL committed
3406
MIDIpresent%=TRUE : REM Disable untested MIDI bits
3407 3408 3409
ENDPROC

DEF PROCinitialise_miscellany
ROOL's avatar
ROOL committed
3410
DIM Block% 255
3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423
TIME=0
AwaitingAck%=FALSE
SAVING%=FALSE
load_pending% = FALSE
pending_filename$ = ""
ret_code% = -1
doing_scrap_load%=FALSE : REM Used to indicate that we're loading from another application
INITIALISED%=FALSE
shutdown% = FALSE
quit_sender% = 0
my_ref% = -1 : REM unset
CHANGED%=FALSE
FILE%=FALSE
ROOL's avatar
ROOL committed
3424
REM File attributes
ROOL's avatar
ROOL committed
3425
FileName$=FNmessage_lookup(Messages_h%,"Untitled")
ROOL's avatar
ROOL committed
3426 3427
MusicFileType%=&AF1
DIM ftime% 5
3428 3429 3430 3431 3432 3433 3434 3435
ENDPROC

DEF PROCinitialise_screen
DIM modeblockin  40
DIM modeblockout 40
PROCgetmodeinfo(TRUE)
ENDPROC

3436
DEF PROCgetmodeinfo(new%)
3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458

  LOCAL S_Rows%, S_Columns%

  modeblockin!0=0 : REM ModeFlags
  modeblockin!4=1 : REM ScrRCol
  modeblockin!8=2 : REM ScrBCol
  modeblockin!12=3 : REM NColour
  modeblockin!16=4 : REM XEigFactor
  modeblockin!20=5 : REM YEigFactor
  modeblockin!24=11 : REM XWindLimit
  modeblockin!28=12 : REM YWindLimit
  modeblockin!32=-1 : REM terminate list

  SYS "OS_ReadVduVariables",modeblockin, modeblockout
  REM no point in reading sizes since they are fixed by the wimp
  Hi%=1<<(modeblockout!16)
  Vi%=1<<(modeblockout!20)
  S_Width%=Hi% * ((modeblockout!24)+1)
  S_Height%=Vi% * ((modeblockout!28)+1)
  Hi%=2: Vi%=4
  C_Width%=8*Hi% : C_Height%=8*Vi%

3459 3460 3461 3462 3463 3464 3465 3466 3467
  IF new%=FALSE THEN
    SYS "Wimp_ReadPixTrans",&200,SprBlk%,S%(0),,,,factors%,pixtrans%
    j%=7
    FOR i%=0 TO 15
      fpixtrans%?i%=pixtrans%?j%
      IF i%>6 THEN j%-=1
    NEXT
  ENDIF  
  
3468 3469 3470
ENDPROC

DEF PROCinitialise_sprites
ROOL's avatar
ROOL committed
3471
LOCAL path$,size%,file%,yeig%
3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484
path$="<Maestro$Dir>.Sprites"
SYS "OS_ReadModeVariable",-1,5 TO ,,yeig%
IF yeig%=1 THEN path$=path$+"22"
IF yeig%=0 THEN path$=path$+"11"
file%=OPENIN path$
size%=EXT#file%+16
CLOSE#file%

DIM SprBlk% size%
SprBlk%!0=size%
SprBlk%!8=16
SYS "OS_SpriteOp",&109,SprBlk%
SYS "OS_SpriteOp",&10A,SprBlk%,path$
3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499

SprPlot% = &234  : REM sprite plot code
LOCAL I%,x%,y%,W%,H%
RESTORE
Num%=-1
READS$
WHILES$<>""
READx%,y%,W%,H%,S$
Num%+=1
ENDWHILE
IFFNassert(Num%>=0,"SpriteLoad") STOP
DIM S$(Num%),x%(Num%),y%(Num%),X%(Num%),Y%(Num%), S%(Num%) : REM S%() are sprite pointers
RESTORE
FORI%=0TONum%
READ S$(I%),x%,y%,W%,H%
ROOL's avatar
ROOL committed
3500
SYS "OS_SpriteOp", 256+24, SprBlk%, S$(I%) TO ,,S%(I%) : REM get pointer to sprite
3501 3502 3503 3504 3505 3506 3507 3508 3509
x%(I%)=x%*Hi%
y%(I%)=y%*Vi%
X%(I%)=(W%-x%)*Hi%
Y%(I%)=(H%-y%)*Vi%
NEXT
note%=0
rest%=16
accidental%=24
clef%=32
3510 3511 3512 3513
dot%=40
ledger%=43
tie%=44
bar%=51
3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531
time%=55
key%=56
SCORING%=FALSE:REM Flag indicating a sprite is to be drawn under the pointer
wasSCORING%=FALSE
stopSCORING%=TRUE
DIM SCRIBE%(18):REM Details of symbol under pointer
REM 0-7 Channel pointers into N%()
sx%=8:REM Current sprite X position
sy%=9:REM Current sprite Y position
drawn%=10:REM Flag indicating sprite presence
sprite%=11:REM Sprite number
valid%=12:REM Valid stave positions
stave%=13:REM Current stave symbol is on
sclef%=14:REM Current clef applying to symbol (used for key sigs)
line%=15:REM Current stave line symbol is on
posx%=16:REM X music position
sgp%=17:REM Symbol gate pointer
sc%=18:REM Channel number if symbol specified close to note
3532 3533 3534 3535
REM Set up sprite colours for fixed and floating objects
DIM factors% 15
DIM pixtrans% 15
DIM fpixtrans% 15
3536 3537 3538
PROCgetmodeinfo(FALSE)
ENDPROC
REM name, x, y, w, h
3539 3540 3541
DATA B,7,3,24,6,SB,0,2,13,5,Mu,0,2,12,17,Cu,0,2,12,17
DATA Qu,0,2,19,17,SQu,0,2,20,17,DSQu,0,2,20,17,SDSQu,0,2,20,17
DATA B,7,3,24,6,SB,0,2,13,5,Md,0,14,12,17,Cd,0,14,12,17
3542
DATA Qd,0,14,12,17,SQd,0,14,12,17,DSQd,0,14,12,17,SDSQd,0,14,12,17
ROOL's avatar
ROOL committed
3543
DATA Rest,-1,0,8,4,Rest1,2,-2,16,6,Rest2,2,0,16,2,Rest4,-2,5,9,13
3544 3545
DATA Rest8,-1,4,9,9,Rest16,0,6,12,13,Rest32,1,7,15,16,Rest64,2,9,18,19
DATA Natural,8,6,7,12,Natural,8,6,7,12,Sharp,10,6,9,13,Flat,8,3,8,11
3546 3547 3548
DATA Sharp2,9,2,10,5,Flat2,14,3,16,11,NSharp,17,6,17,12,NFlat,15,6,16,14
DATA Treble,0,14,23,31,Alto,0,8,23,17,Alto,0,4,23,17,Bass,0,5,24,14
DATA ldg5,2,28,16,17,ldg4,2,24,16,13,ldg3,2,20,16,9,ldg2,2,16,16,5,ldg1,2,12,16,1
Chris Wraight's avatar
Chris Wraight committed
3549
DATA Dot1,-14,1,4,2,Dot2,-14,1,9,2,Dot3,-14,1,14,2,Tie,-8,-3,20,4,Tie2,-8,7,20,4
3550
DATA ldg1,2,-12,16,1,ldg2,2,-12,16,5,ldg3,2,-12,16,9,ldg4,2,-12,16,13,ldg5,2,-12,16,17
ROOL's avatar
ROOL committed
3551
DATA Bar,-1,8,4,16,DBar,-1,8,6,16,Bar,-2,8,1,16,Bar,-2,8,1,16
ROOL's avatar
ROOL committed
3552
DATA Time,1,9,14,16,Key,0,8,9,17
3553 3554 3555 3556
DATA 2,3,0,14,8,3,3,0,14,8,4,3,0,14,8,5,3,0,14,8,6,3,0,14,8
DATA 7,3,0,14,8,8,3,0,14,8,9,3,0,14,8,10,3,0,26,8,11,3,0,26,8
DATA 12,3,0,26,8,13,3,0,26,8,14,3,0,26,8,15,3,0,26,8,16,3,0,26,8
DATA ""
3557 3558 3559

DEF PROCinitialise_wimp
PROCenumerate_wimp_offsets
ROOL's avatar
ROOL committed
3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573
Block%!0=2
Block%!4=3
Block%!8=4
Block%!12=5
Block%!16=10
Block%!20=&502
Block%!24=&400C1
Block%!28=11
Block%!32=12
Block%!36=8
Block%!40=1
Block%!44=&400C0
Block%!48=0
SYS "Wimp_Initialise",300,&4B534154,maestro$,Block% TO wimpversion%,Task_h%
3574 3575 3576 3577 3578
message_buffer%!0 = (32 + LEN(maestro$)) AND NOT(3)
message_buffer%!12 = 0
message_buffer%!16 = 11
message_buffer%!20 = 7
message_buffer%!24 = 0
ROOL's avatar
ROOL committed
3579 3580
$(message_buffer%+28)=maestro$+CHR$(0)
SYS "Wimp_SendMessage",17,message_buffer%,0
3581
REPEAT
ROOL's avatar
ROOL committed
3582
  SYS "Wimp_Poll",&1972,message_buffer% TO R%
3583 3584 3585 3586 3587 3588 3589 3590
  IF R% = 17 OR R% = 18 OR R% = 19 THEN
    IF message_buffer%!16 = 12 AND message_buffer%!20 = 7 THEN
      R% = FNCheckOK($(message_buffer% + 28), 1)
      PROCterminate
    ENDIF
  ENDIF
UNTIL R% = 0
DIM SpriteName% 14
ROOL's avatar
ROOL committed
3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602
$SpriteName%=FNmessage_lookup (Messages_h%, "SpriteName")
REM Create icon bar icon
Block%!0=-1
Block%!4=0
Block%!8=0
Block%!12=64
Block%!16=68
Block%!20=(&311A OR (0<<24) OR (7<<28))
Block%!24=SpriteName%
Block%!28=1
Block%!32=12
SYS "Wimp_CreateIcon",,Block% TO Maestro_h%
3603
PROCload_templates
ROOL's avatar
ROOL committed
3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614
REM Initialise various window settings
OldX%=0
FirstOpen%=TRUE
PopUpIcon%=0
ScoreClosed%=TRUE
PROCwimp_seticontext(ProgInfo_h%,3,FNmessage_lookup(Messages_h%,"_Version"))
Block%!0=NotesPane_h%
SYS "Wimp_GetWindowState",,Block%
PaneHeight%=Block%!16-Block%!8
SelW%=-1
SelI%=-1
3615 3616 3617
ENDPROC

DEF PROCenumerate_wimp_offsets
ROOL's avatar
ROOL committed
3618
Window%=Block%+4
3619 3620 3621 3622 3623 3624
handle%=-4
x0%=0:y0%=4:x1%=8:y1%=12
scx%=16:scy%=20
ENDPROC

DEF PROCload_templates
ROOL's avatar
ROOL committed
3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642
wblock%=FNwimp_opentemplates("<Maestro$Dir>.Templates")
  ProgInfo_h%=FNwimp_loadtemplate("ProgInfo",wblock%,FALSE)
  QuitQuery_h%=FNwimp_loadtemplate("query",wblock%,FALSE)
  FileInfo_h%=FNwimp_loadtemplate("FileInfo",wblock%,FALSE)
  TimeSig_h%=FNwimp_loadtemplate("TimeSigW",wblock%,FALSE) 
  Bar_h%=FNwimp_loadtemplate("BarW",wblock%,FALSE)
  ClearQuery_h%=FNwimp_loadtemplate("close",wblock%,FALSE)
  ScoreWind_h%=FNwimp_loadtemplate("ScoreWind",wblock%,SprBlk%)
  Print_h%=FNwimp_loadtemplate("print_db",wblock%,FALSE)
  Save_h%=FNwimp_loadtemplate("xfer_send",wblock%,FALSE)
  InstrWind_h%=FNwimp_loadtemplate("InstrWind",wblock%,SprBlk%)
  SharpsPane_h%=FNwimp_loadtemplate("SharpsPane",wblock%,SprBlk%)
  NotesPane_h%=FNwimp_loadtemplate("NotesPane",wblock%,SprBlk%)
  Clef_h%=FNwimp_loadtemplate("ClefW",wblock%,SprBlk%)
  Barline_h%=FNwimp_loadtemplate("BarLineW",wblock%,SprBlk%)
  StaveWind_h%=FNwimp_loadtemplate("StaveWind",wblock%,FALSE)
SYS "Wimp_CloseTemplate"
ENDPROC  
3643 3644

DEF PROCinitialise_menu
ROOL's avatar
ROOL committed
3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705
LOCAL i%,f%
VolumeMenu%=FNwimp_makemenu(FNmessage_lookup(Messages_h%,"Volume"),8)
FOR i%=0 TO 7
  IF i%<7 THEN f%=0 ELSE f%=128
  PROCwimp_additem(VolumeMenu%,i%,f%,-1,FNmessage_lookup(Messages_h%,"V"+STR$(i%)))
NEXT
TempoMenu%=FNwimp_makemenu(FNmessage_lookup(Messages_h%,"Tempo"),15)
FOR i%=0 TO 14
  IF i%<14 THEN f%=0 ELSE f%=128
  PROCwimp_additem(TempoMenu%,i%,f%,-1,FNmessage_lookup(Messages_h%,"T"+STR$(i%)))
NEXT
FileMenu%=FNwimp_makemenu(FNmessage_lookup(Messages_h%,"File"),3)
PROCwimp_additem(FileMenu%,0,0,FileInfo_h%,FNmessage_lookup(Messages_h%,"FInfo"))
PROCwimp_additem(FileMenu%,1,0,Save_h%,FNmessage_lookup(Messages_h%,"Save"))
PROCwimp_additem(FileMenu%,2,136,Print_h%,FNmessage_lookup(Messages_h%,"Print"))
EditMenu%=FNwimp_makemenu(FNmessage_lookup(Messages_h%,"Edit"),2)
PROCwimp_additem(EditMenu%,0,8,Bar_h%,FNmessage_lookup(Messages_h%,"Goto"))
PROCwimp_additem(EditMenu%,1,128,-1,FNmessage_lookup(Messages_h%,"Clear"))
ScoreMenu%=FNwimp_makemenu(FNmessage_lookup(Messages_h%,"Score"),4)
PROCwimp_additem(ScoreMenu%,0,0,-1,FNmessage_lookup(Messages_h%,"Staves"))
PROCwimp_additem(ScoreMenu%,1,0,-1,FNmessage_lookup(Messages_h%,"Instruments"))
PROCwimp_additem(ScoreMenu%,2,0,VolumeMenu%,FNmessage_lookup(Messages_h%,"Volume"))
PROCwimp_additem(ScoreMenu%,3,128,TempoMenu%,FNmessage_lookup(Messages_h%,"Tempo"))
MainMenu%=FNwimp_makemenu(FNmessage_lookup(Messages_h%,"Maestro"),4)
PROCwimp_additem(MainMenu%,0,0,FileMenu%,FNmessage_lookup(Messages_h%,"File"))
PROCwimp_additem(MainMenu%,1,0,EditMenu%,FNmessage_lookup(Messages_h%,"Edit"))
PROCwimp_additem(MainMenu%,2,2,ScoreMenu%,FNmessage_lookup(Messages_h%,"Score"))
PROCwimp_additem(MainMenu%,3,128,-1,FNmessage_lookup(Messages_h%,"Play"))
IconMenu%=FNwimp_makemenu(FNmessage_lookup(Messages_h%,"Maestro"),3)
PROCwimp_additem(IconMenu%,0,0,ProgInfo_h%,FNmessage_lookup(Messages_h%,"Info"))
PROCwimp_additem(IconMenu%,1,0,-1,FNmessage_lookup(Messages_h%,"Help"))
PROCwimp_additem(IconMenu%,2,128,-1,FNmessage_lookup(Messages_h%,"Quit"))
ChannelMenu%=FNwimp_makemenu(FNmessage_lookup(Messages_h%,"Channel"),16)
FOR i%=0 TO 15
  IF i%<15 THEN f%=0 ELSE f%=128
  PROCwimp_additem(ChannelMenu%,i%,f%,-1,STR$(i%+1))
NEXT
VoiceMenu%=FNwimp_makemenu(FNmessage_lookup(Messages_h%,"Voices"),MAX_VOICES%-2)
FOR i%=0 TO MAX_VOICES%-3
  IF i%<MAX_VOICES%-3 THEN f%=0 ELSE f%=128
  PROCwimp_additem(VoiceMenu%,i%,f%,-1,STRING$(32,"X"))
NEXT
OutputMenu%=FNwimp_makemenu(FNmessage_lookup(Messages_h%,"Output"),2)
PROCwimp_additem(OutputMenu%,0,0,-1,FNmessage_lookup(Messages_h%,"OT0"))
PROCwimp_additem(OutputMenu%,1,128,-1,FNmessage_lookup(Messages_h%,"OT1"))
MajorMenu%=FNwimp_makemenu(FNmessage_lookup(Messages_h%,"Major"),15)
FOR i%=0 TO 14
  IF i%<14 THEN f%=0 ELSE f%=128
  IF i%=6 OR i%=7 THEN f%=2
  PROCwimp_additem(MajorMenu%,i%,f%,-1,FNmessage_lookup(Messages_h%,"MJ"+STR$(i%)))
NEXT
MinorMenu%=FNwimp_makemenu(FNmessage_lookup(Messages_h%,"Minor"),15)
FOR i%=0 TO 14
  IF i%<14 THEN f%=0 ELSE f%=128
  IF i%=6 OR i%=7 THEN f%=2
  PROCwimp_additem(MinorMenu%,i%,f%,-1,FNmessage_lookup(Messages_h%,"MN"+STR$(i%)))
NEXT
KeyMenu%=FNwimp_makemenu(FNmessage_lookup(Messages_h%,"KeySig"),2)
PROCwimp_additem(KeyMenu%,0,0,MajorMenu%,FNmessage_lookup(Messages_h%,"Major"))
PROCwimp_additem(KeyMenu%,1,128,MinorMenu%,FNmessage_lookup(Messages_h%,"Minor"))
ENDPROC
3706 3707 3708

DEF FNinitialise_sound
LOCAL SoundEnable%
ROOL's avatar
ROOL committed
3709 3710
SYS "Sound_Configure",8 TO OldConfigure
SYS "Sound_Enable",0 TO SoundEnable%
3711
REM disconnect midi interpreter
ROOL's avatar
ROOL committed
3712
IF MIDIpresent% THEN SYS MIDI_SoundEnable%,0
3713 3714 3715
=SoundEnable%

DEF PROCClearAllMusic
ROOL's avatar
ROOL committed
3716
LOCAL f%
3717 3718 3719 3720 3721 3722 3723 3724
FINE%()=MUSIC%()
PP%=MUSIC%:P%()=MUSIC%()
NBars%=0 :REM number of bars
BAR%=0:REM Current bar number
PBAR%=0:REM Playing bar counter
SBAR%=0:REM scrolling bar number
PROCstart_music:REM Set pointers to start of music store
EP%=GP%
ROOL's avatar
ROOL committed
3725 3726 3727
REM Set BarlineType to single
BarlineType%=0
PROCwimp_seticonval(SharpsPane_h%,13,"Ssbarline")
3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742
PROCbar:REM Music always starts off with a bar
GATE%=MUSIC%+2:REM End of minimum score
PX%(0)=0:REM Score starts at left window edge
PXn%(BAR%)=0
PW%(0)=4*Hi%:REM Bar width
PTYPE%(0)=Bar%:REM Score starts with a bar
GP%=MUSIC%:REM Reset gate pointer for set score
GPn%(BAR%)=GP%
PROCrescore(0)
PROCSetExtent(S_Width%)
PROCrelease
wasSCORING%=FALSE
stopSCORING%=TRUE
SCORING%=FALSE
CHANGED%=FALSE
ROOL's avatar
ROOL committed
3743 3744 3745
REM Reset file information
FileName$=FNmessage_lookup(Messages_h%,"Untitled")
PROCUpdateTitle(FileName$)
ROOL's avatar
ROOL committed
3746 3747
PROCwimp_seticontext(FileInfo_h%,2,FNmessage_lookup(Messages_h%,"No"))
PROCwimp_seticontext(Save_h%,1,FNmessage_lookup(Messages_h%,"MusicFile")+CHR$(0))
ROOL's avatar
ROOL committed
3748
f%=FNGetFileInfo("")
3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888
ENDPROC

REM   PROCEDURE: initialise_music
REM
REM  STRUCTURES: Queue of Gate;
REM              Queue of Music;
REM
REM       TYPES:  Gate= byte0>0 -> Gate_Mask
REM                     byte0=0 -> Music_Attribute
REM
REM          Gate_Mask= byte; bitn=1 -> gate 1 note/rest from music queue n (n=0-7)
REM
REM    Music_Attribute= word; bit0_7=0
REM                             bit8=1 -> Time signature bit12_9 No. beats-1
REM                                                      bit15_13 Beat type
REM                             bit9=1 -> Key signature bit10 0-#, 1-b
REM                                                     bit13_11 0-7
REM                            bit10=1 -> Clef bit12_11 Treble,Alto,Tenor,Bass
REM                                            bit15_14 Stave 1-4
REM                            bit11=1 -> Slur switch bit12 on/off
REM                                                   bit15_14 Stave 1-4
REM                            bit12=1 -> Octave shift bit13 0-up,1-down
REM                                                    bit15_14 Stave 1-4
REM                            bit13=1 -> bar
REM                         (bit13_0=0 -> Reserved)
REM
REM              Music= word; bit7_3>0 -> Note
REM                           bit7_3=0 -> Rest
REM
REM               Note= word; bit0=     Stem orientation: 0-up,1-down
REM                           bit1=1 -> Join barbs to next note
REM                           bit2=1 -> Tie with next note
REM                         bit7_3>0    Note stave line position 1 to 31 (16=centre line)
REM                        bit10_8>0 -> Accidental N,#,b,X,bb,N#,Nb
REM                       bit12_11=     Number of dots 0 to 3
REM                       bit15_13=     Type: Breve to Semi-demi-semiquaver
REM
REM               Rest= word; bit0=0    NB If a rest coincides with a note, its
REM                           bit1=0       position is determined by the
REM                           bit2=0       following note on the same channel.
REM                         bit7_3=0
REM                        bit10_8=0
REM                       bit12_11=     Number of dots 0 to 3
REM                       bit15_13=     Type: Breve rest to Semi-demi-semiquaver rest
REM
REM      Position_type= 0 -> Note
REM                     1 -> Time Signature
REM                     2 -> Key Signature
REM                     3 -> Clef
REM                     4 -> Slur
REM                     5 -> Octave shift
REM                     6 -> Bar
:
DEF PROCinitialise_music
LOCALN%,C%:REM Note,channel
PROCinitialise_options
Note%=0:Time%=1:Key%=2:Clef%=4:Slur%=8:Octave%=16:Bar%=32:REM Stave attribute enumerators
DIM Ninc%(6),Line(42),Aoff(7),Clef%(3),Key%(6),Key_Sig%(15,6),Length%(31),Duration%(NTempos%),Accidental%(3,31)
FORN%=0TO6:Ninc%(N%)=ASCMID$("024579;",N%+1)AND15:NEXT:REM Note increment
LOCALST:ST=&1000/12:REM Semitone increment
FORN%=0TO42
Line(N%)=(1+N%DIV7<<12)+Ninc%(N%MOD7)*ST+.49
NEXT:REM Notes corresponding to stave lines (C octave 1 TO C octave 7)
Aoff(2)=ST:Aoff(3)=-ST:Aoff(4)=ST*2:Aoff(5)=-ST*2:Aoff(6)=ST:Aoff(7)=-ST:REM Accidental offsets
Clef%(0)=11:Clef%(1)=5:Clef%(2)=3:Clef%(3)=-1:REM Line offsets for each clef
FORC%=2TO15
FORN%=0TO(C%>>1)-1
Key_Sig%(C%,(7+Key_Y%(1,C%AND%1,N%))MOD7)=C%MOD2+2
NEXT
NEXT:REM Set up note offsets for each key signature
FORC%=0TO31
Length%(C%)=(%1<<7-(C%>>2))*(%1111000>>(C%AND3)AND%1111):REM Length of each possible note/rest (dotted) in tempo beats
NEXT
LOCALD%:REM Duration
FORN%=0TONTempos%
DIM C% 32:Duration%(N%)=C%:REM Reserve space for each tempo
FORC%=0TO31
D%=75/Tempo%(N%)*Length%(C%)/8+.5:REM Durations of note+dot combinations in 20ths of a second for each tempo (Max 22.5 Seconds)
IFD%>254 D%=254:REM Limit to maximum possible duration (12.7 Seconds)
Duration%(N%)?C%=D%
NEXT
NEXT
TIE%=&FF:REM Tie state of channels - Each bit corresponds to a channel, 0 if tied
SPACE%=HIMEM-END :REM find available space to tailor allocation
IFFNassert(SPACE%>1024,"OutMem3") STOP

Max_Gate%=SPACE%/100 :REM max no. of gates (events, chords)
REM Me thinks this is a hatchet job. Let's try and be more accurate...
REM But there again, all space reserved is USED. Inefficiency alert!
REM 400K odd used for a 26K tune. Whew!
Max_Bar%=Max_Gate%/4 :REM maximum number of bars

PLAYING%=FALSE:REM Flag indicating play in process
SCROLLING%=FALSE:REM Flag indicating auto-scrolling while playing

DIM Q%(Max_Stave%+2):REM Time positions of next notes on each stave
DIM QI%(Max_Stave%+2):REM Time increments of Q%() for each stave

B1%=0:B2%=0:REM Alternate beat counters used to detect zero wrap
::
DIM GPn%(Max_Bar%) :REM GP% at the start of each bar
DIM PX%(Max_Gate%),PW%(Max_Gate%),PTYPE%(Max_Gate%):REM Notation screen positions, widths & types
REM PX% is PX%() index to screen positions & types - Always refers to the note/attribute just passed
DIM PXn%(Max_Bar%) :REM PX% at start of each bar
DIM BPn%(7,Max_Bar%):REM Indices to (8) note queues at the start of each bar
DIM MUSIC%(7),FINE%(7):REM Pointers delimiting music storage
DIM N%(7),n%(7):REM Pointers to current notes (n%()=copy, cf PROCproximate)
DIM C%(7),c%(7):REM Indexes of gate channels used in sorting
DIM CLEF%(Max_Stave%),clef%(Max_Stave%):REM Current clef on each stave (& clef copy)
DIM CLEFn%(Max_Stave%,Max_Bar%) :REM CLEF% at start of each bar
DIM SIG%(1),sig%(1):REM Base bar key & time sigs, current and copy
DIM SIGn%(1,Max_Bar%) :REM SIG% at the start of each bar
DIM P%(7),PCLEF%(3):REM Playing note pointers, clef

MaxNotesInBar%=128
REM while one half of draw q is being filled, the other half is being used for scrolling.

LOCAL n%
n%=MaxNotesInBar%*4

DIM MIDI_OFF%(1,n%) :REM an array of MIDI noteoffs if playing is stopped in the middle of a bar
MIDI_OFF%(0,0)=-1
MIDI_OFF%(1,0)=-1
MIDI_Notenum%=0

SPACE%=HIMEM-END
SPACE%-=&6000:REM Leave a few K for the program and Basic stack (was &4000 before printing)

IFFNassert(SPACE%>1024,"OutMem3") STOP
DIM MUSIC% SPACE%+8:REM Allocate main music space (+8: Extra bytes in case of overrun)

FINE%=MUSIC%+SPACE%:REM End of music memory
GATE%=MUSIC%:REM No gate space used as yet

FORC%=0TO7
  MUSIC%(C%)=MUSIC%+(C%+1)*SPACE%/9
NEXT:REM Share out storage (NB No bounds checking ever occurs!)

FINE%()=MUSIC%():REM No music defined yet

ROOL's avatar
ROOL committed
3889
Pgap%=X%(2)DIV2:REM Symbol spacing (half note blob width)
3890 3891 3892
ENDPROC

DEF PROCinitialise_options
ROOL's avatar
ROOL committed
3893
REM Tempo
3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911
NTempos%=14
DIM Tempo%(NTempos%)
Tempo%(0)=40
Tempo%(1)=50
Tempo%(2)=60
Tempo%(3)=65
Tempo%(4)=70
Tempo%(5)=80
Tempo%(6)=90
Tempo%(7)=100
Tempo%(8)=115
Tempo%(9)=130
Tempo%(10)=145
Tempo%(11)=160
Tempo%(12)=175
Tempo%(13)=190
Tempo%(14)=210

ROOL's avatar
ROOL committed
3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925
REM Time signatures
DIM TIME_SIG%(1)   :REM Current time signature numerator(0) and denominator(1)

REM Key signatures
DIM KEY_SIG%(1)    :REM Current key signature (0: 0/1 flats/sharps; 1: 0-7)
DIM Key_Y%(3,1,6)  :REM Key signature 0-sharp/1-flat stave line positions
LOCAL C%,A%,P%     :REM Indices
FOR C%=0 TO 3      :REM For each clef
  FOR A%=0 TO 1    :REM For each accidental type
    FOR P%=0 TO 6  :REM For each accidental position
      REM Position offsets of key signature accidentals from centre stave line
      Key_Y%(C%,1-A%,P%)=3*(P%AND%1)-P%DIV2+(P%-3)*A%+(A%ANDC%<>2AND(P%AND5)=0)*7-1-(C%-1>>1)-2*(C%=2)
    NEXT
  NEXT
3926
NEXT
ROOL's avatar
ROOL committed
3927 3928 3929 3930 3931 3932 3933

REM Volumes
NVolumes%=7
DIM Volume%(NVolumes%),Volumes%(7)
FOR R%=0 TO NVolumes%
  REM don't permit full volume so that dynamics are possible
  Volume%(R%)=(R%+1)*120/(NVolumes%+1)-1
3934
NEXT
ROOL's avatar
ROOL committed
3935 3936 3937 3938 3939 3940 3941 3942
SYS "Sound_Volume" TO R%
RestoreVolume=R%
REM don't permit full volume. This is reserved for dynamics
Volume%=R%*(NVolumes%+1)/120-.5
IF Volume%<0 THEN Volume%=0
SYS "Sound_Volume",Volume%(Volume%)
FOR R%=0 TO 7
  Volumes%(R%)=6
3943
NEXT
ROOL's avatar
ROOL committed
3944 3945

REM Staves
3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967
Max_Stave%=3
Li%=2*Vi%
Stave_Height%=Li%*8
DIM Y_STAVE%(Max_Stave%+2)
STAVE%=0   :REM means 1 stave
PERC%=0    :REM means no percussion
Score_Width%=S_Width%
PROCposition_staves
LOCALS%
DIM Stave_Channels%(Max_Stave%+2,7):REM Primary stave allocation of channels 0-7 for each stave structure
FOR S%=0 TO Max_Stave%
FOR R%=0 TO 7
Stave_Channels%(S%,R%)=(S%+1)*R%DIV8:REM Close formula to desired data
NEXT
NEXT
Stave_Channels%(2,1)=1
Stave_Channels%(2,2)=1
Stave_Channels%(2,5)=2:REM Correct the exceptions to formula
DIM S_C%(7):REM Current stave allocation
FORR%=0TO7
S_C%(R%)=Stave_Channels%(STAVE%,R%):REM Initialise channel allocation (Also reset each time stave structure is changed)
NEXT
ROOL's avatar
ROOL committed
3968 3969

REM Voices
3970
LOCALI$,L%,M%:REM Instrument name, Number of instruments, Instrument name length, Max
ROOL's avatar
ROOL committed
3971 3972 3973 3974 3975 3976 3977
MAX_VOICES%=32+1           :REM 1-32 Voice slots + MIDI
DIM Voice$(MAX_VOICES%)    :REM Names of available voices
DIM RestoreVoice%(7)       :REM Table of original voice/channel allocation
DIM Instrument%(7,1)       :REM Instrument information: 0=Stave,1=Voice
REM Get number of voices available
SYS "Sound_InstallVoice" TO I$,NVoices%
NVoices%-=1
3978
IF FNassert(NVoices%>0,"NoVoices") STOP
ROOL's avatar
ROOL committed
3979 3980 3981
REM Get names of available voices
FOR R%=1 TO NVoices%
  SYS "Sound_InstallVoice",2,R% TO ,,,Voice$(R%)
3982
NEXT
ROOL's avatar
ROOL committed
3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995
REM Get voice allocation for each channel
FOR R%=0 TO 7
  SYS "Sound_AttachVoice",R%+1,0 TO L%,S%
  RestoreVoice%(R%)=S%
  REM Make sure a voice is attached to all channels
  IF S%<1 OR S%>NVoices% THEN S%=1
  SYS "Sound_AttachVoice",L%,S%
  REM Create instrument information (stave and voice)
  Instrument%(R%,0)=S_C%(R%)+1
  Instrument%(R%,1)=S%
NEXT

REM Stereo positions
3996
NStereos%=6
ROOL's avatar
ROOL committed
3997 3998 3999
DIM Stereo%(NStereos%),Stereo_Position%(7)
FOR R%=0 TO NStereos%
  Stereo%(R%)=(2*R%/NStereos%-1)*127
4000
NEXT
ROOL's avatar
ROOL committed
4001 4002 4003
FOR R%=0 TO 7
  Stereo_Position%(R%)=NStereos% DIV 2
  SYS "Sound_Stereo",R%+1,Stereo%(Stereo_Position%(R%))
4004 4005
NEXT

ROOL's avatar
ROOL committed
4006 4007 4008 4009 4010 4011
REM Audio output
REM 0=Internal sound system
REM 1=MIDI device
OutputType%=0

REM MIDI options
4012 4013
NMIDIChannels%=16
DIM MIDIChannel%(7)
ROOL's avatar
ROOL committed
4014 4015
FOR c%=0 TO 7
  MIDIChannel%(c%)=c%+1
4016 4017 4018 4019 4020 4021
NEXT
ENDPROC

DEF PROCrestore
REMOSCLI("audio off")
FORR%=0TO7    :REM restore channel/voice allocation
ROOL's avatar
ROOL committed
4022
  SYS "Sound_AttachVoice",R%+1,RestoreVoice%(R%)
4023
NEXT
ROOL's avatar
ROOL committed
4024 4025
SYS "Sound_Volume", RestoreVolume :REM restore volume
SYS "Sound_Configure",OldConfigure
4026 4027 4028 4029 4030 4031 4032 4033 4034 4035
ENDPROC

DEF PROCexit
PROCCloseWindow(ScoreWind_h%)
PROCrestore
ENDPROC

DEF PROCterminate
IF INITIALISED% IF PLAYING% PROCplay_stop
*UnSet Maestro$Running
ROOL's avatar
ROOL committed
4036
PROCclose_messagefile(Messages_h%)
4037
IF Task_h%>0 THEN
ROOL's avatar
ROOL committed
4038
 ON ERROR SYS "Wimp_CloseDown", Task_h%, "TASK" : END
4039 4040 4041 4042 4043
ELSE
 ON ERROR END
ENDIF
IF INITIALISED% PROCexit :PROCCloseWindow(QuitQuery_h%)
ON ERROR END
ROOL's avatar
ROOL committed
4044
IF Task_h%>0 THEN SYS "Wimp_CloseDown", Task_h%, "TASK"
4045 4046 4047 4048 4049
END
ENDPROC

DEF FNassert(E%,A$)
LOCAL e%
ROOL's avatar
ROOL committed
4050 4051
A$=FNmessage_lookup(Messages_h%, A$)
IFE% THEN=FALSE ELSE e%=FNCheckOK(FNmessage_lookupN(Messages_h%, "Fatal", A$, "", "", ""),1)
4052 4053 4054 4055 4056 4057 4058 4059 4060 4061
PROCterminate
=TRUE


DEF PROCerror

  LOCAL e%
  LOCAL ERROR
  ON ERROR LOCAL PROCterminate: STOP
  SYS "Hourglass_Smash"
ROOL's avatar
ROOL committed
4062
  E$=FNmessage_lookupN(Messages_h%, "IntErr", REPORT$, STR$ERL, "", "")
4063 4064 4065

  FILE%=FILE%:IF FILE% CLOSE#FILE% : FILE%=FALSE
  IF INITIALISED% THEN
ROOL's avatar
ROOL committed
4066
    E$+=" "+FNmessage_lookup (Messages_h%, "ExProg")
4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082
    IF FNCheckOK(E$,3) PROCterminate
  ELSE
    e%=FNCheckOK(E$,1) : PROCterminate
  ENDIF

ENDPROC


DEF PROCOpenDiscardCancelBox

  REM Dbox saying "Blah edited", Discard, Cancel

  LOCAL c%,d%
  LOCAL mc_dx%,mc_dy%,mc_sw%,mc_sh%,scrx%,scry%

  Window%!handle%=QuitQuery_h%
ROOL's avatar
ROOL committed
4083
  SYS "Wimp_GetWindowState", ,Window%+handle%
4084 4085 4086 4087 4088 4089 4090 4091 4092

  SYS"OS_ReadModeVariable",-1,4 TO ,,mc_dx%:mc_dx%=1<<mc_dx%
  SYS"OS_ReadModeVariable",-1,5 TO ,,mc_dy%:mc_dy%=1<<mc_dy%
  SYS"OS_ReadModeVariable",-1,11 TO ,,mc_sw%:mc_sw%+=1
  SYS"OS_ReadModeVariable",-1,12 TO ,,mc_sh%:mc_sh%+=1
  scrx%=mc_sw%*mc_dx%
  scry%=mc_sh%*mc_dy%
  c%=(scrx%-(Window%!8 - Window%!0 )) DIV 2
  d%=(scry%-(Window%!4 - Window%!12)) DIV 2
ROOL's avatar
ROOL committed
4093
  SYS "Wimp_CreateMenu",,QuitQuery_h%,c%,d%
4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105

ENDPROC


DEF PROCOpenSaveDiscardCancelBox

  REM Dbox saying "Blah not saved", Save, Discard, Cancel

  LOCAL c%,d%
  LOCAL mc_dx%,mc_dy%,mc_sw%,mc_sh%,scrx%,scry%

  Window%!handle%=ClearQuery_h%
ROOL's avatar
ROOL committed
4106
  SYS "Wimp_GetWindowState", ,Window%+handle%
4107 4108 4109 4110 4111 4112 4113 4114 4115

  SYS"OS_ReadModeVariable",-1,4 TO ,,mc_dx%:mc_dx%=1<<mc_dx%
  SYS"OS_ReadModeVariable",-1,5 TO ,,mc_dy%:mc_dy%=1<<mc_dy%
  SYS"OS_ReadModeVariable",-1,11 TO ,,mc_sw%:mc_sw%+=1
  SYS"OS_ReadModeVariable",-1,12 TO ,,mc_sh%:mc_sh%+=1
  scrx%=mc_sw%*mc_dx%
  scry%=mc_sh%*mc_dy%
  c%=(scrx%-(Window%!8 - Window%!0 )) DIV 2
  d%=(scry%-(Window%!4 - Window%!12)) DIV 2
ROOL's avatar
ROOL committed
4116
  SYS "Wimp_CreateMenu",,ClearQuery_h%,c%,d%
4117 4118 4119 4120 4121 4122 4123

ENDPROC


DEF FNCheckOK(E$, boxes%)
LOCAL E%
FILE%=FILE%:IF FILE% CLOSE#FILE% : FILE%=FALSE
4124 4125 4126
!err_block%=0
$(err_block%+4)=LEFT$(E$,200)+CHR$0
SYS "Wimp_ReportError", err_block%, boxes%, maestro$ TO ,E%
4127 4128 4129
=E%=1 :  REM return TRUE if OK pressed

DEF FNCheckOKTag(E$, boxes%)
ROOL's avatar
ROOL committed
4130
=FNCheckOK(FNmessage_lookup(Messages_h%, E$), boxes%)
4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153

DEF FNopen_messagefile(name$)
LOCAL type%,len%
SYS "OS_File",17,name$ TO type%,,,,len%
IF type%<>1 SYS "OS_File",19,name$,type%
DIM message_data% (len% + 19) AND &FFFFFFFC
SYS "MessageTrans_OpenFile", message_data%, name$, message_data%+16
=message_data%

DEF PROCclose_messagefile(mh%)
SYS "MessageTrans_CloseFile", mh%
ENDPROC

DEF FNmessage_lookup(mh%, tag$)
LOCAL s$
SYS "MessageTrans_Lookup", mh%, tag$, 0, 0, 0, 0, 0, 0 TO ,,s$
=s$

DEF FNmessage_lookupN(mh%, tag$, ss0$, ss1$, ss2$, ss3$)
LOCAL s$
SYS "MessageTrans_Lookup", mh%, tag$, message_buffer%, 256, ss0$, ss1$, ss2$, ss3$ TO ,,s$
=s$

ROOL's avatar
ROOL committed
4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176
REM // Print //

DEF PROCprint_init
LOCAL pname$
LOCAL ERROR
ON ERROR LOCAL:RESTORE ERROR:PROCwimp_seticontext(Print_h%,1,FNmessage_lookup(Messages_h%,"NoPrinter")):PROCwimp_seticonstate(Print_h%,2,22,TRUE):ENDPROC 
SYS "PDriver_Info" TO ,,,,pname$
PROCwimp_seticontext(Print_h%,1,pname$)
PROCwimp_seticonstate(Print_h%,2,22,FALSE)
ENDPROC

DEF PROCprint_click (icon%,button%)
CASE icon% OF
  WHEN 2:REM Print
    IF FNwimp_geticontext(Print_h%,1)<>FNmessage_lookup(Messages_h%,"NoPrinter") THEN
      PROCprint
    ELSE
      icon%=FNCheckOKTag("NoPrinter",1)
    ENDIF
    IF button%=1 THEN SYS "Wimp_CreateMenu",,-1
ENDCASE
ENDPROC

4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200
DEF PROCprint
    LOCAL Hi%, Vi%
    LOCAL f%, e%, h%
    LOCAL page_x%, page_y%, margin%
    LOCAL score_height%, staves_per_page%
    LOCAL p%, gp%
    LOCAL y1%, y2%, y3%
    LOCAL x1%, x2%
    LOCAL s%
    LOCAL l%, i%
    LOCAL page%, last_page%
    LOCAL title$
    LOCAL r0%
    LOCAL lx0%, lx1%, ly0%, ly1% : REM returned by bound_note
    LOCAL C%, P%, R%             : REM Channel, Max prefix, Max width
    LOCAL G%, A%, a%, W%                 : REM Note, Attribute
    LOCAL N%()                   : REM Note indices for each channel
    LOCAL n_start%(), n%()       : REM Local copy of N%
    LOCAL SIG%(), sig%()
    LOCAL clef%()
    LOCAL nbars%
    LOCAL NC0%, NC1%
    LOCAL x%, y%, lasty%, checkstagger%
    LOCAL N%
ROOL's avatar
ROOL committed
4201
    LOCAL note_factors%, factors%
4202 4203 4204 4205 4206 4207 4208
    LOCAL bars_plotted%
    LOCAL on_page%, last_pos%, index%, last_x_offset%
    LOCAL current_gate%, n_bars%, multiplier, w1, xs
    LOCAL events%, events_start%
    LOCAL bar_at_front%, bar_at_front_start%
    LOCAL x_res%, y_res%, ptr%, features%

ROOL's avatar
ROOL committed
4209
    DIM note_factors% 16, factors% 16
4210 4211 4212 4213 4214 4215 4216 4217 4218
    DIM N%(7)
    DIM n_start%(7), n%(7)
    DIM SIG%(1), sig%(1)
    DIM clef%(Max_Stave%)
    DIM PrintRectangle%16, PrintTransform%16, PrintPos%8

    SYS "PDriver_Info" TO ,x_res%,y_res%, features%
    IF x_res% < 120 OR y_res% < 144 THEN
        REM Keep the above resolutions in step with the error in the messages file
ROOL's avatar
ROOL committed
4219
        IF NOT FNCheckOK(FNmessage_lookupN(Messages_h%,"LoRes",FNwimp_geticontext(Print_h%,1),STR$(x_res%),STR$(y_res%),""),3) THEN ENDPROC
4220 4221 4222
    ENDIF

    Hi% = 2
ROOL's avatar
ROOL committed
4223
    Vi% = 4
4224 4225 4226 4227 4228 4229 4230

    x_mul_scale% = 3
    y_mul_scale% = 3
    x_div_scale% = 4
    y_div_scale% = 4

    SYS "Hourglass_On"
ROOL's avatar
ROOL committed
4231 4232

    SYS "Wimp_ReadPixTrans",&200,SprBlk%,S%(0),,,,factors%,pixtrans%
4233 4234 4235 4236 4237 4238 4239 4240 4241 4242
    note_factors%!0  = factors%!0 * x_mul_scale%
    note_factors%!4  = factors%!4 * y_mul_scale%
    note_factors%!8  = (factors%!8) * x_div_scale%
    note_factors%!12 = (factors%!12) * y_div_scale%

    clef%() = CLEF%()
    clef%() = 0
    N%() = MUSIC%()
    SIG%(0)=%01100111
    SIG%(1)=%00000010
ROOL's avatar
ROOL committed
4243
    title$=FNwimp_geticontext(Save_h%,1)
4244 4245 4246 4247 4248 4249 4250 4251 4252
    l% = LEN(title$)
    s% = 1
    FOR i% = 1 TO l% - 1
        c$ = MID$(title$, i%, 1)
        IF (c$ = ":" OR c$ = ".") THEN s% = i% + 1
    NEXT i%
    title$ = MID$(title$, s%)

    LOCAL ERROR
ROOL's avatar
ROOL committed
4253
    ON ERROR LOCAL: RESTORE ERROR:e%=FNCheckOK (FNmessage_lookupN(Messages_h%, "IntErr", REPORT$, STR$(ERL), "", ""), 1):ENDPROC
4254 4255 4256 4257 4258 4259 4260

    f% = OPENOUT("printer:")
    SYS "PDriver_SelectJob", f%, title$

    SYS "PDriver_CurrentJob" TO h%

REM    IF (h% = 0) THEN
ROOL's avatar
ROOL committed
4261
REM        e% = FNCheckOK (FNmessage_lookup (Messages_h%, "JobFailed"), 3)
4262 4263 4264 4265 4266 4267 4268 4269
REM        SYS "Hourglass_Off"
REM        ENDPROC
REM    ENDIF

    IF (features% AND (1 << 29)) <> 0 THEN
        SYS "PDriver_DeclareFont", 0, 0, 0 : REM We don't want to use any
    ENDIF

ROOL's avatar
ROOL committed
4270
    ON ERROR LOCAL: RESTORE ERROR: SYS "PDriver_AbortJob", f%: CLOSE#f%:e%=FNCheckOK (FNmessage_lookupN(Messages_h%, "IntErr", REPORT$, STR$(ERL), "", ""), 1):ENDPROC
4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378
    SYS "PDriver_PageSize" TO ,page_x%,page_y%, l_edge%, b_edge%, r_edge%, t_edge%

    page_x% = page_x% / 400: page_y% = page_y% / 400
    REM  l_edge% = 0: r_edge% = page_x%
    l_edge% = l_edge% / 400: r_edge% = r_edge% / 400
    b_edge% = b_edge% / 400: t_edge% = t_edge% / 400
    r_edge% -= (Hi% * 12)

    SYS "ColourTrans_SetGCOL", &00000000,,,0,0

    score_height%=(PERC%+1+3*(STAVE%+1))*Stave_Height%
    margin% = Stave_Height%
    staves_per_page% = (page_y% - 2 * margin%) / score_height%

    gp% = MUSIC%
    page% = 0
    last_page% = 0
    p% = gp%
    index% = 0
    last_x_offset% = 0
    x2% = 0
    last_pos% = 0
    current_bar% = 0
    current_gate% = 0
    n%() = N%()
    events% = 0
    bar_at_front% = TRUE

    bars_plotted% = 0

    WHILE gp% < GATE%
        last_page% = page%
        page% += 1
        PrintRectangle%!0 = 0
        PrintRectangle%!4 = 0
        PrintRectangle%!8 = page_x%
        PrintRectangle%!12 = page_y%
        PrintTransform%!0 = &10000
        PrintTransform%!4 = &00000
        PrintTransform%!8 = &00000
        PrintTransform%!12 = &10000
        PrintPos%!0 = 0
        PrintPos%!4 = 0
        SYS "PDriver_GiveRectangle", 0, PrintRectangle%, PrintTransform%, PrintPos%, &ffffffff
        SYS "PDriver_DrawPage", 1, PrintRectangle%, page%, STR$(page%) TO r0%

        p% = gp%
        index_start% = index%
        last_x_offset_start% = last_x_offset%
        x2_start% = x2%
        last_pos_start% = last_pos%
        current_gate_start% = current_gate%
        current_bar_start% = current_bar%
        n_start%() = n%()
        events_start% = events%
        bar_at_front_start% = bar_at_front%
        sig%() = SIG%()

        WHILE r0%

            IF last_page% = page% THEN
                index% = index_start%
                last_x_offset% = last_x_offset_start%
                x2% = x2_start%
                last_pos% = last_pos_start%
                current_gate% = current_gate_start%
                current_bar% = current_bar_start%
                gp% = p%
                n%() = n_start%()
                events% = events_start%
                bar_at_front% = bar_at_front_start%
                SIG%() = sig%()
            ENDIF

            y1% = page_y% - margin% + Stave_Height%
            y3% = page_y% - margin%
            IF PERC% THEN
                y3% -= Stave_Height%
            ENDIF

            stave_n% = 0
            WHILE stave_n% < staves_per_page% AND gp% < GATE%

                y2% = (y3% - score_height% + Stave_Height% / 2)
                IF PERC% THEN
                    y2% -= ((stave_n%+1) * Stave_Height%)
                ENDIF
                FOR s% = 0 TO STAVE%+PERC%
                    IF (PERC%) AND (s% = 0) THEN
                        LINE l_edge%, y2%, r_edge%, y2%
                    ELSE
                        LINE l_edge%, y2% - Li% * 4, r_edge%, y2% - Li% * 4
                        LINE l_edge%, y2% - Li% * 2, r_edge%, y2% - Li% * 2
                        LINE l_edge%, y2%, r_edge%, y2%
                        LINE l_edge%, y2% + Li% * 2, r_edge%, y2% + Li% * 2
                        LINE l_edge%, y2% + Li% * 4, r_edge%, y2% + Li% * 4
                    ENDIF
                    y2% = y2% + 3 * Stave_Height%
                NEXT s%
                y3% = y3% - score_height%
                y2% = y1% - Stave_Height%
                IF NOT PERC% THEN
                    y2% -= Stave_Height%
                ELSE
                    y3% = y3% : REM- (2 * Stave_Height%)
                ENDIF

                IF bar_at_front% AND GP% < GATE% THEN
ROOL's avatar
ROOL committed
4379
                    PROCprint_barline(l_edge%, y2%, 0, FALSE)
4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393
                ENDIF

                x1% = l_edge% : REM + (Hi% * 13):
                on_page% = TRUE
                n_bars% = 0
                x% = 0
                xs = l_edge%
                multiplier = 1
                bars_plotted% = 0

                IF gp% < GATE% THEN
                    PROCfind_last_bar_on_stave (last_pos%, index%, events%, last_x_offset%, n_bars%, r_edge%, l_edge%)
                    multiplier = (r_edge% - 35) / (last_pos%)
                ELSE
ROOL's avatar
ROOL committed
4394
                    PROCprint_barline(r_edge%, y2%, gp%>>6, TRUE)
4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415
                ENDIF

                WHILE gp% < GATE% AND on_page%
                    IF gp%?0 THEN
                        G% = gp%?0
                        x% = FNnote_posn(current_bar%, current_gate%, x2%) : REM Extract pre-calulated positions
                        IF x% > last_pos% AND n_bars% = 0 THEN on_page% = FALSE
                        xs = x% * multiplier
                        checkstagger% = FALSE
                        C% = -1
                        P% = 0
                        R% = 0
                        REPEAT
                            REPEAT
                                C% += 1
                            UNTIL G% AND %1 << C%
                            PROCbound_note(!n%(C%))
                            IF lx0% > P% THEN P% = lx0%
                            IF lx1% > R% THEN R% = lx1%
                            NC0% = n%(C%)?0: NC1% = n%(C%)?1
                            y% = y2% + Y_STAVE%(S_C%(C%))
ROOL's avatar
ROOL committed
4416
                            IF (NC0% AND &F8) THEN
4417 4418 4419 4420 4421 4422 4423 4424 4425
                                l% = (NC0% >> 3) - 16
                                IF ABS(l%) > 5 PROCprint_sprite(ledger% + l% DIV 2, xs, y%, factors%)
                                y% += Li%*l%
                                s% = NC1% >> 5 OR NC0% << 3 AND 8
                                IF checkstagger% THEN
                                    IF ABS(lasty% - y%) < 2 * Li% THEN
                                        PROCprint_sprite(s%,xs+Pgap%, y%, note_factors%)
                                        checkstagger% = FALSE
                                        IF NC1% AND 24 PROCprint_sprite(dot%+(NC1%>>3 AND 3), xs + x%(s%) + Pgap%, y%, note_factors%)
Chris Wraight's avatar
Chris Wraight committed
4426
                                        IF (NC0% AND &4) THEN PROCprint_tie(xs,y%)
4427 4428 4429
                                    ELSE
                                        PROCprint_sprite(s%, xs, y%, note_factors%)
                                        IF NC1% AND 24 PROCprint_sprite(dot%+(NC1%>>3 AND 3), xs + x%(s%), y%, note_factors%)
Chris Wraight's avatar
Chris Wraight committed
4430
                                        IF (NC0% AND &4) THEN PROCprint_tie(xs,y%)
4431 4432 4433 4434
                                    ENDIF
                                ELSE
                                    PROCprint_sprite(s%, xs, y%, note_factors%)
                                    IF NC1% AND 24 PROCprint_sprite(dot%+(NC1%>>3 AND 3), xs + x%(s%), y%, note_factors%)
Chris Wraight's avatar
Chris Wraight committed
4435
                                    IF (NC0% AND &4) THEN PROCprint_tie(xs,y%)
4436 4437 4438 4439 4440 4441
                                    checkstagger%=TRUE
                                ENDIF
                                lasty% = y%
                                IF NC1% AND 7 PROCprint_sprite(accidental% OR NC1% AND 7, xs - x%(s%), y%, note_factors%)
                            ELSE
                                s% = rest% OR NC1% >> 5
ROOL's avatar
ROOL committed
4442 4443 4444
                                l% = (NC1% AND &7)
                                IF l% = 0 THEN l% = 4
                                y% += Li% * (l% - 4) * 2
4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472
                                PROCprint_sprite(s%, xs, y%, factors%)
                                IF NC1% AND 24 PROCprint_sprite(dot%+(NC1%>>3 AND 3), xs + x%(s%), y%, note_factors%)
                            ENDIF
                            n%(C%) += 2
                        UNTIL (2 << C%) > G%
                        gp% += 1
                        current_gate% += 1
                        IF x% >= last_pos% THEN
                            on_page% = FALSE
                            bar_at_front% = FALSE
                        ELSE
                            bar_at_front% = TRUE
                        ENDIF
                    ELSE
                        A% = gp%?1
                        N% = -1: REPEAT N% += 1: UNTIL A% AND %1 << N%
                        CASE N% OF
                            WHEN 0
                                REM Time Sig
                                sig%(0) = A%
                                B$ = STR$((A% >> 1 AND 15) + 1)
                                D$ = STR$(%1 << (A% >> 5) - 1)
                                x% = FNtime_posn(current_bar%, x2%)
                                xs = x% * multiplier
                                w1 = xs
                                IF LEN B$ < 2 THEN w1 += 5 * Hi%
                                IF LEN D$ < 2 THEN xs += 5 * Hi%
                                FOR S% = 0 TO STAVE%+PERC%
4473 4474
                                    PROCprint_sprite(time%+VAL(B$),w1,y2% + Y_STAVE%(S%), note_factors%)
                                    PROCprint_sprite(time%+VAL(D$),w1,y2% + Y_STAVE%(S%)-32, note_factors%)
4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511
                                NEXT S%
                            WHEN 1
                                REM Key Sig
                                x% = FNkey_posn(current_bar%, x2%)
                                xs = x% * multiplier
                                IF A% AND 56 sig%(1)=A% ELSE SWAP A%,sig%(1):a%=accidental%+1
                                N%=(A%>>3AND7)-1
                                IF N% >= 0 THEN
                                    A%=A%>>2AND%1
                                    IF a% ELSE a%=accidental%+2+A%
                                    W%=x%(a%)+X%(a%)
                                    x%+=x%(a%)
                                    xs = x% * multiplier
                                    FOR C%=0 TO N%
                                        FOR S%=0TOSTAVE%
                                            PROCprint_sprite(a%,xs,y2%+Y_STAVE%(S%)+Li%*Key_Y%(clef%(S%),A%,C%), note_factors%)
                                        NEXT
                                    x%+=W%
                                    xs = x% * multiplier
                                    NEXT
                                ENDIF

                            WHEN 2
                                REM Clef
                                sig%(0) = A%  : REM Hack
                                S% = A% >> 6
                                x% = FNclef_posn(current_bar%, x2%)
                                xs = x% * multiplier
                                clef%(S%) = (A% >> 3) AND 3
                                IF S% <= STAVE% PROCprint_sprite(clef% + (A% >> 3 AND 3), xs, y2% + Y_STAVE%(S%), factors%)
                            WHEN 3
                                REM Slur

                            WHEN 4
                                REM Octave Shift

                            WHEN 5
ROOL's avatar
ROOL committed
4512
                                REM Barline
4513 4514
                                x% = FNbar_posn(current_bar%, x2%)
                                IF x% >= last_pos% THEN
ROOL's avatar
ROOL committed
4515
                                    PROCprint_barline(r_edge%, y2%, A%>>6, TRUE)
4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526
                                    on_page% = FALSE
                                    IF (current_bar% + 1) MOD 5 = 0 THEN
                                        MOVE r_edge%-2-((1+LEN(STR$(current_bar%+1))-1)*16),y2% + (Y_STAVE%(0) / 2): REM ***
                                        PRINT STR$(current_bar%+1)
                                    ENDIF
                                ELSE
                                    IF x% = l_edge% THEN
                                        xs = x% - 7: REM &&&
                                    ELSE
                                        xs = x% * multiplier
                                    ENDIF
ROOL's avatar
ROOL committed
4527 4528 4529
                                    
                                    PROCprint_barline(xs+2, y2%, A%>>6, FALSE)
                                   
4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542
                                    IF (current_bar% + 1) MOD 5 = 0 THEN
                                        MOVE xs-1-((LEN(STR$(current_bar%+1)))*16),y2% + (Y_STAVE%(0) / 2): REM ***
                                        PRINT STR$(current_bar%+1)
                                    ENDIF
                                ENDIF

                                current_bar% += 1
                                bars_plotted% += 1
                        ENDCASE
                        gp% += 2
                    ENDIF               : REM gp%?0
                ENDWHILE                : REM on_page%
                IF gp% >= GATE% THEN
ROOL's avatar
ROOL committed
4543
                    PROCprint_barline(r_edge%, y2%, 0, FALSE)
4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561
                ENDIF
                x2% += (last_pos% - l_edge%)
                y1% = y1% - score_height%
                IF PERC% THEN
                    y1% = y1% - margin%
                ENDIF
                stave_n% += 1
            ENDWHILE
            last_page% = page%
            SYS "PDriver_GetRectangle", ,PrintRectangle% TO r0%
        ENDWHILE
    ENDWHILE
    RESTORE ERROR
    SYS "PDriver_EndJob", f%
    CLOSE#f%
    SYS "Hourglass_Off"
ENDPROC

ROOL's avatar
ROOL committed
4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585
DEF PROCprint_barline (bx%,by%,type%,right%)
LOCAL s%,y%,h%
REM Adjust xpos if along right-hand edge
IF right% THEN bx%=bx%-(X%(bar%+type%)+x%(bar%+type%))+2
FOR s%=0 TO STAVE%+PERC%
  REM Get ypos and height
  y%=Y_STAVE%(s%)-(Stave_Height% DIV 2)  
  IF ((STAVE%+1) AND 2) AND s%=STAVE% THEN
    h%=Stave_Height%*3
  ELSE
    h%=Stave_Height%
  ENDIF
  REM Draw barline 
  CASE type% OF
    WHEN 0:REM Single barline
      MOVE bx%,by%+y%
      DRAW bx%,by%+y%+h%
    WHEN 1:REM Double barline
      MOVE bx%,by%+y%
      DRAW bx%,by%+y%+h%
      RECTANGLE FILL bx%+6,by%+y%,5,h%  
  ENDCASE
NEXT
ENDPROC
4586

Chris Wraight's avatar
Chris Wraight committed
4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611
DEF PROCprint_tie (sx%,sy%)
LOCAL s%,g%,span%,oldx%
IF (NC0% AND &2) THEN s%=tie%+1 ELSE s%=tie%
sx%=sx%-(x%(s%))
sy%=sy%-y%(s%)
g%=gp%+1
span%=1
WHILE g%<=GATE%
  IF ?g% THEN
    IF ?g% AND (1<<C%) THEN
      g%=GATE%+1
    ELSE
      span%+=1
      g%+=1
    ENDIF
  ELSE
    g%+=2
  ENDIF
ENDWHILE
oldx%=factors%!0
factors%!0=span%
SYS "OS_SpriteOp",SprPlot%,SprBlk%,S%(s%),sx%,sy%,8,factors%,pixtrans%
factors%!0=oldx%
ENDPROC

4612 4613
DEF PROCprint_sprite(s%, X%, Y%, scale_factors%) : REM plot sprite S%(s%) at X%,Y%

ROOL's avatar
ROOL committed
4614
  LOCAL spr_y, scaled_y, y_add, x_add
4615 4616 4617 4618 4619 4620 4621 4622 4623

  spr_y = ((S%(s%)!20) + 1) * (2 / 4)    : REM actual height
  scaled_y = spr_y * (2 / 4) : REM scaled height

  x_add = 0

REM Below are various hack factors to deal with the fact that the sprites are scaled down.

  CASE S$(s%) OF
4624 4625
    WHEN "Md", "Cd","Qd", "SQd", "DSQd", "SDSQd"
       x_add = 6
4626
    WHEN "Mu", "Cu", "Qu", "SQu", "DSQu", "SDSQu", "M", "SB", "Natural", "Sharp", "Flat", "Sharp2", "Flat2", "NSharp", "NFlat", "Tie", "Dot1", "Dot2", "Dot3"
4627
      y_add = 2 : x_add = 6
4628 4629 4630 4631 4632 4633 4634 4635
    WHEN "B"
      y_add = 2.5 : x_add = 6
    WHEN "Bar"
      y_add = -0.5
    OTHERWISE
      y_add = 0 : x_add = 6
  ENDCASE

ROOL's avatar
ROOL committed
4636
  SYS "OS_SpriteOp", SprPlot%, SprBlk%, S%(s%), X%-(x%(s%)) + x_add, Y% - y%(s%) + y_add, 8, factors%, pixtrans%
4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790
ENDPROC



DEF PROCfind_last_bar_on_stave (RETURN last_pos%, RETURN index%, RETURN events%, RETURN last_x_offset%, RETURN n_bars%, r_edge%, l_edge%)

    LOCAL loop%, x%, last_x%

    x% = l_edge% + PX%(index%) - x2%

    WHILE ((index% < (GATE% - MUSIC%)) AND x% < (r_edge% - 35))

        CASE PTYPE%(index%) OF
            WHEN 0
                IF n_bars% = 0 THEN
                    last_pos% = x%
                ENDIF
                events% += 1
            WHEN 32
                IF x% <> l_edge% THEN
                    last_pos% = x%
                    n_bars% += 1
                ENDIF
                events% += 2
            OTHERWISE
                IF n_bars% = 0 THEN
                    last_pos% = x%
                ENDIF
                events% += 2
        ENDCASE

        index% += 1
        x% = l_edge% + PX%(index%) - x2%

    ENDWHILE

    IF index% >= GATE% - MUSIC% THEN
         last_pos% = r_edge% - 35
    ENDIF
ENDPROC



DEF FNnote_posn(bar%, gate%, x2%)

    LOCAL loop%, enc%, x%, events%

    enc% = 0
    loop% = 0
    events% = 0

    REPEAT
        IF PTYPE%(loop%) = 0 THEN
            enc% += 1
            x% = l_edge% + PX%(loop%) - x2%
        ENDIF
        loop% += 1
    UNTIL (enc% > gate% OR loop% > GATE% - MUSIC%)

= x%

DEF FNclef_posn(bar%, x2%)

    LOCAL loop%, enc%, x%, events%

    enc% = 0
    loop% = 0
    events% = 0

    REPEAT
        IF PTYPE%(loop%) = 4 THEN
            enc% += 1
            x% = l_edge% + PX%(loop%) - x2%
        ENDIF
        loop% += 1
    UNTIL (enc% >= bar% OR loop% > GATE% - MUSIC%)

= x%


DEF FNkey_posn(bar%, x2%)

    LOCAL loop%, enc%, x%, events%

    enc% = 0
    loop% = 0
    events% = 0

    REPEAT
        IF PTYPE%(loop%) = 2 THEN
            enc% += 1
            x% = l_edge% + PX%(loop%) - x2%
        ENDIF
        loop% += 1
    UNTIL (enc% >= bar% OR loop% > GATE% - MUSIC%)

= x%


DEF FNbar_posn(bar%, x2%)

    LOCAL loop%, enc%, x%, events%

    enc% = 0
    loop% = 0
    events% = 0

    REPEAT
        IF PTYPE%(loop%) = 32 THEN
            enc% += 1
            x% = l_edge% + PX%(loop%) - x2%
        ENDIF
        loop% += 1
    UNTIL (enc% > bar% OR loop% > GATE% - MUSIC%)

= x%


DEF FNtime_posn(bar%, x2%)

    LOCAL loop%, enc%, x%, events%

    enc% = 0
    loop% = 0
    events% = 0

    REPEAT
        IF PTYPE%(loop%) = 1 THEN
            enc% += 1
            x% = l_edge% + PX%(loop%) - x2%
        ENDIF
        loop% += 1
    UNTIL (enc% >= bar% OR loop% > GATE% - MUSIC%)


= x%

REM  DEF PROCdebug (a$)
REM
REM      LOCAL a%, i%, l%
REM
REM      i% = LENa$
REM      a% = TRACE
REM      IF i% = 0 THEN ENDPROC
REM
REM      FOR l% = 1 TO i%
REM          BPUT#a%, ASC(MID$(a$, l%, 1))
REM      NEXT
REM      BPUT#a%, 10
REM      BPUT#a%, 13
REM
REM
REM  ENDPROC
REM
ROOL's avatar
ROOL committed
4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019

REM // Wimp //

DEF FNwimp_opentemplates (path$)
REM Opens a Template file and returns buffer for largest definition
LOCAL size%,largest%,next%,w%,s$
SYS "Wimp_OpenTemplate",,path$
largest%=0
s$="*"+STRING$(11,CHR$(0))
SYS "Wimp_LoadTemplate",,0,,,,s$,0 TO ,size%,,,,,next%
WHILE next%
  IF size%>largest% THEN largest%=size%
  s$="*"+STRING$(11,CHR$(0))
  SYS "Wimp_LoadTemplate",,0,,,,s$,next% TO ,size%,,,,,next%
ENDWHILE
DIM w% largest%+16
=w%

DEF FNwimp_loadtemplate (name$,buffer%,sprites%)
REM Loads a window definition from a Template file
LOCAL indarea%,indsize%,s$,window%
s$=name$+CHR$(13)+STRING$(12-LEN(name$)+1,CHR$(0))
SYS "Wimp_LoadTemplate",,0,,,,s$ TO ,,indsize%
DIM indarea% indsize%
SYS "Wimp_LoadTemplate",,buffer%,indarea%,indarea%+indsize%,-1,s$
IF sprites% THEN buffer%!64=sprites%
SYS "Wimp_CreateWindow",,buffer% TO window%
=window%

DEF FNwimp_geticontext (window%,icon%)
REM Gets indirected text from an icon
Block%!0=window%
Block%!4=icon%
SYS "Wimp_GetIconState",,Block%
=$(Block%!28)

DEF PROCwimp_seticontext (window%,icon%,text$)
REM Sets the indirected text in an icon
Block%!0=window%
Block%!4=icon%
SYS "Wimp_GetIconState",,Block%
$(Block%!28)=text$
Block%!8=0
Block%!12=0
SYS "Wimp_SetIconState",,Block%
ENDPROC

DEF FNwimp_geticonstate (window%,icon%,bit%)
REM Returns TRUE if bit is set, FALSE if not
Block%!0=window%
Block%!4=icon%
SYS "Wimp_GetIconState",,Block%
IF (Block%!24 AND (1<<bit%)) THEN =TRUE ELSE =FALSE

DEF PROCwimp_seticonstate (window%,icon%,bit%,action%)
REM Sets an icon state bit
Block%!0=window%
Block%!4=icon%
SYS "Wimp_GetIconState",,Block%
IF action% THEN Block%!8=1<<bit%:Block%!12=1<<bit%
IF NOT action% THEN Block%!8=0:Block%!12=1<<bit%
SYS "Wimp_SetIconState",,Block%
ENDPROC

DEF PROCwimp_seticonval (window%,icon%,text$)
REM Sets the validation string in an indirected icon
Block%!0=window%
Block%!4=icon%
SYS "Wimp_GetIconState",,Block%
$(Block%!32)=text$
Block%!8=0
Block%!12=0
SYS "Wimp_SetIconState",,Block%
ENDPROC

DEF FNwimp_makemenu (title$,items%)
REM Create a menu
LOCAL buffer%,tbuffer%
DIM buffer% 28+(24*items%)
IF LEN(title$)>12 THEN
  DIM tbuffer% LEN(title$)+1
  buffer%!0=tbuffer%
  buffer%!4=0
  buffer%!8=LEN(title$)+1
  $(tbuffer%)=title$
ELSE
  $(buffer%)=title$
ENDIF
buffer%!12=&00070207
buffer%!16=80
buffer%!20=44
buffer%!24=0
=buffer%

DEF PROCwimp_additem (menu%,item%,flags%,link%,text$)
REM Add a menu item
LOCAL buffer%
menu%!(28+(item%*24))=flags%
menu%!(28+(item%*24)+4)=link%
IF LEN(text$)>12 THEN
  DIM buffer% LEN(text$)+1
  menu%!(28+(item%*24)+8)=&07000121
  menu%!(28+(item%*24)+12)=buffer%
  menu%!(28+(item%*24)+16)=0
  menu%!(28+(item%*24)+20)=LEN(text$)+1
  $(buffer%)=text$
ELSE
  menu%!(28+(item%*24)+8)=&07000021
  $(menu%+28+(item%*24)+12)=text$
ENDIF
ENDPROC

DEF FNwimp_getmenutext (menu%,item%)
REM Gets indirected text from a menu item
LOCAL menuptr%,indarea%,text$
menuptr%=menu%+28
menuptr%+=item%*24
indarea%=menuptr%!12
text$=$(indarea%)
=text$

DEF PROCwimp_setmenutext (menu%,item%,text$)
REM Sets the indirected text in an menu item
LOCAL menuptr%,indarea%
menuptr%=menu%+28
menuptr%+=item%*24
indarea%=menuptr%!12
$(indarea%)=text$
ENDPROC

DEF FNwimp_getmenustate (menu%,item%,bit%)
REM Gets a menu item's flag state
LOCAL menuptr%
menuptr%=menu%+28+(item%*24)
IF ?menuptr% AND (1<<bit%) THEN =TRUE ELSE =FALSE

DEF PROCwimp_setmenustate (menu%,item%,bit%,state%)
REM Sets a menu item's flags
LOCAL menuptr%
menuptr%=menu%+28+(item%*24)
CASE bit% OF
  WHEN 0,1,2,3,4,7,8:
    IF state% THEN menuptr%!0=menuptr%!0 OR (1<<bit%)
    IF NOT state% THEN menuptr%!0=menuptr%!0 AND NOT (1<<bit%)
  WHEN 22:
    IF state% THEN menuptr%!8=menuptr%!8 OR (1<<bit%)
    IF NOT state% THEN menuptr%!8=menuptr%!8 AND NOT (1<<bit%)
ENDCASE
ENDPROC

DEF PROCwimp_radiotick (menu%,item%)
REM Ticks one menu item and unticks the rest
LOCAL menuptr%,i%
menuptr%=menu%+4
REPEAT
  menuptr%+=24
  IF i%=item% THEN
    menuptr%!0=menuptr%!0 OR (1<<0)
  ELSE
    menuptr%!0=menuptr%!0 AND NOT (1<<0)
  ENDIF
  i%+=1
UNTIL menuptr%!0 AND &80
ENDPROC

DEF PROCwimp_opendialogue (dialogue%,window%,type%)
REM Opens a dialogue box centred over a window/over iconbar
REM type%=0 transient; type%=1 persistent
LOCAL x%,y%,dwidth%,dheight%,width%,height%
Block%!0=dialogue%
SYS "Wimp_GetWindowState",,Block%
dwidth%=Block%!12-Block%!4
dheight%=Block%!16-Block%!8
IF window%=-2 THEN
  Block%!0=window%
  Block%!4=IconHandle%
  SYS "Wimp_GetIconState",,Block%
  x%=Block%!8-64    :REM Same x-offset as default menu
  y%=dheight%+136   :REM Iconbar height + 40
ELSE
  Block%!0=window%
  SYS "Wimp_GetWindowState",,Block%
  x%=Block%!4
  y%=Block%!16
  width%=Block%!12-x%
  height%=y%-Block%!8  
  x%=x%+(width% DIV 2)-(dwidth% DIV 2)
  y%=y%-(height% DIV 2)+(dheight% DIV 2)
ENDIF
CASE type% OF
  WHEN 0:REM Open transient dialogue box
    SYS "Wimp_CreateMenu",,dialogue%,x%,y%
  WHEN 1:REM Open persistent dialogue box
    Block%!0=dialogue%
    Block%!4=x%
    Block%!8=y%-dheight%
    Block%!12=x%+dwidth%
    Block%!16=y%
    Block%!20=0
    Block%!24=0
    Block%!28=-1
    SYS "Wimp_OpenWindow",,Block%
ENDCASE
ENDPROC

DEF PROCwimp_closedialogue (dialogue%,window)
REM Closes a persistent dialogue box and returns focus to window
Block%!0=dialogue%
SYS "Wimp_CloseWindow",,Block%
Block%!0=window%
SYS "Wimp_GetWindowState",,Block%
IF Block%!32 AND (1<<16) THEN
  SYS "Wimp_SetCaretPosition",window%,-1
ENDIF 
ENDPROC

DEF FNwimp_popup (window%,menu%,icon%)
REM Opens a popup menu and returns menu handle
LOCAL x%,y%
Block%!0=window%
SYS "Wimp_GetWindowState",,Block%
x%=Block%!4
y%=Block%!16
Block%!4=icon%
SYS "Wimp_GetIconState",,Block%
x%=x%+Block%!16
y%=y%+Block%!20
SYS "Wimp_CreateMenu",,menu%,x%,y%
=menu%