Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tornupnegatives committed May 27, 2024
1 parent bd216bd commit e9a84f1
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 136 deletions.
8 changes: 8 additions & 0 deletions src/ui/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,12 @@ std::vector<float> Application::synthesizeFrameTable(
return synthesizer.synthesize(frame_table);
}

///////////////////////////////////////////////////////////////////////////////
// Accessors //////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

SharedParameters* Application::getSharedParams() {
return &shared_params_;
}

}; // namespace tms_express
18 changes: 10 additions & 8 deletions src/ui/Application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@ class Application {
void exportBitstream(const std::vector<Frame>& frame_table,
BitstreamParameters params, const std::string& path);

///////////////////////////////////////////////////////////////////////////
// Bitstream Routines /////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////

// void encodeAudioFileToFrameTable();

// void

///////////////////////////////////////////////////////////////////////////
// LPC Analysis ///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -118,7 +110,17 @@ class Application {
std::vector<float> synthesizeFrameTable(
const std::vector<Frame>& frame_table) const;

///////////////////////////////////////////////////////////////////////////
// Accessors //////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////

SharedParameters* getSharedParams();

protected:
///////////////////////////////////////////////////////////////////////////
// Members ////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////

SharedParameters shared_params_;
};

Expand Down
104 changes: 12 additions & 92 deletions src/ui/cli/CommandLineApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

#include "ui/cli/CommandLineApp.hpp"

#include <memory>

