Keriew / augustus

An open source re-implementation of Caesar III
GNU Affero General Public License v3.0
1.57k stars 121 forks source link

Surround sound bad audio #194

Closed synthc closed 3 years ago

synthc commented 3 years ago

I just did a fresh install of Augustus using my Ceasar III CD for the game files. The game runs just fine except that the audio level is WAY too high - deafening on the lowest volume level in Windows and clipping horribly.

Opening the mixer in Windows, I can see that the audio level is indeed maxed out (it doesn't pass a certain point regardless of where the volume slider is for the game). Changing my speaker settings to stereo fixes the problem.

Augustus 2.0.1 Using C3 from CD-ROM with the patch (also tried without the patch). Windows 10 Pro 64-bit Sound Blaster Audigy 5/Rx

webfischi commented 3 years ago

Interesting, I don't have an issue with my surround set-up: Sound Blaster Z with Logitech 5.1 surround system. Do you use analog or digital output?

synthc commented 3 years ago

On further testing, it only happens when it's set to 7.1. It seems to be fine with 5.1. I'm using analog output, though that shouldn't matter.

crudelios commented 3 years ago

If I have to guess I'd say SDL doesn't support 8 channel audio, only 6, but I'm not entirely sure that has anything to do with the issue.

Still, @synthc, please try Julius. It uses the newest version of SDL, so maybe that bug is already fixed there. In that case it would simply be a matter of updating SDL for Augustus too.

synthc commented 3 years ago

@crudelios It's exactly the same in Julius.

webfischi commented 3 years ago

Doesn't look "fixed" for me https://bugzilla.libsdl.org/show_bug.cgi?id=248 just "assigned"

crudelios commented 3 years ago

Still, from that report Windows should be supported.

@bvschaik do you have any clue? I think it's SDL related.

bvschaik commented 3 years ago

I have no clue. In Julius/Augustus we don't do anything special with the audio: we just tell SDL_mixer either "play this sound", or "play this sound panned x% right and y% left". After that it's up to SDL_mixer to do the panning and play it on the right set of speakers with the right volume.

crudelios commented 3 years ago

I thought as much... Either this is an SDL bug or something wrong/strange with @synthc's configuration...

I'm not sure if there is anyone else using 7.1 audio to confirm.

synthc commented 3 years ago

Two other things I should mention:

  1. Audio level is extremely high with both 7.1 and 5.1 - I imagine this is just the result of the audio engine duplicating channels.
  2. Clipping is very bad in 7.1 and occurs on the title screen music; however clipping does also occur in 5.1, but is far less noticeable - in the 0 to 1000 pop music, there is some clipping in the left channel during the intro when the horns come in.

I listened to the source audio files of both tracks and levels are normal and there is no clipping. I should note that I'm a sound engineer and I use studio monitors; so both my ears and my speakers are very sensitive to clipping. That said, the clipping in the 7.1 title music is very bad and should be easily noticeable using normal speakers. The 5.1 clipping is much more subtle and may be hard to hear without good speakers/headphones.

crudelios commented 3 years ago

Just to make sure, are you using the latest SDL2 library? I mean, when you tried Julius, did you also replace the SDL2.dll file?

webfischi commented 3 years ago
1. Audio level is extremely high with both 7.1 and 5.1 - I imagine this is just the result of the audio engine duplicating channels.

That would explain why I have to turn the volume down, when I start the game. Playing the raw file in VLC really sounds different. In game ROME1 sounds really heavy, with the big drum at much higher peak. You should do a video showing the clipping in software.

synthc commented 3 years ago

Just to make sure, are you using the latest SDL2 library? I mean, when you tried Julius, did you also replace the SDL2.dll file?

No, I didn't replace any files, just installed and ran it.

crudelios commented 3 years ago

I went to have a look at SDL's source code. It seems SDL converts the stereo audio to a pseudo 5.1 sound, and then from 5.1 to 7.1. They even mention in the source code their conversion can cause clipping.

I think ideally the conversion should keep only the stereo channels and mute the others, but I'm not sure it's possible to do such a thing from our side right now.

Can you please record the audio so I have a sample to add a bug report to SDL?

synthc commented 3 years ago

That's what I'd expect - a two stage upmix. Though I'm surprised there's no limiter to prevent clipping. Does SDL have any preferences at all? Like a limiter, or some way to adjust the gain? Just turning down the gain a bit would solve the problem entirely for surround sound.

Keeping only stereo and muting the others would be possibly worse than just running the game in pure stereo (because upmixing happens and can change the original stereo signal going to the front speakers). The upmixed audio actually sounds quite nice aside from the clipping.

I tried recording the audio, but my sound card only outputs stereo for internal recording, and the game doesn't clip in stereo. I could try recording the speakers themselves with a microphone, but I don't really have a proper mic for that on hand.

crudelios commented 3 years ago

@synthc the only way that SDL provides to adjust gain is already used when setting the volume in the game preferences.

I'd suggest you go to Options -> Sound settings then reduce the volume to about 50% and check if clipping stops. I don't know how else to fix it.

synthc commented 3 years ago

Oh, I didn't realize there was a separate in-game sound menu. I tried adjusting that, but it makes no difference with regard to the clipping. It's a problem with the internal mixer level, I'm sure.

crudelios commented 3 years ago

I'll need to file an issue in the SDL bugtracker, I just haven't gotten around to doing it.

crudelios commented 3 years ago

Since this is not an Augustus but an SDL issue, I did what I could, which was properly reference it on SDL's issue tracker.

As such, I'm closing this issue and hoping it gets fixed on SDL 2.0.16.

1bsyl commented 3 years ago

Hey, Could you check with that commits: See https://github.com/libsdl-org/SDL/issues/4104

crudelios commented 3 years ago

@1bsyl Thanks for working on the issue.

I'll create an Win32 SDL build from the latest github commit and post it here for testing.

crudelios commented 3 years ago

@synthc @webfischi Please try this custom build with the proposed fix by @1bsyl and check if it's solved:

http://public.josecadete.net/C3/augustus-new-sdl.zip

webfischi commented 3 years ago

My feeling says, it sounds different, when I compare 3.0.1 and this build. My DB meter confirms that, 3.0.1 avg is 60 DB and this build shows avg 55 DB, this was non scientific because the measure conditions weren't 100% ideal. I would still wait for @synthc's results, because he uses a 7.1 surround system, which should make the differences more prominent.

synthc commented 3 years ago

I just tested and levels are considerably lower, clipping is fixed, and it sounds better in general (tested on the title music and 0-1000 pop music which was the worst).

I am, however, noticing a new problem with the 0-1000 pop music, which is rapid popping/clicking noises from all channels. It is particularly prominent when woodwinds or brass are playing (quite bothersome). Occasionally there will be a pop and a brief pause in one of the instruments as well. This sounds like buffer underruns to my ears (though there are other things that could cause this) - maybe the audio buffer size needs to be increased? I tested on version 2.0.1 and this issue is not present. The problem is also present when running the game in stereo.

1bsyl commented 3 years ago

@webfischi you use use 5.1 ? maybe the formula should be changed. because the central channel is subtracted twice, which could be the reason why sound is globally lower. Can you retry, patching SDL with:

in file src/audio/SDL_audio.c:

SDL_ConvertStereoTo51 (line 258)

change:
       dst[0] = 0.5f * (lf + (lf - ce));  /* FL */
       dst[1] = 0.5f * (rf + (rf - ce));  /* FR */
to:       
       dst[0] = 0.571f * (lf + (lf - 0.5f * ce));  /* FL */
       dst[1] = 0.571f * (rf + (rf - 0.5f * ce));  /* FR */

SDL_ConvertQuadTo51 (line 288)

same, change:
       dst[0] = 0.5f * (lf + (lf - ce));  /* FL */
       dst[1] = 0.5f * (rf + (rf - ce));  /* FR */
to:       
       dst[0] = 0.571f * (lf + (lf - 0.5f * ce));  /* FL */
       dst[1] = 0.571f * (rf + (rf - 0.5f ce));  /* FR */

@synthc you're using 7.1 ? If so I should double-check this also.

about the underruns: are you on linux ? if so, you can try changing the SDL audio back-end. set the environment variable SDL_AUDIODRIVER to something various, like: also, pulseaudio, pipewire, jack, etc. not sure, about windows backend available ...

(cc @icculus )

crudelios commented 3 years ago

@1bsyl I think both @webfischi and @synthc are using Windows as both reported their findings when I provided the new dll. I'll patch in your suggested changes and provide a new download link.

I believe the buffer underflow is caused by sdl as we haven't changed the audio code lately. Also, for reference, our version 2.0.1 shipped with sdl 2.0.12.

1bsyl commented 3 years ago

ok, so for Windows, there seems to be also various audio backends to try (with same env variable SDL_AUDIODRIVER):

wasapi directsound winmm

1bsyl commented 3 years ago

windows default is wasapi ... if this appeared to be solved when using directsound,

maybe, I would worth double checking those commits ... https://github.com/libsdl-org/SDL/commit/723d01433665c9387c16e5d55f6ca08b7b8db988 https://github.com/libsdl-org/SDL/commit/4c2be472071bf676390214cb942e4569f57a158f https://github.com/libsdl-org/SDL/commit/b98b5adcaea159fc6a9753f808875acf7d3ee945

synthc commented 3 years ago

There are no underruns with directsound - sounds great now. I also tried forcing wasapi via the environment variable just to double check and there were indeed underruns.

1bsyl commented 3 years ago

So, wasapi audio back-end regression seems confirmed. Try reverting the previous commits one by one, to spot the one involved.

crudelios commented 3 years ago

@1bsyl @synthc @webfischi:

Here's a new SDL build: http://public.josecadete.net/C3/augustus-new-sdl.zip

This one has the above mentioned patch.

synthc commented 3 years ago

@crudelios Not sure what you changed, but that build still has underruns with wasapi.

crudelios commented 3 years ago

The above build should somewhat louder than the previous one I made available on 5.1/7.1 without clipping.

1bsyl commented 3 years ago

@crudelios the change is only on 5.1 (stereo to 5.1 and quad to 5.1). Haven't modified 7.1 yet

Enrada commented 3 years ago

Thank you crudelios. I placed this issue in the wrong lcation, so I closed this issue and opened a new issue in correct location:

436

1bsyl commented 3 years ago

@webfischi what about the last version ? is the 5.1 modification gives a better DB average ?