larrybtoys
Full Member
 
Retired Part Time IT Professional
Posts: 147
|
Post by larrybtoys on Jan 23, 2023 22:11:14 GMT 1
I would like to play background music in a game and have it continue to loop until I tell it to stop. I finally got some MCI code to play the background music but I can't get it to loop each time it ends. I want the music to continue looping while the game is being played until I tell it to stop. This is the code I have so far:
Procedure PlaySND3(Sound_Name$)
If Exist(path$ + "Sound_Files\" + Sound_Name$) And Sound_Active& = 1
Long_Sound_Name$ = path$ + "Sound_Files\" + Sound_Name$
'
Local A_tfn$ = TempFileName("mp3", "mp3")
FileCopy Long_Sound_Name$ To A_tfn$
Mci "close all"
Mci "open " & A_tfn$ & " alias sfx"
Mci "play sfx from 1"
'
EndIf
EndProcedure
TIA
|
|
|
Post by (X) on Jan 23, 2023 22:54:36 GMT 1
The idea is that you must "lie" to the computer...
Mci "close all" Dim fn$ = ShortPathName(App.Path & "\beep1.wav") Trace fn$ Mci "open " & #34 & fn$ & #34 & " type mpegvideo alias beep " Mci "play beep from 1 repeat" OpenW 1 Do : Sleep : Until MouseK CloseW 1 Mci "close all" By telling the MCI driver/software that you are trying to play an mpegvideo, it will let you use the 'repeat' key word.
|
|
larrybtoys
Full Member
 
Retired Part Time IT Professional
Posts: 147
|
Post by larrybtoys on Jan 24, 2023 0:57:52 GMT 1
That worked great...Thanks
|
|
|
Post by (X) on Jan 24, 2023 13:05:31 GMT 1
If I remember correctly, the "type mpegvideo"* also lets one change the "volume" for the left and right channels or both, which the "type waveaudio" or default does not.
There may be other MCI types and features that I am not aware of. I only found this* out by chance.
|
|
larrybtoys
Full Member
 
Retired Part Time IT Professional
Posts: 147
|
Post by larrybtoys on Jan 25, 2023 13:13:12 GMT 1
I have 2 additional questions as I explore MCI:
Question 1: Will either of the 2 examples work the same? EXAMPLE 1: Long_Sound_Name$ = path$ + "Sound_Files\" + "Out.mp3" Mci "open " & #34 & Long_Sound_Name$ & #34 & " alias Out"
EXAMPLE 2: Long_Sound_Name$ = path$ + "Sound_Files\" + "Out.mp3" Stored_Sound$ = TempFileName("mp3", "mp3") FileCopy Long_Sound_Name$ To Stored_Sound$ Mci "open " & Stored_Sound$ & " alias Out"
QUESTION 2: I read in and store all my sounds (1, 45 second intro & 6 much smaller game sounds) the game seems to work great as long as I have my 45 second intro sound continue to repeat however if I let the 45 second intro sound play 1 time and do not have it repeat the program stops for a 1/2 second or so every time another much smaller sound is told to play.
|
|
|
Post by (X) on Jan 25, 2023 15:37:01 GMT 1
I have 2 additional questions as I explore MCI: Question 1: Will either of the 2 examples work the same? EXAMPLE 1: Long_Sound_Name$ = path$ + "Sound_Files\" + "Out.mp3" Mci "open " & #34 & Long_Sound_Name$ & #34 & " alias Out" EXAMPLE 2: Long_Sound_Name$ = path$ + "Sound_Files\" + "Out.mp3" Stored_Sound$ = TempFileName("mp3", "mp3") FileCopy Long_Sound_Name$ To Stored_Sound$ Mci "open " & Stored_Sound$ & " alias Out" QUESTION 2: I read in and store all my sounds (1, 45 second intro & 6 much smaller game sounds) the game seems to work great as long as I have my 45 second intro sound continue to repeat however if I let the 45 second intro sound play 1 time and do not have it repeat the program stops for a 1/2 second or so every time another much smaller sound is told to play. For Q1: I think these are functionally equivalent, but, using a temp file is probably better. For ex.: If you store the sound file as a resource (in the :File section) you'll need to copy it to disk first before you can access it via MCI functions. Using a TempFileName for a location is probably the best place, since it should be deleted automatically and won't add anything to your program directory.
For Q2, You might try setting the output on repeat, turning the volume down to 0, pause or stop it instead letting it finish. Could you post a simplified demo?
|
|
larrybtoys
Full Member
 
Retired Part Time IT Professional
Posts: 147
|
Post by larrybtoys on Jan 25, 2023 15:48:38 GMT 1
The overall code is 1700+ lines but let me see if I can create a working demo
|
|
|
Post by (X) on Jan 25, 2023 15:55:37 GMT 1
I am modifying the demo I submitted to see if I can add short sound effects while music is in a loop and not (let run out)...
|
|
|
Post by (X) on Jan 25, 2023 19:23:54 GMT 1
This code seems to compensate in 2 ways for possible noticeable delays or silence before playing the next sound file. 1) If you edit a file in Audacity I noticed there is alway a silent buffer at the start and end that is added even when I am careful to select a region that has sound values in it. This can be compensated for by specifying the "from <pos>" in the MCI play command string. 2) You can play a sound and play another different alias on top of it "willy nilly" with no delay, but, if you try to loop or play the same alias sound back to back, there is always a noticeable delay. The solution seems to create 2 different aliases of the same sound file and then play them alternately one after the other and perhaps even better, with an overlap, this is achievable by tracking the position of the currently playing sound and starting the following sound before the first one ends.
|
|
larrybtoys
Full Member
 
