Basic Sound for FreeBasic

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
KLBear
Posts: 113
Joined: Jul 23, 2008 9:32

Basic Sound for FreeBasic

Post by KLBear »

deleted
Last edited by KLBear on Mar 16, 2022 4:27, edited 1 time in total.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Basic Sound for FreeBasic

Post by dodicat »

Hi KLBEAR
Yes, I used this thing in pinball maths, a final run, but it invokes the motherboard speaker I believe, and laptops, among others, can't use it.
I dug it up from somewhere else in the forums, or QB, I can't remember.
But for some it's a handy little sub.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Post by fxm »

- Instruction "Out" outputs a value to a hardware port :
Using true port access in the Windows version requires the program to install a device driver for the present session. For that reason, Windows executables using hardware port access should be run with administrator permits each time the computer is restarted. Further runs don't require admin rights as they just use the already installed driver. The driver is only 3K in size and is embedded in the executable.
(extract of documentation)

- An alternative to your function "Sound" is to call the windows function "Beep" (from Windows NT for full functioning) that generates simple tones on the speaker. The function is synchronous; it does not return control to its caller until the sound finishes. The parameters are identical (frequency in hertz of the sound, duration in milliseconds of the sound).

See below the program modified

Code: Select all

Declare Sub Sound Alias "Beep" (Byval freq As Uinteger, dur As Uinteger)


'klbear sound demo
'Declare Sub Sound(Byval freq As Uinteger, dur As Uinteger)
Common Shared As Integer c,d,e,f,g,a,b,c1,i,n,dur

'musical notes c1 is c an octave higher
c = 523:d = 587:e = 659:f = 698
g = 784:a = 880:b = 988:c1 = 1047

'Sound effect
dur = 40
For i = 1000 To 1 Step -20
Sound(i,dur)
Next
Sleep 10,1
For i = 1 To 1000 Step 20
Sound(i,dur)
Next

Sleep 200,1

'musical scale
dur = 300

Sound(c, dur):Sound(d, dur):Sound(e, dur)
Sound(f, dur):Sound(g, dur):Sound(a, dur)
Sound(b, dur):Sound(c1, dur)
Sleep 500,1

'lower octave
c = 131:d = 147:e = 165:f = 175
g = 196:a = 220:b = 247:c1 = 262

'a musical tune
dur = 200

Sound(c, dur):Sound(d, dur):Sound(e, dur)
Sound(d, dur):Sound(c, dur):Sound(d, dur)
Sound(e, dur)
dur = 400
Sound(c, dur)
Sleep 40,1
Sound(c, dur)

End

'Sub Sound(Byval freq As Uinteger, dur As Uinteger)
'Dim t As Double,f1 As Unsigned Short
'f1 = 1193181 \ freq
'Out &h61,Inp(&h61) Or 3
'Out &h43,&hb6
'Out &h42,LoByte(f1)
'Out &h42,HiByte(f1)
't=Timer
'While ((Timer - t) * 1000) < dur
'Sleep 0,1
'Wend
'turn off sound
'Out &h61,Inp(&h61) And &hfc
'End Sub
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Post by dodicat »

Nice one fxm, couldn't be easier.
KLBear
Posts: 113
Joined: Jul 23, 2008 9:32

Re: Basic Sound for FreeBasic

Post by KLBear »

deleted
Post Reply