hx3d  1
2D/3D Simple Game Framework
audio.cpp
1 /*
2  Audio management.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #include "hx3d/audio/audio.hpp"
22 #include "hx3d/audio/effect.hpp"
23 
24 #include "hx3d/utils/log.hpp"
25 
26 #include <SDL2/SDL_mixer.h>
27 
28 namespace hx3d {
29 namespace audio {
30 
31 int AudioDevice::PostChannel = MIX_CHANNEL_POST;
32 
33 AudioDevice::AudioDevice() {
34 
35  Log.Info("Audio device initialization.");
36 
37  int flags = Mix_Init(~0);
38  std::string types;
39 
40  if (flags & MIX_INIT_FLAC)
41  types += "\t- FLAC\n";
42  if (flags & MIX_INIT_MOD)
43  types += "\t- MOD\n";
44  if (flags & MIX_INIT_MP3)
45  types += "\t- MP3\n";
46  if (flags & MIX_INIT_OGG)
47  types += "\t- OGG\n";
48  if (!flags)
49  types += "\t- None\n";
50 
51  Log.Info("Supported audio types: \n%s", types.c_str());
52 
53  _bufferSize = 4096;
54 
55  Mix_OpenAudio(44100, AUDIO_S16SYS, 1, _bufferSize);
56  Mix_QuerySpec(&_audioRate, &_audioFormat, &_audioChannels);
57 
58  _bits = _audioFormat & 0xFF;
59  _sampleSize = (_bits / 8.f) + _audioChannels;
60 
61  Log.Info(
62  "Audio device information: \n\
63  \t - %d Hz / %d bits / %s / %d bytes buffer. \n\
64  \t - Sample size: %d\n",
65  _audioRate, _bits, _audioChannels > 1 ? "Stereo" : "Mono", _bufferSize,
66  _sampleSize);
67 }
68 
69 AudioDevice::~AudioDevice() {
70  Mix_Quit();
71 }
72 
73 void AudioDevice::registerEffect(const int channel, Effect& effect) {
74  Mix_RegisterEffect(
75  channel,
76  [](int channel, void* stream, int len, void* udata){
77  Effect* effect = (Effect*)udata;
78  effect->onFunction(channel, stream, len);
79  },
80  [](int channel, void* udata){
81  Effect* effect = (Effect*)udata;
82  effect->onDone(channel);
83  }, &effect);
84 }
85 
86 void AudioDevice::clearEffects(const int channel) {
87  Mix_UnregisterAllEffects(channel);
88 }
89 
91  return _audioRate;
92 }
93 
95  return _sampleSize;
96 }
97 
98 } /* audio */
99 } /* hx3d */
static int PostChannel
Post Channel Identifier.
Definition: audio.hpp:85
void clearEffects(const int channel)
Clear all the effects for a channel.
Definition: audio.cpp:86
hx3d framework namespace
Definition: audio.hpp:26
Audio effect manipulation.
Definition: effect.hpp:32
virtual void onFunction(const int channel, const void *stream, const int length)
Audio effect function.
Definition: effect.cpp:29
static hx3d::LogImpl Log
Current log implementation.
Definition: log.hpp:91
unsigned int getFrequencyRate()
Get the audio device frequency rate.
Definition: audio.cpp:90
unsigned int getSampleSize()
Get the audio device sample size.
Definition: audio.cpp:94
void registerEffect(const int channel, Effect &effect)
Register an effect on a channel.
Definition: audio.cpp:73
virtual void onDone(const int channel)
Audio effect done function.
Definition: effect.cpp:30
void Info(const std::string fmt,...)
Write an info message.
Definition: log.cpp:58