Use a more sane streaming buffer length
[alure.git] / examples / alure-reverb.cpp
blobcb22f7eb53c2f365e04007cc1bbe4f7b5f2f79f8
1 /*
2 * An example showing how to load and apply a reverb effect to a source.
3 */
5 #include <iostream>
6 #include <iomanip>
7 #include <cstring>
8 #include <thread>
9 #include <chrono>
11 #include "alure2.h"
13 #include "efx-presets.h"
15 template<typename T, size_t N>
16 static inline size_t countof(const T(&)[N])
17 { return N; }
19 #define DECL(x) { #x, EFX_REVERB_PRESET_##x }
20 static const struct {
21 const char name[32];
22 EFXEAXREVERBPROPERTIES props;
23 } reverblist[] = {
24 DECL(GENERIC),
25 DECL(PADDEDCELL),
26 DECL(ROOM),
27 DECL(BATHROOM),
28 DECL(LIVINGROOM),
29 DECL(STONEROOM),
30 DECL(AUDITORIUM),
31 DECL(CONCERTHALL),
32 DECL(CAVE),
33 DECL(ARENA),
34 DECL(HANGAR),
35 DECL(CARPETEDHALLWAY),
36 DECL(HALLWAY),
37 DECL(STONECORRIDOR),
38 DECL(ALLEY),
39 DECL(FOREST),
40 DECL(CITY),
41 DECL(MOUNTAINS),
42 DECL(QUARRY),
43 DECL(PLAIN),
44 DECL(PARKINGLOT),
45 DECL(SEWERPIPE),
46 DECL(UNDERWATER),
47 DECL(DRUGGED),
48 DECL(DIZZY),
49 DECL(PSYCHOTIC),
51 DECL(CASTLE_SMALLROOM),
52 DECL(CASTLE_SHORTPASSAGE),
53 DECL(CASTLE_MEDIUMROOM),
54 DECL(CASTLE_LARGEROOM),
55 DECL(CASTLE_LONGPASSAGE),
56 DECL(CASTLE_HALL),
57 DECL(CASTLE_CUPBOARD),
58 DECL(CASTLE_COURTYARD),
59 DECL(CASTLE_ALCOVE),
61 DECL(FACTORY_SMALLROOM),
62 DECL(FACTORY_SHORTPASSAGE),
63 DECL(FACTORY_MEDIUMROOM),
64 DECL(FACTORY_LARGEROOM),
65 DECL(FACTORY_LONGPASSAGE),
66 DECL(FACTORY_HALL),
67 DECL(FACTORY_CUPBOARD),
68 DECL(FACTORY_COURTYARD),
69 DECL(FACTORY_ALCOVE),
71 DECL(ICEPALACE_SMALLROOM),
72 DECL(ICEPALACE_SHORTPASSAGE),
73 DECL(ICEPALACE_MEDIUMROOM),
74 DECL(ICEPALACE_LARGEROOM),
75 DECL(ICEPALACE_LONGPASSAGE),
76 DECL(ICEPALACE_HALL),
77 DECL(ICEPALACE_CUPBOARD),
78 DECL(ICEPALACE_COURTYARD),
79 DECL(ICEPALACE_ALCOVE),
81 DECL(SPACESTATION_SMALLROOM),
82 DECL(SPACESTATION_SHORTPASSAGE),
83 DECL(SPACESTATION_MEDIUMROOM),
84 DECL(SPACESTATION_LARGEROOM),
85 DECL(SPACESTATION_LONGPASSAGE),
86 DECL(SPACESTATION_HALL),
87 DECL(SPACESTATION_CUPBOARD),
88 DECL(SPACESTATION_ALCOVE),
90 DECL(WOODEN_SMALLROOM),
91 DECL(WOODEN_SHORTPASSAGE),
92 DECL(WOODEN_MEDIUMROOM),
93 DECL(WOODEN_LARGEROOM),
94 DECL(WOODEN_LONGPASSAGE),
95 DECL(WOODEN_HALL),
96 DECL(WOODEN_CUPBOARD),
97 DECL(WOODEN_COURTYARD),
98 DECL(WOODEN_ALCOVE),
100 DECL(SPORT_EMPTYSTADIUM),
101 DECL(SPORT_SQUASHCOURT),
102 DECL(SPORT_SMALLSWIMMINGPOOL),
103 DECL(SPORT_LARGESWIMMINGPOOL),
104 DECL(SPORT_GYMNASIUM),
105 DECL(SPORT_FULLSTADIUM),
106 DECL(SPORT_STADIUMTANNOY),
108 DECL(PREFAB_WORKSHOP),
109 DECL(PREFAB_SCHOOLROOM),
110 DECL(PREFAB_PRACTISEROOM),
111 DECL(PREFAB_OUTHOUSE),
112 DECL(PREFAB_CARAVAN),
114 DECL(DOME_TOMB),
115 DECL(PIPE_SMALL),
116 DECL(DOME_SAINTPAULS),
117 DECL(PIPE_LONGTHIN),
118 DECL(PIPE_LARGE),
119 DECL(PIPE_RESONANT),
121 DECL(OUTDOORS_BACKYARD),
122 DECL(OUTDOORS_ROLLINGPLAINS),
123 DECL(OUTDOORS_DEEPCANYON),
124 DECL(OUTDOORS_CREEK),
125 DECL(OUTDOORS_VALLEY),
127 DECL(MOOD_HEAVEN),
128 DECL(MOOD_HELL),
129 DECL(MOOD_MEMORY),
131 DECL(DRIVING_COMMENTATOR),
132 DECL(DRIVING_PITGARAGE),
133 DECL(DRIVING_INCAR_RACER),
134 DECL(DRIVING_INCAR_SPORTS),
135 DECL(DRIVING_INCAR_LUXURY),
136 DECL(DRIVING_FULLGRANDSTAND),
137 DECL(DRIVING_EMPTYGRANDSTAND),
138 DECL(DRIVING_TUNNEL),
140 DECL(CITY_STREETS),
141 DECL(CITY_SUBWAY),
142 DECL(CITY_MUSEUM),
143 DECL(CITY_LIBRARY),
144 DECL(CITY_UNDERPASS),
145 DECL(CITY_ABANDONED),
147 DECL(DUSTYROOM),
148 DECL(CHAPEL),
149 DECL(SMALLWATERROOM),
151 #undef DECL
154 int main(int argc, char *argv[])
156 alure::DeviceManager &devMgr = alure::DeviceManager::get();
158 int fileidx = 1;
159 alure::Device dev = [argc,argv,&devMgr,&fileidx]() -> alure::Device
161 if(argc > 3 && strcmp(argv[1], "-device") == 0)
163 fileidx = 3;
164 try {
165 return devMgr.openPlayback(argv[2]);
167 catch(...) {
168 std::cerr<< "Failed to open \""<<argv[2]<<"\" - trying default" <<std::endl;
171 return devMgr.openPlayback();
172 }();
173 std::cout<< "Opened \""<<dev.getName()<<"\"" <<std::endl;
175 alure::Context ctx = dev.createContext();
176 alure::Context::MakeCurrent(ctx);
178 bool gotreverb = false;
179 alure::Effect effect = ctx.createEffect();
181 int i = fileidx;
182 if(argc-i >= 2 && strcasecmp(argv[i], "-preset") == 0)
184 for(size_t j = 0;j < countof(reverblist);j++)
186 if(strcasecmp(argv[i+1], reverblist[j].name) == 0)
188 std::cout<< "Loading preset "<<reverblist[j].name <<std::endl;
189 std::cout.flush();
191 effect.setReverbProperties(reverblist[j].props);
192 gotreverb = true;
193 break;
196 i += 2;
198 if(!gotreverb)
200 std::cout<< "Loading generic preset" <<std::endl;
201 std::cout.flush();
203 effect.setReverbProperties(EFX_REVERB_PRESET_GENERIC);
206 alure::AuxiliaryEffectSlot auxslot = ctx.createAuxiliaryEffectSlot();
207 auxslot.applyEffect(effect);
209 for(;i < argc;i++)
211 alure::SharedPtr<alure::Decoder> decoder(ctx.createDecoder(argv[i]));
212 alure::Source source = ctx.createSource();
214 source.setAuxiliarySend(auxslot, 0);
216 source.play(decoder, 12000, 4);
217 std::cout<< "Playing "<<argv[i]<<" ("<<alure::GetSampleTypeName(decoder->getSampleType())<<", "
218 <<alure::GetChannelConfigName(decoder->getChannelConfig())<<", "
219 <<decoder->getFrequency()<<"hz)" <<std::endl;
221 float invfreq = 1.0f / decoder->getFrequency();
222 while(source.isPlaying())
224 std::cout<< "\r "<<std::setiosflags(std::ios::fixed)<<std::setprecision(2)<<
225 (source.getOffset()*invfreq)<<" / "<<(decoder->getLength()*invfreq);
226 std::cout.flush();
227 std::this_thread::sleep_for(std::chrono::milliseconds(25));
228 ctx.update();
230 std::cout<<std::endl;
232 source.release();
233 decoder.reset();
236 auxslot.release();
237 effect.destroy();
239 alure::Context::MakeCurrent(nullptr);
240 ctx.destroy();
241 dev.close();
243 return 0;