Skip to content

Commit

Permalink
provide sound for XP11
Browse files Browse the repository at this point in the history
  • Loading branch information
hotbso committed Jul 31, 2024
1 parent 25335ac commit 88b96c4
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Makefile.lin64
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
11 changes: 6 additions & 5 deletions Makefile.mgw64
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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"
Expand Down
10 changes: 6 additions & 4 deletions Makefile.osxcross
Original file line number Diff line number Diff line change
Expand Up @@ -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+=
Expand Down Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
114 changes: 110 additions & 4 deletions os_sound_xp11.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,32 +21,137 @@
*/

#include <stddef.h>

#include "openSAM.h"
#include "os_jw.h"
#include "os_jw_impl.h"

#if APL
# include <OpenAL/al.h>
# include <OpenAL/alc.h>
#else
# include <AL/al.h>
# include <AL/alc.h>
#endif

#include <XPLMCamera.h>

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);
}

0 comments on commit 88b96c4

Please sign in to comment.