summary refs log tree commit diff stats
path: root/src/mixer.cpp
blob: bdcb7cef991b643620d7df8107cd908896396d43 (plain) (blame)
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
#include "mixer.h"

void Mixer::playSound(std::string_view filename) {
  Mix_Chunk* chunk = getChunkByFilename(std::string(filename));

  if (Mix_PlayChannel(-1, chunk, 0) == -1) {
    throw mix_error();
  }
}

int Mixer::loopSound(std::string_view filename) {
  Mix_Chunk* chunk = getChunkByFilename(std::string(filename));

  int ret = Mix_PlayChannel(-1, chunk, -1);
  if (ret == -1) {
    throw mix_error();
  }

  return ret;
}

void Mixer::stopChannel(int channel) {
  Mix_HaltChannel(channel);
}

Mix_Chunk* Mixer::getChunkByFilename(std::string filename) {
  if (!sounds_.count(filename)) {
    Mix_Chunk* sample = Mix_LoadWAV(filename.c_str());
    if (!sample) {
      throw mix_error();
    }

    sounds_[filename] = chunk_ptr(sample);
  }

  return sounds_[filename].get();
}