       ͻ
         Lesson 7 Part 080 F-PC 3.5 Tutorial by Jack Brown 
       ͼ

        Ŀ
         Creating a Music Language with CREATE ... DOES>  
        

And now the fun begins.  We are going to use CREATE ... DOES> to create
a music language.  but first we need some words that will make some
tones in our computers speaker.  The ones I give will work for a PC and
are quite primitive.

\  PC!  ( byte  n   --  )  Output byte to port number n.
\  PC@  ( n   --   byte )  Input  byte from port number n.

HEX
:   S.ON  ( --  -- )      \  Turn speaker on.
        61 PC@  3  OR   61 PC! ;

:   S.OFF ( --  -- )       \ Turn speaker off.
        61 PC@  FFFC AND  61 PC! ;
DECIMAL


\ Here is the word to create a tone in you PC's speaker:

: TONE  ( freq  -- )       \ Make tone of specified frequency.
    21 MAX                 \ Lowest frequency.
    1.190000  ROT          \ Get divisor for timer.
    MU/MOD                 \ 16bit.rem   32bit.quot
    DROP NIP  [ HEX ]      \ Keep 16-bit quotient only.
    0B6   043 PC!          \ Write to timer mode register.
    100  /MOD SWAP         \ Split into hi and low byte.
    42 PC! 42 PC!          \ Store low and high byte in timer.
      S.ON ;               \ turn speaker on.
DECIMAL

The words S.OFF  sound off,  S.ON  sound on  and  TONE are the only
system dependent words in our application.

OK,  Here we go.  We create our music language using the three steps
outlined in the previous paragraphs.

Step 1.  We extend the FORTH compiler adding musical NOTE defining
capability.  Here is the definition of our new defining word called NOTE
It has a few support words so things can be tuned and adjusted later.

\ The music NOTE compiler.
        VARIABLE OCTAVE         \ Octave to play
        VARIABLE BEAT           \ Number of beats for this note
      5 CONSTANT SPEED          \ Alter to change 1/4 note time.

: DELAY  ( -- )
         SPEED TENTHS  ;      \ Approx .5 sec delay with SPEED of 5

\ Make it easy to change the beat and octave.
: 1/1  4 BEAT   ! ;  : 1/2   2 BEAT   ! ;  : 1/4  1 BEAT   ! ;
: 1ST  1 OCTAVE ! ;  : 2ND   2 OCTAVE ! ;  : 3RD  4 OCTAVE ! ;

\ Rest for current number of beats.
: REST ( -- )
       BEAT @ 0 ?DO DELAY LOOP  ;      : R   REST ;

\ The note compiler.
: NOTE   CREATE   ,                     \ Compile time like constant.
       DOES>  @  OCTAVE @ *  5 + 10 /   \ Run time, fetch value, compute
                TONE  REST S.OFF ;      \ frequency and play the note.

The part before the word DOES> and after the word NOTE describes how to
make notes.  The part after the word DOES> and before the  ;  tells how
to play a note, or what is to happen when you execute a note type object.

\ This is step 2,  we make some notes.
\ Create the notes with the note compiler.
1308 NOTE C     1386 NOTE C#    1468 NOTE D     1556 NOTE D#
1648 NOTE E     1746 NOTE F     1850 NOTE F#    1960 NOTE G
2077 NOTE G#    2200 NOTE A     2331 NOTE A#    2469 NOTE B

\ This is step 3,  we use some of the created notes.
\ Test the created notes.
: SCALE  1ST 1/4  C D E F G A B 2ND 1/2 C R
             1/4  C D E F G A B 3RD 1/2 C R ;

ķ
  Problem 7.5  
Ľ
Implement the music language above and test its operation with SCALE.
Make sure that your definitions go into the SOUND  VOCABULARY that you
defined earlier.  A detailed explanation of NOTE follows in the next
Part of Lesson 7.

Ŀ
  Please Move to Lesson 7 Part 090 


