forked from hkrn/nanoem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_audio.cc
125 lines (116 loc) · 4.82 KB
/
plugin_audio.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "emapp/Allocator.h"
#include "emapp/emapp.h"
#include "emapp/internal/StubEventPublisher.h"
#include "emapp/plugin/DecoderPlugin.h"
#include "emapp/plugin/EncoderPlugin.h"
#include "emapp/sdk/Decoder.h"
#include "emapp/sdk/Encoder.h"
#include "bx/commandline.h"
#include <stdio.h>
#if !BX_PLATFORM_WINDOWS
#define MAX_PATH PATH_MAX
#endif
using namespace nanoem;
using namespace nanoem::internal;
#if BX_PLATFORM_WINDOWS
static void
updateDllDirectory(const char *ptr, const char *base)
{
char buf[MAX_PATH];
strncpy_s(buf, sizeof(buf), base, ptr - base);
SetDllDirectoryA(buf);
}
#endif
static void
run(const char *decoderPluginPath, const char *encoderPluginPath, const char *audioPath)
{
internal::StubEventPublisher publisher;
plugin::DecoderPlugin decoder(&publisher);
plugin::EncoderPlugin encoder(&publisher);
#if BX_PLATFORM_WINDOWS
if (const char *ptr = strrchr(audioPath, '\\')) {
updateDllDirectory(ptr, audioPath);
}
else if (const char *ptr = strrchr(audioPath, '/')) {
updateDllDirectory(ptr, audioPath);
}
#endif
uint32_t fps = 30, width = 128, height = 128;
if (decoder.load(URI::createFromFilePath(decoderPluginPath)) && decoder.create()) {
Error error;
decoder.setOption(NANOEM_APPLICATION_PLUGIN_DECODER_OPTION_FPS, fps, error);
if (decoder.open(URI::createFromFilePath(audioPath), error)) {
Error error;
nanoem_frame_index_t duration =
decoder.audioFormatValue(NANOEM_APPLICATION_PLUGIN_DECODER_AUDIO_FORMAT_DURATION);
if (encoder.load(URI::createFromFilePath(encoderPluginPath)) && encoder.create()) {
encoder.setOption(NANOEM_APPLICATION_PLUGIN_ENCODER_OPTION_AUDIO_NUM_BITS,
decoder.audioFormatValue(NANOEM_APPLICATION_PLUGIN_DECODER_AUDIO_FORMAT_NUM_BITS), error);
encoder.setOption(NANOEM_APPLICATION_PLUGIN_ENCODER_OPTION_AUDIO_NUM_CHANNELS,
decoder.audioFormatValue(NANOEM_APPLICATION_PLUGIN_DECODER_AUDIO_FORMAT_NUM_CHANNELS), error);
encoder.setOption(NANOEM_APPLICATION_PLUGIN_ENCODER_OPTION_AUDIO_NUM_FREQUENCY,
decoder.audioFormatValue(NANOEM_APPLICATION_PLUGIN_DECODER_AUDIO_FORMAT_FREQUENCY), error);
encoder.setOption(NANOEM_APPLICATION_PLUGIN_ENCODER_OPTION_DURATION, duration, error);
encoder.setOption(NANOEM_APPLICATION_PLUGIN_ENCODER_OPTION_FPS, fps, error);
encoder.setOption(NANOEM_APPLICATION_PLUGIN_ENCODER_OPTION_VIDEO_WIDTH, width, error);
encoder.setOption(NANOEM_APPLICATION_PLUGIN_ENCODER_OPTION_VIDEO_HEIGHT, height, error);
if (encoder.open(URI::createFromFilePath("output.mp4"), error)) {
ByteArray audioBuffer, videoBuffer;
videoBuffer.resize(width * height * 4);
for (nanoem_frame_index_t i = 0; i < duration; i++) {
decoder.decodeAudioFrame(i, audioBuffer, error);
encoder.encodeAudioFrame(i, audioBuffer.data(), audioBuffer.size(), error);
encoder.encodeVideoFrame(i, videoBuffer.data(), videoBuffer.size(), error);
}
encoder.close(error);
}
encoder.destroy();
}
decoder.close(error);
}
else {
fprintf(stderr, "%s:%s\n", decoder.failureReason(), decoder.recoverySuggestion());
}
decoder.destroy();
}
decoder.unload();
encoder.unload();
}
int
main(int argc, char *argv[])
{
const bx::CommandLine command(argc, argv);
Allocator::initialize();
const char *commonPluginPath = command.findOption("plugin");
const char *decoderPluginPath = command.findOption("decoder", commonPluginPath);
if (!decoderPluginPath) {
fprintf(stderr, "decoder plugin (--encoder or --plugin) is missing\n");
exit(-1);
}
const char *encoderPluginPath = command.findOption("encoder", commonPluginPath);
if (!encoderPluginPath) {
fprintf(stderr, "decoder plugin (--encoder or --plugin) is missing\n");
exit(-1);
}
const char *audioPath = command.findOption("input");
if (!audioPath) {
fprintf(stderr, "audio (--input) is missing\n");
exit(-1);
}
run(decoderPluginPath, encoderPluginPath, audioPath);
Allocator::destroy();
return 0;
}
#if BX_PLATFORM_WINDOWS && !BX_PLATFORM_WINRT
int WINAPI
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
BX_UNUSED_3(hInstance, hPrevInstance, nCmdShow);
SetDllDirectoryW(L"");
char *argv[64], buffer[1024];
uint32_t size = BX_COUNTOF(buffer);
int argc = 0;
bx::tokenizeCommandLine(lpCmdLine, buffer, size, argc, argv, BX_COUNTOF(argv));
return main(argc, argv);
}
#endif