Untitled Game engine no.5  1.0
Clip.hpp
1 //
2 // Created by jibbo on 2/22/21.
3 //
4 
5 
6 #ifndef MONOREPO_JSTRACESKI_CLIP_H
7 #define MONOREPO_JSTRACESKI_CLIP_H
8 
9 #include <vector>
10 #include <SDL2/SDL.h>
11 #include <Base.hpp>
12 
13 namespace Engine {
19  struct Clip {
20  public:
21  Uint32 clipLen;
22  Uint32 trackPos = 0;
24  Uint8 *data;
25  SDL_AudioSpec spec;
27  bool loop = false;
28  bool active = false;
29  static std::vector<Ref<Clip>> sounds;
34  static void InitAudio() {
35  SDL_AudioSpec fmt;
36 
37  fmt.freq = 44100;
38  fmt.format = AUDIO_S16;
39  fmt.channels = 2;
40  fmt.samples = 4096;
41  fmt.callback = Clip::callback;
42  fmt.userdata = nullptr;
43 
44  /* Open the audio device */
45  if ( SDL_OpenAudio(&fmt, nullptr) < 0 ){
46  fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
47  }
48 
49  /* Start playing */
50  SDL_PauseAudio(0);
51  }
52 
58  static Ref<Clip> LoadClip(const std::string& path);
59 
60  //TODO: add static and master list
70  static void callback(void *userdata, Uint8 *stream, int len) {
71  memset(stream, 0, len);
72 
73  for (const auto& s : sounds) {
74  if (s->active) {
75  if (s->trackPos + len >= s->clipLen) {
76  Uint32 last = s->clipLen - s->trackPos;
77  SDL_MixAudio(stream, &s->data[s->trackPos], last, SDL_MIX_MAXVOLUME);
78 
79  if (s->loop) {
80  Uint32 extra = len - last;
81  SDL_MixAudio(&stream[last], &s->data[0], extra, SDL_MIX_MAXVOLUME);
82  s->trackPos = extra;
83  } else {
84  s->active = false;
85  }
86 
87  } else {
88  SDL_MixAudio(stream, &s->data[s->trackPos], len, SDL_MIX_MAXVOLUME);
89  s->trackPos += len;
90  }
91  }
92  }
93  }
94 
96  void playClip();
97 
99  void loopClip();
100 
102  void endClip();
103 
109  ~Clip() {
110  delete data;
111  }
112  };
113 
114 }
115 
116 
117 #endif //MONOREPO_JSTRACESKI_CLIP_H
Engine::Clip::clipLen
Uint32 clipLen
Definition: Clip.hpp:21
Engine::Clip::InitAudio
static void InitAudio()
Definition: Clip.hpp:34
Engine::Clip::active
bool active
Definition: Clip.hpp:28
Engine::Ref
std::shared_ptr< T > Ref
Has stuff for making references a lot more easily shared smart pointer.
Definition: Base.hpp:21
Engine::Clip::loopClip
void loopClip()
play the referenced Clip and loop it
Definition: Clip.cpp:28
Engine::Clip::endClip
void endClip()
end the referenced Clip
Definition: Clip.cpp:33
Engine::Clip::spec
SDL_AudioSpec spec
Definition: Clip.hpp:25
Engine::Clip
Struct to hold music data.
Definition: Clip.hpp:19
Engine::Clip::~Clip
~Clip()
Definition: Clip.hpp:109
Engine::Clip::playClip
void playClip()
play the referenced Clip
Definition: Clip.cpp:24
Engine::Clip::callback
static void callback(void *userdata, Uint8 *stream, int len)
Callback passed into the SDL Audio library.
Definition: Clip.hpp:70
Engine::Clip::LoadClip
static Ref< Clip > LoadClip(const std::string &path)
Definition: Clip.cpp:12
Engine
Definition: Animation.hpp:14
Engine::Clip::data
Uint8 * data
Definition: Clip.hpp:24
Engine::Clip::trackPos
Uint32 trackPos
Definition: Clip.hpp:22
Engine::Clip::sounds
static std::vector< Ref< Clip > > sounds
Definition: Clip.hpp:29
Engine::Clip::loop
bool loop
Definition: Clip.hpp:27