Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/game/StarSongbook.hpp
blob: 6f15242112313449134dd94f8e4ac53fcf02871a (plain)
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
#pragma once

#include "StarThread.hpp"
#include "StarJson.hpp"
#include "StarNetElementSystem.hpp"
#include "StarGameTypes.hpp"

namespace Star {

STAR_CLASS(Audio);
STAR_CLASS(AudioInstance);
STAR_CLASS(World);
STAR_CLASS(Songbook);
STAR_CLASS(RenderCallback);

class Songbook : public NetElementSyncGroup {
public:
  Songbook(String const& species);
  ~Songbook();

  void update(EntityMode mode, World* world);
  void render(RenderCallback* renderCallback);

  // instrument needs to tell the songbook what type it is, and needs to keep
  // calling it to signal
  // the instrument is still equiped
  void keepAlive(String const& instrument, Vec2F const& position);

  void stop();
  void play(Json const& song, String const& timeSource);
  bool active() const;
  bool instrumentPlaying() const;

  Maybe<String> timeSource() const;
  Maybe<String> instrument() const;
  Json song() const;

private:
  struct Note {
    String instrument;
    String file;
    double timecode;
    double duration;
    double fadeout;
    double velocity;
  };

  struct HeldNote {
    AudioInstancePtr audio;
    double start;
    double end;
  };

  struct NoteMapping {
    List<String> files;
    double frequency;
    double velocity;
    double fadeout;
  };

  struct TimeSource {
    int64_t keepalive;
    int64_t epoch;
  };

  static double fundamentalFrequency(double p);
  static double fundamentalPitch(double p);

  void netElementsNeedLoad(bool full) override;
  void netElementsNeedStore() override;

  static StringMap<shared_ptr<TimeSource>> s_timeSources;
  static Mutex s_timeSourcesMutex;

  String m_species;
  Vec2F m_position;

  bool m_serverMode;

  int64_t m_globalNowDelta;
  int m_activeCooldown;
  bool m_dataUpdated;
  bool m_dataChanged;

  String m_timeSource;
  int64_t m_timeSourceEpoch;
  bool m_epochUpdated;
  String m_instrument;
  Json m_song;
  bool m_stopped;

  Deque<Note> m_track;

  List<HeldNote> m_heldNotes;
  shared_ptr<TimeSource> m_timeSourceInstance;

  List<AudioInstancePtr> m_pendingAudio;

  List<Note> parseABC(String const& abc);

  StringMap<Map<int, NoteMapping>> m_noteMapping;

  NoteMapping& noteMapping(String const& instrument, String const& species, int note);

  StringMap<AudioConstPtr> m_uncompressedSamples;

  void playback();

  NetElementData<Json> m_songNetState;
  NetElementInt m_timeSourceEpochNetState;
  NetElementString m_timeSourceNetState;
  NetElementBool m_activeNetState;
  NetElementString m_instrumentNetState;
};

}