Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrohanea committed Jan 11, 2025
1 parent a74a3d3 commit 66d2b86
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ macos_arm64_coreml:

ios:
rm -rf build/ios
cmake $(CMAKE_PARAMETERS) -DCMAKE_OSX_SYSROOT="iphoneos" -S . -B build/ios
cmake $(CMAKE_PARAMETERS) -DCMAKE_OSX_SYSROOT="iphoneos" -DCMAKE_SYSTEM_NAME=iOS -S . -B build/ios
cmake --build build/ios
mkdir -p runtimes/Whisper.net.Runtime/ios-device
cp build/ios/whisper.cpp/src/libwhisper.dylib runtimes/Whisper.net.Runtime/ios-device/libwhisper.dylib
Expand Down Expand Up @@ -274,7 +274,7 @@ tvos_simulator_arm64:

tvos:
rm -rf build/tvos
cmake $(CMAKE_PARAMETERS) -DCMAKE_OSX_SYSROOT="appletvos" -DCMAKE_OSX_ARCHITECTURES="arm64" -S . -B build/tvos
cmake $(CMAKE_PARAMETERS) -DCMAKE_OSX_SYSROOT="appletvos" -DCMAKE_SYSTEM_NAME=tvOS -DCMAKE_OSX_ARCHITECTURES="arm64" -S . -B build/tvos
cmake --build build/tvos
mkdir -p runtimes/Whisper.net.Runtime/tvos-device
cp build/tvos/whisper.cpp/src/libwhisper.dylib runtimes/Whisper.net.Runtime/tvos-device/libwhisper.dylib
Expand Down
5 changes: 5 additions & 0 deletions Whisper.net/Internals/Native/INativeWhisper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ internal interface INativeWhisper : IDisposable
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public delegate IntPtr whisper_print_system_info();

