Hello,
this was my little homework for the weekend. I was not aware that you can still use the Beep function. It may not work with 64-bit versions of Win XP and Win Vista. The function can still be used for small gimmicks.
This little app plays two melodies. I named it TheBeeper.o2bas:
$ filename "TheBeeper.exe"
'uses rtl32
'uses rtl64
uses console
! SetConsoleCursorInfo lib "kernel32.dll" (sys hConsoleOutput, CONSOLE_CURSOR_INFO *lpConsoleCursorInfo) as bool
! SetConsoleWindowInfo lib "kernel32.dll" (sys hConsoleOutput, bool bAdsolute, SMALL_RECT *lpConsoleWindow)
! GetTickCount lib "kernel32.dll" () as dword
! Beep lib "kernel32.dll" (dword dwFreq=800, dword dwDuration=200) as bool
! Sleep lib "kernel32.dll" 'dwMilliseconds
% RAND_MAX = 32767
! srand lib "Msvcrt.dll" (uint seed)
! rand lib "Msvcrt.dll" () as int
srand(GetTickCount())
function inRange(int range_min, range_max) as int
return (double rand() / RAND_MAX) * (range_max - range_min) + range_min
end function
type CONSOLE_CURSOR_INFO ' cci
dword dwSize
bool bVisible
end type
sub setcolor(int fg, bg)
SetConsoleTextAttribute (ConsOut, fg+bg*16)
end sub
sub locate (int col,int row, optional int visible=1,int shape=12)
CONSOLE_CURSOR_INFO cci
SetPos(col-1,row-1)
cci.bVisible = visible
cci.dwSize = shape
SetConsoleCursorInfo(ConsOut, cci)
end sub
sub display(int col, row, string txt, optional int visible=1,int shape=12)
locate(col, row, visible)
print txt
end sub
SetConsoleTitle "Playing with the Beep function"
cls
type music_note
int i
string n_s 'major scale (with sharp)
string n_f 'minor scale (with flat)
dword f
end type
' The piano key table
music_note note[] =
{
{ 1,"A0" ,"A0" , 27.50},
{ 2,"A#0","Ab0", 29.14},
{ 3,"B0" ,"B0" , 30.87},
{ 4,"C1" ,"C1" , 32.70},
{ 5,"C#1","Db1", 34.65},
{ 6,"D1" ,"D1" , 36,71},
{ 7,"D#1","Eb1", 38.89},
{ 8,"E1" ,"E1" , 41.20},
{ 9,"F1" ,"F1" , 43.65},
{10,"F#1","Gb1", 46.25},
{11,"G1" ,"G1" , 49.00},
{12,"G#1","Ab1", 51.91},
{13,"A1" ,"A1" , 55.00},
{14,"A#1","Bb1", 58.27},
{15,"B1" ,"B1" , 61.74},
{16,"C2" ,"C2" , 65.41},
{17,"C#2","Db2", 69.30},
{18,"D2" ,"D2" , 73.42},
{19,"D#2","Eb2", 77.78},
{20,"E2" ,"E2" , 82.41},
{21,"F2" ,"F2" , 87.31},
{22,"F#2","Gb2", 92.50},
{23,"G2" ,"G2" , 98.00},
{24,"G#2","Ab2", 103.83},
{25,"A2" ,"A2" , 110.00},
{26,"A#2","Bb2", 116.54},
{27,"B2" ,"B2" , 123.47},
{28,"C3" ,"C3" , 130.81},
{29,"C#3","Db3", 138.59},
{30,"D3" ,"D3" , 146.83},
{31,"D#3","Eb3", 155.56},
{32,"E3" ,"E3" , 164.81},
{33,"F3" ,"F3" , 174.61},
{34,"F#3","Gb3", 185.00},
{35,"G3" ,"G3" , 196.00},
{36,"G#3","Ab3", 207.65},
{37,"A3" ,"A3" , 220.00},
{38,"A#3","Bb3", 233.08},
{39,"B3" ,"B3" , 246.94},
{40,"C4" ,"C4" , 261.63},
{41,"C#4","Db4", 277.18},
{42,"D4" ,"D4" , 293.66},
{43,"D#4","Eb4", 311.13},
{44,"E4" ,"E4" , 329.63},
{45,"F4" ,"F4" , 349.23},
{46,"F#4","Gb4", 369.99},
{47,"G4" ,"G4" , 392.00},
{48,"G#4","Ab4", 415.30},
{49,"A4" ,"A4" , 440.00},
{50,"A#4","Bb4", 466.16},
{51,"B4" ,"B4" , 493.88},
{52,"C5" ,"C5" , 523.25},
{53,"C#5","Db5", 554.37},
{54,"D5" ,"D5" , 587.33},
{55,"D#5","Eb5", 622.25},
{56,"E5" ,"E5" , 659.25},
{57,"F5" ,"F5" , 698.46},
{58,"F#5","Gb5", 739.99},
{59,"G5" ,"G5" , 783.99},
{60,"G#5","Ab5", 830.61},
{61,"A5" ,"A5" , 880.00},
{62,"A#5","Bb5", 932.33},
{63,"B5" ,"B5" , 987.77},
{64,"C6" ,"C6" ,1046.50},
{65,"C#6","Db6",1108.73},
{66,"D6" ,"D6" ,1174.66},
{67,"D#6","Eb6",1244.51},
{68,"E6" ,"E6" ,1318.51},
{69,"F6" ,"F6" ,1396.91},
{70,"F#6","Gb6",1479.98},
{71,"G6" ,"G6" ,1567.98},
{72,"G#6","Ab6",1661.22},
{73,"A6" ,"A6" ,1760.00},
{74,"A#6","Bb6",1864.66},
{75,"B6" ,"B6" ,1975.53},
{76,"C7" ,"C7" ,2093.00},
{77,"C#7","Db7",2217.46},
{78,"D7" ,"D7" ,2349.32},
{79,"D#7","Eb7",2489.02},
{80,"E7" ,"E7" ,2637.02},
{81,"F7" ,"F7" ,2793.83},
{82,"F#7","Gb7",2959.96},
{83,"G7" ,"G7" ,3135.96},
{84,"G#7","Ab7",3322.44},
{85,"A7" ,"A7" ,3520.00},
{86,"A#7","Bb7",3729.31},
{87,"B7" ,"B7" ,3951.07},
{88,"C8" ,"C8" ,4186.01},
{89,"GAP","GAP" ,0}
}
' Duration values
int d_0, d_2, d_4, d_8, d_16
sub adapt_duration(int v)
d_0 = v 'whole
d_2 = d_0\2 'half
d_4 = d_2\2 'quarter
d_8 = d_4\2 'eighth
d_16 = d_8\2 'sixteenth
end sub
macro doBeep (idx)
if note (n(idx)).i <> 89 then
Beep(note(n(idx)).f, d(idx))
else
sleep(d(idx))
end if
end macro
adapt_duration(1400)
redim int n(30)
redim int d(30)
'
' a simple childrens song
n[] = {40,42,44,45,47,47, 49,49,49,49,47, 89, 49,49,49,49,47, 89,
45,45,45,45,44,44, 47,47,47,47,40}
d[] = {d_4,d_4,d_4,d_4,d_2,d_2, d_4,d_4,d_4,d_4,d_2, d_4, d_4,d_4,d_4,d_4,d_2, d_4,
d_4,d_4,d_4,d_4,d_2,d_2, d_4,d_4,d_4,d_4,d_2}
int x
Printl "A simple childrens song" + cr : sleep 1000
printl "All my little ducklings"
printl "swimming on the lake,"
for x = 1 to 12
doBeep (x)
next
printl "swimming on the lake,"
for x = 13 to 18
doBeep (x)
next
printl "dip their heads in water,"
for x = 19 to 24
doBeep (x)
next
printl "make their tails all shake."
for x = 25 to 29
doBeep (x)
next
sleep(d_0)
Beep : Beep : Beep
cls
printl "And now TheBeeper makes Popcorn" + cr : sleep 1000
redim int n(0)
redim int n(194)
'Popcorn (a little, very little bit like Gershon Kingsley)
n[] = {49,47,49,44,40,44,37, 89, 49,47,49,44,40,44,37, 89, 49,51, '18
52,51,52,49,51,49,51,47,49,47,49,45,49, 89, 49,47,49,44,40,44, '38
37, 89, 49,47,49,44,40,44,37, 89, 49,51,52,51,52,49,51,49,51,47, '58
49,47,49,51,52, 89, 56,54,56,52,47,52,44, 89, 56,54,56,52,47,52, '78
44, 89, 56,58,59,58,59,56,58,56,57,54,56,54,56,52,56, 89, 56,54, '98
56,52,47,52,44, 89, 56,54,56,52,47,52,44, 89, 56,58,59,58,59,56, '118
58,56,57,54,56,54,51,54,56, 89, 49,47,49,44,40,44,37, 89, 49,47, '138
49,44,40,44,37, 89, 49,51,52,51,52,49,51,49,51,47,49,47,49,45, '158
49, 89, 49,47,49,44,40,44,37, 89, 49,47,49,44,40,44,37, 89, 49,51, '178
52,51,52,49,51,49,51,47,49,47,44,47,49, 89, 89, 89} '194
adapt_duration(900)
for x = 1 to 194
if note (n(x)).i <> 89 then
display(inRange(5,25), inRange(5,16), "**")
Beep(note(n(x)).f, d_4)
else
sleep(d_4)
end if
next
Beep : Beep : Beep
locate(1,20)
print "Enter to continue ..."
waitkey