File: sound_player_openal.h

package info (click to toggle)
megaglest 3.13.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 13,416 kB
  • sloc: cpp: 144,271; ansic: 11,861; sh: 3,233; perl: 1,904; python: 1,751; objc: 142; asm: 42; makefile: 22
file content (132 lines) | stat: -rw-r--r-- 3,187 bytes parent folder | download | duplicates (4)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//This file is part of Glest Shared Library (www.glest.org)
//Copyright (C) 2005 Matthias Braun <matze@braunis.de>

//You can redistribute this code and/or modify it under
//the terms of the GNU General Public License as published by the Free Software
//Foundation; either version 2 of the License, or (at your option) any later
//version.
#ifndef _SHARED_SOUND_SOUNDPLAYEROPENAL_H_
#define _SHARED_SOUND_SOUNDPLAYEROPENAL_H_

#include "sound_player.h"
#include "platform_util.h"
#include "platform_common.h"
#include <SDL.h>
#ifdef __APPLE__
#include <OpenAL/alc.h>
#include <OpenAL/al.h>
#else
#include <AL/alc.h>
#include <AL/al.h>
#endif
#include <vector>
#include "leak_dumper.h"

using std::vector;
using namespace Shared::PlatformCommon;

namespace Shared{ namespace Sound{ namespace OpenAL{

class SoundSource {
public:
	SoundSource();
	virtual ~SoundSource();

	bool playing();
	void stop();
	void unQueueBuffers();

protected:
	friend class SoundPlayerOpenAL;
	ALenum getFormat(Sound* sound);

	ALuint source;
};

class StaticSoundSource : public SoundSource {
public:
	StaticSoundSource();
	virtual ~StaticSoundSource();

	void play(StaticSound* sound);

protected:
	friend class SoundPlayerOpenAL;
	bool bufferAllocated;
	ALuint buffer;
};

class StreamSoundSource : public SoundSource {
public:
	StreamSoundSource();
	virtual ~StreamSoundSource();

	void play(StrSound* sound, int64 fade);
	void update();
	void stop();
	void stop(int64 fade);

protected:
	friend class SoundPlayerOpenAL;
	static const size_t STREAMBUFFERSIZE = 4096 * 500;
	static const size_t STREAMFRAGMENTS = 10;
	static const size_t STREAMFRAGMENTSIZE
		= STREAMBUFFERSIZE / STREAMFRAGMENTS;

	bool fillBufferAndQueue(ALuint buffer);

	StrSound* sound;
	ALuint buffers[STREAMFRAGMENTS];
	ALenum format;

	enum FadeState { NoFading, FadingOn, FadingOff };
	FadeState fadeState;
	Chrono chrono; // delay-fade chrono
	int64 fade;
};

// ==============================================================
//	class SoundPlayerSDL
//
///	SoundPlayer implementation using SDL_mixer
// ==============================================================

class SoundPlayerOpenAL : public SoundPlayer {
public:
	SoundPlayerOpenAL();
	virtual ~SoundPlayerOpenAL();
	virtual bool init(const SoundPlayerParams *params);
	virtual void end();
	virtual void play(StaticSound *staticSound, bool force=false);
	virtual void play(StrSound *strSound, int64 fadeOn=0);
	virtual void stop(StrSound *strSound, int64 fadeOff=0);
	virtual void stopAllSounds(int64 fadeOff=0);
	virtual void updateStreams();	//updates str buffers if needed

private:
	friend class SoundSource;
	friend class StaticSoundSource;
	friend class StreamSoundSource;

	void printOpenALInfo();

	StaticSoundSource* findStaticSoundSource();
	StreamSoundSource* findStreamSoundSource();
	void checkAlcError(string message);
	static void checkAlError(string message);

	ALCdevice* device;
	ALCcontext* context;

	typedef std::vector<StaticSoundSource*> StaticSoundSources;
	StaticSoundSources staticSources;
	typedef std::vector<StreamSoundSource*> StreamSoundSources;
	StreamSoundSources streamSources;

	SoundPlayerParams params;
};

}}}//end namespace

#endif