Retired Part Time IT Professional
Posts: 147
|
Post by larrybtoys on Jan 25, 2023 20:33:13 GMT 1
I have a perfect demo to show the problem. When running the demo a white ball moves back and forth across the screen with a dinging sound each time it changes direction all while a background song is playing. It will work great as long as the background song keeps repeating but if I don't allow the background song to repeat there is a noticeable pause each time the ball changes direction when the dinging sound is told to play. I would like to figure out why this happens and prevent if I don't want the intro song to keep repeating. NOTE: The 2 .MP3 files need to be in a folder called Sound_Files TIA Ball-Sound Demo 01.G32 (4.85 KB) Wall.mp3 (5.93 KB) Intro.mp3 (138.79 KB)
|
|
|
Post by (X) on Jan 25, 2023 21:06:50 GMT 1
|
|
|
Post by (X) on Jan 25, 2023 21:17:17 GMT 1
OK... If you load 2 instances of the wall sound and call the aliases Wall_Right and Wall_Left then you can play them without any delay.
Mci "open " & #34 & Stored_Sound$ & #34 & " alias Wall_Left" Mci "open " & #34 & Stored_Sound$ & #34 & " alias Wall_Right"
/////////////////////////////////////////////////////////////////
If Current_Ball_X > 800 Mci "play Wall_Right from 1" Ball_X_Move = -1 EndIf If Current_Ball_X < 400 Mci "play Wall_Left from 1" Ball_X_Move = 1 EndIf I am guessing that when we try to play the same sound alias back to back, even if the first sound is not playing anymore, the MCI driver re-inits the original sound before playing it again, thus, causing a delay. This does not happen if you try to play a different sound when a sound is currently playing. This leads to the technique of alternating from one copy to another to avoid the delay. You can almost hear the computer saying:"Hey, didn't I just play this very same sound file? Well ok... Sigh! I'll play it again shall I?"
|
|
larrybtoys
Full Member
 
