Sound glitches

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Message
Author
Guest

Sound glitches

#1 Post by Guest » Thu Mar 19, 2009 5:28 am

Hi there!

I'm a new Ren'Py game author (I'll sign up on the guestbook later)., and have already released 2 chapters (with the help of a friend or two). The Ren'Py system is really great, but I have one big problem with it at the moment...

I'm sure this has been asked a thousand times and I'm doing something wrong, but Google and forum searches didn't turn up anything.

I get a lot of sound issues, which I need to work around, but it affects the quality and makes things a lot harder than I think they should be.

Problem 1: Renpy sometimes has problems playing sounds at the same time.

eg 1: If I have music playing, and then play sound effects, the sound effects often have glitches (squeeking sounds, scratches, etc). But when the music is off, the sounds are fine. Changing the volume of the sliders in preferences doesn't seem to help. I don't think it's a clipping issue since softer sounds have the same problem.

What does seem to work is resampling the sound effects to 11,025 Hz. I figured this out when I saw that "punch.wav" from the demo was the only sound in my testing that didn't glitch.

I'm using the latest Ren'Py version on Linux (recent kernel etc), but this problem is also confirmed on Windows Vista.

Is this normal? Is there something else I should be doing?

eg 2: If I play 2 music soundtracks, then there are also glitches, unless I make the one 11,025 khz.

(Actually, the above is my hack to get sounds, like wind, to repeat). Most wind recordings sound terrible at 11,025 khz :-/)

A bit off-topic: Is there a way to sequence sound effects to loop, or into a playlist, similar to music? Or do I need to use a separate music channel for that? I don't mind using embedded Python, but I haven't studied it yet, so any pointers would be welcome.

Problem 2: Get horrible sound glitches with lowered music volume.

I wanted to make the music volume lower (so sound effects are heard more clearly), but seem unable to. I used a simple "music set volume" command at the start of the script, but a lot of sounds start having horrible glitches. So I hacked around the problem in my game by using sound channel raise and lower commands instead.

Interestingly, I don't get the glitches when changing the music volume in the game preferences. Just when I use the volume-setting instruction in the script.

