-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaudio_v1.cpp
More file actions
244 lines (192 loc) · 7.79 KB
/
audio_v1.cpp
File metadata and controls
244 lines (192 loc) · 7.79 KB
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
/******************************************************************************
nomlib - C++11 cross-platform game engine
Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
// NOTE: Audio playback usage example
#include <nomlib/audio.hpp>
#include <nomlib/math.hpp>
#include <nomlib/system.hpp>
#include <nomlib/version.hpp>
#include "tclap/CmdLine.h"
using namespace nom;
const std::string APP_NAME = "nomlib: audio";
/// File path of the resources directory; this must be a relative to the parent
/// working directory.
const std::string APP_RESOURCES_DIR = "Resources";
/// The platform specific file delimiter; '/' on Posix and '\' on Windows.
const nom::Path p;
/// Sound effect resource file
const std::string RESOURCE_AUDIO_SOUND = APP_RESOURCES_DIR + p.native() +
"cursor_wrong.wav";
/// \remarks See program usage by passing --help
struct AppFlags
{
/// The input file source to play
std::string audio_input = RESOURCE_AUDIO_SOUND;
/// Test input source with the null audio back-end
bool use_null_interface = false;
/// Test input source with the music audio interface
///
/// \fixme This interface is broken; the sound buffer memory is not properly
/// deallocated.
bool use_music_interface = false;
real32 audio_volume = 100.0f;
};
int parse_cmdline(int argument_count, char* arguments[], AppFlags& opts)
{
using namespace TCLAP;
if( argument_count < 0 ) {
return NOM_EXIT_FAILURE;
}
try
{
CmdLine cmd( APP_NAME, ' ', nom::NOM_VERSION.version_string() );
std::string null_interface_desc =
"Test the usage of the null interface (defaults to FALSE).";
std::string music_interface_desc =
"Test the usage of the music interface (defaults to FALSE).";
SwitchArg use_null_interface_arg("n", "use-null", null_interface_desc,
cmd, false);
SwitchArg use_music_interface_arg("", "use-music", music_interface_desc,
cmd, false);
ValueArg<real32> audio_volume_arg("v", "volume",
"Gain level of audio playback",
false, opts.audio_volume,
"A number between 0.0f .. 100.0f",
cmd );
ValueArg<std::string> audio_file_arg("i", "input",
"File path to audio to play from",
false, opts.audio_input,
"Resources/audio/hello.wav", cmd );
cmd.parse(argument_count, arguments);
opts.use_null_interface = use_null_interface_arg.getValue();
opts.use_music_interface = use_music_interface_arg.getValue();
opts.audio_input = audio_file_arg.getValue();
opts.audio_volume = audio_volume_arg.getValue();
}
catch(TCLAP::ArgException &e)
{
NOM_LOG_ERR( NOM_LOG_CATEGORY_APPLICATION,
e.error(), "for arg", e.argId() );
return NOM_EXIT_FAILURE;
}
return NOM_EXIT_SUCCESS;
}
int main(int argc, char* argv[])
{
AppFlags args;
nom::IAudioDevice* dev = nullptr; // this must be declared first
nom::IListener* listener = nullptr; // Global audio volume control
nom::ISoundBuffer* buffer = nullptr;
nom::ISoundSource* snd = nullptr;
nom::Timer loops;
nom::SDL2Logger::set_logging_priority(NOM_LOG_CATEGORY_AUDIO,
NOM_LOG_PRIORITY_DEBUG);
if( parse_cmdline(argc, argv, args) != 0 ) {
exit(NOM_EXIT_FAILURE);
}
// Fatal error; if we are not able to complete this step, it means that
// we probably cannot rely on our resource paths!
if( nom::init(argc, argv) == false ) {
NOM_LOG_CRIT(NOM_LOG_CATEGORY_APPLICATION,
"Could not initialize nomlib.");
exit(NOM_EXIT_FAILURE);
}
atexit(nom::quit);
// Quick and dirty method of testing the use of nomlib's audio subsystem
// #undef NOM_USE_OPENAL
// Initialize audio subsystem...
if( args.use_null_interface == true ) {
dev = new nom::NullAudioDevice();
listener = new nom::NullListener();
buffer = new nom::NullSoundBuffer();
} else {
dev = new nom::AudioDevice();
listener = new nom::Listener();
buffer = new nom::SoundBuffer();
}
NOM_LOG_INFO( NOM_LOG_CATEGORY_APPLICATION, "Audio device name:",
dev->getDeviceName() );
listener->set_volume(args.audio_volume);
if( buffer->load_file(args.audio_input) == false ) {
NOM_LOG_ERR( NOM_LOG_CATEGORY_APPLICATION, "Could not load audio file: ",
RESOURCE_AUDIO_SOUND );
return NOM_EXIT_FAILURE;
}
if( args.use_music_interface == true ) {
if( args.use_null_interface == true ) {
snd = new nom::NullMusic();
} else {
// FIXME: This interface is broken; the sound buffer memory is not
// properly deallocated.
snd = new nom::Music();
}
} else if( args.use_music_interface == false ) {
if( args.use_null_interface == true ) {
snd = new nom::NullSound();
} else {
snd = new nom::Sound();
}
}
NOM_ASSERT(snd != nullptr);
snd->load_buffer(*buffer);
snd->setPitch(1.0f);
snd->setPosition( nom::Point3f(0.0f, 0.0f, 100.0f) );
snd->set_velocity( nom::Point3f(0.0f, 0.0f, 0.0f) );
snd->setLooping(true);
snd->Play();
nom::uint32 duration = buffer->duration();
real32 duration_seconds = duration / 1000.0f;
NOM_DUMP(duration_seconds);
Timer kill_timer;
kill_timer.start();
while( kill_timer.ticks() < duration ) {}
#if 0
real32 initial_volume = snd->volume();
real32 current_volume = initial_volume;
real32 fade_seconds = 4.0f;
real32 fade_step = current_volume / fade_seconds;
while( (snd->getStatus() != nom::SoundStatus::Paused) &&
(snd->getStatus() != nom::SoundStatus::Stopped)
)
{
if( current_volume > nom::Listener::min_volume() ) {
std::cout << "\nFading out\n";
snd->set_volume(current_volume);
} else {
snd->Stop();
}
// current_volume = current_volume - fade_step;
// nom::sleep(1000);
} // end loop
#endif
// NOM_LOG_INFO( NOM_LOG_CATEGORY_APPLICATION,
// "Sample Count: ", buffer->getSampleCount() );
// NOM_LOG_INFO( NOM_LOG_CATEGORY_APPLICATION,
// "Channel Count: ", buffer->getChannelCount() );
// NOM_LOG_INFO( NOM_LOG_CATEGORY_APPLICATION,
// "Sample Rate: ", buffer->getSampleRate() );
NOM_DELETE_PTR(snd);
NOM_DELETE_PTR(buffer);
NOM_DELETE_PTR(dev);
NOM_DELETE_PTR(listener);
return NOM_EXIT_SUCCESS;
}