Old lady swallowed a fly: Difference between revisions

Added Commodore BASIC (64. Plus/4, 128 with sound)
m (→‎{{header|Sidef}}: Fix link: Perl 6 --> Raku)
(Added Commodore BASIC (64. Plus/4, 128 with sound))
Line 617:
DISPLAY LINE-OUT.
</lang>
 
 
=={{header|Commodore BASIC}}==
The portion of the program that handles the lyric logic is universal and should work across all Commodore models. Since a sound chip is available in Commodore BASIC on the C64, Plus/4, and 128, it is not difficult to take advantage of this. The 64 and Plus/4 versions demonstrate a very rudimentary music player based on the syntax used for the C128's PLAY command (thus making the data statements nearly identical in all three versions.)
 
To use the program without music, omit the music data statements lines 1100 through 1180, the music playing routines lines 2000 through 2599, and remove all GOSUB references to lines 2000 and 2500.
 
The music is not quite as procedural as the lyric strings, since the music is tailored to the unique number of syllables for each phrase, however, each verse does utilize three basic phrases that can be iterated through.
 
===Commodore 64===
Sound must be played to the Commodore 64 SID chip through direct register access, therefore, the sound table is two-dimensional to account for the high byte and low byte of the appropriate frequencies.
<lang gwbasic>
1 rem rosetta code
2 rem old lady who swallowed a fly - c64
5 print chr$(147);chr$(14)
10 dim a$(10),ex$(10),mu$(8,3):a=1
15 for i=1 to 8:read a$(i),ex$(i):next i:gosub 2500
20 for c=1 to 8
30 print chr$(147):print " Old Lady Who Swallowed A Fly ":print
40 print "I know an old lady who swallowed"
45 print " a ";a$(c):m$=mu$(c,1):gosub 2000
50 for bc=c to 1 step -1
55 if bc=c or bc<=2 then print ex$(bc):m$=mu$(bc,2):gosub 2000
57 if bc=8 then for t=1 to 2000:next
60 if bc>1 then print "She swallowed a "a$(bc)" to catch the "a$(bc-1)";"
61 if bc>1 then m$=mu$(bc,3):gosub 2000
65 next bc
70 print " ... Perhaps she'll die!"
72 m$=mu$(1,3):gosub 2000
75 get k$:if k$="q" then end
77 if k$>"0" and k$<"9" then c=asc(k$)-49
80 print:for t=1 to 1000:next t
90 next c
100 end
 
1000 rem lyrics
1010 data "fly","I don't know why she swallowed a fly..."
1020 data "spider","That wriggled and jiggled and tickled inside her!"
1030 data "bird","How absurd to swallow a bird!"
1040 data "cat","Imagine that! She swallowed a cat!"
1050 data "dog","What a hog, to swallow a dog!"
1060 data "goat","She just opened her throat and swallowed a goat!"
1070 data "cow","I don't know how she swallowed a cow!"
1080 data "horse","...She died, of course!"
 
1100 rem music (optional)
1110 data "8cffaffcffa4f","8ggg4g8feec2c","8c4d8e4f":rem fly
1120 data "8cffaffcffaff","8fgggggfeccc4c","8cffaffc4f8a4f":rem spider
1130 data "8cffaffcffa4f","4g8g4g8feec2c","8cffa4f8c4f8a8ff":rem bird
1140 data "8cffaffcffa4f","8c4g8g4g8feec2c","8cffa4f8c4f8a4f":rem cat
1150 data "8cffaffcffa4f","4g8g4g8feec2c","8cffa4f8c4f8a4f":rem dog
1160 data "8cffaffcffa4f","8ccggg4g8feec2c","8cffa4f8c4f8a4f":rem goat
1170 data "8cffaffcffa4f","8ggg4g8feec2c","8cffa4f8c4f8a4f":rem cow
1180 data "8cffaffcffa2f","4c2d4e2f","8cffa4f8c4f8a4f":rem horse
 
