mame icon indicating copy to clipboard operation
mame copied to clipboard

[CD-i] Sound mixing not faithful to the real machine

Open Slamy opened this issue 1 year ago • 11 comments

MAME version

0.270 (mame0270-59-g9c54072486b)

System information

Debian 12

INI configuration details

No response

Emulated system/software

CD-i (cdimono1)

Incorrect behaviour

Multiple mono audio sources are played back with total stereo separation. For example, Zelda - Wand of Gamelon has the BGM on the left and SFX on the right.

Expected behaviour

Zelda - Wand of Gamelon has the BGM and SFX mixed on both channels.

Steps to reproduce

Play some Zelda - Wand of Gamelon and experience it during while playing a 2D level

Additional details

No response

Slamy avatar Oct 04 '24 12:10 Slamy

Do you have reference video from a real CD-i player connected with stereo cables?

rb6502 avatar Nov 24 '24 23:11 rb6502

CC @MooglyGuy

rb6502 avatar Nov 24 '24 23:11 rb6502

Another example is for Kether. In Kether, you will hear French narration in the left channel, and English narration in the right.

This technique was often employed in bilingual audio tracks, however one of the tracks is likely supposed to be muted depending on your chosen language.

Vincent-Halver avatar Jan 04 '25 07:01 Vincent-Halver

@rb6502 I've totally forgotten to provide some example files here. I don't have a good capture card for video so I'll provide some audio instead if this is ok. wand_of_gamelon.zip This is the whole boot process of Zelda Wand of Gamelon on a CD-i 210/05. During this, the game switches various modes.

The first accuracy issue starts at 0:47 as the hardware mixer will fade out the audio when selecting "Start game". This is missing right now in mame as the volume stays the same.

The same happens again at about 1:14 when selecting "Aru Ainu" on the map.

At 1:20 the audio you hear from a real CD-i is both stereo channels mixed to mono as the BGM is hard on the left and the SFX is on the right. On mame you will notice this with headphones.

Edit: At 1:30 things are again interesting. If you crouch by pressing down and press B2, the menu opens. The external mixer is used to lower the volume. This effect is also missing on mame.

Slamy avatar Jan 04 '25 11:01 Slamy

By quick glance DMADAC passes thru cdislavehle device, I presume it's triggering stuff around here for mixing (you can -oslog or enable error.log file writing to countercheck): https://github.com/mamedev/mame/blob/f7f3257411f7432179b18f2be3d311d9cf7b63c1/src/mame/philips/cdislavehle.cpp#L261-L289

Omake: it also imports sound/cdda.h for no actual device hooked up (it does a custom handling with the CD buffers instead).

angelosa avatar Jan 17 '25 19:01 angelosa

I would like to add even more info. I'm not yet 100% sure how it works but by analyzing the source code of other CD-i emulators and also by measuring the the DAC mixer values of a real CD-i on the slave controller, It can be assumed that there are at least 3 commands from the CPU which are taking effect here. Mute and Unmute are causing a slow fade operation which the slave controller does to the DAC mixer automatically. And then there is the Attenuation value which relates to a syscall of the CD-i operating system of the same name. Here some changes which are able to visualize this on the terminal: https://github.com/mamedev/mame/compare/master...Slamy:mame:cdi_mixer

The Attenuation values are also documented in the green book. There is one thing though which is not yet understood by anyone it seems: There are mute commands and unmute commands. Sometimes Unmute is never called by the software, resulting in a silent game. This is probably why the author has decided to ignore the mute command in the first place. It seems that the emulator "CD-i emu" only uses the Attenuation values while also ignoring Mute/Unmute

Slamy avatar Jan 17 '25 22:01 Slamy

  1. what happens if you touch the volume in settings Image
  2. is Red Book supported, I presume CD-Digital Audio would be that? It just stalls if I try to "CD-i play", and that would be already a good test case for your mixing problems here. Image

angelosa avatar Jan 18 '25 14:01 angelosa

  1. Validation Disc 2 fails mixing tests, everything goes hard left and hard right (samples + redbook?), with also a displeasing DC offset on left in between:

Image

Image

angelosa avatar Jan 19 '25 10:01 angelosa

Golf Tips (Europe) has extremely corrupted menu sounds.

Edit: This turned out to be an unrelated issue.

Vincent-Halver avatar Feb 26 '25 23:02 Vincent-Halver

@Vincent-Halver

Golf Tips (Europe) has extremely corrupted menu sounds.

This is a very interesting finding. It's the first time I've heard of this. I've tried it also on other emulators. cdiemu doesn't even start the disc sadly. My MiSTer core has the same issue. Just to be sure I've burned the image to disc and tested on a 210/05. It has a normal click sound. I think that was kinda expected. This problem doesn't correlate with the sound mixing issues but is an interesting one, nonetheless. I wonder if the audiomap data is actually wrong or a weird coding is used, which wasn't yet found on other titles.

Concerning this mixing problem, I have something in the pipeline. I've grabbed the serial data to the DAC mixing circuit on my 210/05 and the result is now this verilog code. The values provided to that circuit from the slave are derived from the attenuation command. I don't know the exact math behind it as the MiSTer core is executing the code of the slave controller instead of using HLE. This has pros and cons for the emulation. The disadvantage is that I can't fake anything.

I'm not yet able to contribute something here as the introduction of the audio mixing is causing issues with certain titles and I don't know how people see regressions on the MAME code base. The MiSTer community knows that the core is experimental. But I try to avoid regressions if possible... As said earlier, the audio mixing during playback of "Audio Channel" based sound relies on the correct state. Sometimes the slave is kept muted and the CPU is not unmuting it.

I don't know how much you know, so I give some insight: The CDIC implementation in MAME is based on the source code of cdiemu. Moogly got the source code under NDA from CD-ifan. The source code is not public but he is willing to share with some people. This is why I suggest joining the Discord server. I've asked him a while ago and his implementation of the CDIC is based on reverse engineering efforts using disassembly of the cdic driver code. My first verilog version of the CDIC was a replication of the MAME codes behavior. My current approach is different and I hope I get some results from that. I've bought a Mono I pcb (the one Mame tries to emulate) and use the stub loader to stimulate the hardware registers. I hope I get more info using that approach...

I do realize that this might be more extreme. To even get there I had to read into CD-i software development and also buy hardware. But I really want to get this right. CD-i emulation is in a bad state and I want to help fixing that.

Slamy avatar Mar 02 '25 11:03 Slamy

This issue is caused because there is a missing component in the audio processing. The cdicdic directly sends decoded audio to the left and right speaker (dmadac) using the transfer() function.

A correct fix will involve a little refactoring to first send the decoded audio to a mixing function. Then, in the cdislavehle, the audio mixing signal which is currently ignored should be sent to the cdicdic so it can be used in mixing.

The Greenbook additionally hints that the CDIC should keep a "real" attenuation level separate from the desired attenuation level. The attenuator can slowly ramp up or down this value to reduce audio artifacts caused by sudden changes in attenuation.

Edit: The master volume settings on the console uses a different function than the mixer. My understanding is that is an additional, final step that adds volume to the output signal, after all mixing is complete. I would recommend that it be considered a separate missing feature than the missing left/right channel mixing.

Vincent-Halver avatar Nov 03 '25 03:11 Vincent-Halver