Skip to content

Commit

Permalink
--verbose for wavetable-import
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnfrishert committed Dec 4, 2018
1 parent e269ce4 commit 4a660d6
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
5 changes: 3 additions & 2 deletions lsdj_wavetable_import/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ cmake_minimum_required(VERSION 3.0.0)

set(Boost_USE_STATIC_LIBS ON)
find_package(Boost REQUIRED COMPONENTS filesystem program_options)
find_library(Fmt fmt)

# Create the executable target
add_executable(lsdj-wavetable-import main.cpp ../common/common.hpp ../common/common.cpp)
source_group(\\ FILES main.cpp ../common/common.hpp ../common/common.cpp)

target_compile_features(lsdj-wavetable-import PUBLIC cxx_std_14)
target_include_directories(lsdj-wavetable-import PUBLIC ${Boost_INCLUDE_DIRS})
target_link_libraries(lsdj-wavetable-import liblsdj ${Boost_LIBRARIES})
target_link_libraries(lsdj-wavetable-import liblsdj ${Boost_LIBRARIES} ${Fmt})

install(TARGETS lsdj-wavetable-import DESTINATION bin)
install(TARGETS lsdj-wavetable-import DESTINATION bin)
62 changes: 52 additions & 10 deletions lsdj_wavetable_import/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>

#include <fmt/format.h>

#include "../common/common.hpp"
#include "../liblsdj/project.h"

bool zero = false;
bool force = false;
bool verbose = false;

int apply(const std::string& projectName, const std::string& outputName, const std::string& wavetableName, unsigned char wavetableIndex)
{
Expand All @@ -68,6 +71,9 @@ int apply(const std::string& projectName, const std::string& outputName, const s
return 1;
}

if (verbose)
std::cout << "Loaded project " + projectPath.string() << std::endl;

lsdj_song_t* song = lsdj_project_get_song(project);

// Find the wavetable file
Expand Down Expand Up @@ -99,19 +105,30 @@ int apply(const std::string& projectName, const std::string& outputName, const s

// Compute the amount of frames we will write
const auto frameCount = wavetableSize / 16;
if (verbose)
fmt::print("Found {} frames in {}\n", frameCount, wavetablePath.string());

const auto actualFrameCount = std::min<unsigned int>(0x100 - wavetableIndex, frameCount);
if (frameCount != actualFrameCount)
std::cout << "Last " << std::to_string(frameCount - actualFrameCount) << " frames won't fit in the song" << std::endl;
{
fmt::print("Last {} frames won't fit in the song\n", frameCount - actualFrameCount);

if (verbose)
fmt::print("Writing only {} frames due to space limits\n", actualFrameCount);
}

// Check to see if we're overwriting non-default wavetables
if (!force)
{
if (verbose)
std::cout << "Comparing frames to ensure no overwriting" << std::endl;

for (auto frame = 0; frame < actualFrameCount; frame++)
{
lsdj_wave_t* wave = lsdj_song_get_wave(song, wavetableIndex + frame);
if (memcmp(wave->data, LSDJ_DEFAULT_WAVE, LSDJ_WAVE_LENGTH) != 0)
{
std::cout << "Some of the wavetable frames you are trying to overwrite already contain data. Do you want to continue? y/n\n> ";
std::cout << "Some of the wavetable frames you are trying to overwrite already contain data.\nDo you want to continue? y/n\n> ";
char answer = 'n';
std::cin >> answer;
if (answer != 'y')
Expand All @@ -121,27 +138,50 @@ int apply(const std::string& projectName, const std::string& outputName, const s
} else {
break;
}
} else if (verbose) {
fmt::print("Frame 0x{0:02X} is default\n", wavetableIndex + frame);
}
}
}

// Apply the wavetable
std::array<char, LSDJ_WAVE_LENGTH> table;
for (auto frame = 0; frame < actualFrameCount; frame++)
for (unsigned char frame = 0; frame < actualFrameCount; frame++)
{
wavetableStream.read(table.data(), sizeof(table));
lsdj_wave_t* wave = lsdj_song_get_wave(song, wavetableIndex + frame);
memcpy(wave->data, table.data(), sizeof(table));
wavetableStream.read(reinterpret_cast<char*>(wave->data), sizeof(wave->data));

if (verbose)
{
fmt::print("Wrote {} bytes to frame 0x{:02X} (", sizeof(wave->data), wavetableIndex + frame);

for (auto i = 0; i < sizeof(wave->data); i++)
fmt::print("{:02X}", wave->data[i]);
std::cout << ")" << std::endl;
}
}

// Write zero wavetables
if (zero)
{
if (verbose)
std::cout << "Padding empty frames" << std::endl;

std::array<char, LSDJ_WAVE_LENGTH> table;
table.fill(0x88);
for (auto frame = actualFrameCount; frame < 16; frame++)

for (unsigned char frame = actualFrameCount; frame < 16; frame++)
{
wavetableStream.read(table.data(), sizeof(table));
lsdj_wave_t* wave = lsdj_song_get_wave(song, wavetableIndex + frame);
memcpy(wave->data, table.data(), sizeof(table));

if (verbose)
{
fmt::print("Wrote {} bytes to frame 0x{:02X} (", sizeof(wave->data), wavetableIndex + frame);

for (auto i = 0; i < sizeof(wave->data); i++)
fmt::print("{:02X}", wave->data[i]);
std::cout << ")" << std::endl;
}
}
}

Expand All @@ -154,7 +194,7 @@ int apply(const std::string& projectName, const std::string& outputName, const s
return 1;
}

std::cout << "Successfully wrote " << std::to_string(actualFrameCount) << " frames starting at " << std::hex << std::to_string(wavetableIndex) << " to " << outputPath.filename().string() << std::endl;
fmt::print("Wrote {} frames starting at 0x{:02X} to {}\n", actualFrameCount, wavetableIndex, outputPath.filename().string());

return 0;
}
Expand Down Expand Up @@ -195,7 +235,8 @@ int main(int argc, char* argv[])
("zero,0", "Pad the wavetable with empty frames if the file < 256 bytes")
("force,f", "Force writing the frames, even though non-default data may be in them")
("output,o", boost::program_options::value<std::string>(), "The output .lsdsng to write to")
("index,i", "The index should be interpreted as a wavetable index instead of synth");
("index,i", "The index should be interpreted as a wavetable index instead of synth")
("verbose,v", "Verbose output");

boost::program_options::options_description options;
options.add(cmdOptions).add(hidden);
Expand All @@ -221,6 +262,7 @@ int main(int argc, char* argv[])
} else if (vm.count("project") && vm.count("wavetable") && vm.count("synth")) {
zero = vm.count("zero");
force = vm.count("force");
verbose = vm.count("verbose");

auto project = vm["project"].as<std::string>();
auto output = vm.count("output") ? vm["output"].as<std::string>() : project;
Expand Down

0 comments on commit 4a660d6

Please sign in to comment.