From fea857478425a10ee285818e1c987d1efdbb6a5c Mon Sep 17 00:00:00 2001
From: David Roman <davidroman96@gmail.com>
Date: Tue, 30 Jan 2024 23:34:48 +0100
Subject: [PATCH 1/3] Add CmakeLists.txt file to compile phrazor

Closes: #828
---
 CMakeLists.txt | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
 create mode 100644 CMakeLists.txt

diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 000000000..cf79faf5b
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,19 @@
+cmake_minimum_required(VERSION 3.10)
+project(phazor)
+
+add_library(phazor SHARED src/phazor/phazor.c)
+
+find_package(PkgConfig REQUIRED)
+
+pkg_check_modules(phazor REQUIRED flac libopenmpt libmpg123 opus opusfile wavpack samplerate)
+
+find_package(kissfft CONFIG COMPONENTS float)
+if(NOT kissfft_FOUND)
+	add_subdirectory(src/phazor/kissfft)
+endif()
+
+target_include_directories(phazor PRIVATE ${phazor_INCLUDE_DIRS})
+target_link_libraries(phazor kissfft::kissfft ${phazor_LIBRARIES})
+
+
+install(TARGETS phazor DESTINATION ${CMAKE_SOURCE_DIR}/lib)

From 44785f42480b7e53fda707b839153c3665ae4df0 Mon Sep 17 00:00:00 2001
From: David Roman <davidroman96@gmail.com>
Date: Thu, 1 Feb 2024 19:35:58 +0100
Subject: [PATCH 2/3] Add setup.py and fix install_directory for Linux installs

---
 setup.py            | 16 ++++++++++++++++
 t_modules/t_main.py |  4 ++++
 tauon.py            | 15 +++++++++------
 3 files changed, 29 insertions(+), 6 deletions(-)
 create mode 100644 setup.py

diff --git a/setup.py b/setup.py
new file mode 100644
index 000000000..dbfa6257a
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,16 @@
+from setuptools import setup, find_packages, Extension
+
+with open('requirements.txt') as f:
+    required = f.read().splitlines()
+
+setup(
+    name = 'Tauon Music Box',
+    packages = find_packages(),
+    version = '7.7.2',
+    url = 'https://tauonmusicbox.rocks/',
+    license = 'GPL-3',
+    install_requires=required,
+	classifiers=[
+		'Programming Language :: Python :: 3'
+	],
+)
diff --git a/t_modules/t_main.py b/t_modules/t_main.py
index ebbd325b8..7cbb9782c 100644
--- a/t_modules/t_main.py
+++ b/t_modules/t_main.py
@@ -141,6 +141,10 @@
 # if system == "windows" or msys:
 #     os.environ["PYSDL2_DLL_PATH"] = install_directory + "\\lib"
 
+# Assume that it's a classic Linux install, use standard paths
+if install_directory.startswith("/usr/"):
+    install_directory = "/usr/share/TauonMusicBox"
+
 # Set data folders (portable mode)
 user_directory = install_directory
 config_directory = user_directory
diff --git a/tauon.py b/tauon.py
index 6dec8f340..8c502a1a3 100755
--- a/tauon.py
+++ b/tauon.py
@@ -78,11 +78,6 @@ def transfer_args_and_exit():
      os.environ["PATH"] += ":" + sys._MEIPASS
      os.environ['SSL_CERT_FILE'] = os.path.join(install_directory, "certifi", "cacert.pem")
 
-user_directory = os.path.join(install_directory, "user-data")
-config_directory = user_directory
-
-asset_directory = os.path.join(install_directory, "assets")
-
 # If we're installed, use home data locations
 install_mode = False
 if install_directory.startswith("/opt/")\
@@ -91,6 +86,14 @@ def transfer_args_and_exit():
         or install_directory.startswith("/snap/") or sys.platform == "darwin" or sys.platform == 'win32':
     install_mode = True
 
+# Assume that it's a classic Linux install, use standard paths
+if install_directory.startswith("/usr/"):
+    install_directory = "/usr/share/TauonMusicBox"
+
+user_directory = os.path.join(install_directory, "user-data")
+config_directory = user_directory
+asset_directory = os.path.join(install_directory, "assets")
+
 if install_directory.startswith("/app/"):
     # Its Flatpak
     t_id = "com.github.taiko2k.tauonmb"
@@ -345,7 +348,7 @@ def transfer_args_and_exit():
 del flags
 del img_path
 
-if pyinstaller_mode or sys.platform == "darwin":
+if pyinstaller_mode or sys.platform == "darwin" or install_mode:
     from t_modules import t_main
 else:
     # Using the above import method breaks previous pickles. Could be fixed

From e5f16cfa1176421f4a3b7990a8d30158c9141ae6 Mon Sep 17 00:00:00 2001
From: David Roman <davidroman96@gmail.com>
Date: Fri, 2 Feb 2024 15:56:37 +0100
Subject: [PATCH 3/3] Search phazor.so from standard LD_LIBRARY_PATH

By removing the hardcoded path we allow LoadLibrary to search the
library from LD_LIBRARY_PATH directories
---
 t_modules/t_phazor.py | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/t_modules/t_phazor.py b/t_modules/t_phazor.py
index 2ec5724c9..0c629f62c 100644
--- a/t_modules/t_phazor.py
+++ b/t_modules/t_phazor.py
@@ -55,7 +55,12 @@ def player4(tauon):
     loaded_track = None
     fade_time = 400
 
-    aud = ctypes.cdll.LoadLibrary(pctl.install_directory + "/lib/libphazor.so")
+    if os.path.isfile(pctl.install_directory + "/lib/libphazor.so"):
+        # XXX: May be unnecesary. It's only to ensure compatibility with WWindows
+        aud = ctypes.cdll.LoadLibrary(pctl.install_directory + "/lib/libphazor.so")
+    else:
+        aud = ctypes.cdll.LoadLibrary("libphazor.so")
+
     aud.init()
 
     aud.get_device.restype = ctypes.c_char_p
