looping a wav file

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by cfriedel.

Hi all. Wondering if this has been done yet. Basically what I want to do is loop a song over and over. As playsound has no return, I am wondering how to do this. The only thing I could think of is using peek commands, but I am not sure at which address to check. Can somebody help me on this one. Thanks.

Cliff
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Paul.

You could use MCI commands to start a WAV or MIDI file and then check to see when it has finished, then just start it again, and again, and again...

Code: Select all

Global buffer$,TotalTime.l,TimeLeft.l
InitSprite(0)
InitKeyboard()
buffer$=Space(256)


Procedure Mci(Cde.s)
  Retour.l=MciSendString_(Cde,@buffer$,256,0)
  ProcedureReturn Retour
EndProcedure


Procedure PlayMCI(ToPlay.s)
  If Mci("open "+ToPlay+" alias audio")
    ProcedureReturn 0
  Else
    Mci("set audio time format ms")    
    Mci("seek audio To End wait") 
    Mci("status audio position wait")
    TotalTime = Val(buffer$) 
    Mci("seek audio To start")
    Mci("play audio from 1")
    ProcedureReturn 1
  EndIf
EndProcedure


quit=PlayMCI("file.wav")
Repeat
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Escape):quit=0:EndIf
    
  Delay(20)
  Mci("status audio position")
    
  TimeLeft=Val(buffer$)
  If TimeLeft=TotalTime 
    Mci("play audio from 1")
  EndIf
Until quit=0
Mci("close audio")   

End

(most of this code is thanks to Mr.Skunk... from a project he helped me with way back when)

Edited by - paul on 13 November 2001 06:12:50
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by cfriedel.

Thanks for your help. Saw some of this in the MP3 player after I wrote this. Looks like this will provide a lot better flexibility in what formats I can use also. As for APIs, which ones can be used by PB? Can all of them be used or just specific ones. The only one I have seen on the forums or in examples has been the MCI API. Thanks again. Hope someday I can be of service.

Cliff
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

The suggested method to play sound in loop is good, but not suitable to game as it didn't use DirectX. Therefore, I've simply added an option to PlyaSound(#Sound, Flag) to play a sound in loop or not. MCI is very useful for normal applications which would need sounds..

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by cfriedel.

Is this option available as of 2.60a or something that will be released later? If it is available now, what flags can the function take? Thanks and sorry for all the questions.

Cliff
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Paul.

Hey Fred,

It would be really useful to have a command that returns a true/false if a sound/movie is currently playing...

IsPlaying(#media)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Loop sound command will be available for the next release (2.70).

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by cfriedel.

Ok, MCI is causing some problems. Ever since I have put the code into my program to play midi, things have begun to break. Looks like things run ok the first time, then second time things die. Upon looking at the resource meter, I see an approximate 10% drop in resources after my program runs the first time. second time crash.

How much resources does MCI use on average? Also, does ENDing a program cause it to release its resources or do I need to specifically tell it to drop all of the memory it is using?

I will put up the source code at the URL listed below if you would like to look at it. I plan to rewrite it anyway, so don't hesitate to let me know if it is wrong or needs changed. Hopefully DX and MCI can live in harmony. Very nice to use midi files and mp3 for music rather than wave. Thanks for all the help you have given me so far. Please let me know if there is anything else I can provide.

Cliff

URL: http://www.cliffsconsult.com/game/MCISpaceGame.zip
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Right, this work but it should not be used for that. Instead you can use the following code:

Code: Select all

 
; Try to initialize the DirectShow functions to replay quickly mp3, midi etc..
;
If InitMovie()
  EnableMusic = 1
EndIf

...

OpenScreen()...

If EnableMusic
  LoadMovie(0, "Sounds\d_in_cit.mid")
  PlayMovie(0, ScreenID())
EndIf

I've tested and it works pretty well.. Yes, Movie commands can be used to replay any kind of media than Windows suppots (AVI, DivX :wink:, Mp3, Midi etc..). As it use DirectShow, it's very fast and it's especially suited for games.

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by cfriedel.

This looks like it would work. The only question I have is: Is it possible to detect when the song is over and can it be restarted. Thanks.

Cliff



Edited by - cfriedel on 13 November 2001 23:17:15
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by cfriedel.

Fred,

Tried the new code (using DX calls instead of MCI). Same result. First time ok, second time crash. Upon looking at it more with system information wizard and resource manager, it looks like a lot of resources do not release upon the END call. Thought it may have been just the midi, so I tried it with an MP3 and I got the same thing. Looks like there may be a resource leak either in my code or in the call made from PB. Not sure which. I have put the code in the URL below. In there you can see I do a free on the movie portion to see if that would alleviate the problem. It doesn't seem to. Please let me know if there is anything in the source I can do to get you more information or to stop the leak. Also, if there is anything else I can provide, feel free to ask. Thanks for all of the work you have done thus far with me.

Cliff

URL: http://www.cliffsconsult.com/game/MCISpaceGame.zip

NOTE: Included the MP3 I used for testing so the file is about 2MB. If you do not want it in there, feel free to leave a note and I can rezip it wothout the file. Thanks.

Edited by - cfriedel on 14 November 2001 03:54:59
Post Reply