2000 rem note player
2005 for z=1 to len(m$):el$=mid$(m$,z,1)
2010 if el$>="a" and a$<="g" then gosub 2050
2015 if el$>="1" and el$<="8" then du=int(ms/(asc(el$)-48))
2020 next z:return
2050 rem play note
2055 n=asc(el$)-64
2060 poke 54272,nt(n,2):poke 54273,nt(n,1):poke 54276,17
2065 for i=1 to du:next i:poke 54276,16
2070 return
 
2500 rem set up sound table and sid
2505 dim nt(7,2):nt(3,1)=16:nt(3,2)=195
2506 nt(4,1)=18:nt(4,2)=209
2510 nt(5,1)=21:nt(5,2)=31
2515 nt(6,1)=22:nt(6,2)=96
2520 nt(7,1)=25:nt(7,2)=30
2525 nt(1,1)=28:nt(1,2)=49
2530 nt(2,1)=31:nt(2,2)=165
2535 poke 54296,15:poke 54277,9:poke 54278,0
2540 du=250:ms=1000
2550 for i=1 to 8:for j=1 to 3:read mu$(i,j):next j:next i
2599 return
</lang>
 
===Commodore Plus/4===
BASIC 3.5 on the Commodore Plus/4 at least features the SOUND command which simplifies getting the sound processor to play notes, however, a "tuned" sound table still needs to be defined with the appropriate frequencies.
<lang gwbasic>
1 rem rosetta code
2 rem old lady who swallowed a fly - plus/4
5 print chr$(147);chr$(14)
10 dim an$(10),ex$(10),mu$(8,3):a=1
15 for i=1 to 8:read an$(i),ex$(i):next i:gosub 2500
20 for c=1 to 8
30 print chr$(147):print " Old Lady Who Swallowed A Fly ":print
40 print "I know an old lady who swallowed"
45 print " a ";an$(c):m$=mu$(c,1):gosub 2000
50 for bc=c to 1 step -1
55 if bc=c or bc<=2 then print ex$(bc):m$=mu$(bc,2):gosub 2000
57 if bc=8 then for t=1 to 2000:next t
60 if bc>1 then print "She swallowed a "an$(bc)" to catch the "an$(bc-1)";"
61 if bc>1 then m$=mu$(bc,3):gosub 2000
65 next bc
70 print " ... Perhaps she'll die!"
72 m$=mu$(1,3):gosub 2000
75 get k$:if k$="q" then end
77 if k$>"0" and k$<"9" then c=asc(k$)-49:rem jump to animal
80 print:for t=1 to 1000:next t
90 next c
100 end
 
1000 rem lyrics
1010 data "fly","I don't know why she swallowed a fly..."
1020 data "spider","That wriggled and jiggled and tickled inside her!"
1030 data "bird","How absurd to swallow a bird!"
1040 data "cat","Imagine that! She swallowed a cat!"
1050 data "dog","What a hog, to swallow a dog!"
1060 data "goat","She just opened her throat and swallowed a goat!"
1070 data "cow","I don't know how she swallowed a cow!"
1080 data "horse","...She died, of course!"
 
1100 rem music (optional)
1110 data "8cffaffcffa4f","8ggg4g8feec2c","8c4d8e4f":rem fly
1120 data "8cffaffcffaff","8fgggggfeccc4c","8cffaffc4f8a4f":rem spider
1130 data "8cffaffcffa4f","4g8g4g8feec2c","8cffa4f8c4f8a8ff":rem bird
1140 data "8cffaffcffa4f","8c4g8g4g8feec2c","8cffa4f8c4f8a4f":rem cat
1150 data "8cffaffcffa4f","4g8g4g8feec2c","8cffa4f8c4f8a4f":rem dog
1160 data "8cffaffcffa4f","8ccggg4g8feec2c","8cffa4f8c4f8a4f":rem goat
1170 data "8cffaffcffa4f","8ggg4g8feec2c","8cffa4f8c4f8a4f":rem cow
1180 data "8cffaffcffa2f","4c2d4e2f","8cffa4f8c4f8a4f":rem horse
 
