-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplayer.hpp
More file actions
260 lines (224 loc) · 7.11 KB
/
player.hpp
File metadata and controls
260 lines (224 loc) · 7.11 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
#ifndef SGR_PLAYER_HPP
#define SGR_PLAYER_HPP
/**
* @file player.hpp
* Contains everything required to play the sgr::notation::song instances.
*
* @author Gašper Ažman, gasper.azman@gmail.com
* @since 2012-02-05
*/
#include "math.hpp"
#include "notation.hpp"
#include "player_instrument.hpp"
#include "player_timing.hpp"
#include "scalars.hpp"
#include "player_pitch.hpp"
#include "player_volume.hpp"
#include "exceptions.hpp"
#include <memory>
#include <queue>
#include <vector>
#include <list>
#include <cassert>
#include <iostream> //for cerr
#include <sstream>
namespace sgr {
namespace player {
struct instruction {
typedef std::shared_ptr<const instruction> pointer_type;
pitch::pitch::pointer_type pitch;
instrument::instrument::pointer_type instrument;
volume::volume::pointer_type volume;
timing::period bounds;
instruction(const notation::note& n, units::time start_t, units::time end_t)
: pitch{pitch::factory(*n.pitch)}
, instrument{instrument::factory(*n.instrument)}
, volume{volume::factory(*n.volume)}
, bounds{timing::time(start_t, n.duration.start),
timing::time(end_t, n.duration.start + n.duration.duration)}
{}
std::string str() {
std::stringstream ss;
ss << "Instrument: " << instrument->str() << std::endl;
ss << "Pitch: " << pitch->str() << std::endl;
ss << "Volume: " << volume->str() << std::endl;
ss << "Period: " << bounds.str() << std::endl;
return ss.str();
}
};
struct tempo {
typedef std::list<notation::timing::timing::pointer_type> timing_queue;
private:
timing_queue timings;
timing::time now;
timing::time global;
public:
tempo()
: timings()
, now(units::time{0},units::beat{0},units::time{0},units::beat{0})
, global(units::time{0},units::beat{0},units::time{0},units::beat{0}) {}
void add_timing(const notation::timing::timing::pointer_type timing)
{
timings.push_back(timing);
}
void advance(units::time dt) throw (err::end_of_song)
{
using units::time;
assert((dt >= time{0}) && "must be greater than zero!");
timing::time old(now);
global.t += dt;
global.dt = dt;
now.t += dt;
now.dt = dt;
auto t = timings.front();
if (t->full_time() <= now.t) { /* if finished with timing sequence */
/* switch the timing sequence */
/* remember how much time and beat remained */
auto rest_of_t = t->full_time() - old.t;
auto rest_of_b = t->duration - old.beat;
now.t = dt - rest_of_t; /* current time is dt - remaining */
old.beat = -rest_of_b; /* dbeat = now.beat - old.beat */
/* change the timing */
timings.pop_front();
if (timings.size() == 0) {
throw err::end_of_song()
<< err::reason("Ran out of timing specs!");
}
t = timings.front();
}
now.beat = t->time_to_beat(now.t);
now.dbeat = now.beat - old.beat;
global.beat += now.dbeat;
global.dbeat = now.dbeat;
// std::cout << "dt: " << dt.value << " dbeat: " << now.dbeat.value << "\n";
}
/** Returns the time of a specified beat.
* Relative to beginning of song.
* DO NOT USE for beat that have already passed!
*/
units::time beat_to_time(units::beat beat) throw (sgr::err::invalid_beat)
{
using namespace sgr::err;
if (beat < global.beat) {
throw invalid_beat() << beat_val(beat.value)
<< reason("Beat in the past.");
}
auto t = global.t;
beat -= global.beat - now.beat;
for (auto l : timings) {
// std::cerr << l->full_time() << std::endl;
if (beat <= l->duration) {
return t + l->beat_to_time(beat);
} else {
t += l->full_time();
beat -= l->duration;
}
}
throw invalid_beat() << beat_val(beat.value) <<
reason("Beat past end of song.");
}
const timing::time& get() { return global; }
};
/**
* A class for playing piano rolls.
* Use like so:
* // create piano roll
* player pl(music);
* while(true) {
* sample s = pl.sound();
* pl.advance(0.001);
* }
*/
class player
{
struct ptrcmp {
bool operator()(
const instruction& a,
const instruction& b
) {
return a.bounds.start.beat > b.bounds.start.beat;
}
};
typedef std::priority_queue<
instruction,
std::vector<instruction>,
ptrcmp
> piano_roll;
tempo now;
piano_roll roll;
std::list<instruction> active;
scalars::sample sound_;
public:
player(notation::song song)
: now()
, roll()
, active()
, sound_()
{
for (auto timing : song.timings()) {
now.add_timing(timing);
}
for (auto instr : song.notes()) {
auto start_t = now.beat_to_time(instr.duration.start);
auto end_t = now.beat_to_time(instr.duration.start + instr.duration.duration);
roll.push(instruction(instr, start_t , end_t));
}
}
/**
* Advances the composition by dt seconds.
* @param dt the time to advance, in seconds. Must be >= 0.
*/
void advance(units::time dt)
{
using std::max;
assert((dt >= units::time{0}) && "Not meant to go backwards!");
now.advance(dt);
auto t = now.get();
// add all instruments that have to sound at this t
if (!roll.empty()) {
auto top = roll.top();
if (top.bounds.start.beat <= t.beat) {
std::cout << "Beat: " << t.beat.value << ". Adding " <<
top.str();
active.push_back(top);
roll.pop();
}
}
// remove all instructions that are to be removed.
auto p = active.begin();
auto e = active.end();
while (p != e) {
if (p->bounds.end.beat < t.beat) {
// std::cout << "Beat: " << t.beat.value << ". removing " <<
// p->str();
p = active.erase(p);
} else {
++p;
}
}
// compute sound
double normalize = 0;
scalars::sample s{0, 0};
for (auto p : active) {
// std::cout << p.str();
auto vol = p.volume->get_volume(p.bounds, t);
auto pitch = p.pitch->get_pitch(p.bounds, t);
s += p.instrument->get_sample(p.bounds, t, vol, pitch);
normalize += max(vol.right, vol.left);
}
normalize = max(normalize, 1.0);
s *= scalars::volume{1/normalize, 1/normalize};
sound_ = s;
}
/**
* Extract the current soundsample.
* @return the current sound amplitude.
*/
scalars::sample sound()
{
return sound_;
}
}; /* end struct player */
} /* end namespace player */
}/* end namespace sgr */
#endif