IonicWind Software

IWBasic => General Questions => Topic started by: barry on December 31, 2006, 09:07:21 AM

Title: Making simple sounds
Post by: barry on December 31, 2006, 09:07:21 AM
I've found playwave and playmidi but does Ebasic have anything simpler that can create a simple notification beep?  I tried searching the help on "beep" and "sound" but no luck.

And, if I have to include a wav file to do it what's the best way to do that and still have a standalone exe?  Can my program rely on the Windows system sounds having consistant names and locations?  Packing "ding.wav" into a string seemingly won't work since it's longer than the allowable maximum constant string length. I don't see any obvious way to make it a resource.

I have the feeling I'm overlooking something real obvious here so go ahead and embarass me.  :D

Barry
Title: Re: Making simple sounds
Post by: Barney on December 31, 2006, 11:14:02 AM
This should do it.

DECLARE IMPORT, MessageBeep(uType AS UINT), INT

CONST MB_ICONASTERISK = 0x00000040
CONST MB_ICONEXCLAMATION = 0x00000030
CONST MB_ICONHAND = 0x00000010
CONST MB_ICONQUESTION = 0x00000020
CONST MB_OK = 0x00000000

DECLARE IMPORT, Beep(dwFreq as INT, dwDuration as INT),INT

Beep(1000,1000)
MessageBeep(MB_ICONASTERISK)
Beep(2000,1000)
MessageBeep(MB_ICONEXCLAMATION)
Beep(3000,1000)
MessageBeep(MB_ICONHAND)
Beep(4000,1000)
MessageBeep(MB_ICONQUESTION)
Beep(5000,1000)
MessageBeep(MB_OK)

END


The MessageBeep function plays a waveform sound. The waveform sound for each sound type is identified by an entry in the registry.

The Beep function generates simple tones on the speaker. The function is synchronous; it performs an alertable wait and does not return control to its caller until the sound finishes. Frequency and duration parameters are ignored under Win9x.

Barney
Title: Re: Making simple sounds
Post by: Ionic Wind Support Team on December 31, 2006, 11:17:13 AM
See the asteroids and smashout examples.  I convert wave files to DATA statements, load them into memory at startup and then play them as needed.

See also the update announcement to 1.57. http://www.ionicwind.com/forums/index.php/topic,1270.0.html which shows how to use DirectSound to create simple tones directly.

If you want to embed wave sounds as DATA statments here is a little program to help you along:


'program to convert a wave file
'into program DATA statements for EBASIC
'see the smashout! demo for ideas on
'how to use the embedded sound
'the same technique could be used
'to convert any binary data into
'data statements

def filename,filter,out:string
def waveMem:MEMORY
def waveFile:BFILE
def text:FILE
def length:int
def c:char
def x,z:int
filter = "WAVE files (*.wav)|*.wav|All Files (*.*)|*.*||"
filename = filerequest("Load WAVE file",0,1,filter)
if len(filename) > 0
if(OpenFile(waveFile,filename,"R") = 0)
length = len(waveFile)
AllocMem waveMem,length,1
Read waveFile,waveMem
closefile waveFile
endif
endif

if(waveMem <> NULL)
filter = "EBASIC (*.eba)|*.eba||"
filename = filerequest("Save EBASIC file",0,0,filter)
if(len(filename))
if(OpenFile(text,filename,"W") = 0)
write text,"DATABEGIN " + "wave_name"
write text,"DATA " + ltrim$(str$(length))
out = "DATA "
for x = 0 to length-1
readmem waveMem,x+1,c
out = out + ltrim$(str$(c))
if z = 25
z = 0
write text,out
out = "DATA "
else
if x <> (length-1)
out = out + ","
endif
endif
z = z + 1
next x
if(z < 26)
write text,out
endif
write text,"DATAEND"
closefile text
endif
endif
endif

freemem waveMem

end


Have fun!
Paul.
Title: Re: Making simple sounds
Post by: barry on December 31, 2006, 11:57:39 AM
Two good answers.  I'll try them both.

Thanks, guys.

Barry
Title: Re: Making simple sounds
Post by: Boris on December 31, 2006, 12:05:00 PM
I frequently use:


declare "kernel32",Beep(dwFreq:INT,dwDuration:INT),INT
'
'
'
'
beep (freq,duration)


(1000,100) should give you a generic short beep. Experiment with the values. I think the frequency is in Hz and the duration is in milliseconds.

Note: The program will 'pause' for the duration of the beep so try not to use long durations.