-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
329 lines (273 loc) · 9.89 KB
/
main.cpp
File metadata and controls
329 lines (273 loc) · 9.89 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "lib/tinyfiledialogs.h"
#include <SFML/OpenGL.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <climits>
#include <cmath>
#include <sstream>
#include <functional>
#include <queue>
//Vector helper macros
#define vlength(a) (float)sqrt((a).x*(a).x + (a).y*(a).y)
#define vnormalize(a) (a) / vlength(a)
#define vdot(a,b) ((a).x*(b).x)+((a).y*(b).y)
#define vproject(a,b) (a)*(vdot(a,b)/(vlength(a)*vlength(a)))
//Math helper macros
#define msign(a) (a==0 ? 0 : (a>0 ? 1 : -1))
#define mclamp(a,b,c) ((a)<(b)?(b):((a)>(c)?(c):(a)))
//TODO: Screen resolution should not be constant and probably be exposed as a command line argument
#define WINDOW_WIDTH 1920
#define WINDOW_HEIGHT 1080
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
void drawWaveform(sf::RenderTarget& target, sf::SoundBuffer& buffer, int playbackPos, const uint8 index, const uint8 count, const uint32 length);
short averageSample(const short* samples, const uint32 startIndex, const uint8 count, const uint32 length);
void encodeFrame(FILE* ffmpeg, sf::Mutex* mutex, std::queue<sf::Image>* buffer, bool* isDone);
int main(int argc, char* argv[])
{
bool preview = true;
//TODO: Add command line argument to change this
int sampleLen = 1500;
std::string outputFile = "output.mp4";
std::vector<std::string> inputFiles;
if(argc == 1) {
char const * openFilters[] = { "*.wav" };
char const * saveFilters[] = { "*.mp4" };
const char* path = tinyfd_openFileDialog("Select master audio", NULL, 1, openFilters, "Wave-files", 0);
if(path == NULL)
return -1;
inputFiles.push_back(path);
path = tinyfd_openFileDialog("Select waveform channels", NULL, 1, openFilters, "Wave-files", 1);
if(path == NULL)
return -1;
std::string paths = path;
std::string delimiter = "|";
size_t pos = 0;
std::string token;
while((pos = paths.find(delimiter)) != std::string::npos) {
token = paths.substr(0, pos);
inputFiles.push_back(token);
paths.erase(0, pos + delimiter.length());
}
inputFiles.push_back(paths);
//TODO: Set thickness/color(s)/other settings
path = tinyfd_saveFileDialog("Set output file", NULL, 1, saveFilters, "MP4 video");
if (path == NULL)
return -1;
std::string output = path;
if (output.find(".mp4") == std::string::npos) {
std::cout << "File ending missing!" << std::endl;
output += ".mp4";
}
outputFile = output;
} else {
std::string prevCmd = "";
for(uint8 i = 1; i < argc; i++) {
std::string cmd = std::string(argv[i]);
if(prevCmd != "" && cmd[0] != '-') {
if(prevCmd == "-i") {
std::string input = std::string(argv[i]);
inputFiles.push_back(input);
std::cout << "Input:" << input << std::endl;
} else if(prevCmd == "-o") {
outputFile = std::string(argv[i]);
std::cout << "Output: " << outputFile << std::endl;
}
} else {
//TODO: Add command line argument for gain
if(cmd == "-i" || cmd == "-o") {
prevCmd = std::string(argv[i]);
} else if(cmd == "-h") {
preview = false;
} else if(cmd == "-?") {
std::cout << "Oscigen commands" << std::endl << std::endl
<< "-i\tSpecifies audio files used. First is master audio." << std::endl
<< "-o\tOutput file. Default is \"output.mp4\"." << std::endl
//TODO: Fix headless mode
<< "-h\tHeadless mode, disables preview window. (broken)" << std::endl
<< "-?\tPrints all available commands." << std::endl;
return -1;
} else {
std::cout << cmd << " is not a recognised command! Use -? for list of available commands." << std::endl;
return -1;
}
}
}
}
std::vector<sf::SoundBuffer*> buffers;
for (uint8 i = 0; i < inputFiles.size(); i++) {
sf::SoundBuffer* buffer = new sf::SoundBuffer();
std::cout << "Loading " << inputFiles[i] << "..." << std::endl;
if(buffer->loadFromFile(inputFiles[i])) {
buffers.push_back(buffer);
} else {
std::cout << "ERROR: " << inputFiles[i] << " is either invalid or does not exist." << std::endl;
}
}
int bufSize = buffers.size();
if(bufSize == 0) {
std::cout << "ERROR: No valid input file(s) specified!" << std::endl;
return -1;
}
std::cout << "Trying to open window!" << std::endl;
//TODO: Figure out a way to enable anti-aliasing without using a RenderWindow
sf::ContextSettings settings(8, 8, 16);
sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "Rendering...", sf::Style::Default, settings);
sf::Texture capture;
capture.create(WINDOW_WIDTH, WINDOW_HEIGHT);
std::ostringstream cmd;
cmd << "ffmpeg -r 60 -f rawvideo -pix_fmt rgba -s " << WINDOW_WIDTH << "x" << WINDOW_HEIGHT << " -i - -i " << inputFiles[0] << " -strict -2 " <<
"-threads 0 -preset fast -y -pix_fmt yuv420p -crf 21 " << outputFile;
#if WIN32
FILE* ffmpeg = _popen(cmd.str().c_str(), "w");
if (!ffmpeg)
std::cout << "Failed to open pipe!!!" << std::endl;
#else
FILE* ffmpeg = popen(cmd.str().c_str(), "w");
#endif
const uint16 samplesPerFrame = buffers[0]->getSampleRate() / 60;
const uint32 numFrames = buffers[0]->getSampleCount() / buffers[0]->getChannelCount() / samplesPerFrame;
std::cout << "Preparing to render " << numFrames << " frames" << std::endl;
bool isDone = false;
std::queue<sf::Image> framebuffer;
sf::Mutex encodeMutex;
sf::Thread encodeThread(std::bind(&encodeFrame, ffmpeg, &encodeMutex, &framebuffer, &isDone));
encodeThread.launch();
//TODO: Rendering could probably be even faster if we could render multiple frames on separate threads
// This requires that we get anti-aliasing working on RenderTextures
// Probably limited by FFmpeg encoding speed though
uint32 currentFrame = 0;
while(currentFrame < numFrames && window.isOpen()) {
sf::Event event;
while(window.pollEvent(event)){
if(event.type == sf::Event::Closed)
window.close();
}
window.clear();
uint32 sampleIndex = currentFrame * samplesPerFrame;
if(bufSize > 1) {
for (uint8 i = 0; i < bufSize - 1; i++) {
drawWaveform(window, *buffers[i + 1], sampleIndex, i, bufSize - 1, sampleLen);
}
} else {
drawWaveform(window, *buffers[0], sampleIndex, 0, 1, sampleLen);
}
window.display();
capture.update(window);
sf::Image img = capture.copyToImage();
encodeMutex.lock();
framebuffer.push(img);
encodeMutex.unlock();
currentFrame++;
int progress = ceil(((float)currentFrame / (float)numFrames) * 10000) / 100;
std::ostringstream title;
title << "oscigen (" << progress << "%)";
window.setTitle(title.str());
}
isDone = true;
encodeThread.wait();
#if WIN32
_pclose(ffmpeg);
#else
pclose(ffmpeg);
#endif
window.close();
return 0;
}
void encodeFrame(FILE* ffmpeg, sf::Mutex* mutex, std::queue<sf::Image>* buffer, bool* isDone) {
while(!*isDone) {
mutex->lock();
if(buffer->size() > 0) {
fwrite(buffer->front().getPixelsPtr(), WINDOW_WIDTH*WINDOW_HEIGHT*4, 1, ffmpeg);
buffer->pop();
}
mutex->unlock();
}
}
void drawWaveform(sf::RenderTarget& target, sf::SoundBuffer& buffer, int playbackPos, const uint8 index, const uint8 count, const uint32 length) {
float interval = WINDOW_WIDTH / (float)length;
float waveHeight = WINDOW_HEIGHT * (1.0f / count);
float invAmplitude = (1.0f / SHRT_MAX) * waveHeight;
float verticalCenter = WINDOW_HEIGHT / (count + 1) + waveHeight * index;
if(count > 1)
verticalCenter -= waveHeight * 0.25f;
const uint32 numSamples = buffer.getSampleCount();
const uint32 channels = buffer.getChannelCount();
const short* samples = buffer.getSamples();
playbackPos *= channels;
uint16 centerSnap = 0;
while(averageSample(samples, playbackPos + centerSnap + length, channels, numSamples) > 0)
centerSnap += channels;
while(averageSample(samples, playbackPos + centerSnap + length, channels, numSamples) < 0)
centerSnap += channels;
playbackPos += centerSnap;
const int averageCount = 2;
std::vector<sf::Vector2f> points;
for (uint16 i = 0; i < length; i+=2) {
int ampAverage = 0;
for (uint8 s = 0; s < averageCount; s++) {
uint32 samplePos = playbackPos + (i + s) * channels;
ampAverage += averageSample(samples, samplePos, channels, numSamples);
}
ampAverage /= averageCount;
sf::Vector2f pos = sf::Vector2f(i * interval, verticalCenter - ampAverage * invAmplitude);
points.push_back(pos);
}
std::vector<sf::Vertex> triVerts;
sf::Vector2f prevPos = points[0];
sf::Vector2f currPos = points[1];
//TODO: Expose this as a command line argument
const float thickness = 4;
glLineWidth(thickness);
//TODO: Expose this as well?
const int maxSegments = 8;
for(auto it = points.begin() + 1; it != points.end() - 1; it++) {
//TODO: This can probably be optimized even further
sf::Vector2f nextPos = *(it + 1);
sf::Vector2f l1 = vnormalize(nextPos - currPos);
sf::Vector2f n1(l1.y, -l1.x);
sf::Vector2f l2 = vnormalize(currPos - prevPos);
sf::Vector2f n2(l2.y, -l2.x);
if(vdot(n1, l2) < 0) {
n1 = -n1;
n2 = -n2;
}
int segments = ceil((1 - (1 + vdot(n1, n2)) * 0.5) * maxSegments);
float invSegment = 1.0f / segments;
sf::Vector2f s1;
sf::Vector2f s2 = n1;
s2 = vnormalize(s2);
for(int i = 1; i <= segments; i++) {
float s = invSegment * i;
s1 = s2;
s2 = n1 * (1 - s) + n2 * s;
s2 = vnormalize(s2);
triVerts.push_back(currPos);
triVerts.push_back(currPos + s1 * thickness * 0.5f);
triVerts.push_back(currPos + s2 * thickness * 0.5f);
}
n2 *= thickness * 0.5f;
triVerts.push_back(prevPos + n2);
triVerts.push_back(prevPos - n2);
triVerts.push_back(currPos + n2);
triVerts.push_back(prevPos - n2);
triVerts.push_back(currPos + n2);
triVerts.push_back(currPos - n2);
prevPos = currPos;
currPos = nextPos;
}
target.draw(triVerts.data(), triVerts.size(), sf::Triangles);
}
short averageSample(const short* samples, const uint32 startIndex, const uint8 count, const uint32 length) {
if(startIndex + count >= length)
return 0;
short result = 0;
for (uint32 i = startIndex; i < startIndex + count; i++) {
result += samples[i];
}
return result / count;
}