[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public delegate float whisper_full_get_segment_no_speech_prob_from_state(IntPtr state, int index);

whisper_init_from_file_with_params_no_state Whisper_Init_From_File_With_Params_No_State { get; }
whisper_init_from_buffer_with_params_no_state Whisper_Init_From_Buffer_With_Params_No_State { get; }
whisper_free Whisper_Free { get; }
Expand All @@ -103,4 +106,6 @@ internal interface INativeWhisper : IDisposable
whisper_full_get_token_data_from_state Whisper_Full_Get_Token_Data_From_State { get; }
whisper_full_get_token_text_from_state Whisper_Full_Get_Token_Text_From_State { get; }
whisper_print_system_info WhisperPrintSystemInfo { get; }

whisper_full_get_segment_no_speech_prob_from_state Whisper_Full_Get_Segment_No_Speech_Prob_From_State { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ internal class DllImportsNativeLibWhisper : INativeWhisper
[DllImport(NativeConstants.LibWhisperLibraryName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr whisper_print_system_info();

[DllImport(NativeConstants.LibWhisperLibraryName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern float whisper_full_get_segment_no_speech_prob_from_state(IntPtr state, int index);

public INativeWhisper.whisper_init_from_file_with_params_no_state Whisper_Init_From_File_With_Params_No_State => whisper_init_from_file_with_params_no_state;

public INativeWhisper.whisper_init_from_buffer_with_params_no_state Whisper_Init_From_Buffer_With_Params_No_State => whisper_init_from_buffer_with_params_no_state;
Expand Down Expand Up @@ -130,6 +133,8 @@ internal class DllImportsNativeLibWhisper : INativeWhisper

public INativeWhisper.whisper_print_system_info WhisperPrintSystemInfo => whisper_print_system_info;

public INativeWhisper.whisper_full_get_segment_no_speech_prob_from_state Whisper_Full_Get_Segment_No_Speech_Prob_From_State => whisper_full_get_segment_no_speech_prob_from_state;

public void Dispose()
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ internal class DllImportsNativeWhisper : INativeWhisper
[DllImport(NativeConstants.WhisperLibraryName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr whisper_print_system_info();

[DllImport(NativeConstants.WhisperLibraryName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern float whisper_full_get_segment_no_speech_prob_from_state(IntPtr state, int index);

public INativeWhisper.whisper_init_from_file_with_params_no_state Whisper_Init_From_File_With_Params_No_State => whisper_init_from_file_with_params_no_state;

public INativeWhisper.whisper_init_from_buffer_with_params_no_state Whisper_Init_From_Buffer_With_Params_No_State => whisper_init_from_buffer_with_params_no_state;
Expand Down Expand Up @@ -130,6 +133,8 @@ internal class DllImportsNativeWhisper : INativeWhisper

public INativeWhisper.whisper_print_system_info WhisperPrintSystemInfo => whisper_print_system_info;

public INativeWhisper.whisper_full_get_segment_no_speech_prob_from_state Whisper_Full_Get_Segment_No_Speech_Prob_From_State => whisper_full_get_segment_no_speech_prob_from_state;

public void Dispose()
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ internal partial class LibraryImportInternalWhisper : INativeWhisper
[LibraryImport(NativeConstants.InternalLibraryName, StringMarshalling = StringMarshalling.Utf8)]
public static partial IntPtr whisper_print_system_info();

[LibraryImport(NativeConstants.InternalLibraryName, StringMarshalling = StringMarshalling.Utf8)]
public static partial float whisper_full_get_segment_no_speech_prob_from_state(IntPtr state, int index);

public INativeWhisper.whisper_init_from_file_with_params_no_state Whisper_Init_From_File_With_Params_No_State => whisper_init_from_file_with_params_no_state;

public INativeWhisper.whisper_init_from_buffer_with_params_no_state Whisper_Init_From_Buffer_With_Params_No_State => whisper_init_from_buffer_with_params_no_state;
Expand Down Expand Up @@ -130,6 +133,8 @@ internal partial class LibraryImportInternalWhisper : INativeWhisper

public INativeWhisper.whisper_print_system_info WhisperPrintSystemInfo => whisper_print_system_info;

public INativeWhisper.whisper_full_get_segment_no_speech_prob_from_state Whisper_Full_Get_Segment_No_Speech_Prob_From_State => whisper_full_get_segment_no_speech_prob_from_state;

public void Dispose()
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ internal partial class LibraryImportLibWhisper : INativeWhisper
[LibraryImport(NativeConstants.LibWhisperLibraryName, StringMarshalling = StringMarshalling.Utf8)]
public static partial IntPtr whisper_print_system_info();

[LibraryImport(NativeConstants.LibWhisperLibraryName, StringMarshalling = StringMarshalling.Utf8)]
public static partial float whisper_full_get_segment_no_speech_prob_from_state(IntPtr state, int index);

public INativeWhisper.whisper_init_from_file_with_params_no_state Whisper_Init_From_File_With_Params_No_State => whisper_init_from_file_with_params_no_state;

public INativeWhisper.whisper_init_from_buffer_with_params_no_state Whisper_Init_From_Buffer_With_Params_No_State => whisper_init_from_buffer_with_params_no_state;
Expand Down Expand Up @@ -130,6 +133,8 @@ internal partial class LibraryImportLibWhisper : INativeWhisper

public INativeWhisper.whisper_print_system_info WhisperPrintSystemInfo => whisper_print_system_info;

public INativeWhisper.whisper_full_get_segment_no_speech_prob_from_state Whisper_Full_Get_Segment_No_Speech_Prob_From_State => whisper_full_get_segment_no_speech_prob_from_state;

public void Dispose()
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public NativeLibraryWhisper(IntPtr whisperLibraryHandle)
Whisper_Full_Get_Token_Data_From_State = Marshal.GetDelegateForFunctionPointer<whisper_full_get_token_data_from_state>(NativeLibrary.GetExport(whisperLibraryHandle, nameof(whisper_full_get_token_data_from_state)));
Whisper_Full_Get_Token_Text_From_State = Marshal.GetDelegateForFunctionPointer<whisper_full_get_token_text_from_state>(NativeLibrary.GetExport(whisperLibraryHandle, nameof(whisper_full_get_token_text_from_state)));
WhisperPrintSystemInfo = Marshal.GetDelegateForFunctionPointer<whisper_print_system_info>(NativeLibrary.GetExport(whisperLibraryHandle, nameof(whisper_print_system_info)));
Whisper_Full_Get_Segment_No_Speech_Prob_From_State = Marshal.GetDelegateForFunctionPointer<whisper_full_get_segment_no_speech_prob_from_state>(NativeLibrary.GetExport(whisperLibraryHandle, nameof(whisper_full_get_segment_no_speech_prob_from_state)));

this.whisperLibraryHandle = whisperLibraryHandle;
}
Expand Down Expand Up @@ -88,6 +89,8 @@ public NativeLibraryWhisper(IntPtr whisperLibraryHandle)

public whisper_print_system_info WhisperPrintSystemInfo { get; }

public whisper_full_get_segment_no_speech_prob_from_state Whisper_Full_Get_Segment_No_Speech_Prob_From_State { get; }

public void Dispose()
{
NativeLibrary.Free(whisperLibraryHandle);
Expand Down
12 changes: 11 additions & 1 deletion Whisper.net/WhisperProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ private void OnNewSegment(IntPtr state)
var numberOfTokens = nativeWhisper.Whisper_Full_N_Tokens_From_State(state, segmentIndex);
var languageId = nativeWhisper.Whisper_Full_Lang_Id_From_State(state);
var language = Marshal.PtrToStringAnsi(nativeWhisper.Whisper_Lang_Str(languageId));
var noSpeechProbability = nativeWhisper.Whisper_Full_Get_Segment_No_Speech_Prob_From_State(state, segmentIndex);

var tokens = new WhisperToken[numberOfTokens];

Expand Down Expand Up @@ -738,7 +739,16 @@ private void OnNewSegment(IntPtr state)

if (!string.IsNullOrEmpty(textAnsi))
{
var eventHandlerArgs = new SegmentData(textAnsi!, t0, t1, minimumProbability, maximumProbability, (float)(sumProbability / numberOfTokens), language!, tokens);
var eventHandlerArgs = new SegmentData(
textAnsi!,
t0,
t1,
minimumProbability,
maximumProbability,
(float)(sumProbability / numberOfTokens),
noSpeechProbability,
language!,
tokens);

foreach (var handler in options.OnSegmentEventHandlers)
{
Expand Down
13 changes: 12 additions & 1 deletion Whisper.net/WhisperProcessorEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,16 @@ public class WhisperToken
/// <param name="maxProbability"></param>
/// <param name="probability"></param>
/// <param name="language"></param>
public class SegmentData(string text, TimeSpan start, TimeSpan end, float minProbability, float maxProbability, float probability, string language, WhisperToken[] tokens)
public class SegmentData(
string text,
TimeSpan start,
TimeSpan end,
float minProbability,
float maxProbability,
float probability,
float noSpeechProbability,
string language,
WhisperToken[] tokens)
{

/// <summary>
Expand Down Expand Up @@ -94,4 +103,6 @@ public class SegmentData(string text, TimeSpan start, TimeSpan end, float minPro
/// The tokens of the current segment.
/// </summary>
public WhisperToken[] Tokens { get; } = tokens;

public float NoSpeechProbability { get; } = noSpeechProbability;
}
21 changes: 13 additions & 8 deletions windows-scripts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,13 @@ function BuildWindows() {
[Parameter(Mandatory = $false)] [bool]$NoAvx = $false,
[Parameter(Mandatory = $false)] [string]$Configuration = "Release"
)
#if not exist "build" create the directory
# Ensure the build directory exists
if (!(Test-Path "build")) {
New-Item -ItemType Directory -Force -Path "build"
}

Write-Host "Building Windows binaries for $Arch with cuda: $Cuda"


$platform = Get-MSBuildPlatform $Arch
if ([string]::IsNullOrEmpty($platform)) {
Write-Host "Unknown architecture $Arch"
Expand All @@ -59,6 +58,14 @@ function BuildWindows() {

$runtimePath = "./runtimes/Whisper.net.Runtime"

# Configure toolchain for ARM builds to use CLANG
if ($Arch -eq "arm64" -or $Arch -eq "arm") {
$options += "-DCMAKE_C_COMPILER=clang"
$options += "-DCMAKE_CXX_COMPILER=clang++"
$options += "-T ClangCL" # Use CLANG toolset
Write-Host "Using CLANG for ARM build: $Arch"
}

if ($Cuda) {
$options += "-DGGML_CUDA=1"
$buildDirectory += "-cuda"
Expand Down Expand Up @@ -90,13 +97,13 @@ function BuildWindows() {
$options += $avxOptions

if ((Test-Path $buildDirectory)) {
Write-Host "Deleting old build files for $buildDirectory";
Write-Host "Deleting old build files for $buildDirectory"
Remove-Item -Force -Recurse -Path $buildDirectory
}

$cmakePath = (Get-Command cmake -ErrorAction SilentlyContinue).Source
if ([string]::IsNullOrEmpty($cmakePath)) {
# CMake is not defined in the system's path, search for it in Visual Studio
# Search for CMake in Visual Studio
$visualStudioPath = Get-VisualStudioCMakePath
if ([string]::IsNullOrEmpty($visualStudioPath)) {
Write-Host "CMake is not found in the system or Visual Studio."
Expand All @@ -107,8 +114,7 @@ function BuildWindows() {

New-Item -ItemType Directory -Force -Path $buildDirectory

# call CMake to generate the makefiles

# Call CMake to generate the build files
Write-Host "Running 'cmake $options'"

cmake $options
Expand Down Expand Up @@ -141,8 +147,7 @@ function BuildWindows() {

function BuildWindowsArm([Parameter(Mandatory = $false)] [string]$Configuration = "Release") {
BuildWindows -Arch "arm64" -Configuration $Configuration;
# BuildWindows -Arch "arm" -Configuration $Configuration;
# Arm build not working anymore with VS
BuildWindows -Arch "arm" -Configuration $Configuration;
}

function BuildWindowsIntel([Parameter(Mandatory = $false)] [string]$Configuration = "Release") {
Expand Down

0 comments on commit 66d2b86

Please sign in to comment.