-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
109 lines (89 loc) · 2.48 KB
/
Copy pathmain.cpp
File metadata and controls
109 lines (89 loc) · 2.48 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
#include <iostream>
#include <chrono>
#include <thread>
#include <fstream>
#include <vector>
#include <string>
#ifdef __linux__
#include <AL/al.h>
#include <AL/alut.h>
#elif WIN32
#include <al.h>
#include <alut.h>
#endif
#include "notes.h"
static void play(std::vector<ALfloat>& freq, ALfloat duration) {
if(freq.size() == 0) {
std::this_thread::sleep_for(std::chrono::duration<float>(duration));
return;
}
std::vector<ALuint> buf(freq.size());
std::vector<ALuint> sources(freq.size());
alGenSources(sources.size(), sources.data());
for(unsigned int i = 0; i < freq.size(); i++) {
buf[i] = alutCreateBufferWaveform(ALUT_WAVEFORM_SINE, freq[i], 0, duration);
alSourcef(sources[i], AL_PITCH, 1.0);
alSourcef(sources[i], AL_GAIN, 1.0f / 3);
alSourcei(sources[i], AL_LOOPING, 0);
alSource3f(sources[i], AL_POSITION, 0, 0, 0);
alSource3f(sources[i], AL_VELOCITY, 0, 0, 0);
alSourcei(sources[i], AL_BUFFER, buf[i]);
}
alSourcePlayv(freq.size(), sources.data());
std::this_thread::sleep_for(std::chrono::duration<float>(duration + 0.05f));
alSourceStop(sources[0]);
alDeleteSources(freq.size(), sources.data());
alDeleteBuffers(freq.size(), buf.data());
}
int main(int argc, char **argv) {
if(argc != 2)
return -1;
alutInit(&argc, argv);
ALfloat ori[] = {0, 0, 1, 0, 1, 0};
alListener3f(AL_POSITION, 0, 0, 0);
alListener3f(AL_VELOCITY, 0, 0, 0);
alListenerfv(AL_ORIENTATION, ori);
std::ifstream file(argv[1], std::ifstream::in);
size_t index1;
size_t index2;
std::string line;
while(!file.eof()) {
std::getline(file, line);
if(line[0] == '#')
continue;
guitar_chord chord;
int line_type;
index1 = 0;
index2 = 0;
while(index2 != std::string::npos) {
index2 = line.find(" ", index1);
if(index2 != std::string::npos) {
std::string s = line.substr(index1, index2 - index1);
if(index1 == 0) {
if (s == "b")
line_type = 0;
else if(s == "n")
line_type = 1;
else if(s == "c")
line_type = 2;
} else {
if(line_type == 0) {
chord.notes = {};
} else if(line_type == 1) {
chord.notes.push_back(note_map[s]);
} else {
chord.notes = chord_map[s];
}
}
index1 = index2 + 1;
} else if(index1 != 0) {
std::string s = line.substr(index1);
chord.duration = std::atof(s.c_str());
}
}
play(chord.notes, chord.duration);
}
file.close();
alutExit();
return 0;
}