Looping .ogg files smoothly. Is it possible?

Blitz3D Forums/Blitz3D Programming/Looping .ogg files smoothly. Is it possible?

QuickSilva(Posted 2005) [#1]
If I use loadsound to load a .wav file and play it with playsound it works fine apart from the fact that it auto loops (not sure why this is and not always a wanted feature either) but if I load an .ogg file (which doesn`t autoloop) and try to loop it with loopsound I get a pause inbetween loops. Is there anyway around this pause of a second or two as it destroys the professional feel of my game.
This doesn`t happen with .wav files, they loop flawlessly, but I suspect that the .ogg files have to decompress each time. Is this the case? Has anyone found a way to loop .ogg files so that they play without loop hitches? And why do .wav files autoloop? Can this be disabled?

Jason.


Warren(Posted 2005) [#2]
This won't be any help to you, but I do what you're describing and my ogg files loop perfectly.

1. LoadSound
2. LoopSound

Maybe it's your audio drivers?


JazzieB(Posted 2005) [#3]
WAV files don't auto-loop! At least they never have with me and I've been coding in Blitz for 3 years! Is there anything in the WAV file format that can force them to loop - I don't know, I'm just asking.

I've not had any trouble with OGG files myself when using LoadSound/LoopSound. There is an initial pause when you first load them (which is when it first decompresses), but I've not experienced a long pause when it loops. Is it possible you're reloading the music?

An alternative to LoadSound/LoopSound is to use PlayMusic. This streams the music from your hard drive and is decompressed as it plays, which may help. The downside is that you have to keep checking if the music has finished playing and then start it up again.

Global musicChannel=PlaySound("music.ogg")

While Not KeyHit(1)
   ; check music every game loop
  If Not ChannelPlaying(musicChannel) Then
     musicChannel=PlaySound("music.ogg")
  EndIf
Wend

PlaySound also consumes less system memory, whereas loading an OGG file could consume as much as 40MB to 100MB of memory, as the file has to be fully decompressed to play. PlaySound uses a much smaller buffer instead, less than 10MB I think, but don't quote me as I can't remember what the consumption was when I tested the two methods out (it was a while ago).

Hope that was of some use.


QuickSilva(Posted 2005) [#4]
Strange. Maybe one of you could provide a small test for me to try on my computer that works fine on yours with a small looping .ogg file. And also a .wav no looping .wav file.

I would be very greatful. As this is annoying me now.

I`m using Fruity Loops 5.02 for my sounds. What are you guys using to save yours? My files work fine in WinAMP but not in Blitz. Maybe they are being saved incorrectly or maybe I am using the wrong settings?

Jason.


Neochrome(Posted 2005) [#5]
music = loadsound("music.ogg"):loopsound(music)
playsound(music)


this works well
for me


QuickSilva(Posted 2005) [#6]
Thats exactly what I am doing but with no luck. Maybe I will try another song file and see if that works.

Jason.


Neochrome(Posted 2005) [#7]
you could use .wav (they are bigger, but they do loadsound) if not, you could use MGP3?

other than that, i found the nearest i could get was a module music


John Blackledge(Posted 2005) [#8]
Just a quick comment on the auto-looping of wav files (without loopsound).

This _has_ happened to me, after I recorded and saved a few wavs from Audio Cleaning Lab (but not with every wav!).

Of course I debugged my code for days, then managed to define it as a) only when produced by ACL, and b) only when played in Blitz.
ACL must somehow have been adding a marker (?) at the end of the wav which caused Blitz to loop.

Finally I had to take each wav into CoolEdit 96 (yes, a 16-bit program!) and chop the last couple of millisecs off, and resave.
Then they all played properly.


Paolo(Posted 2005) [#9]
I don't know why but sometimes when I save an ogg or mp3,
a small delay is created at the beginning or at the end
of the file and this cause the loop to 'fail' but
it is not a Blitz fail,
if you can, try saving the wav as ogg but with another program ...
or try to open the ogg file in a program
where you can see the sound's spectrum to see if it has
a small 'silence section' anywhere ...


Hotcakes(Posted 2005) [#10]
OGG does -NOT- have a gap at beginning or ending of the sample and can be used perfectly for looping. (See footnote 1)

MP3 DOES have a buffer at the beginning and end of each sample and CANNOT be used for looping.

Footnote 1 : It may depend on the program you are using to save the ogg. It may internally use buffers that are translated into the saved file... which would be no good for your purpose. I can confirm that Goldwave saves oggs perfectly... that's the only program I've used. There would obviously be others.

If Fruityloops is saving buffers at the beginning or end of an ogg, maybe you would be better off saving as a wav and converting to ogg in a different program... Or, Fruityloops may save out gaps at the beginning/end of a wav as well. Which would be likely, as it's a complex mixing program with many effect processors screwing with the audio output... it could be saving a small buffer of 'echo' or somesuch at the end of the sample. Probably is in fact. In that case your best bet would be save as wav and use a different program to truncate the sample and then save as ogg.

Never use mp3. Ever. Although I do believe mp2 does not suffer from the buffer...


Damien Sturdy(Posted 2005) [#11]

I`m using Fruity Loops 5.02 for my sounds.


And exporting what type of file from it?


QuickSilva(Posted 2005) [#12]
OK after much playing around I have found that Fruity Loops 5 is in fact the cause of the endlessly looping .wavs even if they are not set to loop in Blitz they still do and cannot be stopped. When saved in a different program or converted to .oggs everything works as it should.

If I load the looping .wav file (that was created with Fruity Loops 5.02) back into FL then on inspection it contains two red markers, one at each end which obviously show that the sample is set to loop. Proper working .wavs produced by other programs do not contain these red markers when loaded into FL which must be embedded in the file somehow but I cannot find a way to turn them off. Has anybody got any ideas apart from using another program. Surely they can be disabled in FL`s. I do not remember this happening with Fruity Loops version 4 so this must be a new feature which hopefully can be swithed on and off.

My question is can these loop markers in FLStudio be disabled as after reading the docs I cannot find an answer.

Thanks for any help,
Jason.


Damien Sturdy(Posted 2005) [#13]
On export, turn of the Acidise option :D


tesuji(Posted 2005) [#14]
The .wav format can be used to embed all sorts of metadata. Many audio editing applications allow you to embed 'loop markers' to specify start & end points for looping.

I've noticed that blitz3d does actually honour these loop markers in .wav files *even* if you don't explicitly use LoopSound, however I've not managed to store a loop marker inside an .ogg file yet and converting wavs to .ogg format appears to strip them out. Loop markers are very useful if you want to loop between arbitrary points in the sample.

If you want to just treat the sample as a one shot either remove the loop markers in a audio editing app or convert the file to .ogg format. I'm not sure if you can override the behavoir from blitz (not looked tbh).


Damien Sturdy(Posted 2005) [#15]
....Or dont export with Acidise WAV enabled :P


QuickSilva(Posted 2005) [#16]
Even with Acidise turned off I`m still getting the same problems :( If you try to load the exported wav back into a Fruity Loops sample channel you will see the red loop markers show up in the sample display. This doesn`t happen with wavs created with other programs if you load them into FL. Surely they can be disabled?

Jason.


tesuji(Posted 2005) [#17]
@QuickSilva

Just downloaded FL 5 and couldn't see an obvious way to remove loop markers either. I think the Acidise thing is a red herring as loop markers can exist fine in non acid samples as well. I use sound forge which can remove loop markers no problem. If you don't have this app I've found another way of doing it with freeware...

Cheap way to remove those pesky loop markers :-

Download Audacity and load in the wav file in question (File->open). The loop markers won't be visible as I don't think Audacity supports them. Now, simply export as wav (or whatever format you want) and this will remove the loop markers for good.

I've tried this myself by creating a looping sample in sound forge, confirming the loopyness in blitz, stripping out the loop data with Audacity and confirming the lack of loopyness in Blitz again.

Hope that helps.


QuickSilva(Posted 2005) [#18]
Thanks it does :) Wonder why FL5 doesn`t allow this though? I might contact the author.

Thanks for all the help everyone.
Jason.


ClayPigeon(Posted 2010) [#19]
I just want to stick this in for anyone in the future with this problem, or just in case Audacity gains support for loop markers. Loop markers can be removed in Wavosaur. Just load the wav into Wavosaur, and go to "Tools >> Loop >> Kill Loop" and your loop markers will disappear. Then, just export as WAV.


Adam Novagen(Posted 2010) [#20]
Thats exactly what I am doing but with no luck.

OF COURSE, silly. Use PlayMusic, and channel manipulation.

Global BGMusic = PlayMusic("[FILE NAME].ogg")

;If you need to stop the BGM, use this:
StopChannel BGMusic



jfk EO-11110(Posted 2010) [#21]
I remember there was a problem with PlayMusic, therefor I wrote a replacement, using Loadsound with a Stack and a function that automaticly removes Sounds that have finished (as PlayMusic does).

I however know the Loop-Pause Problem of OGG and MP3. I think this happens because the soundsystem has to extract and initialize certain things whenever an ogg/mp3 sound (re)starts, something that takes time. I have solved the issue this way:
The length of the sound in Millisecs is stored somewhere. I then store the Millisecs Timer when the Sound starts playing. Every Frame of the Game I will then compare the current Millisecs() to (starttime+length). If it's bigger then I manually restart the sound. You can't do that with a sound when you don't know the length in millisecs, of course.


Adam Novagen(Posted 2010) [#22]
Well, that and the fact that the .MP3 file format automatically inserts a small silence at the beginning of the while. Don't ask me why, it just does. :p


_PJ_(Posted 2010) [#23]
I think this happens because the soundsystem has to extract and initialize certain things whenever an ogg/mp3 sound (re)starts,



Certainly ogg and mp3s requiring decoding in a format not as 'standard' (I couldnt think of a better word) to Windows as 'wav' files would mean they take that miniscule amount of extra time to process, however fast or slight it's still going to be an overhead.

Presumably once loaded, the files still aren't 'processed' until it comes to play them?


jfk EO-11110(Posted 2010) [#24]
It doesn't matter that much if there is an inserted pause at the beginning or at the end. The problem that appears when you loop it is, it will be out of rythm and this sounds bad. When the sound is restarted by code after the right amount of millisecs then the music can hold the rythm and fix the problem almost.