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
cjsdurj
2025-03-10 17:21:23 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Committed by
GitHub
2025-03-10 17:21:23 +0800
Commit
b87fce9a7f7bcb96bc1916a789f9803d28105b5b
b87fce9a
1 parent
6e261ed6
c-api add wave write to buffer. (#1962)
Co-authored-by: jian.chen03 <jian.chen03@transwarp.io>
隐藏空白字符变更
内嵌
并排对比
正在显示
5 个修改的文件
包含
48 行增加
和
8 行删除
.gitignore
sherpa-onnx/c-api/c-api.cc
sherpa-onnx/c-api/c-api.h
sherpa-onnx/csrc/wave-writer.cc
sherpa-onnx/csrc/wave-writer.h
.gitignore
查看文件 @
b87fce9
...
...
@@ -136,3 +136,6 @@ kokoro-multi-lang-v1_0
sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16
cmake-build-debug
README-DEV.txt
##clion
.idea
\ No newline at end of file
...
...
sherpa-onnx/c-api/c-api.cc
查看文件 @
b87fce9
...
...
@@ -1337,6 +1337,16 @@ int32_t SherpaOnnxWriteWave(const float *samples, int32_t n,
return
sherpa_onnx
::
WriteWave
(
filename
,
sample_rate
,
samples
,
n
);
}
int64_t
SherpaOnnxWaveFileSize
(
int32_t
n_samples
)
{
return
sherpa_onnx
::
WaveFileSize
(
n_samples
);
}
SHERPA_ONNX_API
void
SherpaOnnxWriteWaveToBuffer
(
const
float
*
samples
,
int32_t
n
,
int32_t
sample_rate
,
char
*
buffer
)
{
sherpa_onnx
::
WriteWave
(
buffer
,
sample_rate
,
samples
,
n
);
}
const
SherpaOnnxWave
*
SherpaOnnxReadWave
(
const
char
*
filename
)
{
int32_t
sample_rate
=
-
1
;
bool
is_ok
=
false
;
...
...
sherpa-onnx/c-api/c-api.h
查看文件 @
b87fce9
...
...
@@ -1049,6 +1049,18 @@ SHERPA_ONNX_API int32_t SherpaOnnxWriteWave(const float *samples, int32_t n,
int32_t
sample_rate
,
const
char
*
filename
);
// the amount of bytes needed to store a wave file which contains a
// single channel and has 16-bit samples.
SHERPA_ONNX_API
int64_t
SherpaOnnxWaveFileSize
(
int32_t
n_samples
);
// Similar to SherpaOnnxWriteWave , it writes wave to allocated buffer;
//
// in some case (http tts api return wave binary file, server do not need to
// write wave to fs)
SHERPA_ONNX_API
void
SherpaOnnxWriteWaveToBuffer
(
const
float
*
samples
,
int32_t
n
,
int32_t
sample_rate
,
char
*
buffer
);
SHERPA_ONNX_API
typedef
struct
SherpaOnnxWave
{
// samples normalized to the range [-1, 1]
const
float
*
samples
;
...
...
sherpa-onnx/csrc/wave-writer.cc
查看文件 @
b87fce9
...
...
@@ -4,6 +4,7 @@
#include "sherpa-onnx/csrc/wave-writer.h"
#include <cstring>
#include <fstream>
#include <string>
#include <vector>
...
...
@@ -35,8 +36,12 @@ struct WaveHeader {
}
// namespace
bool
WriteWave
(
const
std
::
string
&
filename
,
int32_t
sampling_rate
,
const
float
*
samples
,
int32_t
n
)
{
int64_t
WaveFileSize
(
int32_t
n_samples
)
{
return
sizeof
(
WaveHeader
)
+
n_samples
*
sizeof
(
int16_t
);
}
void
WriteWave
(
char
*
buffer
,
int32_t
sampling_rate
,
const
float
*
samples
,
int32_t
n
)
{
WaveHeader
header
{};
header
.
chunk_id
=
0x46464952
;
// FFIR
header
.
format
=
0x45564157
;
// EVAW
...
...
@@ -61,21 +66,26 @@ bool WriteWave(const std::string &filename, int32_t sampling_rate,
samples_int16
[
i
]
=
samples
[
i
]
*
32676
;
}
memcpy
(
buffer
,
&
header
,
sizeof
(
WaveHeader
));
memcpy
(
buffer
+
sizeof
(
WaveHeader
),
samples_int16
.
data
(),
n
*
sizeof
(
int16_t
));
}
bool
WriteWave
(
const
std
::
string
&
filename
,
int32_t
sampling_rate
,
const
float
*
samples
,
int32_t
n
)
{
std
::
string
buffer
;
buffer
.
resize
(
WaveFileSize
(
n
));
WriteWave
(
buffer
.
data
(),
sampling_rate
,
samples
,
n
);
std
::
ofstream
os
(
filename
,
std
::
ios
::
binary
);
if
(
!
os
)
{
SHERPA_ONNX_LOGE
(
"Failed to create %s"
,
filename
.
c_str
());
return
false
;
}
os
.
write
(
reinterpret_cast
<
const
char
*>
(
&
header
),
sizeof
(
header
));
os
.
write
(
reinterpret_cast
<
const
char
*>
(
samples_int16
.
data
()),
samples_int16
.
size
()
*
sizeof
(
int16_t
));
os
<<
buffer
;
if
(
!
os
)
{
SHERPA_ONNX_LOGE
(
"Write %s failed"
,
filename
.
c_str
());
return
false
;
}
return
true
;
}
...
...
sherpa-onnx/csrc/wave-writer.h
查看文件 @
b87fce9
...
...
@@ -22,6 +22,11 @@ namespace sherpa_onnx {
bool
WriteWave
(
const
std
::
string
&
filename
,
int32_t
sampling_rate
,
const
float
*
samples
,
int32_t
n
);
void
WriteWave
(
char
*
buffer
,
int32_t
sampling_rate
,
const
float
*
samples
,
int32_t
n
);
int64_t
WaveFileSize
(
int32_t
n_samples
);
}
// namespace sherpa_onnx
#endif // SHERPA_ONNX_CSRC_WAVE_WRITER_H_
...
...
请
注册
或
登录
后发表评论