OpenFodder / openfodder

Open Fodder: An open source port of Cannon Fodder
http://openfodder.com
GNU General Public License v3.0
459 stars 42 forks source link

Distorted audio when many sound effects play at one #84

Closed vanfanel closed 8 months ago

vanfanel commented 8 months ago

Describe the bug When several sound effects play at once, audio gets very distorted. I guess this is because several values add, going beyond expected values.

To Reproduce The easiest way to reproduce is making the enemy HQ explode in Phase 2-2.

Expected behavior Several fx should play at one at high volume without distortion.

Desktop (please complete the following information):

Additional context Sorry for adding so many issues! I love this engine too much :dancing_men:

segrax commented 8 months ago

Hah! As much as I cant really spend the time on it, the bugs need fixing :)

I've just pushed a fix for it, there was a piece of code missing in Sound_Play which restricts the number of sound effects playing and uses a priority to decide which to drop

vanfanel commented 8 months ago

@segrax I have tested the latest GIT version, and in fact there is much less distortion, but on destroying enemy HQ on Phase 2-2, I still hear distortion. Do you hear it too?

Also, in Phase 3-1 (Antartic Adventure) the ambient sound are wrong. Sounds like cracking wood, but should be antartic winds instead...

segrax commented 8 months ago

@vanfanel yeah I can hear it, dont thinki had my sound turned up enough yesterday to notice. Ive pushed another fix for it, now it stops playing existing sounds if they're overriding,

As for the music on phase3, it seems the playback speed is wrong, altho i've got no idea what to do with it.. tried a few guesses adjusting the rate and doubling it makes that track sound slightly closer to original, but breaks all others perhaps a feature in the RJP routine is missing (all RJP code is from ScummVM)

vanfanel commented 8 months ago

@segrax yes, now theres no more distortion! Thanks!

I dont know what could be wrong with the ambient sounds play... Maybe a newer code is available on Scummvm?

segrax commented 8 months ago

The RJP code was just updated a couple of months back, the code in ScummVM hasn't been changed in 3 years though

Just randomly trying things, and discovered that "Track 2" is the correct 'song'.. of course this breaks every other background song :)

segrax commented 8 months ago

@vanfanel its even more complex than i realized. the PC version didn't implement any of this

TileIDs are WORDs, which are always & 0x1FF before being rendered.

It turns out the highest 3 bits are used to change the background music

Track2 is played by default This is from the first mission/first phase, it sets the track to 3. image

Theres a function which compares the distance to the squad leader to an array of these tiles and if in range, will change the track

vanfanel commented 8 months ago

Wow that's crazy! Sorry to have opened the can of worms...

segrax commented 8 months ago

Hah not your fault. i am surprised this has never been noticed before, it really stands out now. When playing M2 Phase1 on Amiga, walk near jungle.. you hear jungle walk near river, you hear water running walk empty area.. you hear nothing

segrax commented 8 months ago

ok. this is much better

https://github.com/OpenFodder/openfodder/assets/1327406/beb64ac8-b43f-4d59-adff-23136360568d

https://github.com/OpenFodder/openfodder/assets/1327406/ba5aeee0-92c0-4719-a164-c9743e7a915f

vanfanel commented 8 months ago

@segrax Just built the latest version and indeed it sounds much better now! Ambient sounds seem spot-on when compared against Amiga!

Only helicopter sound seems different: on Amiga it simply fades out, while in openfodder it repeats with a dropout in the middle. I have recorded the Amiga sound:

https://github.com/OpenFodder/openfodder/assets/837585/6b721757-4540-4c1e-beb8-ecfc0db1d2ac

Another difference is that the cursor seems to move smoothly on Amiga, while on openfodder cursor movement is way less smooth, like on PC. My guess is that, on Amiga, it's a hardware cursor moving independently from the game engine, right?

segrax commented 8 months ago

Yeah, the Amiga version has part of the "Phase Loop" running in an interrupt

This includes updating mouse x/y from the registers, updating the camera, and the music fading routine implemented yesterday

Although I am wondering now what could be done to "simulate" this

segrax commented 8 months ago

regarding the helicopter noise, that has been mentioned before, and i can only guess there must be some channel manipulation going on, There is no separate noise for it, its just one track/song playing

vanfanel commented 8 months ago

Regarding the mouse, maybe the cursor could be updated on every screen update (60 times per second) while the game itself would be updated only "when needed"? I believe that would look more like the Amiga game. But maybe the engine architecture doesn't allow that?

segrax commented 8 months ago

i pushed some changes yesterday for helicopter screen, but don't think it changes the issue you mentioned.. but, i cant even really hear the issue :) the helicopter moves the same as the Amiga version now though, and various music tracks fade in/out

vanfanel commented 8 months ago

Ah yes!! I have built today's GIT code and indeed songs now fade in and out as in Amiga! Great improvemente, these details make the engine SO Amiga-like :+1:

