summaryrefslogtreecommitdiff
path: root/g_src/music_and_sound_openal.h
blob: b4f3de92900f2b7dc076635e8cfd0a9d756ae531 (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
#ifndef MUSIC_AND_SOUND_OPENAL_H
#define MUSIC_AND_SOUND_OPENAL_H

#include <AL/al.h>
#include <AL/alc.h>

// HACKY HACKY HACK
// Fixes sndfile.h, until the bug is properly fixed
#include <stdio.h>
#include <sys/types.h>
#define _MSCVER
typedef int64_t __int64;
#include <sndfile.h>
#undef _MSCVER
// END HACKY HACKY HACK

#include <string>
#include <vector>
#include <list>
#include <map>
#include <algorithm>
#include <utility>

#define SOUND_CHANNELNUM 16

// Preferred mixer frequency. Should be the same as what the ogg files
// use, to avoid resampling.
#define SOUND_FREQUENCY 44100

// If the bool is false, a sound; otherwise a song
typedef std::pair<bool,int> slot;

class musicsoundst
{
 public:
  bool initsound(); // Returns false if it failed
  void update() {}
  void set_master_volume(long newvol);

  void set_song(std::string &filename, slot slot);
  void set_song(std::string &filename, int slot);
  void playsound(slot slot);
  void playsound(int slot); // Assumes sound

  void startbackgroundmusic(slot slot);
  void startbackgroundmusic(int slot); // Assumes song
  void stopbackgroundmusic();
  void stop_sound();
  void stop_sound(slot slot);
  void playsound(int s,int channel);
  void set_sound(std::string &filename,int slot,int pan=-1,int priority=0);
  void deinitsound();
                 
  // Deprecated:
  void forcebackgroundmusic(int slot, unsigned long time);
  void playsound(int s,int min_channel,int max_channel,int force_channel);
  void set_sound_params(int slot,int p1,int vol,int pan,int priority);

  musicsoundst() {
    functional = false;
    background_slot = slot(false,-1);
  }

  ~musicsoundst() {
    deinitsound();
  }
  
 private:
  bool functional;
  ALCdevice *device;
  ALCcontext *context;

  std::map<std::string,ALuint> buffers; // OpenAL buffers
  std::map<std::string,ALuint> sources; // And sources
  std::map<slot, ALuint> slot_buffer; // Mappings from DF slots to openal
  std::map<slot, ALuint> slot_source;

  slot background_slot; // Currently playing background music, or -1
};


#endif