2 * A simple example showing how to stream a file through a source.
17 // Helper class+method to print the time with human-readable formatting.
21 inline std::ostream
&operator<<(std::ostream
&os
, const PrettyTime
&rhs
)
23 using hours
= std::chrono::hours
;
24 using minutes
= std::chrono::minutes
;
25 using seconds
= std::chrono::seconds
;
26 using centiseconds
= std::chrono::duration
<int64_t, std::ratio
<1, 100>>;
27 using std::chrono::duration_cast
;
29 centiseconds t
= duration_cast
<centiseconds
>(rhs
.mTime
);
36 // Only handle up to hour formatting
38 os
<< duration_cast
<hours
>(t
).count() << 'h' << std::setfill('0') << std::setw(2)
39 << duration_cast
<minutes
>(t
).count() << 'm';
41 os
<< duration_cast
<minutes
>(t
).count() << 'm' << std::setfill('0');
42 os
<< std::setw(2) << (duration_cast
<seconds
>(t
).count() % 60) << '.' << std::setw(2)
43 << (t
.count() % 100) << 's' << std::setw(0) << std::setfill(' ');
49 int main(int argc
, char *argv
[])
51 alure::ArrayView
<const char*> args(argv
, argc
);
55 std::cerr
<< "Usage: "<<args
.front()<<" [-device \"device name\"] files..." <<std::endl
;
60 alure::DeviceManager devMgr
= alure::DeviceManager::getInstance();
63 if(args
.size() > 2 && args
[0] == alure::StringView("-device"))
65 dev
= devMgr
.openPlayback(args
[1], std::nothrow
);
66 if(!dev
) std::cerr
<< "Failed to open \""<<args
[1]<<"\" - trying default" <<std::endl
;
69 if(!dev
) dev
= devMgr
.openPlayback();
70 std::cout
<< "Opened \""<<dev
.getName()<<"\"" <<std::endl
;
72 alure::Context ctx
= dev
.createContext();
73 alure::Context::MakeCurrent(ctx
);
75 for(;!args
.empty();args
= args
.slice(1))
77 alure::SharedPtr
<alure::Decoder
> decoder
= ctx
.createDecoder(args
.front());
78 alure::Source source
= ctx
.createSource();
80 source
.play(decoder
, 12000, 4);
81 std::cout
<< "Playing "<<args
.front()<<" ("
82 << alure::GetSampleTypeName(decoder
->getSampleType())<<", "
83 << alure::GetChannelConfigName(decoder
->getChannelConfig())<<", "
84 << decoder
->getFrequency()<<"hz)" <<std::endl
;
86 double invfreq
= 1.0 / decoder
->getFrequency();
87 while(source
.isPlaying())
89 std::cout
<< "\r "<<PrettyTime
{source
.getSecOffset()}<<" / "<<
90 PrettyTime
{alure::Seconds(decoder
->getLength()*invfreq
)};
92 std::this_thread::sleep_for(std::chrono::milliseconds(25));
100 alure::Context::MakeCurrent(nullptr);