Skip to content
Open
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
28 changes: 23 additions & 5 deletions .github/workflows/build-libpiper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,49 @@ defaults:
shell: bash

jobs:
format-check:
name: Check Formatting
runs-on: ubuntu-latest
defaults:
run:
working-directory: libpiper

steps:
- uses: actions/checkout@v4

- name: Install clang-format
run: sudo apt-get update && sudo apt-get install -y clang-format

- name: Configure for format check
run: cmake -B build -DCLANG_FORMAT_ARGS="--dry-run;--Werror"

- name: Run clang-format check
run: cmake --build build --target format

build:
name: Build on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
defaults:
run:
working-directory: libpiper

steps:
- uses: actions/checkout@v4

- name: Configure
working-directory: libpiper
run: cmake -B build -DPIPER_BUILD_TESTS=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$PWD/install"
run: cmake -B build -DPIPER_BUILD_TESTS=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_INSTALL_PREFIX="$PWD/install"

- name: Build
working-directory: libpiper
run: cmake --build build --config Release

- name: Test
working-directory: libpiper
run: ctest -C Release --output-on-failure --test-dir build

- name: Install
working-directory: libpiper
run: cmake --install build --config Release

- name: Upload install artifact
Expand Down
9 changes: 9 additions & 0 deletions libpiper/ .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Language: Cpp
BasedOnStyle: Google
# Configure for your project's style
IndentWidth: 2
UseTab: Never
BreakBeforeBraces: Allman
AllowShortFunctionsOnASingleLine: None
IndentCaseLabels: false
ColumnLimit: 80
34 changes: 34 additions & 0 deletions libpiper/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,37 @@ if(PIPER_BUILD_TESTS)
enable_testing()
add_subdirectory(tests)
endif()



# ---- clang format ---
find_program(CLANG_FORMAT clang-format)

set(CLANG_FORMAT_ARGS "-i" CACHE STRING "Arguments for clang-format")

if(CLANG_FORMAT)
file(GLOB_RECURSE FORMAT_SOURCES
"src/*.cpp"
"src/*.h"
"src/*.hpp"
"src/**/*.cpp"
"src/**/*.h"
"src/**/*.hpp"
"tests/**/*.cpp"
"tests/**/*.h"
"tests/**/*.hpp"
"tests/*.cpp"
"tests/*.h"
"include/piper.h"
"include/piper_impl.hpp"
)

add_custom_target(format
COMMAND ${CLANG_FORMAT} ${CLANG_FORMAT_ARGS} ${FORMAT_SOURCES}
COMMENT "Formatting C/C++ files with clang-format"
)
message(STATUS "Added 'format' target to run clang-format.")
add_dependencies(piper format)
else()
message(WARNING "clang-format not found. The 'format' target will not be available.")
endif()
10 changes: 4 additions & 6 deletions libpiper/include/piper.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
#include <stdint.h>
#include <uchar.h>

#if defined (WIN32) && (defined(__GNUC__) || defined (_MSC_VER))
#if defined(WIN32) && (defined(__GNUC__) || defined(_MSC_VER))
#if defined(BUILDING_LIBPIPER)
#define EXPORT_SYMBOL __declspec( dllexport )
#define EXPORT_SYMBOL __declspec(dllexport)
#else
#define EXPORT_SYMBOL __declspec( dllimport )
#define EXPORT_SYMBOL __declspec(dllimport)
#endif
#else
#define EXPORT_SYMBOL
Expand Down Expand Up @@ -225,14 +225,12 @@ int piper_synthesize_start(piper_synthesizer *synth, const char *text,
EXPORT_SYMBOL
int piper_synthesize_next(piper_synthesizer *synth, piper_audio_chunk *chunk);


/**
/**
* \return piper version
*/
EXPORT_SYMBOL
char const *piper_version(void);


#ifdef __cplusplus
}
#endif
Expand Down
73 changes: 36 additions & 37 deletions libpiper/include/piper_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const float DEFAULT_NOISE_W_SCALE = 0.8f;

const int DEFAULT_HOP_LENGTH = 256;


// espeak
#define CLAUSE_INTONATION_FULL_STOP 0x00000000
#define CLAUSE_INTONATION_COMMA 0x00001000
Expand All @@ -48,52 +47,52 @@ const int DEFAULT_HOP_LENGTH = 256;
#define CLAUSE_COMMA (20 | CLAUSE_INTONATION_COMMA | CLAUSE_TYPE_CLAUSE)
#define CLAUSE_QUESTION (40 | CLAUSE_INTONATION_QUESTION | CLAUSE_TYPE_SENTENCE)
#define CLAUSE_EXCLAMATION \
(45 | CLAUSE_INTONATION_EXCLAMATION | CLAUSE_TYPE_SENTENCE)
(45 | CLAUSE_INTONATION_EXCLAMATION | CLAUSE_TYPE_SENTENCE)
#define CLAUSE_COLON (30 | CLAUSE_INTONATION_FULL_STOP | CLAUSE_TYPE_CLAUSE)
#define CLAUSE_SEMICOLON (30 | CLAUSE_INTONATION_COMMA | CLAUSE_TYPE_CLAUSE)

