Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
131 lines (108 loc) · 3.75 KB
/
Copy pathmain.cpp
File metadata and controls
131 lines (108 loc) · 3.75 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
#include <atomic>
#include <iostream>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <libusb.h>
#include "player.hpp"
using namespace std;
namespace {
shs::Player* g_player_for_signal = nullptr;
std::atomic<int> g_last_note_per_channel[shs::kChannelCount] = {
{shs::kNoteStop}, {shs::kNoteStop}};
void display_played_notes(int channel, int note) {
const char* text_per_channel[shs::kChannelCount] = {
"LEFT haptic : ", ", RIGHT haptic : "};
const char* note_base_name[12] = {
"C-", "C#", "D-", "D#", "E-", "F-",
"F#", "G-", "G#", "A-", "A#", "B-"};
if (channel < 0 || channel >= shs::kChannelCount) return;
// Match original ordering: store reversed so channel 0 (right) shows on the right.
g_last_note_per_channel[shs::kChannelCount - 1 - channel].store(note);
for (int i = 0; i < shs::kChannelCount; ++i) {
int n = g_last_note_per_channel[i].load();
cout << text_per_channel[i];
if (n == shs::kNoteStop) {
cout << "OFF ";
} else {
cout << note_base_name[n % 12];
int octave = (n / 12) - 1;
cout << octave;
if (octave >= 0) cout << " ";
}
}
cout << "\r";
cout.flush();
}
bool parse_arguments(int argc, char** argv, shs::PlayerOptions* options) {
int c;
while ((c = getopt(argc, argv, "d:i:p")) != -1) {
unsigned long int value;
switch (c) {
case 'd':
value = strtoul(optarg, NULL, 10);
if (value >= LIBUSB_LOG_LEVEL_NONE && value <= LIBUSB_LOG_LEVEL_DEBUG) {
options->libusb_debug_level = static_cast<int>(value);
}
break;
case 'i':
value = strtoul(optarg, NULL, 10);
if (value <= 1000000 && value > 0) {
options->interval_usec = static_cast<unsigned int>(value);
}
break;
case 'p':
options->repeat = true;
break;
case '?':
return false;
default:
break;
}
}
if (optind == argc - 1) {
options->midi_path = argv[optind];
return true;
}
return false;
}
void on_sigint(int) {
if (g_player_for_signal) {
g_player_for_signal->request_stop();
g_player_for_signal->silence_all();
}
cout << endl << "Aborted " << endl;
cout.flush();
exit(1);
}
} // namespace
int main(int argc, char** argv) {
cout << "Steam Haptics Singer by Crazy, Steam Controller Singer by Pila" << endl;
shs::PlayerOptions options;
if (!parse_arguments(argc, argv, &options)) {
cout << "Usage: steam-haptics-singer [-p] [-y] [-d DEBUG_LEVEL] [-i INTERVAL] MIDI_FILE\n\n"
" -i INTERVAL\t\tPlayer sleep interval (in microseconds). Lower generally means better song fidelity, but higher cpu usage, and at some point going lower won't improve any more. Default value is 10000\n"
" -d DEBUG_LEVEL\tLibusb debug level. Default is 0, no debug output. max is 4, max verbosity output\n"
" -p\tRepeat song, plays again after ending\n"
" -y\tLegacy playback, forces usage of the old Steam Controller haptic instruction instead of the new one (causes issues)"
<< endl;
return 1;
}
shs::Player player;
g_player_for_signal = &player;
player.set_log_callback([](const std::string& msg) {
cout << msg << endl;
});
player.set_note_callback(&display_played_notes);
if (!player.init_usb(options.libusb_debug_level)) {
return 1;
}
if (!player.open_controller()) {
return 1;
}
signal(SIGINT, on_sigint);
player.play(options);
player.close_controller();
return 0;
}