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
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ project(samples C CXX)

set(CMAKE_CXX_STANDARD 17)

if(MSVC)
# Silence the C4996 deprecation warnings for the standard CRT (fopen,
# sprintf, etc.) that MSVC tags as "unsafe". The samples use the
# portable C API on purpose so that the same source builds on every
# platform.
add_definitions(-D_CRT_SECURE_NO_WARNINGS)

# Treat source files as UTF-8 so that Japanese string literals do not
# trigger C4566 ("character cannot be represented in the current code
# page (1252)").
add_compile_options(/utf-8)
endif()

# When OFF, skip every sample that requires OpenCV. Useful when only
# audio/NLP samples (whisper, gpt-sovits-v2-pro, etc.) are needed and
# OpenCV is unavailable on the build host.
Expand Down
2 changes: 1 addition & 1 deletion audio_processing/clap/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.1)
cmake_minimum_required(VERSION 3.10)

set (PROJECT_NAME clap)
set (SRC_FILES ${PROJECT_NAME}.cpp clap_utils.cpp)
Expand Down
47 changes: 19 additions & 28 deletions audio_processing/clap/clap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,6 @@ static int argument_parser(int argc, char **argv)
return AILIA_STATUS_SUCCESS;
}

static void print_net(AILIANetwork *net){
char* buf;
unsigned int length = 0;
ailiaGetSummaryLength(net, &length);
buf = (char*)malloc(length);
ailiaSummary(net, buf, length);
PRINT_OUT("%s\n", buf);
free(buf);
}

