Line data Source code
1 : #include "audio/recorder/recorder.h"
2 :
3 : #include <cstdio>
4 : #include <cstring>
5 : #include <ctime>
6 : #include <iostream>
7 :
8 : #include "audio/recorder/recorder_impl.h"
9 :
10 : #ifdef _WIN32
11 : #include <direct.h>
12 : #define MKDIR(path) _mkdir(path)
13 : #else
14 : #include <sys/stat.h>
15 : #define MKDIR(path) mkdir(path, 0755)
16 : #endif
17 :
18 : namespace Amplitron {
19 :
20 81 : void mkdirs(const std::string& path) {
21 81 : std::string current;
22 1271 : for (char c : path) {
23 1190 : current += c;
24 1190 : if (c == '/' || c == '\\') {
25 57 : MKDIR(current.c_str());
26 26 : }
27 : }
28 81 : MKDIR(path.c_str());
29 81 : }
30 :
31 1190 : Recorder::Recorder() {
32 456057 : for (auto& v : waveform_buf_) v.store(0.0f);
33 889 : }
34 :
35 2004 : Recorder::~Recorder() {
36 889 : if (recording_) stop();
37 2004 : }
38 :
39 9 : std::string Recorder::get_recordings_dir() {
40 : #if defined(__APPLE__)
41 3 : const char* home = std::getenv("HOME");
42 3 : if (home) {
43 3 : std::string dir = std::string(home) + "/Documents/Amplitron/recordings";
44 3 : mkdirs(dir);
45 3 : return dir;
46 3 : }
47 : #elif !defined(_WIN32)
48 3 : const char* home = std::getenv("HOME");
49 3 : if (home) {
50 3 : std::string dir = std::string(home) + "/.local/share/amplitron/recordings";
51 3 : mkdirs(dir);
52 3 : return dir;
53 3 : }
54 : #endif
55 3 : std::string dir = "recordings";
56 3 : MKDIR(dir.c_str());
57 3 : return dir;
58 3 : }
59 :
60 6 : std::string Recorder::generate_filename() {
61 6 : std::time_t now = std::time(nullptr);
62 2 : char buf[64];
63 2 : std::tm time_info;
64 : #ifdef _WIN32
65 2 : localtime_s(&time_info, &now);
66 : #else
67 4 : localtime_r(&now, &time_info);
68 : #endif
69 6 : std::strftime(buf, sizeof(buf), "%Y%m%d_%H%M%S", &time_info);
70 12 : return get_recordings_dir() + "/" + std::string(buf) + ".wav";
71 0 : }
72 :
73 : } // namespace Amplitron
|