Print the running time in the examples
[alure.git] / examples / alure-stream.cpp
blobaabb43a2f494f3c2808a7f9636b3cf5040a93cc7
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
18 #include <iostream>
19 #include <iomanip>
21 #include "alure2.h"
23 int main(int argc, char *argv[])
25 alure::DeviceManager *devMgr = alure::DeviceManager::get();
27 alure::Device *dev = devMgr->openPlayback();
28 std::cout<< "Opened \""<<dev->getName(alure::PlaybackDevType_Basic)<<"\"" <<std::endl;
30 alure::Context *ctx = dev->createContext();
31 alure::Context::MakeCurrent(ctx);
33 for(int i = 1;i < argc;i++)
35 alure::Decoder *decoder = ctx->createDecoder(argv[i]);
36 alure::Source *source = ctx->getSource();
37 source->play(decoder, 32768, 4);
38 std::cout<< "Playing "<<argv[i]<<" ("<<alure::GetSampleTypeName(decoder->getSampleType())<<", "
39 <<alure::GetSampleConfigName(decoder->getSampleConfig())<<", "
40 <<decoder->getFrequency()<<"hz)" <<std::endl;
42 float invfreq = 1.0f / decoder->getFrequency();
43 while(source->isPlaying())
45 std::cout<< "\r "<<std::setiosflags(std::ios::fixed)<<std::setprecision(2)<<
46 (source->getOffset()*invfreq)<<" / "<<(decoder->getLength()*invfreq);
47 std::cout.flush();
48 Sleep(25);
49 ctx->update();
51 std::cout<<std::endl;
53 ctx->finalize(source);
54 source = 0;
55 delete decoder;
56 decoder = 0;
59 alure::Context::MakeCurrent(0);
60 ctx->destroy();
61 ctx = 0;
62 dev->close();
63 dev = 0;
65 return 0;