(SOLVED) Using sounds

New to FreeBASIC? Post your questions here.
Post Reply
vinians
Posts: 127
Joined: Jun 07, 2005 12:36
Location: Rio de Janeiro, Brasil
Contact:

(SOLVED) Using sounds

Post by vinians »

Hi again guys!
Im creating a shotter and I want to fire a missle. So I need to put a sound.
I was wondering on using SndPlaySound from Win32, so I tryed a function like this:
Sub play_sound(file_name as string)
sndPlaySound(file_name, SND_ASYNC)
End Sub
But this code is not compiling.
Yes Ive included "windows.bi"
Whats wrong?
Thanks in advance
Last edited by vinians on Sep 29, 2010 15:18, edited 1 time in total.
kiyotewolf
Posts: 1009
Joined: Oct 11, 2008 7:42
Location: ABQ, NM
Contact:

Post by kiyotewolf »

Why a missle? Why not a flaming duck?

I'm going to give you the #include section of KikiAI.

She uses sndPlay.. whatever..

#define WIN_INCLUDEALL
#include once "windows.bi"
kiyotewolf
Posts: 1009
Joined: Oct 11, 2008 7:42
Location: ABQ, NM
Contact:

Post by kiyotewolf »

Please post your actual compiler error.

It looks like you have a glitch somewhere else.

Parts or all of your code might help make light of the issue as well.



~Kiyote!
vinians
Posts: 127
Joined: Jun 07, 2005 12:36
Location: Rio de Janeiro, Brasil
Contact:

Post by vinians »

Thanks, I discovered the error, Its was missing the include file "mmsystem.bi", now its compiles properly.
But still not working, no sound at all. My funcion below:

Code: Select all

Sub som_toca(ByVal filename As String)
	SndPlaySound(filename, SND_ASYNC Or SND_FILENAME)
End Sub
Ive tryed put another type of string etc etc but no sound..
Whats wrong?
SARG
Posts: 1764
Joined: May 27, 2005 7:15
Location: FRANCE

Post by SARG »

Try "playsound"
Be sure to use a full name or the file must be in the directory of the exe.

Code: Select all

#include once "windows.bi"
#include once "win\mmsystem.bi"

PlaySound("c:\windows\media\tada.wav", NULL, SND_ASYNC Or SND_FILENAME or SND_LOOP)

Sleep
If you want to play more than one file at the same time use this code

Code: Select all

        #include once "windows.bi"
        #include once "win/mmsystem.bi"
        #include Once "vbcompat.bi"
        Dim Shared MCIAliases(4095) As String
        Function MCIPlaySound(filename_ As String) As Integer
                Var filename = Trim(Ucase(filename_))        
                Dim soundalias As String
                For i As Integer = 0 To 4095        
                        If MCIAliases(i) = filename Then soundalias = "SPMCIPS" & Format(i, "0000"): Exit For        
                Next
                If soundalias = "" Then        
                        For i As Integer = 0 To 4095        
                                If MCIAliases(i) = "" Or i = 4095 Then        
                                        mciSendString("close SPMCIPS" & Format(i, "0000"), NULL, 0, 0)        
                                        Function = 1
                                        If mciSendString("open " & Chr(34) & filename & Chr(34) & " alias SPMCIPS" & Format(i, "0000"), NULL, 0, 0) = FALSE Then        
                                                Function = 0
                                                MCIAliases(i) = filename
                                                mciSendString ("set SPMCIPS" & Format(i, "0000") & " time format milliseconds", NULL, 0, 0)
                                                soundalias = "SPMCIPS" & Format(i, "0000")
                                        Endif        
                                        Exit For        
                                Endif
                        Next
                Endif        
                If soundalias <> "" Then        
                        mciSendString("play " & soundalias & " from 0", NULL, 0, 0)        
                Endif
        End Function        
                
        Sub MCIUnloadAll()
                For i As Integer = 0 To 4095        
                        mciSendString("close SPMCIPS" & Format(i, "0000"), NULL, 0, 0)        
                Next
        End Sub
        