struct piper_synthesizer {
// From config JSON file
std::string espeak_voice;
int sample_rate;
int num_speakers;
PhonemeIdMap phoneme_id_map;
int hop_length = DEFAULT_HOP_LENGTH;

// Default synthesis settings for the voice
float synth_length_scale = DEFAULT_LENGTH_SCALE;
float synth_noise_scale = DEFAULT_NOISE_SCALE;
float synth_noise_w_scale = DEFAULT_NOISE_W_SCALE;

// onnx
std::unique_ptr<Ort::Session> session;
Ort::AllocatorWithDefaultOptions session_allocator;
Ort::SessionOptions session_options;
Ort::Env session_env;

// synthesize state
std::queue<std::pair<std::vector<Phoneme>, std::vector<PhonemeId>>>
phoneme_id_queue;
std::vector<float> chunk_samples;
std::vector<int> chunk_phoneme_ids;
std::vector<Phoneme> chunk_phonemes;
std::vector<int> chunk_alignments;
float length_scale = DEFAULT_LENGTH_SCALE;
float noise_scale = DEFAULT_NOISE_SCALE;
float noise_w_scale = DEFAULT_NOISE_W_SCALE;
SpeakerId speaker_id = 0;
// From config JSON file
std::string espeak_voice;
int sample_rate;
int num_speakers;
PhonemeIdMap phoneme_id_map;
int hop_length = DEFAULT_HOP_LENGTH;

// Default synthesis settings for the voice
float synth_length_scale = DEFAULT_LENGTH_SCALE;
float synth_noise_scale = DEFAULT_NOISE_SCALE;
float synth_noise_w_scale = DEFAULT_NOISE_W_SCALE;

// onnx
std::unique_ptr<Ort::Session> session;
Ort::AllocatorWithDefaultOptions session_allocator;
Ort::SessionOptions session_options;
Ort::Env session_env;

// synthesize state
std::queue<std::pair<std::vector<Phoneme>, std::vector<PhonemeId>>>
phoneme_id_queue;
std::vector<float> chunk_samples;
std::vector<int> chunk_phoneme_ids;
std::vector<Phoneme> chunk_phonemes;
std::vector<int> chunk_alignments;
float length_scale = DEFAULT_LENGTH_SCALE;
float noise_scale = DEFAULT_NOISE_SCALE;
float noise_w_scale = DEFAULT_NOISE_W_SCALE;
SpeakerId speaker_id = 0;
};

// Get the first UTF-8 codepoint of a string
std::optional<Phoneme> get_codepoint(std::string s) {
auto view = una::views::utf8(s);
auto it = view.begin();
auto view = una::views::utf8(s);
auto it = view.begin();

if (it != view.end()) {
return *it;
}
if (it != view.end()) {
return *it;
}

return std::nullopt;
return std::nullopt;
}

#endif // PIPER_IMPL_H_
25 changes: 12 additions & 13 deletions libpiper/src/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
#include <mach-o/dyld.h>
#endif

#include "utils/process.hpp"
#include "utils/main_utils.hpp"
#include "utils/process.hpp"

#include "json.hpp"
#include "piper.h"
Expand Down Expand Up @@ -68,21 +68,20 @@ int main(int argc, char *argv[]) {
#endif
#endif

if (runConfig.eSpeakDataPath) {
// User provided path
runConfig.eSpeakDataPath = runConfig.eSpeakDataPath.value().string();
} else {
// Assume next to piper executable
runConfig.eSpeakDataPath =
std::filesystem::absolute(
exePath.parent_path().append("espeak-ng-data"))
.string();
}
if (runConfig.eSpeakDataPath) {
// User provided path
runConfig.eSpeakDataPath = runConfig.eSpeakDataPath.value().string();
} else {
// Assume next to piper executable
runConfig.eSpeakDataPath =
std::filesystem::absolute(
exePath.parent_path().append("espeak-ng-data"))
.string();
}
auto startTime = chrono::steady_clock::now();
piper = piper_create(runConfig.modelPath.string().c_str(),
runConfig.modelConfigPath.string().c_str(),
runConfig.eSpeakDataPath->string().c_str()
);
runConfig.eSpeakDataPath->string().c_str());
auto endTime = chrono::steady_clock::now();

piper_synthesize_options options;
Expand Down
22 changes: 12 additions & 10 deletions libpiper/src/main/utils/main_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,27 @@ void printUsage(char *argv[]) {
<< std::endl;
std::cerr << " -s NUM --speaker NUM id of speaker (default: 0)"
<< std::endl;
std::cerr << " --noise_scale NUM generator noise (default: 0.667)"
<< std::endl;
std::cerr
<< " --noise_scale NUM generator noise (default: 0.667)"
<< std::endl;
std::cerr << " --length_scale NUM phoneme length (default: 1.0)"
<< std::endl;
std::cerr << " --noise_w NUM phoneme width noise (default: 0.8)"
<< std::endl;
std::cerr << " --espeak_data DIR path to espeak-ng data directory"
<< std::endl;
std::cerr
<< " --noise_w NUM phoneme width noise (default: 0.8)"
<< std::endl;
std::cerr
<< " --espeak_data DIR path to espeak-ng data directory"
<< std::endl;
std::cerr << " --json-input stdin input is lines of JSON "
"instead of plain text"
<< std::endl;
std::cerr << std::endl;
}

void ensureArg(int argc, char *argv[], int argi) {
if ((argi + 1) >= argc)
{
throw ArgError(std::string("Missing argument for ") + argv[argi]);
}
if ((argi + 1) >= argc) {
throw ArgError(std::string("Missing argument for ") + argv[argi]);
}
}

// Parse command-line arguments
Expand Down
1 change: 0 additions & 1 deletion libpiper/src/main/utils/main_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <optional>
#include <stdexcept>


namespace piper {

enum OutputType { OUTPUT_FILE, OUTPUT_DIRECTORY, OUTPUT_STDOUT };
Expand Down
Loading
Loading