Are these problems normal? Is it an SDL issue? Is there something else I should be doing with the sound mixing to avoid the problem? (hopefully I don't have to always make all sounds 11,025 hz...).

Thanks in advance for your advice. Let me know if you need more info.

-wizzardx.

User avatar
DaFool
Lemma-Class Veteran
Posts: 4171
Joined: Tue Aug 01, 2006 12:39 pm
Contact:

Re: Sound glitches

#2 Post by DaFool » Thu Mar 19, 2009 6:43 am

Hmmm... is your system pretty powerful or is it one of those low-cost ones with integrated everything? On the other hand, could it be a really powerful system? I used to develop on an old Pentium 4 and everything was fine, but later when I replayed by games on a Core 2 Duo I started noticing more glitching on music tracks. My solution would be to encode future music at 128-192kbps instead of 96kbps (which I used because I wanted the games small enough for dialup, at the time).

Usually 44.1khz is standard (I've heard mention of 48khz but that could cause problems later on -- i.e. chipmunks -- when processing) 11khz is imho too low for anything.

Do you keep the sounds as .wav? Have you tried converting them to ogg (like the music) and do they behave the same?

User avatar
wizzardx
Regular
Posts: 72
Joined: Thu Mar 19, 2009 2:35 am
Projects: An Ordinary Life, and various short Ar tonelico VNs
Location: At Frelia's side
Contact:

Re: Sound glitches

#3 Post by wizzardx » Thu Mar 19, 2009 7:09 am

Hi, and thanks for your reply.

My machine is fairly powerful. When I boot to Windows I can play Crysis with full quality settings with a high framerate.

I'm not sure about the Vista person's PC, but I assume so. He plays a lot of games on there, and does a lot of graphic design with Photoshop, etc. I can check if it will help.

Does PyGame work better when all files have the same frequency or bitrate? Or are there particular frequencies or bitrates that have fewer playback issues?

I agree, 11khs is way too low, but I seem forced to use it at the moment for sfx, at least while music is playing.

I've tried different formats (ogg, wav, mp3), and they seem to behave the same. But I can check again (maybe I missed something before).

- wizzardx

EDIT: A bit off-topic, but also related to sound problems:

When I loop sound effects by playing them in a music channel, there is a gap between each loop, which makes things like footstep recordings (meant to be played end to end) sound pretty bad/stilted. My current work-around is to make 1-minute-long recordings by copy & paste in a sound editor. But it does make the sound files a lot larger. Any better ways to do this?

User avatar
DaFool
Lemma-Class Veteran
Posts: 4171
Joined: Tue Aug 01, 2006 12:39 pm
Contact:

Re: Sound glitches

#4 Post by DaFool » Thu Mar 19, 2009 7:48 am

Hmmm.... not sure yet, but I think there's a way sound the default sound buffer (one of the older posts here has the code I think).

Code: Select all

init python hide:
    import os
    os.environ["RENPY_SOUND_BUFSIZE"] = "4096"
Don't know yet what are 'correct' values -- actually just search the forums for 'sound buffer' and you should hit upon all topics that explain the issue (and hinted that the issue would have been solved by newer versions, I guess it's back).
wizzardx wrote: EDIT: A bit off-topic, but also related to sound problems:

When I loop sound effects by playing them in a music channel, there is a gap between each loop, which makes things like footstep recordings (meant to be played end to end) sound pretty bad/stilted. My current work-around is to make 1-minute-long recordings by copy & paste in a sound editor. But it does make the sound files a lot larger. Any better ways to do this?
When you edit the sound and play it repeatedly it should loop smoothly. If not it may be an issue with the recording itself, in which case you just crop a bit more off the tail end of the white noise. I know certain processing programs (Timidity for instance) like to cut-off the audio a few milliseconds than usual.

User avatar
wizzardx
Regular
Posts: 72
Joined: Thu Mar 19, 2009 2:35 am
Projects: An Ordinary Life, and various short Ar tonelico VNs
Location: At Frelia's side
Contact:

Re: Sound glitches

#5 Post by wizzardx » Thu Mar 19, 2009 8:15 am

Eh, I just posted a reply but it's not showing up. Sorry if this is a dupe or my reply ended up somewhere else.
DaFool wrote:Hmmm.... not sure yet, but I think there's a way sound the default sound buffer (one of the older posts here has the code I think).

Code: Select all

init python hide:
    import os
    os.environ["RENPY_SOUND_BUFSIZE"] = "4096"
Don't know yet what are 'correct' values -- actually just search the forums for 'sound buffer' and you should hit upon all topics that explain the issue (and hinted that the issue would have been solved by newer versions, I guess it's back).
Thanks for the tip! I'll give it a try later. Also I'll do that research you suggested.
When you edit the sound and play it repeatedly it should loop smoothly. If not it may be an issue with the recording itself, in which case you just crop a bit more off the tail end of the white noise. I know certain processing programs (Timidity for instance) like to cut-off the audio a few milliseconds than usual.
Interesting info. Which reminds me, often in sound editors, I see a short gap at the end of MP3s. I think that MP3s need to round up to the nearest full "frame". Since I convert wavs (and oggs, as part of my glitch workaround) to MP3s, this might account for the extra gap that's making my looping samples sound bad. I'll check this later.