About the cursor movement, updating it with every frame wouldn't be enough to make it move as smooth as on Amiga?

segrax commented 8 months ago

currently the cursor is updated every frame, but it appears the amiga version updates the cursor on the screen every time the interrupt fires

segrax commented 8 months ago

https://github.com/OpenFodder/openfodder/assets/1327406/d5917a69-c35b-46e8-98c5-6c459b7a73a5

ok some big changes.. expecting bugs.

camera panning is much smoother now. feels like playing under winuae

vanfanel commented 8 months ago

@segrax Yes, camera and cursor move much smoother now! However, smoothness during the scrolling sequences is totally absent now. Maybe the game is not synchronized to display refresh anymore?

segrax commented 8 months ago

@vanfanel is this better?

https://github.com/OpenFodder/openfodder/assets/1327406/e2e69377-ed56-4bf8-b7cb-e4065c59342f

vanfanel commented 8 months ago

@segrax that looks smother, at least on video. But I would have to see it running here to actually see how smooth it is, videos are never the same as the real thing in regards to screen refresh synchronization.

Tried to git pull but apparently the new code isn't there yet, am I right?

segrax commented 8 months ago

@vanfanel just pushed now, had some issues to fix first

vanfanel commented 8 months ago

Ah, that's much better! Except the wrong values are there again for the "middle" and "back" layer increment in https://github.com/OpenFodder/openfodder/blob/d8237348714515361973b99aac554e0c6d463141/Source/Amiga/Graphics_Amiga.cpp#L1584 and https://github.com/OpenFodder/openfodder/blob/d8237348714515361973b99aac554e0c6d463141/Source/Amiga/Graphics_Amiga.cpp#L1589

These values should be word_42871 += 2; for Middle and word_42873 += 1; for Back, as in this previously merged PR:

https://github.com/OpenFodder/openfodder/pull/79/commits

Also, you may have noticed, but soldiers on the hill scene are now FAST, like Sonic the Hedgehog levels of fast :)

segrax commented 8 months ago

Yeah i changed it while adjusting everything, the 4,3,2,1 combination is closer to the Amiga.

The hill screen is now implemented identical to the Amiga version, if you set WinUAE CPU Emulation Speed to "Fastest possible", you'll see it

put some side by side demos together,

"nosync" is using 50Hz timing between frame changes (VSYNC is still enabled, ) "sync" is running just on VSYNC

https://github.com/OpenFodder/openfodder/assets/1327406/03d8f34f-d8e1-42b1-be1e-90a02a902d19

https://github.com/OpenFodder/openfodder/assets/1327406/227c6992-2c73-4787-805b-50cb1e2148d3

https://github.com/OpenFodder/openfodder/assets/1327406/b85c6353-d9c0-4aae-8200-48f23263f849

https://github.com/OpenFodder/openfodder/assets/1327406/f6aced7b-2b28-4ea8-9975-0ca0d87e797f

segrax commented 8 months ago

I was supposed to post that 30mins ago, since then.. ive gone further and implemented the background movement routine from the Amiga version,

now its accurate in movement to the frame... except its moving in reverse 🤣

segrax commented 8 months ago

https://github.com/OpenFodder/openfodder/assets/1327406/a6af4f43-5858-494e-9007-ddb4eb8907ac

segrax commented 8 months ago

text is now scrolling ;)

https://github.com/OpenFodder/openfodder/assets/1327406/f503f4bd-7d24-4eeb-86f8-76dbaf2dbf65

vanfanel commented 8 months ago

@segrax Can you believe I hadn't noticed the scrolling text on the Amiga version? Even if I DO have the Amiga version right here on the MiSTer! But now, scrolling looks perfect again!

I have however found that the "Lost in service" scene shows very heavy flickering. Only the "Lost in Service" scene seems affected. I tried to record a video, but it's only noticeable on the actual game.

segrax commented 8 months ago

think alot of people have never noticed, and i was happy with that because i was tired of working on it :D

segrax commented 8 months ago

also the music bug is now fixed, the "pop pop pop" sound (quote: @drnovice :)) in the helicopter screen. RJP decoder bug (fix submitted upstream, https://github.com/scummvm/scummvm/pull/5722)

drnovice commented 8 months ago

Thank you so much for the effort @segrax , hope this investigation will help the RJ Player to play better with other games too, it's a small diamond of the old school games anyway! :)

vanfanel commented 8 months ago

@segrax I have just built from latest sources and: -Helicopter sound is EXACT to Amiga now! -Scrolling is perfect! -"Lost in service" scene is fixed!

Only ultra-fast recruit speed on the hill scene seems to remain. This is totally Amiga-like.

segrax commented 8 months ago

@vanfanel yeah im unsure what to do about the hill yet, will think about it today :D