1 /**
2  * Copyright (c) 2006-2016 LOVE Development Team
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty.  In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  *    claim that you wrote the original software. If you use this software
14  *    in a product, an acknowledgment in the product documentation would be
15  *    appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  *    misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  **/
20 
21 #ifndef LOVE_AUDIO_OPENAL_SOURCE_H
22 #define LOVE_AUDIO_OPENAL_SOURCE_H
23 
24 // LOVE
25 #include "common/config.h"
26 #include "common/Object.h"
27 #include "audio/Source.h"
28 #include "sound/SoundData.h"
29 #include "sound/Decoder.h"
30 
31 // OpenAL
32 #ifdef LOVE_APPLE_USE_FRAMEWORKS
33 #ifdef LOVE_IOS
34 #include <OpenAL/alc.h>
35 #include <OpenAL/al.h>
36 #else
37 #include <OpenAL-Soft/alc.h>
38 #include <OpenAL-Soft/al.h>
39 #endif
40 #else
41 #include <AL/alc.h>
42 #include <AL/al.h>
43 #endif
44 
45 namespace love
46 {
47 namespace audio
48 {
49 namespace openal
50 {
51 
52 class Audio;
53 class Pool;
54 
55 // Basically just a reference-counted non-streaming OpenAL buffer object.
56 class StaticDataBuffer : public love::Object
57 {
58 public:
59 
60 	StaticDataBuffer(ALenum format, const ALvoid *data, ALsizei size, ALsizei freq);
61 	virtual ~StaticDataBuffer();
62 
getBuffer()63 	inline ALuint getBuffer() const
64 	{
65 		return buffer;
66 	}
67 
getSize()68 	inline ALsizei getSize() const
69 	{
70 		return size;
71 	}
72 
73 private:
74 
75 	ALuint buffer;
76 	ALsizei size;
77 
78 }; // StaticDataBuffer
79 
80 class Source : public love::audio::Source
81 {
82 public:
83 
84 	Source(Pool *pool, love::sound::SoundData *soundData);
85 	Source(Pool *pool, love::sound::Decoder *decoder);
86 	Source(const Source &s);
87 	virtual ~Source();
88 
89 	virtual love::audio::Source *clone();
90 	virtual bool play();
91 	virtual void stop();
92 	virtual void pause();
93 	virtual void resume();
94 	virtual void rewind();
95 	virtual bool isStopped() const;
96 	virtual bool isPaused() const;
97 	virtual bool isFinished() const;
98 	virtual bool update();
99 	virtual void setPitch(float pitch);
100 	virtual float getPitch() const;
101 	virtual void setVolume(float volume);
102 	virtual float getVolume() const;
103 	virtual void seekAtomic(float offset, void *unit);
104 	virtual void seek(float offset, Unit unit);
105 	virtual float tellAtomic(void *unit) const;
106 	virtual float tell(Unit unit);
107 	virtual double getDurationAtomic(void *unit);
108 	virtual double getDuration(Unit unit);
109 	virtual void setPosition(float *v);
110 	virtual void getPosition(float *v) const;
111 	virtual void setVelocity(float *v);
112 	virtual void getVelocity(float *v) const;
113 	virtual void setDirection(float *v);
114 	virtual void getDirection(float *v) const;
115 	virtual void setCone(float innerAngle, float outerAngle, float outerVolume);
116 	virtual void getCone(float &innerAngle, float &outerAngle, float &outerVolume) const;
117 	virtual void setRelative(bool enable);
118 	virtual bool isRelative() const;
119 	void setLooping(bool looping);
120 	bool isLooping() const;
121 	virtual void setMinVolume(float volume);
122 	virtual float getMinVolume() const;
123 	virtual void setMaxVolume(float volume);
124 	virtual float getMaxVolume() const;
125 	virtual void setReferenceDistance(float distance);
126 	virtual float getReferenceDistance() const;
127 	virtual void setRolloffFactor(float factor);
128 	virtual float getRolloffFactor() const;
129 	virtual void setMaxDistance(float distance);
130 	virtual float getMaxDistance() const;
131 	virtual int getChannels() const;
132 
133 	bool playAtomic();
134 	void stopAtomic();
135 	void pauseAtomic();
136 	void resumeAtomic();
137 	void rewindAtomic();
138 
139 private:
140 
141 	void reset();
142 
143 	void setFloatv(float *dst, const float *src) const;
144 
145 	/**
146 	 * Gets the OpenAL format identifier based on number of
147 	 * channels and bits.
148 	 * @param channels Either 1 (mono) or 2 (stereo).
149 	 * @param bitDepth Either 8-bit samples, or 16-bit samples.
150 	 * @return One of AL_FORMAT_*, or 0 if unsupported format.
151 	 **/
152 	ALenum getFormat(int channels, int bitDepth) const;
153 
154 	int streamAtomic(ALuint buffer, love::sound::Decoder *d);
155 
156 	Pool *pool;
157 	ALuint source;
158 	bool valid;
159 
160 	static const unsigned int MAX_BUFFERS = 8;
161 	ALuint streamBuffers[MAX_BUFFERS];
162 
163 	StrongRef<StaticDataBuffer> staticBuffer;
164 
165 	float pitch;
166 	float volume;
167 	float position[3];
168 	float velocity[3];
169 	float direction[3];
170 	bool relative;
171 	bool looping;
172 	bool paused;
173 	float minVolume;
174 	float maxVolume;
175 	float referenceDistance;
176 	float rolloffFactor;
177 	float maxDistance;
178 
179 	struct Cone
180 	{
181 		int innerAngle = 360; // degrees
182 		int outerAngle = 360; // degrees
183 		float outerVolume = 0.0f;
184 	} cone;
185 
186 	float offsetSamples;
187 	float offsetSeconds;
188 
189 	int sampleRate;
190 	int channels;
191 	int bitDepth;
192 
193 	StrongRef<love::sound::Decoder> decoder;
194 
195 	unsigned int toLoop;
196 
197 }; // Source
198 
199 } // openal
200 } // audio
201 } // love
202 
203 #endif // LOVE_AUDIO_OPENAL_SOURCE_H
204