/
tjh_audio.h
298 lines (249 loc) · 9.24 KB
/
tjh_audio.h
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#ifndef TJH_AUDIO_H_HEADER
#define TJH_AUDIO_H_HEADER
////// UNLICENSE ///////////////////////////////////////////////////////////////
//
// This is free and unencumbered software released into the public domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.
//
// In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit
// of the public at large and to the detriment of our heirs and
// successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this
// software under copyright law.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// For more information, please refer to <http://unlicense.org/>
////// DEPENDENCIES ////////////////////////////////////////////////////////////
//
// This single header library relies on the following:
//
// LIBNAME vX.X.X which is Copyright (C) HOLDER_NAME and distributed under the
// LICENSE_NAME license. See here for more LINK_TO_RELEVANT_PAGE
////// DOCUMENTATION ///////////////////////////////////////////////////////////
//
// ABOUT
//
//
//
// USAGE
//
// Simply drop this file in your project directory and in any ONE .cpp file do
// the following:
//
// #define TJH_AUDIO_H_IMPLEMENTATION
// #include "tjh_audio.h"
//
// You can then #include "tjh_audio.h" as normal in every other file that wishes to
// use any of the window functions.
////// LIBRARY OPTIONS /////////////////////////////////////////////////////////
//
// You can modify the following defines to customize the library for your needs
// (also feel free to reach in and modify the code if you so desire).
// Change this to use a custom printf like function, for example SDL_Log
#define TJH_AUDIO_PRINTF printf
// Change this to use a different namespace if this one conflicts
#define TJH_AUDIO_NAMESPACE Audio
#define TJH_AUDIO_SOURCE_TYPENAME AudioSource
#define TJH_AUDIO_BUFFER_TYPENAME AudioBuffer
////// TODO ////////////////////////////////////////////////////////////////////
//
// - list dependancies, their licenses, and their respective copyright holders
// - write ABOUT documentation
// - add examples to USAGE documentation
// - play short sound effects
// - stream longer audio like music
// - replace SDL dependancy with the single header vorbis, wav, flac libs
// - cache audio buffers
// - reload buffers
////// HEADER //////////////////////////////////////////////////////////////////
#include <SDL2/SDL.h>
#include <OpenAL/OpenAL.h>
#include <string>
// Struct representing an audio buffer
class TJH_AUDIO_BUFFER_TYPENAME
{
public:
TJH_AUDIO_BUFFER_TYPENAME( ALuint id ) : id_(id) {}
inline ALuint bufferId() const { return id_; }
private:
ALuint id_ = 0;
};
// Struct representing an audio source
class TJH_AUDIO_SOURCE_TYPENAME
{
public:
TJH_AUDIO_SOURCE_TYPENAME()
{
alGenSources( 1, &id_ );
// TODO: error check soure generated ok
}
inline void play( const TJH_AUDIO_BUFFER_TYPENAME& buffer )
{
alSourcei( id_, AL_BUFFER, buffer.bufferId() );
alSourcePlay( id_ );
}
// default pitch is 1, doubling increases by an octave, halfing decreases by an octave, 0 is invalid
inline void setPitch( float pitch ) const { alSourcef( id_, AL_PITCH, pitch ); }
// gain should be in range 0 to 1
inline void setGain( float gain ) const { alSourcef( id_, AL_GAIN, gain ); }
inline void setLooping( bool looping ) const { alSourcei( id_, AL_LOOPING, looping ? AL_TRUE : AL_FALSE ); }
inline void setPosition( float x, float y, float z ) const { alSource3f( id_, AL_POSITION, x, y, z ); }
inline void setVelocity( float x, float y, float z ) const { alSource3f( id_, AL_VELOCITY, x, y, z ); }
inline ALuint sourceId() const { return id_; }
private:
ALuint id_ = 0;
};
namespace TJH_AUDIO_NAMESPACE
{
bool init();
void shutdown();
TJH_AUDIO_BUFFER_TYPENAME load( std::string filepath );
}
#endif // END HEADER
////// IMPLEMENTATION //////////////////////////////////////////////////////////
#ifdef TJH_AUDIO_H_IMPLEMENTATION
namespace TJH_AUDIO_NAMESPACE
{
// 'PRIVATE' MEMBER VARIABLES
ALCdevice* device_ = nullptr;
ALCcontext* context_ = nullptr;
// 'PRIVATE' MEMBER FUNCTIONS
bool load_wav( std::string filepath, ALuint buffer_id );
std::string extract_filetype( std::string filepath );
bool init()
{
bool success = true;
// Create the audio device
if( !device_ )
{
device_ = alcOpenDevice(nullptr);
if( !device_ )
{
TJH_AUDIO_PRINTF("ERROR: failed to open audio device\n");
success = false;
}
}
// Create the audio context
if( !context_ && success )
{
context_ = alcCreateContext( device_, nullptr );
if( !context_ )
{
TJH_AUDIO_PRINTF("ERROR: failed to create audio context\n");
success = false;
}
}
if( success )
{
alcMakeContextCurrent( context_ );
}
return success;
}
void shutdown()
{
alcDestroyContext( context_ );
context_ = nullptr;
alcCloseDevice( device_ );
device_ = nullptr;
}
TJH_AUDIO_BUFFER_TYPENAME load( std::string filepath )
{
ALuint buffer_id;
alGenBuffers( 1, &buffer_id );
// TODO: error check buffer generation
bool loaded = false, known_extension = true;
std::string extension = extract_filetype( filepath );
if( extension == "wav" )
{
loaded = load_wav( filepath, buffer_id );
}
else known_extension = false;
if( !loaded || !known_extension )
{
if( !known_extension ) TJH_AUDIO_PRINTF( "ERROR: unsupported audio extension%s\n", extension.c_str() );
if( !loaded ) TJH_AUDIO_PRINTF( "ERROR: failed to load audio file '%s'\n", filepath.c_str() );
if( buffer_id )
{
alDeleteBuffers( 1, &buffer_id );
buffer_id = 0;
}
}
return TJH_AUDIO_BUFFER_TYPENAME( buffer_id );
}
bool load_wav( std::string filepath, ALuint buffer_id )
{
bool success = true;
SDL_AudioSpec wav_spec;
Uint32 wav_length;
Uint8 *wav_buffer;
if( !SDL_LoadWAV( filepath.c_str(), &wav_spec, &wav_buffer, &wav_length ) )
{
TJH_AUDIO_PRINTF( "ERROR: could not load audio file" );
success = false;
return success;
}
ALenum format = 0;
// Figure out what type of audio we have loaded
// TODO: if the format is not natively supported by OpenAL (8 bit unsigned and 16 bit signed) the we should do a conversion
// formats that should be supported include signed 8bit unsigned 16bit, signed and unsigned 32 bit
switch( wav_spec.channels )
{
case 1: // Mono audio
switch( wav_spec.format )
{
case AUDIO_U8: format = AL_FORMAT_MONO8; break;
case AUDIO_S16: format = AL_FORMAT_MONO16; break;
default:
TJH_AUDIO_PRINTF("ERROR: Audio has unsupported format: %d", wav_spec.format );
success = false;
break;
}
break;
case 2: // Stereo (does not support 3d positioning)
switch( wav_spec.format )
{
case AUDIO_U8: format = AL_FORMAT_STEREO8; break;
case AUDIO_S16: format = AL_FORMAT_STEREO16; break;
default:
TJH_AUDIO_PRINTF("ERROR: Audio has unsupported format: %d", wav_spec.format );
success = false;
break;
}
break;
default:
TJH_AUDIO_PRINTF("ERROR: unsupported number of audio channels: %d", wav_spec.channels );
success = false;
break;
}
alBufferData( buffer_id, format, wav_buffer, wav_length, wav_spec.freq );
// Now we have stored the wav data in the buffer we can free it
SDL_FreeWAV( wav_buffer );
return success;
}
std::string extract_filetype( std::string filepath )
{
size_t dot = filepath.find_last_of('.');
if( dot == std::string::npos )
{
// Did not find a dot
return "";
}
// return everything after the dot
return filepath.substr(dot+1);
}
}
#undef TJH_AUDIO_H_IMPLEMENTATION
#endif // END IMPLEMENTATION