A non-async stream is dead, don't try refilling it
[alure.git] / examples / alure-reverb.cpp
blob5b94db29a5649f7a25cba515278a559082453860
1 #ifdef _WIN32
2 #define WIN32_LEAN_AND_MEAN
3 #include <windows.h>
4 #else
5 #include <errno.h>
6 #include <time.h>
7 #include <stdint.h>
8 inline void Sleep(uint32_t ms)
10 struct timespec ts, rem;
11 ts.tv_sec = ms / 1000;
12 ts.tv_nsec = (ms % 1000) * 1000000;
13 while(nanosleep(&ts, &rem) == -1 && errno == EINTR)
14 ts = rem;
16 #endif
17 #include <string.h>
19 #include <iostream>
20 #include <iomanip>
22 #include "alure2.h"
24 #include "efx-presets.h"
26 template<typename T, size_t N>
27 static inline size_t countof(const T(&)[N])
28 { return N; }
30 #define DECL(x) { #x, EFX_REVERB_PRESET_##x }
31 static const struct {
32 const char name[32];
33 EFXEAXREVERBPROPERTIES props;
34 } reverblist[] = {
35 DECL(GENERIC),
36 DECL(PADDEDCELL),
37 DECL(ROOM),
38 DECL(BATHROOM),
39 DECL(LIVINGROOM),
40 DECL(STONEROOM),
41 DECL(AUDITORIUM),
42 DECL(CONCERTHALL),
43 DECL(CAVE),
44 DECL(ARENA),
45 DECL(HANGAR),
46 DECL(CARPETEDHALLWAY),
47 DECL(HALLWAY),
48 DECL(STONECORRIDOR),
49 DECL(ALLEY),
50 DECL(FOREST),
51 DECL(CITY),
52 DECL(MOUNTAINS),
53 DECL(QUARRY),
54 DECL(PLAIN),
55 DECL(PARKINGLOT),
56 DECL(SEWERPIPE),
57 DECL(UNDERWATER),
58 DECL(DRUGGED),
59 DECL(DIZZY),
60 DECL(PSYCHOTIC),
62 DECL(CASTLE_SMALLROOM),
63 DECL(CASTLE_SHORTPASSAGE),
64 DECL(CASTLE_MEDIUMROOM),
65 DECL(CASTLE_LARGEROOM),
66 DECL(CASTLE_LONGPASSAGE),
67 DECL(CASTLE_HALL),
68 DECL(CASTLE_CUPBOARD),
69 DECL(CASTLE_COURTYARD),
70 DECL(CASTLE_ALCOVE),
72 DECL(FACTORY_SMALLROOM),
73 DECL(FACTORY_SHORTPASSAGE),
74 DECL(FACTORY_MEDIUMROOM),
75 DECL(FACTORY_LARGEROOM),
76 DECL(FACTORY_LONGPASSAGE),
77 DECL(FACTORY_HALL),
78 DECL(FACTORY_CUPBOARD),
79 DECL(FACTORY_COURTYARD),
80 DECL(FACTORY_ALCOVE),
82 DECL(ICEPALACE_SMALLROOM),
83 DECL(ICEPALACE_SHORTPASSAGE),
84 DECL(ICEPALACE_MEDIUMROOM),
85 DECL(ICEPALACE_LARGEROOM),
86 DECL(ICEPALACE_LONGPASSAGE),
87 DECL(ICEPALACE_HALL),
88 DECL(ICEPALACE_CUPBOARD),
89 DECL(ICEPALACE_COURTYARD),
90 DECL(ICEPALACE_ALCOVE),
92 DECL(SPACESTATION_SMALLROOM),
93 DECL(SPACESTATION_SHORTPASSAGE),
94 DECL(SPACESTATION_MEDIUMROOM),
95 DECL(SPACESTATION_LARGEROOM),
96 DECL(SPACESTATION_LONGPASSAGE),
97 DECL(SPACESTATION_HALL),
98 DECL(SPACESTATION_CUPBOARD),
99 DECL(SPACESTATION_ALCOVE),
101 DECL(WOODEN_SMALLROOM),
102 DECL(WOODEN_SHORTPASSAGE),
103 DECL(WOODEN_MEDIUMROOM),
104 DECL(WOODEN_LARGEROOM),
105 DECL(WOODEN_LONGPASSAGE),
106 DECL(WOODEN_HALL),
107 DECL(WOODEN_CUPBOARD),
108 DECL(WOODEN_COURTYARD),
109 DECL(WOODEN_ALCOVE),
111 DECL(SPORT_EMPTYSTADIUM),
112 DECL(SPORT_SQUASHCOURT),
113 DECL(SPORT_SMALLSWIMMINGPOOL),
114 DECL(SPORT_LARGESWIMMINGPOOL),
115 DECL(SPORT_GYMNASIUM),
116 DECL(SPORT_FULLSTADIUM),
117 DECL(SPORT_STADIUMTANNOY),
119 DECL(PREFAB_WORKSHOP),
120 DECL(PREFAB_SCHOOLROOM),
121 DECL(PREFAB_PRACTISEROOM),
122 DECL(PREFAB_OUTHOUSE),
123 DECL(PREFAB_CARAVAN),
125 DECL(DOME_TOMB),
126 DECL(PIPE_SMALL),
127 DECL(DOME_SAINTPAULS),
128 DECL(PIPE_LONGTHIN),
129 DECL(PIPE_LARGE),
130 DECL(PIPE_RESONANT),
132 DECL(OUTDOORS_BACKYARD),
133 DECL(OUTDOORS_ROLLINGPLAINS),
134 DECL(OUTDOORS_DEEPCANYON),
135 DECL(OUTDOORS_CREEK),
136 DECL(OUTDOORS_VALLEY),
138 DECL(MOOD_HEAVEN),
139 DECL(MOOD_HELL),
140 DECL(MOOD_MEMORY),
142 DECL(DRIVING_COMMENTATOR),
143 DECL(DRIVING_PITGARAGE),
144 DECL(DRIVING_INCAR_RACER),
145 DECL(DRIVING_INCAR_SPORTS),
146 DECL(DRIVING_INCAR_LUXURY),
147 DECL(DRIVING_FULLGRANDSTAND),
148 DECL(DRIVING_EMPTYGRANDSTAND),
149 DECL(DRIVING_TUNNEL),
151 DECL(CITY_STREETS),
152 DECL(CITY_SUBWAY),
153 DECL(CITY_MUSEUM),
154 DECL(CITY_LIBRARY),
155 DECL(CITY_UNDERPASS),
156 DECL(CITY_ABANDONED),
158 DECL(DUSTYROOM),
159 DECL(CHAPEL),
160 DECL(SMALLWATERROOM),
162 #undef DECL
165 int main(int argc, char *argv[])
167 alure::DeviceManager *devMgr = alure::DeviceManager::get();
169 alure::Device *dev = devMgr->openPlayback();
170 std::cout<< "Opened \""<<dev->getName(alure::PlaybackDevType_Basic)<<"\"" <<std::endl;
172 alure::Context *ctx = dev->createContext();
173 alure::Context::MakeCurrent(ctx);
175 bool gotreverb = false;
176 alure::Effect *effect = ctx->createEffect();
178 int i = 1;
179 if(argc-i >= 2 && strcasecmp(argv[i], "-preset") == 0)
181 for(size_t j = 0;j < countof(reverblist);j++)
183 if(strcasecmp(argv[i+1], reverblist[j].name) == 0)
185 std::cout<< "Loading preset "<<reverblist[j].name <<std::endl;
186 std::cout.flush();
188 effect->setReverbProperties(reverblist[j].props);
189 gotreverb = true;
190 break;
193 i += 2;
195 if(!gotreverb)
197 std::cout<< "Loading generic preset" <<std::endl;
198 std::cout.flush();
200 effect->setReverbProperties(EFX_REVERB_PRESET_GENERIC);
203 alure::AuxiliaryEffectSlot *auxslot = ctx->createAuxiliaryEffectSlot();
204 auxslot->applyEffect(effect);
206 for(;i < argc;i++)
208 alure::SharedPtr<alure::Decoder> decoder(ctx->createDecoder(argv[i]));
209 alure::Source *source = ctx->getSource();
211 source->setAuxiliarySend(auxslot, 0);
213 source->play(decoder, 32768, 4);
214 std::cout<< "Playing "<<argv[i]<<" ("<<alure::GetSampleTypeName(decoder->getSampleType())<<", "
215 <<alure::GetChannelConfigName(decoder->getChannelConfig())<<", "
216 <<decoder->getFrequency()<<"hz)" <<std::endl;
218 float invfreq = 1.0f / decoder->getFrequency();
219 while(source->isPlaying())
221 std::cout<< "\r "<<std::setiosflags(std::ios::fixed)<<std::setprecision(2)<<
222 (source->getOffset()*invfreq)<<" / "<<(decoder->getLength()*invfreq);
223 std::cout.flush();
224 Sleep(25);
225 ctx->update();
227 std::cout<<std::endl;
229 source->release();
230 source = 0;
231 decoder.reset();
234 auxslot->release();
235 auxslot = 0;
236 effect->destroy();
237 effect = 0;
239 alure::Context::MakeCurrent(0);
240 ctx->destroy();
241 ctx = 0;
242 dev->close();
243 dev = 0;
245 return 0;