#include "bitstream/BitstreamGeneratorParameters.hpp"
#include "ui/Application.hpp"
#include "ui/cli/EncoderCommand.hpp"
#include "ui/cli/GuiCommand.hpp"
#include "ui/cli/SynthesizerCommand.hpp"
Expand All @@ -13,100 +17,16 @@ namespace tms_express::ui {
///////////////////////////////////////////////////////////////////////////////

CommandLineApp::CommandLineApp() {
EncoderCommand::setup(this);
SynthesizerCommand::setup(this);
// GuiCommand::setup(this);

require_subcommand(1);
}

/*
void CommandLineApp::setupRenderer() {
// Shared paremeters
renderer
->add_option("--samplerate", shared_params_.sample_rate_hz,
"Audio sampling rate (Hz)")
->default_val(kDefaultSampleRateHz);
renderer
->add_option("--window", shared_params_.window_width_ms,
"Analysis window width (ms)")
->default_val(kDefaultWindowWidthMs);
// Upper tract
renderer
->add_option("--uhpf", upper_params_.highpass_cutoff_hz,
"Highpass filter cutoff for upper tract (Hz)")
->default_val(kDefaultHighpassCutoffHz);
renderer
->add_option("--ulpf", upper_params_.lowpass_cutoff_hz,
"Lowpass filter cutoff for upper tract (Hz)")
->default_val(kDefaultLowpassCutoffHz);
renderer
->add_option("--ualpha", upper_params_.pre_emphasis_alpha,
"Pre-emphasis coeff for upper tract")
->default_val(kDefaultPreEmphasisAlpha);
// Lower tract
renderer
->add_option("--lhpf", lower_params_.highpass_cutoff_hz,
"Highpass filter cutoff for lower tract (Hz)")
->default_val(kDefaultHighpassCutoffHz);
// Instantiate a shared Application class for use by all the Commands. This
// lets us implement CLI11 subcommands using static member functions while
// forcing the Commands to make use of the Application class
auto app = std::make_shared<Application>(
Application({kDefaultSampleRateHz, kDefaultWindowWidthMs}));

renderer
->add_option("--llpf", lower_params_.lowpass_cutoff_hz,
"Lowpass filter cutoff for lower tract (Hz)")
->default_val(kDefaultLowpassCutoffHz);
EncoderCommand::setup(this, app);
SynthesizerCommand::setup(this, app);

renderer
->add_option("--lalpha", lower_params_.pre_emphasis_alpha,
"Pre-emphasis coeff for lower tract")
->default_val(kDefaultPreEmphasisAlpha);
renderer
->add_option("--minpitch", lower_params_.min_pitch_hz,
"Pitch estimate floor (Hz)")
->default_val(kDefaultMinPitchHz);
renderer
->add_option("--maxpitch", lower_params_.max_pitch_hz,
"Pitch estimate ceiling (Hz)")
->default_val(kDefaultMaxPitchHz);
// Post-processor
renderer->add_option("--gainshift", post_params_.gain_shift, "Gain shift")
->default_val(kDefaultGainShift);
renderer
->add_option("--gainnorm", post_params_.normalize_gain,
"Gain normalize")
->default_val(kDefaultNormalizeGain);
renderer
->add_option("--voicedgain", post_params_.max_voiced_gain_db,
"Voiced gain estimate ceiling (dB)")
->default_val(kDefaultVoicedGainDb);
renderer
->add_option("--unvoicedgain", post_params_.max_unvoiced_gain_db,
"Unvoiced gain estimate ceiling (dB)")
->default_val(kDefaultUnvoicedGainDb);
renderer
->add_option("--repeat", post_params_.detect_repeat_frames,
"Detect repeat frames")
->default_val(kDefaultDetectRepeatFrames);
// Required
renderer->add_option("-i,--input,input", input_path_, "Path to audio file")
->required();
renderer
->add_option("-o,--output,output", output_path_, "Path to output file")
->required();
require_subcommand(1);
}
*/

}; // namespace tms_express::ui
43 changes: 22 additions & 21 deletions src/ui/cli/EncoderCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ namespace tms_express::ui {
// Interface //////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

void EncoderCommand::setup(CLI::App* app) {
void EncoderCommand::setup(CLI::App* parent,
const std::shared_ptr<Application>& app) {
auto* sub =
app->add_subcommand("encode", "Converts audio file to bitstream");
parent->add_subcommand("encode", "Converts audio file to bitstream");

// Parameter allocation ///////////////////////////////////////////////////

auto* shared_params = app->getSharedParams();

auto input_path = std::make_shared<std::string>();
auto output_path = std::make_shared<std::string>();
auto shared_params = std::make_shared<SharedParameters>();
auto upper_params = std::make_shared<UpperVocalTractParameters>();
auto lower_params = std::make_shared<LowerVocalTractParameters>();
auto post_params = std::make_shared<PostProcessorParameters>();
Expand Down Expand Up @@ -120,16 +122,16 @@ void EncoderCommand::setup(CLI::App* app) {

// Callback ///////////////////////////////////////////////////////////////

sub->callback([input_path, output_path, shared_params, upper_params,
lower_params, post_params, bitstream_params]() {
run(*input_path, *output_path, *shared_params, *upper_params,
*lower_params, *post_params, *bitstream_params);
sub->callback([app, input_path, output_path, upper_params, lower_params,
post_params, bitstream_params]() {
run(app, *input_path, *output_path, *upper_params, *lower_params,
*post_params, *bitstream_params);
});
}

int EncoderCommand::run(const std::string& input_path,
int EncoderCommand::run(const std::shared_ptr<Application>& app,
const std::string& input_path,
const std::string& output_path,
const SharedParameters& shared_params,
const UpperVocalTractParameters& upper_params,
const LowerVocalTractParameters& lower_params,
const PostProcessorParameters& post_params,
Expand Down Expand Up @@ -157,31 +159,30 @@ int EncoderCommand::run(const std::string& input_path,

const std::string logger_id = "[encoder:" + audio_input + "]:\t";

auto buffer = AudioBuffer::Create(audio_input, shared_params.sample_rate_hz,
shared_params.window_width_ms);
auto buffer =
AudioBuffer::Create(audio_input, app->getSharedParams()->sample_rate_hz,
app->getSharedParams()->window_width_ms);

std::cout << logger_id << "Loaded " << buffer->getSamples().size()
<< " samples"
<< "(" << buffer->getSampleRateHz() << " Hz)" << std::endl;

Application app(shared_params);
<< " (" << buffer->getSampleRateHz() << " Hz)" << std::endl;

auto pitch_table =
app.analyzeLowerVocalTract(lower_params, buffer->getSamples());
app->analyzeLowerVocalTract(lower_params, buffer->getSamples());
auto [coeff_table, gain_table] =
app.analyzeUpperVocalTract(upper_params, buffer->getSamples());
auto voicing_table = app.estimateVoicing(coeff_table);
app->analyzeUpperVocalTract(upper_params, buffer->getSamples());
auto voicing_table = app->estimateVoicing(coeff_table);

auto frame_table = app.buildFrameTable(pitch_table, coeff_table, gain_table,
voicing_table);
auto frame_table = app->buildFrameTable(pitch_table, coeff_table,
gain_table, voicing_table);

app.postProcessFrameTable(&frame_table, post_params);
app->postProcessFrameTable(&frame_table, post_params);

std::cout << logger_id << "Packed " << frame_table.size() << " frames"
<< std::endl;

auto bitstream_path = output.getPaths().at(0);
app.exportBitstream(frame_table, bitstream_params, bitstream_path);
app->exportBitstream(frame_table, bitstream_params, bitstream_path);
return 0;
}

Expand Down
14 changes: 11 additions & 3 deletions src/ui/cli/EncoderCommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
#ifndef TMS_EXPRESS_SRC_UI_CLI_ENCODERCOMMAND_HPP_
#define TMS_EXPRESS_SRC_UI_CLI_ENCODERCOMMAND_HPP_

#include <memory>
#include <string>

#include <CLI/CLI.hpp>

#include "bitstream/BitstreamGeneratorParameters.hpp"
#include "ui/Application.hpp"

namespace tms_express::ui {

Expand All @@ -23,11 +25,17 @@ class EncoderCommand {
// Interface //////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////

static void setup(CLI::App* app);
static void setup(CLI::App* parent,
const std::shared_ptr<Application>& app);

static int run(const std::string& input_path,
private:
///////////////////////////////////////////////////////////////////////////
// Callback ///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////

static int run(const std::shared_ptr<Application>& app,
const std::string& input_path,
const std::string& output_path,
const SharedParameters& shared_params,
const UpperVocalTractParameters& upper_params,
const LowerVocalTractParameters& lower_params,
const PostProcessorParameters& post_params,
Expand Down
22 changes: 12 additions & 10 deletions src/ui/cli/SynthesizerCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ namespace tms_express::ui {
// Interface //////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

void SynthesizerCommand::setup(CLI::App* app) {
void SynthesizerCommand::setup(CLI::App* parent,
const std::shared_ptr<Application>& app) {
auto* sub =
app->add_subcommand("synth", "Converts bitstream to audio file");
parent->add_subcommand("synth", "Converts bitstream to audio file");

// Parameter allocation ///////////////////////////////////////////////////

auto* shared_params = app->getSharedParams();

auto input_path = std::make_shared<std::string>();
auto output_path = std::make_shared<std::string>();
auto shared_params = std::make_shared<SharedParameters>();
auto upper_params = std::make_shared<UpperVocalTractParameters>();
auto lower_params = std::make_shared<LowerVocalTractParameters>();
auto post_params = std::make_shared<PostProcessorParameters>();
Expand Down Expand Up @@ -74,12 +76,13 @@ void SynthesizerCommand::setup(CLI::App* app) {

// Callback ///////////////////////////////////////////////////////////////

sub->callback([input_path, output_path, post_params]() {
run(*input_path, *output_path, *post_params);
sub->callback([app, input_path, output_path, post_params]() {
run(app, *input_path, *output_path, *post_params);
});
}

int SynthesizerCommand::run(const std::string& input_path,
int SynthesizerCommand::run(const std::shared_ptr<Application>& app,
const std::string& input_path,
const std::string& output_path,
const PostProcessorParameters& post_params) {
// Open input and output files for inspection
Expand All @@ -89,10 +92,9 @@ int SynthesizerCommand::run(const std::string& input_path,
auto bitstream_path = input.getPaths().at(0);
const std::string logger_id = "[synth:" + bitstream_path + "]:\t";

Application app({});
auto frame_table = app.importBitstream(bitstream_path);
app.postProcessFrameTable(&frame_table, post_params);
auto samples = app.synthesizeFrameTable(frame_table);
auto frame_table = app->importBitstream(bitstream_path);
app->postProcessFrameTable(&frame_table, post_params);
auto samples = app->synthesizeFrameTable(frame_table);
std::cout << logger_id << "Loaded " << samples.size() << " samples from "
<< frame_table.size() << " frames" << std::endl;

Expand Down
13 changes: 11 additions & 2 deletions src/ui/cli/SynthesizerCommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
#ifndef TMS_EXPRESS_SRC_UI_CLI_SYNTHESIZERCOMMAND_HPP_
#define TMS_EXPRESS_SRC_UI_CLI_SYNTHESIZERCOMMAND_HPP_

#include <memory>
#include <string>

#include <CLI/CLI.hpp>

#include "bitstream/BitstreamGeneratorParameters.hpp"
#include "ui/Application.hpp"

namespace tms_express::ui {

Expand All @@ -23,9 +25,16 @@ class SynthesizerCommand {
// Interface //////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////

static void setup(CLI::App* app);
static void setup(CLI::App* parent,
const std::shared_ptr<Application>& app);

static int run(const std::string& input_path,
private:
///////////////////////////////////////////////////////////////////////////
// Callback ///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////

static int run(const std::shared_ptr<Application>& app,
const std::string& input_path,
const std::string& output_path,
const PostProcessorParameters& post_params);
};
Expand Down

0 comments on commit e9a84f1

Please sign in to comment.