2000 rem note player
2005 for z=1 to len(m$):el$=mid$(m$,z,1)
2010 if el$>="a" and a$<="g" then gosub 2050
2015 if el$>="1" and el$<="8" then du=int(ms/(asc(el$)-48))
2020 next:return
2050 rem play note
2055 n=asc(el$)-64
2060 sound 1,nt(n),du
2065 for t=1 to du:next
2070 sound 1,0,0
2075 return
 
2500 rem set up sound table
2505 dim nt(7):nt(3)=596
2506 nt(4)=643
2510 nt(5)=685
2515 nt(6)=704
2520 nt(7)=739
2525 nt(1)=770
2530 nt(2)=798
2535 vol 2
2540 du=250:ms=1000
2550 for i=1 to 8:for j=1 to 3:read mu$(i,j):next j:next i
2599 return
</lang>
 
 
===Commodore 128===
BASIC 7.0 on the Commodore 128 features a robust PLAY command which will interpret strings of macro commands for commanding the SID processor. This all but eliminates the bulk of the note playing routines. Note the data statements are slightly different as the macros for note duration are different. The 128 uses '''i''', '''q''', and '''h''' for ''eighth'', ''quarter'', and ''half'' notes respectively, while the 64 and Plus/4 examples were coded to use '''8''', '''4''', and '''2''' as it was simpler to use for math operations, rather than an additional look-up table.
<lang gwbasic>
1 rem rosetta code
2 rem old lady who swallowed a fly - c128
5 print chr$(147);chr$(14)
10 dim an$(10),ex$(10),mu$(8,3):a=1
15 for i=1 to 8:read an$(i),ex$(i):next i:gosub 2500
20 for c=1 to 8
30 print chr$(147):print " Old Lady Who Swallowed A Fly ":print
40 print "I know an old lady who swallowed"
45 print " a ";an$(c):m$=mu$(c,1):gosub 2000
50 for bc=c to 1 step -1
55 if bc=c or bc<=2 then print ex$(bc):m$=mu$(bc,2):gosub 2000
57 if bc=8 then for t=1 to 2000:next t
60 if bc>1 then print "She swallowed a "an$(bc)" to catch the "an$(bc-1)";"
61 if bc>1 then m$=mu$(bc,3):gosub 2000
65 next bc
70 print " ... Perhaps she'll die!"
72 m$=mu$(1,3):gosub 2000
75 get k$:if k$="q" then end
77 if k$>"0" and k$<"9" then c=asc(k$)-49:rem jump to animal
80 print:for t=1 to 1000:next t
90 next c
100 end
 
1000 rem lyrics
1010 data "fly","I don't know why she swallowed a fly..."
1020 data "spider","That wriggled and jiggled and tickled inside her!"
1030 data "bird","How absurd to swallow a bird!"
1040 data "cat","Imagine that! She swallowed a cat!"
1050 data "dog","What a hog, to swallow a dog!"
1060 data "goat","She just opened her throat and swallowed a goat!"
1070 data "cow","I don't know how she swallowed a cow!"
1080 data "horse","...She died, of course!"
 
1100 rem music (optional)
1110 data "icffaffcffaqf","igggqgifeechc","icqdieqf":rem fly
1120 data "icffaffcffaff","ifgggggfecccqc","icffaffcqfiaqf":rem spider
1130 data "icffaffcffaqf","qgigqgifeechc","icffaqficqfiaiff":rem bird
1140 data "icffaffcffaqf","icqgigqgifeechc","icffaqficqfiaqf":rem cat
1150 data "icffaffcffaqf","qgigqgifeechc","icffaqficqfiaqf":rem dog
1160 data "icffaffcffaqf","iccgggqgifeechc","icffaqficqfiaqf":rem goat
1170 data "icffaffcffaqf","igggqgifeechc","icffaqficqfiaqf":rem cow
1180 data "icffaffcffahf","qchdqehf","icffaqficqfiaqf":rem horse
 
2000 rem note player
2005 play m$
2010 return
 
2500 rem set up sid
2510 for i=1 to 8:for j=1 to 3:read mu$(i,j):next j:next i
2520 play "v1o4t0u5"
2599 return
</lang>
 
113

edits