Fix locking when removing the source stream
[alure.git] / examples / alure-play.cpp
blob854bbccf32b68995ede7d20b652edd02f0a58e69
1 /*
2 * A simple example showing how to load and play a sound.
3 */
5 #include <string.h>
7 #include <iostream>
8 #include <iomanip>
9 #include <thread>
10 #include <chrono>
12 #include "alure2.h"
14 int main(int argc, char *argv[])
16 alure::DeviceManager devMgr = alure::DeviceManager::get();
18 int fileidx = 1;
19 alure::Device dev;
20 if(argc > 3 && strcmp(argv[1], "-device") == 0)
22 fileidx = 3;
23 dev = devMgr.openPlayback(argv[2], std::nothrow);
24 if(!dev)
25 std::cerr<< "Failed to open \""<<argv[2]<<"\" - trying default" <<std::endl;
27 if(!dev)
28 dev = devMgr.openPlayback();
29 std::cout<< "Opened \""<<dev.getName()<<"\"" <<std::endl;
31 alure::Context ctx = dev.createContext();
32 alure::Context::MakeCurrent(ctx);
34 for(int i = fileidx;i < argc;i++)
36 alure::Buffer buffer = ctx.getBuffer(argv[i]);
37 alure::Source source = ctx.createSource();
38 source.play(buffer);
39 std::cout<< "Playing "<<argv[i]<<" ("<<alure::GetSampleTypeName(buffer.getSampleType())<<", "
40 <<alure::GetChannelConfigName(buffer.getChannelConfig())<<", "
41 <<buffer.getFrequency()<<"hz)" <<std::endl;
43 float invfreq = 1.0f / buffer.getFrequency();
44 while(source.isPlaying())
46 std::cout<< "\r "<<std::setiosflags(std::ios::fixed)<<std::setprecision(2)<<
47 source.getSecOffset().count()<<" / "<<(buffer.getLength()*invfreq);
48 std::cout.flush();
49 std::this_thread::sleep_for(std::chrono::milliseconds(25));
50 ctx.update();
52 std::cout<<std::endl;
54 source.release();
55 ctx.removeBuffer(buffer);
58 alure::Context::MakeCurrent(nullptr);
59 ctx.destroy();
60 dev.close();
62 return 0;