Retired Part Time IT Professional
Posts: 147
|
Post by larrybtoys on Jan 25, 2023 21:52:31 GMT 1
Even when I created a left and a right version of the WALL sound there was still a pause after the background music completed.
|
|
|
Post by (X) on Jan 25, 2023 21:54:44 GMT 1
Check my version... Is there a pause?
|
|
larrybtoys
Full Member
 
Retired Part Time IT Professional
Posts: 147
|
Post by larrybtoys on Jan 25, 2023 22:01:57 GMT 1
Yes. There is no pause until the background music is finished then there is a noticeable pause each time the ball hits the wall and changes direction.
|
|
|
Post by (X) on Jan 25, 2023 22:06:11 GMT 1
I don't detect an obvious pause...
There is some dead air at the start of the Wall.mp3 sound...
Which I couldn't completely get rid of...
This may help start the sound sooner and avoid any dead air... If Current_Ball_X > 800 Mci "play Wall_Right from 50" Ball_X_Move = -1 EndIf If Current_Ball_X < 400 Mci "play Wall_Left from 50" Ball_X_Move = 1 EndIf
For me, the ball direction change and the sound are simultaneous, while the intro plays and after. I checked behaviour of the exec version and I get the same 'good' results.
|
|
larrybtoys
Full Member
 
Retired Part Time IT Professional
Posts: 147
|
Post by larrybtoys on Jan 26, 2023 2:12:23 GMT 1
OK I figured it out. There must be a hardware built-in motherboard driver my PC is missing. If I simply keep Windows Media Player opened in the background, the pause goes away. I presume that while the background music is playing it is already accessing the needed driver hence NO pause. As soon as the background music stops the missing driver shows it's ugly head. This problem has come up on 3 different computers so far but does not happen on all of them. I thought the MCI routines had corrected this but I guess not. I just need to keep background music playing on every game. Thanks much for your help. I learned a lot of cool stuff recently...
|
|
|
Post by (X) on Jan 26, 2023 2:22:39 GMT 1
I suppose MCI is perhaps not the ultimate way to go. But it's free.
There is also the PlaySound API and perhaps DirectShow.
Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long Public Declare Function waveOutGetNumDevs Lib "winmm" () As Long
Global Const SND_SYNC = &H0 'just after the sound is ended exit function Global Const SND_ASYNC = &H1 'just after the beginning of the sound exit function Global Const SND_NODEFAULT = &H2 'if the sound cannot be found no error message Global Const SND_LOOP = &H8 'repeat the sound until the function is called again Global Const SND_NOSTOP = &H10 'if currently a sound is played the function will return without playing the selected sound
Global Const Flags& = SND_ASYNC Or SND_NODEFAULT BASS and other DLL's may yield better results, but $$$ for commercial use.
|
|
larrybtoys
Full Member
 
Retired Part Time IT Professional
Posts: 147
|
Post by larrybtoys on Jan 26, 2023 2:38:21 GMT 1
PlaySound did the exact same thing. I thought MCI corrected it but I was wrong
|
|
larrybtoys
Full Member
 
Retired Part Time IT Professional
Posts: 147
|
Post by larrybtoys on Jan 26, 2023 22:20:45 GMT 1
Can you assist in 1 more question?
How would I read a .WAV file into a variable and play it using the PlaySound command?
It is easy to use PlaySound to play a .WAV file from the hard drive however I would like to store a few of them in variables and play them from those locations so I don't have to go back to the hard for every replay of the sound.
|
|
|
Post by (X) on Jan 27, 2023 1:17:05 GMT 1
This reference mentions using an alias, so, I imagine it is possible. This is probably what MCI does in the background.
I have never tried to use this feature. So, no quick help demo.
c++ PlaySound(TEXT("MyAppSound"), NULL, SND_ALIAS | SND_APPLICATION); This is probably close to what you want.
|
|
larrybtoys
Full Member
 
Retired Part Time IT Professional
Posts: 147
|
Post by larrybtoys on Jan 27, 2023 3:06:04 GMT 1
I will give it a read
|
|