Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
6 changes: 5 additions & 1 deletion include/whisper.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#ifndef WHISPER_H
#define WHISPER_H
#endif

#include <stdint.h>
#include <stddef.h>

#define WHISPER_SAMPLE_RATE 16000
#define WHISPER_N_FFT 400
Expand Down Expand Up @@ -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,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
82 changes: 60 additions & 22 deletions src/LibraryLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}
Expand All @@ -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
Expand All @@ -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);
}

/**
Expand All @@ -112,7 +150,7 @@ private function addDllDirectory(): void
private function resetDllDirectory(): void
{
if ($this->kernel32 !== null) {
$this->kernel32->SetDllDirectoryA(null);
$this->kernel32?->{'SetDllDirectoryA'}(null);
}
}

Expand Down