// ======================
// Utils
Expand Down Expand Up @@ -188,16 +179,16 @@ static std::vector<float> audio_embedding(AILIANetwork *ailia_audio, std::string
// resample
if(sampleRate != target_sample_rate){
int dst_n = 0;
status = ailiaAudioGetResampleLen(&dst_n, target_sample_rate, audio_waveform.size(), sampleRate);
status = ailiaAudioGetResampleLen(&dst_n, target_sample_rate, (int)audio_waveform.size(), sampleRate);
if (status != AILIA_STATUS_SUCCESS) {
PRINT_ERR("ailiaAudioGetResampleLen failed %d\n", status);
return feature;
}
if (debug){
PRINT_OUT("convert sample rate %d to %d, length %ld to %d\n", sampleRate, target_sample_rate, audio_waveform.size(), dst_n);
PRINT_OUT("convert sample rate %d to %d, length %zu to %d\n", sampleRate, target_sample_rate, audio_waveform.size(), dst_n);
}
std::vector<float> new_audio_waveform(dst_n);
status = ailiaAudioResample(&new_audio_waveform[0], &audio_waveform[0], target_sample_rate, dst_n, sampleRate, audio_waveform.size());
status = ailiaAudioResample(&new_audio_waveform[0], &audio_waveform[0], target_sample_rate, dst_n, sampleRate, (int)audio_waveform.size());
if (status != AILIA_STATUS_SUCCESS) {
PRINT_ERR("ailiaAudioResample failed %d\n", status);
return feature;
Expand All @@ -210,7 +201,7 @@ static std::vector<float> audio_embedding(AILIANetwork *ailia_audio, std::string
float x = *v;
if(x < -1) x = -1;
if(x > 1) x = 1;
int16_t y = 32767.f * x;
int16_t y = (int16_t)(32767.f * x);
*v = (float)y / 32767.f;
}

Expand Down Expand Up @@ -245,7 +236,7 @@ static std::vector<float> audio_embedding(AILIANetwork *ailia_audio, std::string
PRINT_OUT("audio input=%d,%d output=%d mel_fusion shape=[%d,%d,%d]\n", blob_idx_longer, blob_idx_mel_fusion, blob_idx_out0, shape.z, shape.y, shape.x);
}
if(mel_fusion.size() != (shape.x * shape.y * shape.z)){
PRINT_ERR("Invalid length of mel_fusion : %ld must be %d\n", mel_fusion.size(), shape.x * shape.y * shape.z);
PRINT_ERR("Invalid length of mel_fusion : %zu must be %d\n", mel_fusion.size(), shape.x * shape.y * shape.z);
return feature;
}

Expand All @@ -256,7 +247,7 @@ static std::vector<float> audio_embedding(AILIANetwork *ailia_audio, std::string
PRINT_ERR("ailiaSetInputBlobData failed %d\n", status);
return feature;
}
status = ailiaSetInputBlobData(ailia_audio, &mel_fusion[0], mel_fusion.size() * sizeof(float), blob_idx_mel_fusion);
status = ailiaSetInputBlobData(ailia_audio, &mel_fusion[0], (unsigned int)(mel_fusion.size() * sizeof(float)), blob_idx_mel_fusion);
if (status != AILIA_STATUS_SUCCESS) {
PRINT_ERR("ailiaSetInputBlobData failed %d\n", status);
return feature;
Expand All @@ -279,7 +270,7 @@ static std::vector<float> audio_embedding(AILIANetwork *ailia_audio, std::string

// get output
feature = std::vector<float>(shape.x);
status = ailiaGetBlobData(ailia_audio, &feature[0], feature.size() * sizeof(float), blob_idx_out0);
status = ailiaGetBlobData(ailia_audio, &feature[0], (unsigned int)(feature.size() * sizeof(float)), blob_idx_out0);
if (status != AILIA_STATUS_SUCCESS) {
PRINT_ERR("ailiaGetBlobData failed %d\n", status);
return feature;
Expand Down Expand Up @@ -307,9 +298,9 @@ static void tokenize(AILIATokenizer* tokenizer, std::string text,

input_ids = std::vector<TYPE_IDS>(token_length);
attention_mask = std::vector<TYPE_MASK>(token_length);
for (int i = 0; i < token_length; i++){
for (unsigned int i = 0; i < token_length; i++){
if (i < tokens.size()){
input_ids[i] = tokens[i];
input_ids[i] = (TYPE_IDS)tokens[i];
attention_mask[i] = 1;
}else{
input_ids[i] = 1;
Expand All @@ -318,7 +309,7 @@ static void tokenize(AILIATokenizer* tokenizer, std::string text,
}
if (debug){
PRINT_OUT("input Tokens : ");
for (int i = 0; i < input_ids.size(); i++){
for (size_t i = 0; i < input_ids.size(); i++){
PRINT_OUT("%.0f ", input_ids[i]);
}
PRINT_OUT("\n");
Expand Down Expand Up @@ -382,12 +373,12 @@ static std::vector<float> text_embedding(AILIANetwork *ailia_text_robertamodel,
}

// set input of ailia_text_robertamodel
status = ailiaSetInputBlobData(ailia_text_robertamodel, &ary_input_ids[0], ary_input_ids.size() * sizeof(TYPE_IDS), blob_idx_ids);
status = ailiaSetInputBlobData(ailia_text_robertamodel, &ary_input_ids[0], (unsigned int)(ary_input_ids.size() * sizeof(TYPE_IDS)), blob_idx_ids);
if (status != AILIA_STATUS_SUCCESS) {
PRINT_ERR("ailiaSetInputBlobData failed %d\n", status);
return features;
}
status = ailiaSetInputBlobData(ailia_text_robertamodel, &ary_attention_mask[0], ary_attention_mask.size() * sizeof(TYPE_MASK), blob_idx_mask);
status = ailiaSetInputBlobData(ailia_text_robertamodel, &ary_attention_mask[0], (unsigned int)(ary_attention_mask.size() * sizeof(TYPE_MASK)), blob_idx_mask);
if (status != AILIA_STATUS_SUCCESS) {
PRINT_ERR("ailiaSetInputBlobData failed %d\n", status);
return features;
Expand All @@ -402,7 +393,7 @@ static std::vector<float> text_embedding(AILIANetwork *ailia_text_robertamodel,

// get output
branch = std::vector<float>(shape.x * shape.y);
status = ailiaGetBlobData(ailia_text_robertamodel, &branch[0], branch.size() * sizeof(float), blob_idx_out1);
status = ailiaGetBlobData(ailia_text_robertamodel, &branch[0], (unsigned int)(branch.size() * sizeof(float)), blob_idx_out1);
if (status != AILIA_STATUS_SUCCESS) {
PRINT_ERR("ailiaGetBlobData failed %d\n", status);
return features;
Expand Down Expand Up @@ -434,7 +425,7 @@ static std::vector<float> text_embedding(AILIANetwork *ailia_text_robertamodel,
}

// set input of ailia_text_projection
status = ailiaSetInputBlobData(ailia_text_projection, &branch[0], branch.size() * sizeof(float), blob_idx_x);
status = ailiaSetInputBlobData(ailia_text_projection, &branch[0], (unsigned int)(branch.size() * sizeof(float)), blob_idx_x);
if (status != AILIA_STATUS_SUCCESS) {
PRINT_ERR("ailiaSetInputBlobData failed %d\n", status);
return features;
Expand All @@ -449,7 +440,7 @@ static std::vector<float> text_embedding(AILIANetwork *ailia_text_robertamodel,

// get output
features = std::vector<float>(shape.x * shape.y);
status = ailiaGetBlobData(ailia_text_projection, &features[0], features.size() * sizeof(float), blob_idx_text_embed);
status = ailiaGetBlobData(ailia_text_projection, &features[0], (unsigned int)(features.size() * sizeof(float)), blob_idx_text_embed);
if (status != AILIA_STATUS_SUCCESS) {
PRINT_ERR("ailiaGetBlobData failed %d\n", status);
return features;
Expand All @@ -476,7 +467,7 @@ static int get_env_id(void)
for (unsigned int i = 0; i < env_count; i++) {
AILIAEnvironment* env;
status = ailiaGetEnvironment(&env, i, AILIA_ENVIRONMENT_VERSION);
bool is_fp16 = (env->props & AILIA_ENVIRONMENT_PROPERTY_FP16) != 0;
// unused: bool is_fp16 = (env->props & AILIA_ENVIRONMENT_PROPERTY_FP16) != 0;
PRINT_OUT("env_id : %d type : %d name : %s", env->id, env->type, env->name);
PRINT_OUT("\n");
if (args_env_id == env->id){
Expand Down Expand Up @@ -575,10 +566,10 @@ int main(int argc, char **argv)
return -1;
}
PRINT_OUT("Tokenize...\n");
unsigned int num_texts = texts.size();
unsigned int num_texts = (unsigned int)texts.size();
std::vector<TYPE_IDS> ary_input_ids(num_texts * token_length);
std::vector<TYPE_MASK> ary_attention_mask(num_texts * token_length);
for (int i = 0; i < num_texts; i++){
for (size_t i = 0; i < num_texts; i++){
std::vector<TYPE_IDS> input_ids;
std::vector<TYPE_MASK> attention_mask;
tokenize(tokenizer, texts[i], input_ids, attention_mask, token_length);
Expand All @@ -599,7 +590,7 @@ int main(int argc, char **argv)
if(dim_text_feature > 0 && dim_text_feature == audio_feature.size() && text_features.size() > 0){
PRINT_OUT("===== cosine similality between text and audio =====\n");
PRINT_OUT("audio: %s\n", input_wav_path.c_str());
for (int i = 0; i < num_texts; i++){
for (size_t i = 0; i < num_texts; i++){
float sim = cos_sim(&audio_feature[0], &text_features[i * dim_text_feature], dim_text_feature);
PRINT_OUT("cossim=%.4f, word=%s\n", sim, texts[i].c_str());
}
Expand Down
22 changes: 11 additions & 11 deletions audio_processing/clap/clap_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static std::vector<float> get_mel_ailia(std::vector<float>& audio_data, const AU
int frame_n;
std::vector<float> mel;

status = ailiaAudioGetFrameLen(&frame_n, audio_data.size(), audio_cfg.window_size, audio_cfg.hop_size, center);
status = ailiaAudioGetFrameLen(&frame_n, (int)audio_data.size(), audio_cfg.window_size, audio_cfg.hop_size, center);
if (status != AILIA_STATUS_SUCCESS) {
PRINT_ERR("ailiaAudioGetFrameLen failed %d\n", status);
return mel;
Expand All @@ -38,18 +38,18 @@ static std::vector<float> get_mel_ailia(std::vector<float>& audio_data, const AU
status = ailiaAudioGetMelSpectrogram(
&mel[0],
&audio_data[0],
audio_data.size(),
(int)audio_data.size(),
audio_cfg.sample_rate,
audio_cfg.window_size,
audio_cfg.hop_size,
audio_cfg.window_size,
AILIA_AUDIO_WIN_TYPE_HANN,
frame_n,
center,
2.0, // power
2.0f, // power
AILIA_AUDIO_FFT_NORMALIZE_NONE,
audio_cfg.fmin,
audio_cfg.fmax,
(float)audio_cfg.fmin,
(float)audio_cfg.fmax,
mel_n,
AILIA_AUDIO_MEL_NORMALIZE_NONE,
AILIA_AUDIO_MEL_SCALE_FORMULA_HTK
Expand All @@ -60,8 +60,8 @@ static std::vector<float> get_mel_ailia(std::vector<float>& audio_data, const AU
}

// amplitude_to_db
const float ref = 1.0;
const float amin = 1e-10;
const float ref = 1.0f;
const float amin = 1e-10f;
for(auto v=mel.begin(); v!=mel.end(); ++v){
float s = (*v) * (*v);
if(s >= 0 && s < amin) s = amin;
Expand Down Expand Up @@ -135,7 +135,7 @@ std::vector<float> get_audio_features(std::vector<float>& audio_data, unsigned i
int chunk_frames = max_len / audio_cfg.hop_size + 1; // the +1 related to how the spectrogram is computed
int total_frames = frame_n;
if(debug){
PRINT_OUT("shrink audio %ld to be %d, frame_n=%d\n", audio_data.size(), max_len, frame_n);
PRINT_OUT("shrink audio %zu to be %d, frame_n=%d\n", audio_data.size(), max_len, frame_n);
}
if(chunk_frames == total_frames){
// there is a corner case where the audio length is
Expand Down Expand Up @@ -179,16 +179,16 @@ std::vector<float> get_audio_features(std::vector<float>& audio_data, unsigned i
else{ // padding
if(audio_data.size() < max_len){
if(debug){
PRINT_OUT("padding for audio %ld to be %d\n", audio_data.size(), max_len);
PRINT_OUT("padding for audio %zu to be %d\n", audio_data.size(), max_len);
}
std::vector<float> new_audio_data(max_len, 0);
if(data_filling == "repeatpad" || data_filling == "repeat"){
int n_repeat = max_len / audio_data.size();
int n_repeat = (int)(max_len / audio_data.size());
for(int i=0; i<n_repeat; i++){
memcpy(&new_audio_data[i * audio_data.size()], &audio_data[0], audio_data.size() * sizeof(float));
}
if(data_filling == "repeat"){
int rem = max_len - audio_data.size() * n_repeat;
int rem = (int)(max_len - audio_data.size() * n_repeat);
memcpy(&new_audio_data[n_repeat * audio_data.size()], &audio_data[0], rem * sizeof(float));
}
}
Expand Down
2 changes: 1 addition & 1 deletion audio_processing/gpt-sovits-v2-pro/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.1)
cmake_minimum_required(VERSION 3.10)

set (PROJECT_NAME gpt-sovits-v2-pro)
set (SRC_FILES ${PROJECT_NAME}.cpp ../../util/wave_reader.cpp ../../util/wave_writer.cpp)
Expand Down
2 changes: 1 addition & 1 deletion audio_processing/gpt-sovits/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.1)
cmake_minimum_required(VERSION 3.10)

set (PROJECT_NAME gpt-sovits)
set (SRC_FILES ${PROJECT_NAME}.cpp ../../util/wave_reader.cpp ../../util/wave_writer.cpp)
Expand Down
Loading
Loading