TYL PROGRAMMING LANGUAGE
▶ CREATING GLOBAL MODULES
As mentioned in create executable page, stand alone songplayer application has all the program code in the program file. Looking at the code, you can see that it contains module statements and module functions sections, each have sections that are dealing with sound playing statements and functionalities, as well as other sections that have the statements and functionalities that use the sound playing statements and functionalities.

Though the terminology here is quite confusing, because in programming jargon a code can use as well be used in different scenarios, let's refer to the code that is inside the sound sections, as the sound code. So, now stand alone songplayer application includes the sound code inside its code.

In Tyl, it is possible to take all of the code that is in other Tyl program and refer to it as a source module that will be included in a target module.

So let's put all sound code in a separate Tyl program 'soundmod.tyl':
!====  module statements section  =================
soundfreq accord { }
notefactor math.power 2 1 / 12
la4pitch 440
la4key 49
! set keyind so that LA4 value will be la4key
keyind 43
addfreq MI4 keyind ++ 1
addfreq FA4 keyind ++ 1
addfreq SOLB4 keyind ++ 1
addfreq SOL4 keyind ++ 1
addfreq LAB4 keyind ++ 1
addfreq LA4 keyind ++ 1
addfreq SIB4 keyind ++ 1
addfreq SI4 keyind ++ 1
addfreq DO5 keyind ++ 1
addfreq REB5 keyind ++ 1
addfreq RE5 keyind ++ 1
addfreq MIB5 keyind ++ 1
addfreq MI5 keyind ++ 1
addfreq FA5 keyind ++ 1
accind 0
AFA accind ++ 1
ADO accind ++ 1
ASOL7 accind ++ 1
AREM accind ++ 1
ALAM accind ++ 1
AMI7 accind ++ 1
fanotes FA4 LA4 DO5 LA4
accord <- AFA fanotes
donotes SOL4 DO5 MI5 DO5
accord <- ADO donotes
sol7notes SOL4 SI4 RE5 FA5
accord <- ASOL7 sol7notes
remnotes FA4 LA4 RE5 LA4
accord <- AREM remnotes
lammnotes LA4 DO5 MI5 DO5
accord <- ALAM lammnotes
mi7notes MI4 LAB4 SI4 RE5
accord <- AMI7 mi7notes
!====  module functions section  =================
addfreq num: soundfreq <- num math.ceil la4pitch * math.power notefactor num - la4key
playkey num: beep ( soundfreq num ) tempo
playsong name:
print 'Playing song `' + name + '`'
accords song name
accords acckey ~
  notes accord acckey
  notes note ~ playkey note
^
Any code that is a new or updated code is considered raw code and must be built prior to creating an executable program or a global module from it. Therefore, you must build the code. Usually, if the code was pasted or edited, and it is needed to be saved, you can save it anytime before or after creating the global module.

Now that the code is built, it is ready for use as a module or library in any Tyl program and even other modules that reside in the same directory as the file itself. But to use it from anywhere in the file system, it is needed to make it global. So open it with launcher, click global module and from now on, the system will have access to soundmod globally.
Attention: Creating a global module overwrites an existing global module with the same module name, therefore it is advised to save backup of any module in a backup directory!
Now, create a new program 'song player.tyl':
!==== module statements section =================
#mod soundmod
tempo 320
song { }
elritmoaccords ALAM AREM ALAM AMI7, ADO ASOL7 AFA AMI7
song <- 'El Ritmo' elritmoaccords
elcarreteroaccords AMI7 AFA AMI7 AFA, AMI7 AFA AMI7 AFA, AREM AFA AREM AMI7, AREM AFA ADO AMI7
song <- 'El Carretero' elcarreteroaccords
!==== module functions section =================
go:
    ! if TESTMODE is TRUE, the system plays only one
    ! note and prints a message in the result box.
    ! to enable regular activity, set TESTMODE to
    ! FALSE (TESTMODE \f)
    TESTMODE \t
    TESTMODE ?
        print 'Test Mode!'+ newline + newline + 'playing middle LA note'
        playkey LA4
        \
        playrepeated 'El Ritmo' 2
        playrepeated 'El Carretero' 1
    ^
playrepeated name num: i num ~ playsong name
Create a foreground executable program from it, and run. If all went well, you'll see a runner window that ran the program.