Reading sound file name from command line

This commit is contained in:
Daniel Wolf 2015-11-19 21:17:35 +01:00
parent 132adb1083
commit d6f5c2ed1e
10 changed files with 92 additions and 14 deletions

View File

@ -145,6 +145,8 @@
<sourceFolder url="file://$MODULE_DIR$/src/phone_extraction.h" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/platform_tools.h" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/platform_tools_win.cpp" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/tools.cpp" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/tools.h" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />

View File

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.3)
project(LipSync)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
@ -14,7 +14,7 @@ set(Boost_USE_STATIC_RUNTIME ON) # Use static C++ runtime
find_package(Boost REQUIRED COMPONENTS filesystem locale system)
include_directories(${Boost_INCLUDE_DIRS})
set(SOURCE_FILES src/main.cpp src/audio_input/WaveFileReader.cpp src/audio_input/WaveFileReader.h src/audio_input/ChannelDownmixer.cpp src/audio_input/ChannelDownmixer.h src/audio_input/AudioStream.h src/audio_input/SampleRateConverter.cpp src/audio_input/SampleRateConverter.h src/audio_input/wave_file_writing.cpp src/audio_input/wave_file_writing.h src/audio_input/io_tools.h src/platform_tools.h src/phone_extraction.cpp src/phone_extraction.h src/Phone.cpp src/Phone.h src/centiseconds.cpp src/centiseconds.h)
set(SOURCE_FILES src/main.cpp src/audio_input/WaveFileReader.cpp src/audio_input/WaveFileReader.h src/audio_input/ChannelDownmixer.cpp src/audio_input/ChannelDownmixer.h src/audio_input/AudioStream.h src/audio_input/SampleRateConverter.cpp src/audio_input/SampleRateConverter.h src/audio_input/wave_file_writing.cpp src/audio_input/wave_file_writing.h src/audio_input/io_tools.h src/platform_tools.h src/phone_extraction.cpp src/phone_extraction.h src/Phone.cpp src/Phone.h src/centiseconds.cpp src/centiseconds.h src/tools.cpp src/tools.h)
if(WIN32)
set(SOURCE_FILES "${SOURCE_FILES};src/platform_tools_win.cpp")
else()

View File

