Open Vort opened 7 months ago
Please try again with latest code.
I still see problems # 1 and # 2 with 2979ba4. I wasn't able to get crash however.
Race condition happens at exactly the same place as before.
Most likely, because of samples made by fmopl_generator
.
I think the only proper long-term solution is to integrate resample code in a way similar to softfloat. In short-term, however, some hacks may be available, not sure.
Yes, my fix only handles the PCM part of the sound emulation. I'll have a look how to stop the FM and speaker input of the mixer code. You are right, the real bugfix for all platforms would be some resample code in Bochs. Since the default sample rate is 44100 Hz (CD audio quality), resampling for 11025 and 22050 Hz should be relatively easy to implement.
Blackthorne uses not 11025 and 22050, but 11111 and 22222. Also I noticed unusual sample rates in the past while was testing various other software. For example, Impulse Tracker uses 45454. So there will be no easy way I guess.
Arbitrary resampling can be done with filter banks somehow. Some info is here (and in code of GNU Radio of course). Book mentioned there is here (archive), chapter 7.5 starts at page 171 (187). I tried this algorithm in other project and it worked, but I forgot already all details (math knowledge disappears from my brain very quickly for some reason).
I decided to make small program demonstrating arbitrary resampling. Here is source code ArbResamp_src.zip, binary and data ArbResamp_bin.zip. Program is made in C#, but I hope it is similar enough to C++ to be understood properly.
Upon execution, it resamples two files:
sine_1000.wav
, 1000 Hz sine wave, from 2222 Hz to 3000 Hz sample rate.bt33.wav
, explosion sample from the game (can be heard at 1:15 in video), from 11111 Hz to 44100 Hz.Quality of resampling can be estimated by looking at spectrograms made by SoX:
Two effects can be seen there:
if (sampleIndex >= 0 && sampleIndex < inData.Length)
. For realtime processing, same property means that output data will have some lag.bt33_rs
spectrogram spike can be seen. It happens because resampled data may have higher amplitude than original data and clipping is used to fit data into 16 bit range: if (sum < -1.0)
.These spectrograms were made with const int filterLength = 511;
. Having lower tap count, for example, 51
will produce visible artifacts:
Visible artifacts, however, does not mean audible artifacts, so with this option delay, quality and CPU usage can be controlled at the same time.
Since this program is small, it is also slow. With polyphase filter banks results of costly Sin
function calls can be cached, at the cost of slightly worse resampling quality.
When playing Blackthorne game on Windows host, race condition in sound code may happen. It results in one of three consequences:
waveOutPrepareHeader(): error = 5
messages are displayed.Problem reproduces most reliably in such scenario:
https://github.com/bochs-emu/Bochs/assets/1242858/7781460e-de86-4f9f-b9a2-65f94f2cd7bb
During its execution, game switches between 11kHz and 22kHz output, which generates race condition between
bx_soundlow_waveout_win_c::set_pcm_params
(waveOutOpen
) andbx_soundlow_waveout_win_c::output
(waveOutPrepareHeader
) functions.Test files: bthorne.zip. Version: 1ff88fdd05a9b6640029fa0ba04304d58e833b3e.