Toggle navigation
Toggle navigation
此项目
正在载入...
Sign in
xuning
/
sherpaonnx
转到一个项目
Toggle navigation
项目
群组
代码片段
帮助
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Fangjun Kuang
2025-02-17 15:16:23 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Committed by
GitHub
2025-02-17 15:16:23 +0800
Commit
b03f6e6e8c14d1c39d15dfa458fd4e1a8eac6bed
b03f6e6e
1 parent
d95d4311
Add Swift API for FireRedAsr AED Model (#1876)
隐藏空白字符变更
内嵌
并排对比
正在显示
4 个修改的文件
包含
128 行增加
和
2 行删除
.github/scripts/test-swift.sh
swift-api-examples/SherpaOnnx.swift
swift-api-examples/fire-red-asr.swift
swift-api-examples/run-fire-red-asr.sh
.github/scripts/test-swift.sh
查看文件 @
b03f6e6
...
...
@@ -7,6 +7,10 @@ echo "pwd: $PWD"
cd
swift-api-examples
ls -lh
./run-fire-red-asr.sh
rm -rf sherpa-onnx-fire-red-asr-
*
./run-tts-vits.sh
ls -lh
rm -rf vits-piper-
*
...
...
swift-api-examples/SherpaOnnx.swift
查看文件 @
b03f6e6
...
...
@@ -357,6 +357,16 @@ func sherpaOnnxOfflineWhisperModelConfig(
)
}
func
sherpaOnnxOfflineFireRedAsrModelConfig
(
encoder
:
String
=
""
,
decoder
:
String
=
""
)
->
SherpaOnnxOfflineFireRedAsrModelConfig
{
return
SherpaOnnxOfflineFireRedAsrModelConfig
(
encoder
:
toCPointer
(
encoder
),
decoder
:
toCPointer
(
decoder
)
)
}
func
sherpaOnnxOfflineMoonshineModelConfig
(
preprocessor
:
String
=
""
,
encoder
:
String
=
""
,
...
...
@@ -416,7 +426,8 @@ func sherpaOnnxOfflineModelConfig(
bpeVocab
:
String
=
""
,
teleSpeechCtc
:
String
=
""
,
senseVoice
:
SherpaOnnxOfflineSenseVoiceModelConfig
=
sherpaOnnxOfflineSenseVoiceModelConfig
(),
moonshine
:
SherpaOnnxOfflineMoonshineModelConfig
=
sherpaOnnxOfflineMoonshineModelConfig
()
moonshine
:
SherpaOnnxOfflineMoonshineModelConfig
=
sherpaOnnxOfflineMoonshineModelConfig
(),
fireRedAsr
:
SherpaOnnxOfflineFireRedAsrModelConfig
=
sherpaOnnxOfflineFireRedAsrModelConfig
()
)
->
SherpaOnnxOfflineModelConfig
{
return
SherpaOnnxOfflineModelConfig
(
transducer
:
transducer
,
...
...
@@ -433,7 +444,8 @@ func sherpaOnnxOfflineModelConfig(
bpe_vocab
:
toCPointer
(
bpeVocab
),
telespeech_ctc
:
toCPointer
(
teleSpeechCtc
),
sense_voice
:
senseVoice
,
moonshine
:
moonshine
moonshine
:
moonshine
,
fire_red_asr
:
fireRedAsr
)
}
...
...
swift-api-examples/fire-red-asr.swift
0 → 100644
查看文件 @
b03f6e6
import
AVFoundation
extension
AudioBuffer
{
func
array
()
->
[
Float
]
{
return
Array
(
UnsafeBufferPointer
(
self
))
}
}
extension
AVAudioPCMBuffer
{
func
array
()
->
[
Float
]
{
return
self
.
audioBufferList
.
pointee
.
mBuffers
.
array
()
}
}
func
run
()
{
let
encoder
=
"./sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16/encoder.int8.onnx"
let
decoder
=
"./sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16/decoder.int8.onnx"
let
tokens
=
"./sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16/tokens.txt"
let
fireRedAsr
=
sherpaOnnxOfflineFireRedAsrModelConfig
(
encoder
:
encoder
,
decoder
:
decoder
)
let
modelConfig
=
sherpaOnnxOfflineModelConfig
(
tokens
:
tokens
,
debug
:
0
,
fireRedAsr
:
fireRedAsr
)
let
featConfig
=
sherpaOnnxFeatureConfig
(
sampleRate
:
16000
,
featureDim
:
80
)
var
config
=
sherpaOnnxOfflineRecognizerConfig
(
featConfig
:
featConfig
,
modelConfig
:
modelConfig
)
let
recognizer
=
SherpaOnnxOfflineRecognizer
(
config
:
&
config
)
let
filePath
=
"./sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16/test_wavs/0.wav"
let
fileURL
:
NSURL
=
NSURL
(
fileURLWithPath
:
filePath
)
let
audioFile
=
try!
AVAudioFile
(
forReading
:
fileURL
as
URL
)
let
audioFormat
=
audioFile
.
processingFormat
assert
(
audioFormat
.
channelCount
==
1
)
assert
(
audioFormat
.
commonFormat
==
AVAudioCommonFormat
.
pcmFormatFloat32
)
let
audioFrameCount
=
UInt32
(
audioFile
.
length
)
let
audioFileBuffer
=
AVAudioPCMBuffer
(
pcmFormat
:
audioFormat
,
frameCapacity
:
audioFrameCount
)
try!
audioFile
.
read
(
into
:
audioFileBuffer
!
)
let
array
:
[
Float
]
!
=
audioFileBuffer
?
.
array
()
let
result
=
recognizer
.
decode
(
samples
:
array
,
sampleRate
:
Int
(
audioFormat
.
sampleRate
))
print
(
"
\n
result is:
\n\(
result
.
text
)
"
)
if
result
.
timestamps
.
count
!=
0
{
print
(
"
\n
timestamps is:
\n\(
result
.
timestamps
)
"
)
}
}
@main
struct
App
{
static
func
main
()
{
run
()
}
}
...
...
swift-api-examples/run-fire-red-asr.sh
0 → 100755
查看文件 @
b03f6e6
#!/usr/bin/env bash
set
-ex
if
[
! -d ../build-swift-macos
]
;
then
echo
"Please run ../build-swift-macos.sh first!"
exit
1
fi
if
[
! -f ./sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16/encoder.int8.onnx
]
;
then
echo
"Please download the pre-trained model for testing."
echo
"You can refer to"
echo
""
echo
"https://k2-fsa.github.io/sherpa/onnx/pretrained_models/FireRedAsr/index.html"
echo
""
echo
"for help"
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16.tar.bz2
tar xvf sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16.tar.bz2
rm sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16.tar.bz2
ls -lh sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16
fi
if
[
! -e ./fire-red-asr
]
;
then
# Note: We use -lc++ to link against libc++ instead of libstdc++
swiftc
\
-lc++
\
-I ../build-swift-macos/install/include
\
-import-objc-header ./SherpaOnnx-Bridging-Header.h
\
./fire-red-asr.swift ./SherpaOnnx.swift
\
-L ../build-swift-macos/install/lib/
\
-l sherpa-onnx
\
-l onnxruntime
\
-o fire-red-asr
strip fire-red-asr
else
echo
"./fire-red-asr exists - skip building"
fi
export
DYLD_LIBRARY_PATH
=
$PWD
/../build-swift-macos/install/lib:
$DYLD_LIBRARY_PATH
./fire-red-asr
...
...
请
注册
或
登录
后发表评论