@ -24,10 +24,10 @@ enum class Codec {
Float = 0x03
};
WaveFileReader::WaveFileReader(std::string fileName) {
WaveFileReader::WaveFileReader(boost::filesystem::path filePath) {
// Open file
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
file.open(fileName, std::ios::binary);
file.open(filePath, std::ios::binary);
// Read header
uint32_t rootChunkId = read<uint32_t>(file);

View File

@ -4,6 +4,8 @@
#include <string>
#include <cstdint>
#include <fstream>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/fstream.hpp>
#include "AudioStream.h"
enum class SampleFormat {
@ -15,14 +17,14 @@ enum class SampleFormat {
class WaveFileReader : public AudioStream {
public:
WaveFileReader(std::string fileName);
WaveFileReader(boost::filesystem::path filePath);
virtual int getFrameRate() override ;
virtual int getFrameCount() override;
virtual int getChannelCount() override;
virtual bool getNextSample(float &sample) override;
private:
std::ifstream file;
boost::filesystem::ifstream file;
SampleFormat sampleFormat;
int frameRate;
int frameCount;

View File

@ -1,9 +1,11 @@
#include <iostream>
#include "audio_input/WaveFileReader.h"
#include "phone_extraction.h"
#include "platform_tools.h"
using std::exception;
using std::string;
using std::wstring;
using std::unique_ptr;
string getMessage(const exception& e) {
@ -17,9 +19,9 @@ string getMessage(const exception& e) {
return result;
}
unique_ptr<AudioStream> createAudioStream(string fileName) {
unique_ptr<AudioStream> createAudioStream(boost::filesystem::path filePath) {
try {
return unique_ptr<AudioStream>(new WaveFileReader(fileName));
return unique_ptr<AudioStream>(new WaveFileReader(filePath));
} catch (...) {
std::throw_with_nested(std::runtime_error("Could not open sound file.") );
}
@ -27,8 +29,17 @@ unique_ptr<AudioStream> createAudioStream(string fileName) {
int main(int argc, char *argv[]) {
try {
unique_ptr<AudioStream> audioStream = createAudioStream(R"(C:\Users\Daniel\Desktop\audio-test\test 16000Hz 1ch 16bit.wav)");
// Get sound file name
std::vector<wstring> commandLineArgs = getCommandLineArgs(argc, argv);
if (commandLineArgs.size() != 2) {
throw std::runtime_error("Invalid command line arguments. Call with sound file name as sole argument.");
}
wstring soundFileName = commandLineArgs[1];
// Create audio streams
unique_ptr<AudioStream> audioStream = createAudioStream(soundFileName);
// Detect phones
std::map<centiseconds, Phone> phones = detectPhones(std::move(audioStream));
for (auto &pair : phones) {

View File

@ -5,6 +5,8 @@
#include "audio_input/SampleRateConverter.h"
#include "audio_input/ChannelDownmixer.h"
#include "platform_tools.h"
#include "tools.h"
using std::runtime_error;
using std::unique_ptr;
using std::shared_ptr;
@ -12,9 +14,6 @@ using std::string;
using std::map;
using boost::filesystem::path;
template<typename T>
using lambda_unique_ptr = std::unique_ptr<T,std::function<void(T*)>>;
unique_ptr<AudioStream> to16kHzMono(unique_ptr<AudioStream> stream) {
// Downmix, if required
if (stream->getChannelCount() != 1) {

View File

@ -3,6 +3,8 @@
#include <boost/filesystem.hpp>
std::vector<std::wstring> getCommandLineArgs(int argc, char *argv[]);
boost::filesystem::path getBinDirectory();
#endif //LIPSYNC_PLATFORM_TOOLS_H

View File

@ -1,9 +1,31 @@
#include <Windows.h>
#include "tools.h"
#include "platform_tools.h"
#include <Windows.h>
std::vector<std::wstring> getCommandLineArgs(int argc, char **argv) {
UNUSED(argv);
// Get command line as single Unicode string
LPWSTR commandLine = GetCommandLineW();
// Split into individual args
int argumentCount;
LPWSTR* arguments = CommandLineToArgvW(commandLine, &argumentCount);
if (!arguments) throw std::runtime_error("Could not determine command line arguments.");
auto _ = finally([&arguments](){ LocalFree(arguments); });
assert(argumentCount == argc);
// Convert to vector
std::vector<std::wstring> result;
for (int i = 0; i < argumentCount; i++) {
result.push_back(arguments[i]);
}
return result;
}
boost::filesystem::path getBinDirectory() {
std::vector<wchar_t> executablePath(MAX_PATH);
std::vector<WCHAR> executablePath(MAX_PATH);
// Try to get the executable path with a buffer of MAX_PATH characters.
DWORD result = GetModuleFileNameW(0, executablePath.data(), executablePath.size());

1
src/tools.cpp Normal file
View File

@ -0,0 +1 @@
#include "tools.h"

39
src/tools.h Normal file
View File

@ -0,0 +1,39 @@
#ifndef LIPSYNC_TOOLS_H
#define LIPSYNC_TOOLS_H
#include <functional>
#include <memory>
#define UNUSED(x) ((void)(x))
template<typename T>
using lambda_unique_ptr = std::unique_ptr<T, std::function<void(T*)>>;
// The following definitions are taken from https://github.com/Microsoft/GSL.
// final_act allows you to ensure something gets run at the end of a scope
template <class F>
class final_act
{
public:
explicit final_act(F f) noexcept : f_(std::move(f)), invoke_(true) {}
final_act(final_act&& other) noexcept : f_(std::move(other.f_)), invoke_(other.invoke_) { other.invoke_ = false; }
final_act(const final_act&) = delete;
final_act& operator=(const final_act&) = delete;
~final_act() noexcept { if (invoke_) f_(); }
private:
F f_;
bool invoke_;
};
// finally() - convenience function to generate a final_act
template <class F>
final_act<F> finally(const F &f) noexcept { return final_act<F>(f); }
template <class F>
final_act<F> finally(F &&f) noexcept { return final_act<F>(std::forward<F>(f)); }
#endif //LIPSYNC_TOOLS_H