Also, fyi, here are the programs I've been using for sound editing (mainly for working around renpy sound issues):
- audacity - conversion of ogg to mp3
- lame - conversion of wav and mp3 to mp3 with 11,025 hz frequency
- mp3gain - add regain tags to help make the sound levels even (partly because I can't change music volume at the moment without causing sound glitches).

Anyway, thanks for the info you've provided. I'll look into these things a bit later and report on my success/failure.

Btw, is there an earlier version of Ren'Py I should test which might have fewer sound issues than the latest?

User avatar
PyTom
Ren'Py Creator
Posts: 15893
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Sound glitches

#6 Post by PyTom » Thu Mar 19, 2009 10:03 am

Hm... What is the sampling rate of your audio files? Ren'Py expects 44100hz, and can get confused when that isn't the case.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

User avatar
wizzardx
Regular
Posts: 72
Joined: Thu Mar 19, 2009 2:35 am
Projects: An Ordinary Life, and various short Ar tonelico VNs
Location: At Frelia's side
Contact:

Re: Sound glitches

#7 Post by wizzardx » Thu Mar 19, 2009 10:49 am

PyTom wrote:Hm... What is the sampling rate of your audio files? Ren'Py expects 44100hz, and can get confused when that isn't the case.
There's a lot of variation in the files that I'm using. I didn't know this was bad, is it documented somewhere?

I'll see if changing the sampling rate to 44100hz fixes the problem and then post here again.

User avatar
wizzardx
Regular
Posts: 72
Joined: Thu Mar 19, 2009 2:35 am
Projects: An Ordinary Life, and various short Ar tonelico VNs
Location: At Frelia's side
Contact:

Re: Sound glitches

#8 Post by wizzardx » Thu Mar 19, 2009 6:42 pm

PyTom wrote:Hm... What is the sampling rate of your audio files? Ren'Py expects 44100hz, and can get confused when that isn't the case.
Wow, thanks, this seems to have solved the problem (most of the time). I do still get small glitches every few minutes with some short looping samples (when music is playing) (and other samples don't seem to have the problem at all), but it's far better than before (where it was happening almost constantly).

Should I upload test cases?

Case 1: music is 44,100 hz, but sound isn't, and it glitches constantly on my PC

Case 2: music and sfx are 44,100 hz, but the music channel seems to glitch every few minutes.

Also, using the music channel to loop sfx works correctly when I don't convert to mp3 (presumably giving extra silence at end).

Setting the RENPY_SOUND_BUFSIZE variable to higher values doesn't seem to help (might be because my glitches aren't related to sound skipping, or audio lag, which the forum posts seem to be discussing, or could it also cause weird scratches, pops, and squeeks?).

User avatar
DaFool
Lemma-Class Veteran
Posts: 4171
Joined: Tue Aug 01, 2006 12:39 pm
Contact:

Re: Sound glitches

#9 Post by DaFool » Thu Mar 19, 2009 7:03 pm

How about lower sound buffer values?

User avatar
wizzardx
Regular
Posts: 72
Joined: Thu Mar 19, 2009 2:35 am
Projects: An Ordinary Life, and various short Ar tonelico VNs
Location: At Frelia's side
Contact:

Re: Sound glitches

#10 Post by wizzardx » Thu Mar 19, 2009 7:35 pm

DaFool wrote:How about lower sound buffer values?
Just tested that (with both samples at 44,100), with buffer values between 512 and 8192, and had glitches with all of them. I also tried 256, but got a lot of sound skipping so didn't bother to wait for the usual glitches.

As far as I can tell, the buffer size doesn't have an effect on the glitches.

What I just did now, which seems to have completely fixed the problem (!), is encoded the looping sound effect to mp3 (also 44,100 mhz). It was an ogg before, while the music was mp3 (44,100 mhz).

The other samples I tested with, and which didn't glitch, were also 44,100 mhz mp3s. So I think the problem may be one of:
- mp3s and oggs always don't mix well (even with same frequency. Maybe bitrate or something else besides freq causes problems)
- converting from ogg to mp3 ironed out some other issue renpy was having with the ogg
- ogg playback has some other mixing bug.

Anyway, here are a set of steps which should help other people solve the problem (playing 2 sounds at the same time causes glitches - random pops, squeeks, scratches, etc):

1) Try encoding your files to 44,100 hz

