diff --git a/composer.json b/composer.json index b28a7a1..add3ea1 100644 --- a/composer.json +++ b/composer.json @@ -9,10 +9,9 @@ "codewithkyrian/platform-package-installer": "^1.0" }, "require-dev": { - "symfony/var-dumper": "^6.4.11|^7.1.5", + "symfony/var-dumper": "^6.4.11|^7.1.5|^8.0", "pestphp/pest": "^2.36.0|^3.5.0", - "mockery/mockery": "^1.6", - "laravel/pint": "^1.18" + "mockery/mockery": "^1.6" }, "license": "MIT", "autoload": { diff --git a/include/whisper.h b/include/whisper.h index 4739e6c..22d6ce6 100644 --- a/include/whisper.h +++ b/include/whisper.h @@ -1,5 +1,9 @@ #ifndef WHISPER_H #define WHISPER_H +#endif + +#include +#include #define WHISPER_SAMPLE_RATE 16000 #define WHISPER_N_FFT 400 @@ -52,7 +56,7 @@ typedef int32_t whisper_pos; typedef int32_t whisper_token; typedef int32_t whisper_seq_id; - enum whisper_alignment_heads_preset { +enum whisper_alignment_heads_preset { WHISPER_AHEADS_NONE, WHISPER_AHEADS_N_TOP_MOST, // All heads from the N-top-most text-layers WHISPER_AHEADS_CUSTOM, diff --git a/lib/darwin-arm64/libsamplerate.dylib b/lib/darwin-arm64/libsamplerate.0.2.2.dylib similarity index 100% rename from lib/darwin-arm64/libsamplerate.dylib rename to lib/darwin-arm64/libsamplerate.0.2.2.dylib diff --git a/lib/darwin-arm64/libsndfile.dylib b/lib/darwin-arm64/libsndfile.1.2.2.dylib similarity index 100% rename from lib/darwin-arm64/libsndfile.dylib rename to lib/darwin-arm64/libsndfile.1.2.2.dylib diff --git a/lib/darwin-arm64/libwhisper.dylib b/lib/darwin-arm64/libwhisper.1.7.2.dylib similarity index 100% rename from lib/darwin-arm64/libwhisper.dylib rename to lib/darwin-arm64/libwhisper.1.7.2.dylib diff --git a/lib/darwin-x86_64/libsamplerate.dylib b/lib/darwin-x86_64/libsamplerate.0.2.2.dylib similarity index 100% rename from lib/darwin-x86_64/libsamplerate.dylib rename to lib/darwin-x86_64/libsamplerate.0.2.2.dylib diff --git a/lib/darwin-x86_64/libsndfile.dylib b/lib/darwin-x86_64/libsndfile.1.2.2.dylib similarity index 100% rename from lib/darwin-x86_64/libsndfile.dylib rename to lib/darwin-x86_64/libsndfile.1.2.2.dylib diff --git a/lib/darwin-x86_64/libwhisper.dylib b/lib/darwin-x86_64/libwhisper.1.7.2.dylib similarity index 100% rename from lib/darwin-x86_64/libwhisper.dylib rename to lib/darwin-x86_64/libwhisper.1.7.2.dylib diff --git a/lib/linux-arm64/libsamplerate.so b/lib/linux-arm64/libsamplerate.so.0.2.2 similarity index 100% rename from lib/linux-arm64/libsamplerate.so rename to lib/linux-arm64/libsamplerate.so.0.2.2 diff --git a/lib/linux-arm64/libsndfile.so b/lib/linux-arm64/libsndfile.so.1.2.2 similarity index 100% rename from lib/linux-arm64/libsndfile.so rename to lib/linux-arm64/libsndfile.so.1.2.2 diff --git a/lib/linux-arm64/libwhisper.so b/lib/linux-arm64/libwhisper.so.1.7.2 similarity index 100% rename from lib/linux-arm64/libwhisper.so rename to lib/linux-arm64/libwhisper.so.1.7.2 diff --git a/lib/linux-x86_64/libsamplerate.so b/lib/linux-x86_64/libsamplerate.so.0.2.2 similarity index 100% rename from lib/linux-x86_64/libsamplerate.so rename to lib/linux-x86_64/libsamplerate.so.0.2.2 diff --git a/lib/linux-x86_64/libsndfile.so b/lib/linux-x86_64/libsndfile.so.1.2.2 similarity index 100% rename from lib/linux-x86_64/libsndfile.so rename to lib/linux-x86_64/libsndfile.so.1.2.2 diff --git a/lib/linux-x86_64/libwhisper.so b/lib/linux-x86_64/libwhisper.so.1.7.2 similarity index 100% rename from lib/linux-x86_64/libwhisper.so rename to lib/linux-x86_64/libwhisper.so.1.7.2 diff --git a/lib/windows-x86_64/libsamplerate.dll b/lib/windows-x86_64/samplerate-0.2.2.dll similarity index 100% rename from lib/windows-x86_64/libsamplerate.dll rename to lib/windows-x86_64/samplerate-0.2.2.dll diff --git a/lib/windows-x86_64/libsndfile.dll b/lib/windows-x86_64/sndfile-1.2.2.dll similarity index 100% rename from lib/windows-x86_64/libsndfile.dll rename to lib/windows-x86_64/sndfile-1.2.2.dll diff --git a/lib/windows-x86_64/libwhisper.dll b/lib/windows-x86_64/whisper-1.7.2.dll similarity index 100% rename from lib/windows-x86_64/libwhisper.dll rename to lib/windows-x86_64/whisper-1.7.2.dll diff --git a/src/LibraryLoader.php b/src/LibraryLoader.php index 21f4700..92fdb8c 100644 --- a/src/LibraryLoader.php +++ b/src/LibraryLoader.php @@ -10,21 +10,49 @@ class LibraryLoader { - private const LIBRARY_CONFIGS = [ - 'whisper' => ['header' => 'whisper.h', 'library' => 'libwhisper'], - 'sndfile' => ['header' => 'sndfile.h', 'library' => 'libsndfile',], - 'samplerate' => ['header' => 'samplerate.h', 'library' => 'libsamplerate'], + private const LIBRARIES = [ + 'whisper' => [ + 'name' => 'whisper', + 'header' => 'whisper.h', + 'version' => '1.7.2', + ], + 'sndfile' => [ + 'name' => 'sndfile', + 'header' => 'sndfile.h', + 'version' => '1.2.2', + ], + 'samplerate' => [ + 'name' => 'samplerate', + 'header' => 'samplerate.h', + 'version' => '0.2.2', + ], ]; - private const PLATFORM_CONFIGS = [ - 'linux-x86_64' => ['directory' => 'linux-x86_64', 'extension' => 'so',], - 'linux-arm64' => ['directory' => 'linux-arm64', 'extension' => 'so',], - 'darwin-x86_64' => ['directory' => 'darwin-x86_64', 'extension' => 'dylib',], - 'darwin-arm64' => ['directory' => 'darwin-arm64', 'extension' => 'dylib',], - 'windows-x86_64' => ['directory' => 'windows-x86_64', 'extension' => 'dll',], + private const PLATFORMS = [ + 'linux-x86_64' => [ + 'directory' => 'linux-x86_64', + 'path' => 'lib{name}.so.{version}', + ], + 'linux-arm64' => [ + 'directory' => 'linux-arm64', + 'path' => 'lib{name}.so.{version}', + ], + 'darwin-x86_64' => [ + 'directory' => 'darwin-x86_64', + 'path' => 'lib{name}.{version}.dylib', + ], + 'darwin-arm64' => [ + 'directory' => 'darwin-arm64', + 'path' => 'lib{name}.{version}.dylib', + ], + 'windows-x86_64' => [ + 'directory' => 'windows-x86_64', + 'path' => '{name}-{version}.dll', + ], ]; private static array $instances = []; + private ?FFI $kernel32 = null; public function __construct() @@ -54,18 +82,18 @@ public function get(string $library): FFI */ private function load(string $library): FFI { - if (!isset(self::LIBRARY_CONFIGS[$library])) { + $config = self::LIBRARIES[$library] ?? null; + if ($config === null) { throw new RuntimeException("Unsupported library: {$library}"); } - $platformConfig = Platform::findBestMatch(self::PLATFORM_CONFIGS); - if (!$platformConfig) { - throw new RuntimeException("No matching platform configuration found"); + $platform = Platform::findBestMatch(self::PLATFORMS); + if (!$platform) { + throw new RuntimeException("Unsupported platform: ".json_encode(Platform::current())); } - $config = self::LIBRARY_CONFIGS[$library]; $headerPath = $this->getHeaderPath($config['header']); - $libraryPath = $this->getLibraryPath($config['library'], $platformConfig['extension'], $platformConfig['directory']); + $libraryPath = $this->getLibraryPath($config['name'],$config['version'],$platform); return FFI::cdef(file_get_contents($headerPath), $libraryPath); } @@ -78,9 +106,19 @@ private static function getHeaderPath(string $headerFile): string /** * Get path to library file */ - private function getLibraryPath(string $libName, string $extension, string $platformDir): string + private function getLibraryPath(string $libName, string $version, array $platform): string { - return self::joinPaths(dirname(__DIR__), 'lib', $platformDir, "$libName.$extension"); + $libraryDir = self::getLibraryDirectory($platform['directory']); + $template = $platform['path']; + + $filename = str_replace(['{name}', '{version}'], [$libName, $version], $template); + $candidate = self::joinPaths($libraryDir, $filename); + + if (file_exists($candidate)) { + return $candidate; + } + + throw new RuntimeException("Unable to locate library file for {$libName} in {$libraryDir}"); } private static function getLibraryDirectory(string $platformDir): string @@ -95,15 +133,15 @@ private function addDllDirectory(): void { if (!Platform::isWindows()) return; - $platformConfig = Platform::findBestMatch(self::PLATFORM_CONFIGS); - $libraryDir = self::getLibraryDirectory($platformConfig['directory']); + $platform = Platform::findBestMatch(self::PLATFORMS); + $libraryDir = self::getLibraryDirectory($platform['directory']); $this->kernel32 ??= FFI::cdef(" int SetDllDirectoryA(const char* lpPathName); int SetDefaultDllDirectories(unsigned long DirectoryFlags); ", 'kernel32.dll'); - $this->kernel32->SetDllDirectoryA($libraryDir); + $this->kernel32?->{'SetDllDirectoryA'}($libraryDir); } /** @@ -112,7 +150,7 @@ private function addDllDirectory(): void private function resetDllDirectory(): void { if ($this->kernel32 !== null) { - $this->kernel32->SetDllDirectoryA(null); + $this->kernel32?->{'SetDllDirectoryA'}(null); } }