From 88b96c43e83185942fd0fec64ce94386cd73a39f Mon Sep 17 00:00:00 2001 From: Holger Teutsch Date: Wed, 31 Jul 2024 15:19:56 +0200 Subject: [PATCH] provide sound for XP11 --- Makefile.lin64 | 2 +- Makefile.mgw64 | 11 +++-- Makefile.osxcross | 10 ++-- README.md | 14 +++++- os_sound_xp11.c | 114 ++++++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 135 insertions(+), 16 deletions(-) diff --git a/Makefile.lin64 b/Makefile.lin64 index 4eb9f34..75c8963 100644 --- a/Makefile.lin64 +++ b/Makefile.lin64 @@ -33,7 +33,7 @@ $(TARGET_XP12): $(OBJECTS_XP12) cp -p $(TARGET_XP12) "$(PLUGDIR_XP12)/lin_x64/openSAM.xpl" $(TARGET_XP11): $(OBJECTS_XP11) - $(CC) -o $(TARGET_XP11) $(LNFLAGS) $(OBJECTS_XP11) $(LIBS) + $(CC) -o $(TARGET_XP11) $(LNFLAGS) $(OBJECTS_XP11) $(LIBS) -lopenal mkdir -p "$(PLUGDIR_XP11)/lin_x64" cp -p $(TARGET_XP11) "$(PLUGDIR_XP11)/lin_x64/openSAM.xpl" diff --git a/Makefile.mgw64 b/Makefile.mgw64 index fe7ba8e..db4155c 100644 --- a/Makefile.mgw64 +++ b/Makefile.mgw64 @@ -2,12 +2,13 @@ PLUGDIR_XP12=openSAM-pkg/openSAM PLUGDIR_XP11=openSAM-pkg_XP11/openSAM SDK=../SDK +OPENAL=../libOpenAL32 OBJDIR=./OBJ_win include version.mak TARGET_XP12=$(OBJDIR)/openSAM.xpl -TARGET_XP11=$(OBJDIR)/openSAM.xpl_x11 +TARGET_XP11=$(OBJDIR)/openSAM.xpl_xp11 HEADERS=$(wildcard *.h) SOURCES=openSAM.c os_dgs.c os_jw.c os_ui.c os_anim.c sam_xml.c log_msg.c read_wav.c @@ -27,7 +28,7 @@ LD=gcc CFLAGS+=-DVERSION=\"$(VERSION)\" -g0 -O2 -Wextra -Wall -fdiagnostics-color -Wno-format-overflow -Wno-format-truncation \ -DWINDOWS -DWIN32 -DIBM=1 \ - -I$(SDK)/CHeaders/XPLM -I$(SDK)/CHeaders/Widgets + -I$(SDK)/CHeaders/XPLM -I$(SDK)/CHeaders/Widgets -I$(OPENAL) COMPILE.c = $(CC) $(DEPFLAGS) $(CFLAGS) -c @@ -37,7 +38,7 @@ LIBS=-L$(SDK)/Libraries/Win -lXPLM_64 -lXPWidgets_64 -l:libexpat.a $(OBJDIR)/%.o: %.c $(DEPDIR)/%.d version.mak | $(DEPDIR) $(COMPILE.c) -o $@ $< -install: install_xp12 install_xp11 +install: install_xp12 install_xp11 all: install sam_xml_test.exe @@ -54,8 +55,8 @@ install_xp12: $(TARGET_XP12) cp -p $(TARGET_XP12) "$(PLUGDIR_XP12)/win_x64/." $(TARGET_XP11): $(OBJECTS_XP11) - $(LD) -o $(TARGET_XP11) $(LDFLAGS) $(OBJECTS_XP11) $(LIBS) - + $(LD) -o $(TARGET_XP11) $(LDFLAGS) $(OBJECTS_XP11) $(LIBS) -L$(OPENAL) -lopenal32 + install_xp11: $(TARGET_XP11) mkdir -p "$(PLUGDIR_XP11)/win_x64" cp -p $(TARGET_XP11) "$(PLUGDIR_XP11)/win_x64/openSAM.xpl" diff --git a/Makefile.osxcross b/Makefile.osxcross index 520e3a8..1601abf 100644 --- a/Makefile.osxcross +++ b/Makefile.osxcross @@ -24,12 +24,14 @@ CCa=oa64-clang -mmacosx-version-min=12.0 DEFS= -DAPL=1 -fPIC -fno-stack-protector -fvisibility=hidden -ffast-math -DNDEBUG -CFLAGS+=-O2 -Wall -I$(SDK)/CHeaders/XPLM -I$(SDK)/CHeaders/Widgets -DVERSION=\"$(VERSION)\" $(DEFS) +CFLAGS+=-O2 -Wall -Wno-deprecated-declarations \ + -I$(SDK)/CHeaders/XPLM -I$(SDK)/CHeaders/Widgets -DVERSION=\"$(VERSION)\" $(DEFS) LNFLAGS+=-dynamiclib -shared -rdynamic -fvisibility=hidden -Wl,-exported_symbols_list -Wl,linkscript.mac # https://pewpewthespells.com/blog/static_and_dynamic_libraries.html -LIBS+= -F $(SDK)/Libraries/Mac -framework XPLM -framework XPWidgets /osxcross/target/macports/pkgs/opt/local/lib/libexpat.a +LIBS+= -F $(SDK)/Libraries/Mac -framework XPLM -framework XPWidgets \ + /osxcross/target/macports/pkgs/opt/local/lib/libexpat.a # ships with macOS LIBS+= @@ -58,10 +60,10 @@ $(TARGET_XP12): $(TARGET_XP12)_arm $(TARGET_XP12)_x86 cp -p $(TARGET_XP12) "$(PLUGDIR_XP12)/mac_x64/openSAM.xpl" $(TARGET_XP11)_arm: $(OBJECTS_XP11_arm) - $(CCa) -o $@ $(LNFLAGS) $(OBJECTS_XP11_arm) $(LIBS) + $(CCa) -o $@ $(LNFLAGS) $(OBJECTS_XP11_arm) $(LIBS) -framework OpenAL $(TARGET_XP11)_x86: $(OBJECTS_XP11_x86) - $(CCx) -o $@ $(LNFLAGS) $(OBJECTS_XP11_x86) $(LIBS) + $(CCx) -o $@ $(LNFLAGS) $(OBJECTS_XP11_x86) $(LIBS) -framework OpenAL $(TARGET_XP11): $(TARGET_XP11)_arm $(TARGET_XP11)_x86 lipo -create -output $@ $(TARGET_XP11)_arm $(TARGET_XP11)_x86 diff --git a/README.md b/README.md index 1d75b65..59e7fd7 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ An open source implementation of SAM that drives jetways and provides VDGS and Marshaller service. ## Credits -Jonathan Harris (aka Marginal) (https://github.com/Marginal) for creating Autogate +Jonathan Harris (aka Marginal) (https://github.com/Marginal) for creating Autogate\ @Papickx + @cxn0026 for providing better textures and day + night lighting ## License @@ -28,6 +28,16 @@ https://forums.x-plane.org/index.php?/files/file/90865-opensam-an-open-source-re ### Windows The build process is performed on msys2 with the mingw64 personality.\ Install expat with "pacman -S expat" + +For the XP11 version a linkable OpenAL32.dll was obtained as follows: +- get copy of libOpenAL32.dll e.g. from XP11's dll folder +- pick libOpenAL's *include/AL* header files, e.g. from the msys2 system +- run within a msys2 shell: +``` +gendef OpenAL32.dll +dlltool -d OpenAL32.def -D OpenAL32.dll -k -a -l libopenal32.a -v +``` + make -f Makefile.mgw64 ### Linux @@ -43,5 +53,5 @@ omp install -s -arm64 expat make -f Makefile.osxcross ### macOS on macOS -port install expat +universal +port install expat +universal\ make -f Makefile.mac64 diff --git a/os_sound_xp11.c b/os_sound_xp11.c index 0d6aba7..76ed0b5 100644 --- a/os_sound_xp11.c +++ b/os_sound_xp11.c @@ -2,6 +2,7 @@ openSAM: open source SAM emulator for X Plane Copyright (C) 2024 Holger Teutsch + (C) Jonathan Harris 2013 This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -20,32 +21,137 @@ */ +#include + #include "openSAM.h" #include "os_jw.h" #include "os_jw_impl.h" +#if APL +# include +# include +#else +# include +# include +#endif + +#include + sound_t alert; +static ALuint snd_src; +static ALuint snd_buffer; +static const ALfloat zero[3]; +static int paused; + +static XPLMDataRef audio_dr, paused_dr, view_external_dr; + +static const float GAIN_EXTERNAL = 1.0f; +static const float GAIN_INTERNAL = 0.5f; /* Quieter in internal views */ + +#define CHECKERR(msg) \ + { ALuint e = alGetError(); \ + if (e != AL_NO_ERROR) { \ + {log_msg("%s: %d", msg, e); \ + return 0; } \ + } } + int sound_init() { - return 1; -}; + if (NULL == alcGetCurrentContext()) { + log_msg("cannot open XP11's openAL context"); + return 0; + } + + audio_dr = XPLMFindDataRef("sim/operation/sound/sound_on"); + paused_dr = XPLMFindDataRef("sim/time/paused"); + view_external_dr = XPLMFindDataRef("sim/graphics/view/view_is_external"); + + + alGenSources(1, &snd_src); + CHECKERR("can't create sound source"); + + alGenBuffers(1, &snd_buffer); + CHECKERR("can't generate sound buffer"); + + alBufferData(snd_buffer, alert.num_channels == 2 ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16, + alert.data, alert.size, alert.sample_rate); + CHECKERR("alBufferData"); + + alSourcei(snd_src, AL_BUFFER, snd_buffer); + alSourcef(snd_src, AL_PITCH, 1.0f); + alSourcef(snd_src, AL_GAIN, 1.0f); + alSourcei(snd_src, AL_SOURCE_RELATIVE, AL_TRUE); /* Because we're not allowed to manipulate the listener */ + alSourcei(snd_src, AL_LOOPING, AL_TRUE); + alSourcefv(snd_src, AL_POSITION, zero); + alSourcefv(snd_src, AL_VELOCITY, zero); + if (alGetError()) { + log_msg("sound init error"); + return 0; + } + return 1; +} void alert_on(jw_ctx_t *ajw) { - UNUSED(ajw); + if (0 == snd_src) + return; + + alert_setpos(ajw); + alSourcePlay(snd_src); } void alert_off(jw_ctx_t *ajw) { UNUSED(ajw); + if (0 == snd_src) + return; + + alSourceStop(snd_src); } void alert_setpos(jw_ctx_t *ajw) { - UNUSED(ajw); + if (0 == snd_src) + return; + + const sam_jw_t *jw = ajw->jw; + + // Pause sound while sim is paused + if (XPLMGetDatai(paused_dr)) { + if (!paused) + alSourcePause(snd_src); + paused = 1; + return; + } + + if (paused) { + alSourcePlay(snd_src); + paused = 0; + } + + // Calculate relative location ignoring tilt + XPLMCameraPosition_t camera; + XPLMReadCameraPosition(&camera); + camera.heading *= D2R; + float cos_h = cosf(camera.heading); + float sin_h = sinf(camera.heading); + + float rot1 = RA((jw->rotate1 + jw->psi) - 90.0f); + float dx = jw->x + (jw->extent + jw->cabinPos) * cosf(rot1 * D2R) - camera.x; + float dy = jw->y + jw->height - camera.y; + float dz = jw->z + (jw->extent + jw->cabinPos) * sinf(rot1 * D2R) - camera.z; + + ALfloat snd_rel[3]; + snd_rel[0] = cos_h * dx + sin_h * dz; + snd_rel[2] = -sin_h * dx + cos_h * dz; + snd_rel[1] = dy; + + //log_msg("snd_rel: %0.1f, %0.1f, %0.1f", snd_rel[0], snd_rel[1], snd_rel[2]); + alSourcefv(snd_src, AL_POSITION, snd_rel); + alSourcef(snd_src, AL_GAIN, XPLMGetDatai(view_external_dr) ? GAIN_EXTERNAL : GAIN_INTERNAL); }