Skip to content

Commit 4357452

Browse files
committed
move soundfont to shared + always install
This simplifies things a bit at the expense that the soundfont is always included/installed (91 kB). Added get_data_path() function for generic handling the path to common data (eg. /usr/local/share/ultragrid if installed). The idea is to catch all cases - installed (run with absolute or relative path) or run from inside the source directory in one place.
1 parent faf9559 commit 4357452

File tree

9 files changed

+98
-69
lines changed

9 files changed

+98
-69
lines changed

.github/scripts/Windows/prepare_msys.sh

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,10 @@ https://github.com/CESNET/GPUJPEG/releases/download/continuous/"$fname"
9999
cp -r GPUJPEG/* /usr/local/
100100
)}
101101

102-
install_soundfont() {
103-
sf_dir="$GITHUB_WORKSPACE/data/Windows/share/soundfonts"
104-
mkdir -p "$sf_dir"
105-
cp "$GITHUB_WORKSPACE/data/default.sf3" "$sf_dir"
106-
}
107-
108102
# Install cross-platform deps
109103
"$GITHUB_WORKSPACE/.github/scripts/install-common-deps.sh"
110104

111105
build_aja_wrapper
112106
install_deltacast
113107
install_gpujpeg
114-
install_soundfont
115108

.github/scripts/macOS/install_others.sh

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,6 @@ install_ndi() {(
7171
printf '%b' "CPATH=$CPATH\n" >> "$GITHUB_ENV"
7272
}
7373

74-
install_soundfont() {(
75-
sf_dir="$srcroot/data/template/macOS-bundle/Contents/share/soundfonts"
76-
mkdir -p "$sf_dir"
77-
cp "$GITHUB_WORKSPACE/data/default.sf3" "$sf_dir"
78-
)}
79-
8074
install_syphon() {
8175
syphon_dst=/Library/Frameworks
8276
(
@@ -98,7 +92,7 @@ if [ $# -eq 1 ] && { [ "$1" = -h ] || [ "$1" = --help ] || [ "$1" = help ]; }; t
9892
fi
9993

10094
if [ $# -eq 0 ] || [ $show_help ]; then
101-
set -- deltacast glfw libbacktrace ndi soundfont syphon ximea
95+
set -- deltacast glfw libbacktrace ndi syphon ximea
10296
fi
10397

10498
if [ $show_help ]; then

Makefile.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@ install: all
694694
$(INSTALL) -d -m 755 $(DESTDIR)$(man1dir)
695695
if [ -n '@MANPAGES@' ]; then $(INSTALL) -m 644 @MANPAGES@ $(DESTDIR)$(man1dir); fi
696696
if [ -n '@DLL_LIBS@' ]; then $(INSTALL) -m 644 @DLL_LIBS@ $(DESTDIR)$(bindir); fi
697+
$(INSTALL) -D -m 644 "$(srcdir)"/share/ultragrid/default.sf3 -t "$(DESTDIR)$(datarootdir)/ultragrid"
697698
if [ -n "@VULKAN@" ]; then\
698699
$(INSTALL) -D -m 644 "$(srcdir)/share/ultragrid/vulkan_shaders/"* -t "$(DESTDIR)$(datadir)/ultragrid/vulkan_shaders"; \
699700
fi
@@ -716,6 +717,7 @@ uninstall:
716717
$(RM) $(DESTDIR)$(datadir)/metainfo/cz.cesnet.ultragrid.appdata.xml;\
717718
$(RM) $(DESTDIR)$(datadir)/pixmaps/ultragrid.png;\
718719
fi
720+
$(RM) $(DESTDIR)$(datarootdir)/ultragrid/default.sf3
719721
if [ -n "@VULKAN@" ]; then\
720722
$(RM) "$(DESTDIR)$(datadir)/ultragrid/vulkan_shaders/"*;\
721723
rmdir $(DESTDIR)$(datadir)/ultragrid/vulkan_shaders;\

data/scripts/Linux-AppImage/create-appimage.sh

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,6 @@ add_fonts() { # for GUI+testcard2
9292
cp "$FONT_PATH" $APPPREFIX/share/fonts
9393
done
9494
done
95-
if ls $APPPREFIX/lib/*mixer* >/dev/null 2>&1 ||
96-
ls $APPPREFIX/lib/ultragrid/*fluidsynth* >/dev/null 2>&1; then
97-
mkdir -p $APPPREFIX/share/soundfonts
98-
cp "$srcdir/data/default.sf3" $APPPREFIX/share/soundfonts/
99-
fi
10095
}
10196

10297
# copy dependencies

src/audio/capture/fluidsynth.c

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
#include "types.h" // for device_info
5757
#include "utils/color_out.h" // for color_printf, TBOLD, TRED
5858
#include "utils/fs.h" // for get_install_root, get_temp_file
59-
#include "utils/macros.h" // for IS_KEY_PREFIX
59+
#include "utils/macros.h" // for ARR_COUNT, IS_KEY_PREFIX
6060

6161
struct module;
6262

@@ -183,40 +183,35 @@ get_soundfont()
183183
if (env_fs != NULL) {
184184
return strdup(env_fs);
185185
}
186-
const bool force_bundled_sf =
187-
getenv("ULTRAGRID_BUNDLED_SF") != NULL &&
188-
strcmp(getenv("ULTRAGRID_BUNDLED_SF"), "1") == 0;
189-
const char *roots[2] = { "/usr", get_install_root() };
190-
if (force_bundled_sf) {
191-
roots[0] = get_install_root();
192-
roots[1] = "/usr";
193-
}
186+
char bundled[MAX_PATH_SIZE];
187+
snprintf_ch(bundled, "%s/%s", get_data_path(), "default.sf3");
194188
const char *sf_candidates[] = {
195-
// without install prefix
196-
"/share/soundfonts/default.sf2",
197-
"/share/soundfonts/default.sf3",
198-
"/share/sounds/sf2/default-GM.sf2",
199-
"/share/sounds/sf3/default-GM.sf3", // Ubuntu
189+
"/usr/share/soundfonts/default.sf2",
190+
"/usr/share/soundfonts/default.sf3",
191+
"/usr/share/sounds/sf2/default-GM.sf2",
192+
"/usr/share/sounds/sf3/default-GM.sf3", // Ubuntu
193+
bundled,
200194
};
201-
for (size_t i = 0; i < sizeof roots / sizeof roots[0]; ++i) {
202-
for (size_t j = 0;
203-
j < sizeof sf_candidates / sizeof sf_candidates[0]; ++j) {
204-
const char *root = roots[i];
205-
const size_t len =
206-
strlen(root) + strlen(sf_candidates[j]) + 1;
207-
char path[len];
208-
strncpy(path, root, len - 1);
209-
strncat(path, sf_candidates[j], len - strlen(path) - 1);
210-
FILE *f = fopen(path, "rb");
211-
debug_msg(MOD_NAME
212-
"Trying to open sound font '%s': %s\n",
213-
path, f ? "success, setting" : "failed");
214-
if (!f) {
215-
continue;
216-
}
217-
fclose(f);
218-
return strdup(path);
195+
196+
const char *force_bundled_sf = getenv("ULTRAGRID_BUNDLED_SF");
197+
if (force_bundled_sf != NULL && strcmp(force_bundled_sf, "1") == 0) {
198+
for (size_t i = ARR_COUNT(sf_candidates) - 1; i > 0; --i) {
199+
sf_candidates[i] = sf_candidates[i - 1];
200+
}
201+
sf_candidates[0] = bundled;
202+
}
203+
204+
for (size_t i = 0; i < ARR_COUNT(sf_candidates); ++i) {
205+
const char *path = sf_candidates[i];
206+
FILE *f = fopen(path, "rb");
207+
debug_msg(MOD_NAME
208+
"Trying to open sound font '%s': %s\n",
209+
path, f ? "success, setting" : "failed");
210+
if (!f) {
211+
continue;
219212
}
213+
fclose(f);
214+
return strdup(path);
220215
}
221216
MSG(ERROR, "Cannot find any suitable sound font!\n");
222217
return NULL;

src/utils/fs.c

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,19 @@
4040
#include "config.h"
4141
#include "config_unix.h"
4242
#include "config_win32.h"
43+
#else
44+
#define SRCDIR ".."
4345
#endif
4446

4547
#include <stdbool.h>
4648
#include <stdio.h>
4749
#include <stdlib.h>
4850
#include <string.h>
51+
#include <sys/stat.h>
4952

53+
#include "debug.h"
5054
#include "utils/fs.h"
55+
#include "utils/macros.h"
5156
#include "utils/string.h"
5257

5358
// for get_exec_path
@@ -63,6 +68,7 @@
6368
#include "host.h" // for uv_argv
6469
#endif
6570

71+
#define MOD_NAME "[fs] "
6672

6773
/**
6874
* Returns temporary path ending with path delimiter ('/' or '\' in Windows)
@@ -165,8 +171,9 @@ get_exec_path(char *path)
165171
* Linux - default "/usr/local", Windows - top-level directory extracted
166172
* UltraGrid directory
167173
*/
168-
const char *get_install_root(void) {
169-
static __thread char exec_path[MAX_PATH_SIZE];
174+
static bool
175+
get_install_root(char exec_path[static MAX_PATH_SIZE])
176+
{
170177
if (!get_exec_path(exec_path)) {
171178
return NULL;
172179
}
@@ -177,12 +184,61 @@ const char *get_install_root(void) {
177184
*last_path_delim = '\0'; // cut off executable name
178185
last_path_delim = strrpbrk(exec_path, "/\\");
179186
if (!last_path_delim) {
180-
return exec_path;
187+
return true;
181188
}
182189
if (strcmp(last_path_delim + 1, "bin") == 0 || strcmp(last_path_delim + 1, "MacOS") == 0) {
183190
*last_path_delim = '\0'; // remove "bin" suffix if there is one (not in Windows builds) or MacOS in a bundle
184191
}
185-
return exec_path;
192+
return true;
193+
}
194+
195+
static bool
196+
dir_exists(const char *path)
197+
{
198+
struct stat sb;
199+
if (stat(path, &sb) == -1) {
200+
return false;
201+
}
202+
return S_ISDIR(sb.st_mode);
203+
}
204+
205+
/**
206+
* returns path with UltraGrid data (path to `share/ultragrid` if run from
207+
* source, otherwise the corresponding path in system if installed or in
208+
* bundle/appimage/extracted dir)
209+
* @retval the path; NULL if not found
210+
*/
211+
const char *
212+
get_data_path()
213+
{
214+
static __thread char path[MAX_PATH_SIZE];
215+
if (strlen(path) > 0) { // already set
216+
return path;
217+
}
218+
219+
const char suffix[] = "/share/ultragrid";
220+
221+
if (get_install_root(path)) {
222+
size_t len = sizeof path - strlen(path);
223+
if ((size_t) snprintf(path + strlen(path), len,
224+
suffix) >= len) {
225+
abort(); // path truncated
226+
}
227+
if (dir_exists(path)) {
228+
MSG(VERBOSE, "Using data path %s\n", path);
229+
return path;
230+
}
231+
}
232+
233+
snprintf_ch(path, SRCDIR "%s", suffix);
234+
if (dir_exists(path)) {
235+
MSG(VERBOSE, "Using data path %s\n", path);
236+
return path;
237+
}
238+
239+
MSG(WARNING, "No data path could have been found!\n");
240+
path[0] = '\0'; // avoid quick cached return at start
241+
return NULL;
186242
}
187243

188244
/**

src/utils/fs.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @author Martin Bela <[email protected]>
55
*/
66
/*
7-
* Copyright (c) 2018-2023 CESNET, z. s. p. o.
7+
* Copyright (c) 2018-2025 CESNET, zájmové sdružení právnických osob
88
* All rights reserved.
99
*
1010
* Redistribution and use in source and binary forms, with or without
@@ -61,8 +61,8 @@ extern "C" {
6161

6262
const char *get_temp_dir(void);
6363
FILE *get_temp_file(const char **filename);
64-
const char *get_install_root(void);
65-
char *strdup_path_with_expansion(const char *orig_path);
64+
const char *get_data_path(void);
65+
char *strdup_path_with_expansion(const char *orig_path);
6666

6767
#ifdef __cplusplus
6868
} // extern "C"

src/video_display/vulkan/vulkan_display.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,6 @@
5151
#include "debug.h"
5252
#include "utils/fs.h"
5353

54-
#ifdef HAVE_CONFIG_H
55-
#include "config.h" // for SRCDIR
56-
#else
57-
#define SRCDIR ".."
58-
#endif // HAVE_CONFIG_H
59-
6054
#define MOD_NAME "[vulkan] "
6155

6256
using namespace vulkan_display_detail;
@@ -663,19 +657,19 @@ void VulkanDisplay::window_parameters_changed(WindowParameters new_parameters) {
663657
std::string
664658
get_shader_path()
665659
{
666-
constexpr char suffix[] = "/share/ultragrid/vulkan_shaders";
660+
constexpr char suffix[] = "/vulkan_shaders";
667661
// note that get_install_root returns bin/.. if run from build,
668662
// which will not contain the shaders for out-of-tree builds
669-
const char *path = get_install_root();
670-
if (path != nullptr) {
671-
std::string path_to_shaders = std::string(path) + suffix;
663+
const char *data_path = get_data_path();
664+
if (data_path != nullptr) {
665+
std::string path_to_shaders = std::string(data_path) + suffix;
672666
std::filesystem::directory_entry dir{ std::filesystem::path(
673667
path_to_shaders) };
674668
if (dir.exists()) {
675669
return path_to_shaders;
676670
}
677671
}
678-
return std::string(SRCDIR) + suffix;
672+
return {};
679673
}
680674

681675
} //namespace vulkan_display

0 commit comments

Comments
 (0)