If MCIPlaySound("c:\windows\media\tada.wav") Then Print "error"
'Sleep 2000
If MCIPlaySound("c:\windows\media\chimes.wav") Then Print "error"
Sleep 2000
MCIUnloadAll()
 
 
vinians
Posts: 127
Joined: Jun 07, 2005 12:36
Location: Rio de Janeiro, Brasil
Contact:

Post by vinians »

Thanks! Worked perfectly!
(SOLVED)
Theunis Jansen
Posts: 248
Joined: Jul 01, 2010 9:35

Sound

Post by Theunis Jansen »

@Kiyote & SARG

Thanks from me as well. This solves my problem which I posted on 7 August. It was about rundll32, mmsystem.dll to play "Tada.wav".
My snippet used to work on WIN98SE but crashed on XP
Last edited by Theunis Jansen on Sep 30, 2010 6:03, edited 1 time in total.
kiyotewolf
Posts: 1009
Joined: Oct 11, 2008 7:42
Location: ABQ, NM
Contact:

Post by kiyotewolf »

@SARG

I think you just posted help for something I'm struggling with, but put far on the back burner..

Are you loading in sounds to the MCI interface thingy, and then calling them?

Can you break down into a bit more detail, how that code there works?

I'll be sure to take it apart in the mean time on my own, but that's like really cool.

I have been using SNDPLAY and whatnot to do sounds, cause I don't want to use certain libraries & stuff, cause they say you can't make money off what your doing if you use them.



~Kiyote!
I Daniel
Posts: 43
Joined: May 13, 2010 10:34
Location: Centurion, South Africa

Sound - MCI

Post by I Daniel »

@ Kiyote
I tried both the snippets. The first one works for short voice Wav recordings.
The second one also works but it is limited to the "string(4095)". i.e it plays a very short sound. I had to use SLEEP 2000 after the first sound to hear the second sound. The buffer must therefore be made larger to play longer *.wav files.
I could only get it to play *.wav files and not *.mp3 or *.ogg.
If the SND _ASYNC isn't added the sound gives a snick/click on the speakers when it finishes.
I made a string PlayIt=".. and replaced "c\windows\media\Tada.wav" with it.
djsfantasi
Posts: 87
Joined: May 21, 2010 17:38
Location: Massachusetts, USA
Contact:

Post by djsfantasi »

@I Daniel

How short is "short" in the first case?

Will it work for a 10 second .wav file?

This would be useful in my animatron program, for when the 'bot speaks!
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

Hello djsfantasi

the limitation of SndPalySound() or PlaySound() are not in time
it's in size of memory 5MB if i remember me right.

why you all don't give FBSound a try ?
(Open Source Linux and Windows *.WAV, *.MP3, *.OGG)

Joshy
I Daniel
Posts: 43
Joined: May 13, 2010 10:34
Location: Centurion, South Africa

Playsound

Post by I Daniel »

The first snippet appears to play a full length *.wav file. It kept playing in the background until I moved to the second question in my file.
I let it run for 1 minute before I switched off.

Joshy's solution looks much better especially as it plays more than just *.wav files.
Theunis Jansen
Posts: 248
Joined: Jul 01, 2010 9:35

using sound

Post by Theunis Jansen »

@ D.J.Peters
Hi Joshy

I tried it and it works fine for *.wav files. (Using the windows.bi etc.)
I would like to use MP3 and OGG files but can't find FBsound you refer to.
I only found FMOD and DSL which means distribution of the libraries with my progs. I prefer stand-alone progs
I tried the Help file in FBIde but can't find FBSound.
Any pointer to where I can search for or find it will be appreciated.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

there are hundrets of posting about FBsound but it got never a sticky link
(the FB dev team supports only libs written in stable C/C++ code :lol: )

Download: fbsound0.10.zip

Joshy
Theunis Jansen
Posts: 248
Joined: Jul 01, 2010 9:35

using sounds

Post by Theunis Jansen »

That is really great. Thank you Joshy.

This was my fifth attempt to log in. Looks like there is a gremlin.
Post Reply