SwiftWhisper
1.2.0
在Swift中使用耳語的最簡單方法
輕鬆地將轉錄添加到您的應用程序或軟件包中。由Whisper.cpp提供動力。
添加swiftwhisper作為Package.swift中的依賴性。旋轉文件:
let package = Package (
...
dependencies : [
// Add the package to your dependencies
. package ( url : " https://github.com/exPHAT/SwiftWhisper.git " , branch : " master " ) ,
] ,
...
targets : [
// Add SwiftWhisper as a dependency on any target you want to use it in
. target ( name : " MyTarget " ,
dependencies : [ . byName ( name : " SwiftWhisper " ) ] )
]
...
) 添加https://github.com/exPHAT/SwiftWhisper.git在“ Swift Package Manager”選項卡中。
API文檔。
import SwiftWhisper
let whisper = Whisper ( fromFileURL : /* Model file URL */ )
let segments = try await whisper . transcribe ( audioFrames : /* 16kHz PCM audio frames */ )
print ( " Transcribed audio: " , segments . map ( . text ) . joined ( ) ) 您可以通過實現WhisperDelegate並設置whisper.delegate = ...來訂閱細分市場,轉錄進度和錯誤。
protocol WhisperDelegate {
// Progress updates as a percentage from 0-1
func whisper ( _ aWhisper : Whisper , didUpdateProgress progress : Double )
// Any time a new segments of text have been transcribed
func whisper ( _ aWhisper : Whisper , didProcessNewSegments segments : [ Segment ] , atIndex index : Int )
// Finished transcribing, includes all transcribed segments of text
func whisper ( _ aWhisper : Whisper , didCompleteWithSegments segments : [ Segment ] )
// Error with transcription
func whisper ( _ aWhisper : Whisper , didErrorWith error : Error )
} 您可以在此處找到預先訓練的模型以供下載。
要使用Coreml,您需要將Coreml型號文件與後綴-encoder.mlmodelc一起使用與竊竊私語模型相同的名稱(例如: tiny.bin也會坐在一個tiny-encoder.mlmodelc文件旁邊)。除了附加模型文件之外,您還需要使用Whisper(fromFileURL:) initializer。您可以通過在轉錄過程中檢查控制台輸出來驗證Coreml活躍。
將音頻幀進入SwiftWhisper的最簡單方法是使用電聽器。以下示例將輸入音頻文件,轉換和重新示例,並返回16kHz PCM浮子的數組。
import AudioKit
func convertAudioFileToPCMArray ( fileURL : URL , completionHandler : @escaping ( Result < [ Float ] , Error > ) -> Void ) {
var options = FormatConverter . Options ( )
options . format = . wav
options . sampleRate = 16000
options . bitDepth = 16
options . channels = 1
options . isInterleaved = false
let tempURL = URL ( fileURLWithPath : NSTemporaryDirectory ( ) ) . appendingPathComponent ( UUID ( ) . uuidString )
let converter = FormatConverter ( inputURL : fileURL , outputURL : tempURL , options : options )
converter . start { error in
if let error {
completionHandler ( . failure ( error ) )
return
}
let data = try ! Data ( contentsOf : tempURL ) // Handle error here
let floats = stride ( from : 44 , to : data . count , by : 2 ) . map {
return data [ $0 ..< $0 + 2 ] . withUnsafeBytes {
let short = Int16 ( littleEndian : $0 . load ( as : Int16 . self ) )
return max ( - 1.0 , min ( Float ( short ) / 32767.0 , 1.0 ) )
}
}
try ? FileManager . default . removeItem ( at : tempURL )
completionHandler ( . success ( floats ) )
}
}在編譯應用程序以進行Debug構建配置時,您可能會發現轉錄速度的性能慢。這是因為除非設置構建Release ,否則編譯器不會完全優化Swiftwhisper。
您可以通過安裝使用.unsafeFlags(["-O3"])的SwiftWhisper版本來解決此問題。最簡單的方法是在fast分支上使用最新的提交。另外,您可以配置您的方案以在Release配置中構建。
...
dependencies: [
// Using latest commit hash for `fast` branch:
. package ( url : " https://github.com/exPHAT/SwiftWhisper.git " , revision : " deb1cb6a27256c7b01f5d3d2e7dc1dcc330b5d01 " ) ,
] ,
...