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
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.10)

project(NAM VERSION 0.0.1)

include(CTest)

set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")

set(CMAKE_CXX_STANDARD 20)
Expand Down
8 changes: 7 additions & 1 deletion dsp/wav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cstdint>
#include <cstring> // strncmp
#include <cmath> // pow
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
Expand Down Expand Up @@ -341,7 +342,12 @@ dsp::wav::LoadReturnCode dsp::wav::Load(const char* fileName, std::vector<float>
{
// FYI: https://www.mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
// Open the WAV file for reading
std::ifstream wavFile(fileName, std::ios::binary);
#ifdef __cpp_char8_t
const auto filePath = std::filesystem::path(reinterpret_cast<const char8_t*>(fileName));
#else
const auto filePath = std::filesystem::u8path(fileName);
#endif
std::ifstream wavFile(filePath, std::ios::binary);

// Check if the file was opened successfully
if (!wavFile.is_open())
Expand Down
5 changes: 4 additions & 1 deletion tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ include_directories(tools ${NAM_DEPS_PATH}/nlohmann)

add_executable(loadmodel loadmodel.cpp ${AUDIO_DSP_TOOLS_SOURCES})
add_executable(benchmodel benchmodel.cpp ${AUDIO_DSP_TOOLS_SOURCES})
add_executable(test_wav test_wav.cpp ${AUDIO_DSP_TOOLS_SOURCES})

add_test(NAME wav_unicode_path COMMAND test_wav)

source_group(NAM ${CMAKE_CURRENT_SOURCE_DIR} FILES ${AUDIO_DSP_TOOLS_SOURCES})

Expand Down Expand Up @@ -38,4 +41,4 @@ else()
"$<$<CONFIG:DEBUG>:-Og;-ggdb;-Werror>"
"$<$<CONFIG:RELEASE>:-Ofast>"
)
endif()
endif()
55 changes: 55 additions & 0 deletions tools/test_wav.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <array>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

#include "dsp/wav.h"

namespace
{
std::string ToUTF8(const std::filesystem::path& path)
{
const auto utf8 = path.u8string();
return {utf8.begin(), utf8.end()};
}
} // namespace

int main()
{
#ifdef _WIN32
const auto emojiDirectoryName = std::filesystem::path(L"nam-\U0001F3B8");
#else
const auto emojiDirectoryName = std::filesystem::path("nam-\xF0\x9F\x8E\xB8");
#endif
const auto testDirectory = std::filesystem::temp_directory_path() / emojiDirectoryName;
const auto wavPath = testDirectory / "ir.wav";

std::filesystem::create_directories(testDirectory);

// 16-bit, mono, 48 kHz PCM WAV containing one silent sample.
constexpr std::array<unsigned char, 46> wavData{
'R', 'I', 'F', 'F', 38, 0, 0, 0, 'W', 'A', 'V', 'E', 'f', 'm', 't', ' ', 16, 0, 0, 0, 1, 0, 1,
0, 0x80, 0xBB, 0, 0, 0, 0x77, 1, 0, 2, 0, 16, 0, 'd', 'a', 't', 'a', 2, 0, 0, 0, 0, 0};

{
std::ofstream wavFile(wavPath, std::ios::binary);
wavFile.write(reinterpret_cast<const char*>(wavData.data()), wavData.size());
}

std::vector<float> audio;
double sampleRate = 0.0;
const auto utf8Path = ToUTF8(wavPath);
const auto result = dsp::wav::Load(utf8Path.c_str(), audio, sampleRate);

std::filesystem::remove_all(testDirectory);

if (result != dsp::wav::LoadReturnCode::SUCCESS || audio.size() != 1 || sampleRate != 48000.0)
{
std::cerr << "Failed to load WAV from UTF-8 path" << std::endl;
return 1;
}

return 0;
}
Loading