Amuse
SongState.hpp
1 #ifndef __AMUSE_SONGSTATE_HPP__
2 #define __AMUSE_SONGSTATE_HPP__
3 
4 #include <stdint.h>
5 #include <vector>
6 #include <list>
7 #include "optional.hpp"
8 #include "Entity.hpp"
9 
10 namespace amuse
11 {
12 class Sequencer;
13 
14 enum class SongPlayState
15 {
16  Stopped,
17  Playing
18 };
19 
21 class SongState
22 {
23  friend class Voice;
24 
26  struct Header
27  {
28  uint32_t m_version;
29  uint32_t m_chanIdxOff;
30  uint32_t m_chanMapOff;
31  uint32_t m_tempoTableOff;
32  uint32_t m_initialTempo;
33  uint32_t m_unkOff;
34  uint32_t m_chanOffs[64];
35  void swapBig();
36  } m_header;
37 
39  struct ChanHeader
40  {
41  uint32_t m_startTick;
42  uint16_t m_unk1;
43  uint16_t m_unk2;
44  uint16_t m_dataIndex;
45  uint16_t m_unk3;
46  uint32_t m_startTick2;
47  uint16_t m_unk4;
48  uint16_t m_unk5;
49  uint16_t m_unk6;
50  uint16_t m_unk7;
51  void swapBig();
52  };
53 
55  struct TempoChange
56  {
57  uint32_t m_tick;
58  uint32_t m_tempo;
59  void swapBig();
60  };
61 
63  struct Channel
64  {
65  struct Header
66  {
67  uint32_t m_type;
68  uint32_t m_pitchOff;
69  uint32_t m_modOff;
70  void swapBig();
71  };
72 
73  SongState& m_parent;
74  uint8_t m_midiChan;
75  uint32_t m_startTick;
77  const unsigned char* m_dataBase;
78  const unsigned char* m_data;
79  const unsigned char* m_pitchWheelData = nullptr;
80  const unsigned char* m_modWheelData = nullptr;
81  uint32_t m_lastPitchTick = 0;
82  int32_t m_lastPitchVal = 0;
83  uint32_t m_lastModTick = 0;
84  int32_t m_lastModVal = 0;
85  std::array<uint16_t, 128> m_remNoteLengths = {};
87  int32_t m_waitCountdown = 0;
89  Channel(SongState& parent, uint8_t midiChan, uint32_t startTick,
90  const unsigned char* song, const unsigned char* chan);
91  bool advance(Sequencer& seq, int32_t ticks);
92  };
93  std::array<std::experimental::optional<Channel>, 64> m_channels;
94 
96  const TempoChange* m_tempoPtr = nullptr;
97  uint32_t m_tempo = 120;
99  uint32_t m_curTick = 0;
100  SongPlayState m_songState = SongPlayState::Playing;
101  double m_curDt = 0.f;
103 public:
105  void initialize(const unsigned char* ptr);
106 
110  bool advance(Sequencer& seq, double dt);
111 };
112 
113 }
114 
115 #endif // __AMUSE_SONGSTATE_HPP__