Skip to content

Commit 2e19faa

Browse files
committed
slightly better audio implementation
1 parent d481f5f commit 2e19faa

1 file changed

Lines changed: 35 additions & 9 deletions

File tree

src/recorder.cpp

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#include "recorder.hpp"
22
#include <sstream>
33
#include <CCGL.h>
4+
#include <filesystem>
45

56
Recorder::Recorder() : m_width(1280), m_height(720), m_fps(60) {}
67

8+
const float extra_length_at_the_end = 3.f;
9+
710
void Recorder::start(const std::string& path) {
811
m_recording = true;
912
m_finished_level = false;
@@ -12,12 +15,16 @@ void Recorder::start(const std::string& path) {
1215
m_renderer.m_width = m_width;
1316
m_renderer.m_height = m_height;
1417
m_renderer.begin();
15-
auto play_layer = gd::GameManager::sharedState()->getPlayLayer();
18+
auto gm = gd::GameManager::sharedState();
19+
auto play_layer = gm->getPlayLayer();
1620
auto song_file = play_layer->m_level->getAudioFileName();
17-
// these are always false???
1821
auto fade_in = play_layer->m_levelSettings->m_fadeIn;
1922
auto fade_out = play_layer->m_levelSettings->m_fadeOut;
20-
std::thread([&, path, song_file]() {
23+
auto bg_volume = gm->m_fBackgroundMusicVolume;
24+
auto sfx_volume = gm->m_fEffectsVolume;
25+
if (play_layer->m_level->songID == 0)
26+
song_file = CCFileUtils::sharedFileUtils()->fullPathForFilename(song_file.c_str(), false);
27+
std::thread([&, path, song_file, fade_in, fade_out, bg_volume, sfx_volume]() {
2128
std::stringstream stream;
2229
stream << "ffmpeg -y -f rawvideo -pix_fmt rgb24 -s " << m_width << "x" << m_height << " -r " << m_fps
2330
<< " -i - ";
@@ -41,22 +48,41 @@ void Recorder::start(const std::string& path) {
4148
}
4249
m_lock.unlock();
4350
}
44-
process.close();
51+
if (process.close()) {
52+
std::cout << "ffmpeg errored :(" << std::endl;
53+
return;
54+
}
4555
std::cout << "should be done now!" << std::endl;
56+
if (!std::filesystem::exists(song_file)) return;
4657
std::cout << "sike" << std::endl;
58+
char buffer[MAX_PATH];
59+
if (!GetTempFileNameA(std::filesystem::temp_directory_path().string().c_str(), "rec", 0, buffer)) {
60+
std::cout << "error getting temp file" << std::endl;
61+
return;
62+
}
63+
auto temp_path = std::string(buffer) + "." + std::filesystem::path(path).filename().string();
64+
std::filesystem::rename(buffer, temp_path);
4765
auto total_time = m_last_frame_t; // 1 frame too short?
4866
{
4967
std::stringstream stream;
5068
stream << "ffmpeg -y -ss " << m_song_start_offset << " -i \"" << song_file
5169
<< "\" -i \"" << path << "\" -t " << total_time << " -c:v copy "
52-
<< "-filter:a \"";
70+
<< "-filter:a \"volume=" << bg_volume << "[0:a]";
71+
if (fade_in)
72+
stream << ";[0:a]afade=t=in:d=2[0:a]";
73+
if (fade_out)
74+
stream << ";[0:a]afade=t=out:d=2:st=" << (total_time - extra_length_at_the_end - 3.5f) << "[0:a]";
5375
std::cout << "in " << fade_in << " out " << fade_out << std::endl;
54-
stream << "afade=t=in:d=0.5[out];[out]afade=t=out:d=0.5:st=" << (total_time - 0.5f - 3.f - 1.f);
55-
stream << "\" " << path << ".sound.mp4";
76+
stream << "\" \"" << temp_path << "\"";
5677
std::cout << "executing: " << stream.str() << std::endl;
5778
auto process = subprocess::Popen(stream.str());
58-
process.close();
79+
if (process.close()) {
80+
std::cout << "oh god adding the song went wrong cmon" << std::endl;
81+
return;
82+
}
5983
}
84+
std::filesystem::remove(path);
85+
std::filesystem::rename(temp_path, path);
6086
}).detach();
6187
}
6288

@@ -124,7 +150,7 @@ void Recorder::capture_frame() {
124150
}
125151

126152
void Recorder::handle_recording(gd::PlayLayer* play_layer, float dt) {
127-
if (!play_layer->m_hasLevelCompleteMenu || m_after_end_extra_time < 3.f) {
153+
if (!play_layer->m_hasLevelCompleteMenu || m_after_end_extra_time < extra_length_at_the_end) {
128154
if (play_layer->m_hasLevelCompleteMenu) {
129155
m_after_end_extra_time += dt;
130156
m_finished_level = true;

0 commit comments

Comments
 (0)