mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-02 09:36:48 +01:00
b7f2186a19
* fix(audio): the sound comes back after the output device is lost or changed
Pulling out the headphones, or changing the output in the system, could leave the game silent until it was
restarted, also after the very same output came back. The renderer learned about the output only from the
events of the system (ALC_SOFT_system_events), and the check of the connection ran only without them. But:
- the system doesn't report every loss. A stream invalidated without the device being removed and without
another default output (a jack pulled out of a shared output, a change of the format, exclusive mode taken
by another program) only marks the device as disconnected. Nothing looked at that, so nothing reopened it;
- alcReopenDeviceSOFT returns ALC_TRUE also when the new output was opened but failed to start, which happens
when the default output is reported before it is ready. The log said "reopened", the device stayed dead;
- a reopen that failed was never tried again, in spite of "(will retry)" in the log;
- with a device set in eu07.ini (sound.openal.renderer) nothing was followed at all: once that device lost its
output it stayed silent, and a device absent at the start meant no sound for the whole session.
Now the connection of the device is checked once a second next to the events, a removed device has it checked
at once, a reopen counts only if the device is connected afterwards, and a failed one is tried again after
0.5, 1, 2, 4 and then every 4 s, and at once when the system reports any change. A device set in eu07.ini is
left alone while it works; when it is lost it is opened again, the default output stands in for it while it is
absent (also at the start), and the playback moves back when it returns. The failure is logged once per loss.
The decisions are in audio::output_follower (audio/outputfollow.h), free of OpenAL, with unit tests. The time
is the real one (steady_clock) rather than the simulation's, which stands still in pause and runs faster with
time acceleration.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QRgQ1GsKSGmguyc4rhvXWy
* Revert "fix(physicslog): no road-vehicle logs, formatting on the writer, a slow disk and the closing kept short (#408)"
This reverts commit d6c3593520.
* fix(audio): no gaps from devices that aren't the one set, the move back retried, rarer attempts after a minute
Answers the review of #433:
- A move back to the device set in eu07.ini that opened but didn't start
left no output at all (the library lets go of the default output before it
tries the other one). The default output is opened again at once.
- The device set in eu07.ini is tried only when the library lists it, so a
device of any other kind coming (USB, Bluetooth, HDMI of a monitor) no
longer breaks the sound for a moment.
- With no output at all the attempts thin out to every 30 s after the first
minute. An event of the system still has one made at once, but not sooner
than 250 ms after the last one, as a device coming reports a burst.
And two faults found on the way:
- A device name with Polish letters, written in code page 1250 by the
starter, was never found again once the device had been absent: it was
passed to the library as it was. It is matched as 1250 text too, with or
without the "OpenAL Soft on " prefix of the library.
- A move back that failed (the device reported before it was ready) wasn't
tried again until another device came. It is tried again after 1, 2 and 4 s.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* feat(audio): the log says which OpenAL library the game got and whether the sound follows the output with it
At the start the game writes one line naming the OpenAL library it plays through, its version and the file it
was loaded from (OpenAL32.dll of the game folder or of the system). With OpenAL Soft 1.23 or newer the line
says the sound follows the default output of the system, or the device set in eu07.ini
(sound.openal.renderer). With an older one, such as the 1.18.1 the game folder still has, nothing changes: the
sound stays on the device opened at the start, and the line says why and what library follows the output.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: jerrrrycho <jurek.pakula@gmail.com>
223 lines
8.2 KiB
C++
223 lines
8.2 KiB
C++
/*
|
|
This Source Code Form is subject to the
|
|
terms of the Mozilla Public License, v.
|
|
2.0. If a copy of the MPL was not
|
|
distributed with this file, You can
|
|
obtain one at
|
|
http://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include "audio/audio.h"
|
|
#include "audio/outputfollow.h"
|
|
#include "audio/fmradio.h"
|
|
#include "audio/startsample.h"
|
|
#include "model/ResourceManager.h"
|
|
#include "application/uitranscripts.h"
|
|
|
|
// Dodaj brakujące includy
|
|
#include <list>
|
|
#include <stack>
|
|
#include <cstdint>
|
|
#include <atomic>
|
|
#include <optional>
|
|
#include <string_view>
|
|
|
|
// Definicje dla OpenAL 1.25 jeśli brakuje
|
|
#ifndef ALC_CONNECTED
|
|
#define ALC_CONNECTED 0x313
|
|
#endif
|
|
|
|
#ifndef ALC_DEFAULT_ALL_DEVICES_SPECIFIER
|
|
#define ALC_DEFAULT_ALL_DEVICES_SPECIFIER 0x1012
|
|
#endif
|
|
|
|
#define EU07_SOUND_PROOFINGUSESRANGE
|
|
|
|
class sound_source;
|
|
|
|
using uint32_sequence = std::vector<std::uint32_t>;
|
|
|
|
enum class sound_category : unsigned int {
|
|
unknown = 0,
|
|
vehicle,
|
|
local,
|
|
ambient,
|
|
};
|
|
|
|
struct sound_properties {
|
|
glm::dvec3 location;
|
|
float pitch { 1.f };
|
|
sound_category category { sound_category::unknown };
|
|
float gain { 1.f };
|
|
float soundproofing { 1.f };
|
|
// the soundproofing of what stands between the sound and the listener, without the volume trim of the sounds placed
|
|
// on top of the listener: what the low-pass filter muffles the sound by (sound.efx.lowpass)
|
|
float soundproofing_hf { 1.f };
|
|
std::uintptr_t soundproofing_stamp { ~std::uintptr_t{0} };
|
|
};
|
|
|
|
enum class sync_state {
|
|
good,
|
|
bad_distance,
|
|
bad_resource
|
|
};
|
|
|
|
namespace audio {
|
|
|
|
struct openal_source {
|
|
|
|
friend class openal_renderer;
|
|
|
|
using buffer_sequence = std::vector<audio::buffer_handle>;
|
|
|
|
ALuint id { audio::null_resource };
|
|
ALuint last_buffer { audio::null_resource }; // the buffer queued last, the one left playing once those before it are done
|
|
sound_source *controller { nullptr };
|
|
uint32_sequence sounds;
|
|
int sound_index { 0 };
|
|
bool sound_change { false };
|
|
bool is_playing { false };
|
|
bool is_looping { false };
|
|
sound_properties properties;
|
|
sync_state sync { sync_state::good };
|
|
|
|
openal_source() = default;
|
|
|
|
template <class Iterator_>
|
|
openal_source &bind( sound_source *Controller, uint32_sequence Sounds, Iterator_ First, Iterator_ Last );
|
|
|
|
void play();
|
|
void update( double const Deltatime, glm::vec3 const &Listenervelocity );
|
|
void sync_with( sound_properties const &State );
|
|
void stop();
|
|
void loop( bool const State );
|
|
void range( float const Range );
|
|
void pitch( float const Pitch );
|
|
void clear();
|
|
|
|
private:
|
|
double update_deltatime { 0.0 };
|
|
float pitch_variation { 1.f };
|
|
float sound_range { 50.f };
|
|
glm::vec3 sound_distance { 0.f };
|
|
glm::vec3 sound_velocity { 0.f };
|
|
bool is_in_range { false };
|
|
bool is_multipart { false };
|
|
};
|
|
|
|
class openal_renderer {
|
|
|
|
public:
|
|
openal_renderer() = default;
|
|
~openal_renderer();
|
|
|
|
audio::buffer_handle fetch_buffer( std::string const &Filename );
|
|
audio::openal_buffer const &buffer( audio::buffer_handle const Buffer ) const;
|
|
// makes buffers of all sound files still decoded in the background (sound.asyncload), waiting for them
|
|
void complete_buffers();
|
|
// the sound buffers and the bytes of their data, for the debug panel
|
|
memory_count buffers_memory() const { return m_buffers.memory(); }
|
|
|
|
bool init();
|
|
|
|
template <class Iterator_>
|
|
void insert( Iterator_ First, Iterator_ Last, sound_source *Controller, uint32_sequence Sounds ) {
|
|
m_sources.emplace_back( fetch_source().bind( Controller, Sounds, First, Last ) );
|
|
}
|
|
|
|
void erase( sound_source const *Controller );
|
|
void update( double const Deltatime );
|
|
// provides access to the broadcast radio receiver
|
|
audio::fm_radio &fmradio() { return m_fmradio; }
|
|
|
|
// choices the library offers, for the settings
|
|
std::vector<std::string> output_devices() const;
|
|
std::vector<std::string> hrtf_profiles() const;
|
|
std::vector<std::string> resamplers() const;
|
|
// what the device ended up with: device, mixing rate, output mode, HRTF, resampler
|
|
std::string device_status() const;
|
|
// applies changed device options (sound.openal.renderer, outputmode, hrtf, limiter, frequency)
|
|
// without a restart. returns: false if the library can't do it
|
|
bool apply_device_settings();
|
|
// applies changed source options (sound.resampler, doppler, efx.*) to every source
|
|
void apply_source_settings();
|
|
// sets the EFX low-pass filter of a source for given soundproofing (sound.efx.lowpass)
|
|
void soundproofing_filter( ALuint const Source, float const Soundproofing ) const;
|
|
|
|
glm::dvec3 cached_camerapos;
|
|
|
|
private:
|
|
using source_list = std::list<audio::openal_source>;
|
|
using source_sequence = std::stack<ALuint>;
|
|
|
|
bool init_caps();
|
|
void init_extensions();
|
|
void log_output_following( std::string_view Version, bool Pinned ) const;
|
|
std::vector<ALCint> device_attributes() const;
|
|
int hrtf_index( std::string const &Name ) const;
|
|
ALint resampler_index() const;
|
|
void configure_source( ALuint const Source ) const;
|
|
// keeps the sound on an output that works, see audio::output_follower
|
|
void follow_output();
|
|
void reopen_output( output_follower::clock::time_point const Now );
|
|
void restore_preferred_output( output_follower::clock::time_point const Now );
|
|
void reopen_default_output( bool const Pinned, output_follower::clock::time_point const Now );
|
|
std::optional<std::string> find_preferred_output() const;
|
|
bool reopen_output_on( ALCchar const *Devicename );
|
|
bool output_connected() const;
|
|
std::string output_name() const;
|
|
audio::openal_source fetch_source();
|
|
|
|
ALCdevice *m_device { nullptr };
|
|
ALCcontext *m_context { nullptr };
|
|
bool m_ready { false };
|
|
glm::vec3 m_listenervelocity;
|
|
bool m_freeflymode { true };
|
|
bool m_windowopen { true };
|
|
int m_activecab { 0 };
|
|
|
|
buffer_manager m_buffers;
|
|
source_list m_sources;
|
|
source_sequence m_sourcespares;
|
|
audio::fm_radio m_fmradio; // streamed sound, maintained separately from regular emitters
|
|
|
|
// OpenAL Soft extension entry points, resolved at runtime via al(c)GetProcAddress
|
|
LPALDEFERUPDATESSOFT m_alDeferUpdatesSOFT { nullptr };
|
|
LPALPROCESSUPDATESSOFT m_alProcessUpdatesSOFT { nullptr };
|
|
LPALCDEVICEPAUSESOFT m_alcDevicePauseSOFT { nullptr };
|
|
LPALCDEVICERESUMESOFT m_alcDeviceResumeSOFT { nullptr };
|
|
LPALCREOPENDEVICESOFT m_alcReopenDeviceSOFT { nullptr };
|
|
LPALCRESETDEVICESOFT m_alcResetDeviceSOFT { nullptr };
|
|
LPALCGETSTRINGISOFT m_alcGetStringiSOFT { nullptr };
|
|
LPALGETSTRINGISOFT m_alGetStringiSOFT { nullptr };
|
|
LPALGENFILTERS m_alGenFilters { nullptr };
|
|
LPALDELETEFILTERS m_alDeleteFilters { nullptr };
|
|
LPALFILTERI m_alFilteri { nullptr };
|
|
LPALFILTERF m_alFilterf { nullptr };
|
|
|
|
bool m_hasoutputmode { false }; // ALC_SOFT_output_mode
|
|
bool m_haslimiter { false }; // ALC_SOFT_output_limiter
|
|
bool m_hasefx { false }; // ALC_EXT_EFX, with its filter entry points
|
|
ALuint m_lowpassfilter { 0 }; // shared low-pass filter, its parameters are copied to a source when attached
|
|
ALint m_resampler { -1 }; // index of the resampler set on the sources, or -1 to leave the library's default
|
|
|
|
bool m_candetectdisconnect { false };
|
|
std::vector<ALCint> m_contextattributes; // cached for device reopen
|
|
output_follower m_output; // decides when the device is opened again
|
|
|
|
// ALC_SOFT_system_events: the system reports output devices coming, going and the default one changing.
|
|
// Used next to the periodic check of the connection, not in place of it: the events catch a change of the
|
|
// default output at once (e.g. re-plugging headphones), the check catches the losses the system doesn't report.
|
|
LPALCEVENTCONTROLSOFT m_alcEventControlSOFT { nullptr };
|
|
LPALCEVENTCALLBACKSOFT m_alcEventCallbackSOFT { nullptr };
|
|
bool m_usedeviceevents { false };
|
|
std::atomic<unsigned> m_outputevents { 0u }; // bits of output_follower::event, set from the (possibly off-thread) event callback
|
|
static void ALC_APIENTRY device_event_callback( ALCenum eventtype, ALCenum devicetype, ALCdevice *device, ALCsizei length, ALCchar const *message, void *userparam ) noexcept;
|
|
};
|
|
|
|
extern openal_renderer renderer;
|
|
extern bool event_volume_change;
|
|
|
|
} // namespace audio
|