OpenGothic
Open source reimplementation of Gothic I and II
Loading...
Searching...
No Matches
mixer.h
Go to the documentation of this file.
1#pragma once
2
3#include <Tempest/SoundDevice>
4#include <Tempest/SoundEffect>
5
6#include <vector>
7#include <cstdint>
8#include <thread>
9#include <atomic>
10#include <list>
11
12#include "patternlist.h"
13#include "music.h"
14
15namespace Dx8 {
16
17class Mixer final {
18 public:
19 Mixer();
20 ~Mixer();
21
22 void mix(int16_t *out, size_t samples);
23 void setVolume(float v);
24
26 void setMusicVolume(float v);
27 int64_t currentPlayTime() const;
28
29 private:
30 struct Instr;
31
32 struct Active {
33 int64_t at=0;
34 SoundFont::Ticket ticket;
35 Instr* parent=nullptr;
36 };
37
38 struct Step final {
39 int64_t nextOn =std::numeric_limits<int64_t>::max();
40 int64_t nextOff=std::numeric_limits<int64_t>::max();
41 int64_t samples=0;
42 bool isValid() const { return !(nextOn==std::numeric_limits<int64_t>::max() && nextOn==nextOff && samples==0); }
43 };
44
45 struct Instr {
46 PatternList::InsInternal* ptr=nullptr;
47 float volLast=1.f;
48 size_t counter=0;
49 std::shared_ptr<PatternList::PatternInternal> pattern; //prevent pattern from deleting
50 };
51
52 using PatternInternal = PatternList::PatternInternal;
53
54 Step stepInc (PatternInternal &pptn, int64_t b, int64_t e, int64_t samplesRemain);
55 void stepApply(std::shared_ptr<PatternList::PatternInternal> &pptn, const Step& s, int64_t b);
56 void implMix (PatternList::PatternInternal &pptn, float volume, int16_t *out, size_t cnt);
57
58 int64_t nextNoteOn (PatternInternal &part, int64_t b, int64_t e);
59 int64_t nextNoteOff(int64_t b, int64_t e);
60
61 void noteOn (std::shared_ptr<PatternInternal> &pattern, PatternList::Note *r);
62 void noteOn (std::shared_ptr<PatternInternal> &pattern, int64_t time);
63 void noteOff(int64_t time);
64 std::shared_ptr<PatternInternal> checkPattern(std::shared_ptr<PatternInternal> p);
65
66 void nextPattern();
67
68 bool hasVolumeCurves(PatternInternal &part, Instr &ins) const;
69 void volFromCurve(PatternInternal &part, Instr &ins, std::vector<float> &v);
70
71 template<class T>
72 bool checkVariation(const T& item) const;
73 int getGroove() const;
74
75 std::shared_ptr<Music::Internal> current=nullptr;
76 std::shared_ptr<Music::Internal> nextMus=nullptr;
77 std::atomic<DMUS_EMBELLISHT_TYPES> embellishment = {DMUS_EMBELLISHT_NORMAL};
78 int64_t sampleCursor=0;
79
80 std::shared_ptr<PatternInternal> pattern=nullptr;
81 int64_t patStart=0;
82 int64_t patEnd =0;
83 std::atomic<uint32_t> variationCounter={};
84 std::atomic<size_t> grooveCounter={};
85
86 std::atomic<float> volume={1.f};
87 std::vector<Active> active;
88 std::list<Instr> uniqInstr;
89 std::vector<float> pcm, vol, pcmMix;
90 };
91
92}
void setVolume(float v)
Definition mixer.cpp:307
void setMusic(const Music &m, DMUS_EMBELLISHT_TYPES embellishment=DMUS_EMBELLISHT_NORMAL)
Definition mixer.cpp:33
void setMusicVolume(float v)
Definition mixer.cpp:41
int64_t currentPlayTime() const
Definition mixer.cpp:46
void mix(int16_t *out, size_t samples)
Definition mixer.cpp:253
Definition band.h:10
DMUS_EMBELLISHT_TYPES
Definition structs.h:93
@ DMUS_EMBELLISHT_NORMAL
Definition structs.h:94