2) Try making your files the same format (eg: don't play mp3 and ogg files at the same time).

3) Try encoding your files so that as many parameters are the same as possible (not just frequency). eg: stereo/mono, kbps, etc.

4) Try using mp3 instead of ogg.

Hopefully by this point the problem should be fixed.

Could someone put this in the renpy docs for future reference? At least until the problem is fixed.

Anyway, thanks a lot DaFool and TomPy! After this thread's discussion my sound problems seem have all been solved (for the moment).

User avatar
EwanG
Miko-Class Veteran
Posts: 711
Joined: Thu Oct 26, 2006 5:37 pm
Location: San Antonio, TX
Contact:

Re: Sound glitches

#11 Post by EwanG » Thu Mar 19, 2009 8:55 pm

wizzardx wrote: - mp3s and oggs always don't mix well (even with same frequency. Maybe bitrate or something else besides freq causes problems)
Most video editors I've worked with have a similar problem in that if you use MP3 for some and Ogg (or WAV) for others, then you get "interesting" results. The one exception to that is Sony Vegas, and that's not exactly a consumer editor :D
Working on something... might even be something good :D

User avatar
PyTom
Ren'Py Creator
Posts: 15893
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Sound glitches

#12 Post by PyTom » Thu Mar 19, 2009 10:45 pm

These sound glitches appear to occur when the length of the file is not an integer sound frame boundary. Unfortunately, I don't know how to fix this.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

User avatar
wizzardx
Regular
Posts: 72
Joined: Thu Mar 19, 2009 2:35 am
Projects: An Ordinary Life, and various short Ar tonelico VNs
Location: At Frelia's side
Contact:

Re: Sound glitches

#13 Post by wizzardx » Fri Mar 20, 2009 1:20 am

PyTom wrote:These sound glitches appear to occur when the length of the file is not an integer sound frame boundary. Unfortunately, I don't know how to fix this.
I've worked a bit with sound files before, but don't know exactly what you mean by "integer sound frame boundary". Could you give a little more detail?

I'm assuming you mean, eg: mp3 files are usually rounded up to a sound frame (so you get a gap on the end sometimes, like I described before), but other files (eg: ogg) aren't always, and those can cause problems during mixing? Could you point out the mixer source code location where this problem appears? (I'm kind of interested now, and maybe one of the developers on this forum can suggest a fix).

Also, if you can't fix it, could you log warnings (during gameplay) or add info to lint telling the developer that there could be a problem with their sound files (when this condition is detected), and suggesting that they do a format conversion/file edit/etc to avoid problems? So they're aware of the problem and can take measures.

User avatar
PyTom
Ren'Py Creator
Posts: 15893
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Sound glitches

#14 Post by PyTom » Fri Mar 20, 2009 1:37 am

The answer is: I'm not 100% sure this is the problem, but this is an educated guess. I don't actually understand the workings of the decoders well enough to be 100% sure. They are also at a level far removed from Python, which would make reporting such errors a pain.

(A second guess would be something like metadata... ID3 tags and the like.)

I'm looking into replacing the whole audio/video infrastructure ASAP, which should hopefully fix this once and for all.

My background is in programming languages. I've self-taught myself a lot about image processing, but audio and video are largely a mystery to me. I'm sort of hoping that if I can move to a more modern library (FFmpeg), I can make the problem simply go away.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

User avatar
wizzardx
Regular
Posts: 72
Joined: Thu Mar 19, 2009 2:35 am
Projects: An Ordinary Life, and various short Ar tonelico VNs
Location: At Frelia's side
Contact:

Re: Sound glitches

#15 Post by wizzardx » Fri Mar 20, 2009 3:29 am

Great, I'm looking forwards to seeing new versions with fewer problems.

In the meanwhile, could you possibly add info for working around playback issues to the online docs? Or maybe just a link to this thread in the sound section.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Hojoo