2 * An example showing how to use an external decoder to play files through the
20 // Some I/O function callback wrappers for DUMB to read from an std::istream
21 static int cb_skip(void *user_data
, long offset
)
23 std::istream
*stream
= static_cast<std::istream
*>(user_data
);
26 if(stream
->seekg(offset
, std::ios_base::cur
))
31 static long cb_read(char *ptr
, long size
, void *user_data
)
33 std::istream
*stream
= static_cast<std::istream
*>(user_data
);
36 stream
->read(ptr
, size
);
37 return stream
->gcount();
40 static int cb_read_char(void *user_data
)
42 std::istream
*stream
= static_cast<std::istream
*>(user_data
);
46 stream
->read(reinterpret_cast<char*>(&ret
), 1);
47 if(stream
->gcount() > 0) return ret
;
51 static int cb_seek(void *user_data
, long n
)
53 std::istream
*stream
= static_cast<std::istream
*>(user_data
);
61 static long cb_get_size(void *user_data
)
63 std::istream
*stream
= static_cast<std::istream
*>(user_data
);
67 std::streampos pos
= stream
->tellg();
68 if(pos
!= -1 && stream
->seekg(0, std::ios::end
))
70 len
= stream
->tellg();
77 // Inherit from alure::Decoder to make a custom decoder (DUMB for this example)
78 class DumbDecoder final
: public alure::Decoder
{
79 alure::UniquePtr
<std::istream
> mFile
;
81 alure::UniquePtr
<DUMBFILE_SYSTEM
> mDfs
;
84 DUH_SIGRENDERER
*mRenderer
;
86 alure::SampleType mSampleType
;
89 alure::Vector
<sample_t
> mSampleBuf
;
92 DumbDecoder(alure::UniquePtr
<std::istream
> file
, alure::UniquePtr
<DUMBFILE_SYSTEM
> dfs
,
93 DUMBFILE
*dfile
, DUH
*duh
, DUH_SIGRENDERER
*renderer
, alure::SampleType stype
,
95 : mFile(std::move(file
)), mDfs(std::move(dfs
)), mDumbfile(dfile
), mDuh(duh
)
96 , mRenderer(renderer
), mSampleType(stype
), mFrequency(srate
)
98 ~DumbDecoder() override
100 duh_end_sigrenderer(mRenderer
);
106 dumbfile_close(mDumbfile
);
110 ALuint
getFrequency() const override
111 { return mFrequency
; }
112 alure::ChannelConfig
getChannelConfig() const override
114 // We always have DUMB render to stereo
115 return alure::ChannelConfig::Stereo
;
117 alure::SampleType
getSampleType() const override
119 // DUMB renders to 8.24 normalized fixed point, which we convert to
120 // 32-bit float or signed 16-bit samples
124 uint64_t getLength() const override
126 // Modules have no explicit length, they just keep playing as long as
127 // more samples get generated.
131 bool seek(uint64_t) override
137 std::pair
<uint64_t,uint64_t> getLoopPoints() const override
140 return std::make_pair(0, 0);
143 ALuint
read(ALvoid
*ptr
, ALuint count
) override
147 mSampleBuf
.resize(count
*2);
148 alure::Array
<sample_t
*,1> samples
{{mSampleBuf
.data()}};
150 dumb_silence(samples
[0], mSampleBuf
.size());
151 ret
= duh_sigrenderer_generate_samples(mRenderer
, 1.0f
, 65536.0f
/mFrequency
, count
,
153 if(mSampleType
== alure::SampleType::Float32
)
155 ALfloat
*out
= reinterpret_cast<ALfloat
*>(ptr
);
156 for(ALuint i
= 0;i
< ret
*2;i
++)
157 out
[i
] = (ALfloat
)samples
[0][i
] * (1.0f
/8388608.0f
);
161 ALshort
*out
= reinterpret_cast<ALshort
*>(ptr
);
162 for(ALuint i
= 0;i
< ret
*2;i
++)
164 sample_t smp
= samples
[0][i
]>>8;
165 if(smp
< -32768) smp
= -32768;
166 else if(smp
> 32767) smp
= 32767;
175 // Inherit from alure::DecoderFactory to use our custom decoder
176 class DumbFactory final
: public alure::DecoderFactory
{
177 alure::SharedPtr
<alure::Decoder
> createDecoder(alure::UniquePtr
<std::istream
> &file
) override
179 static const alure::Array
<DUH
*(*)(DUMBFILE
*),3> init_funcs
{{
180 dumb_read_it
, dumb_read_xm
, dumb_read_s3m
183 auto dfs
= alure::MakeUnique
<DUMBFILE_SYSTEM
>();
186 dfs
->getc
= cb_read_char
;
187 dfs
->getnc
= cb_read
;
188 dfs
->close
= nullptr;
190 dfs
->get_size
= cb_get_size
;
192 DUMBFILE
*dfile
= dumbfile_open_ex(file
.get(), dfs
.get());
193 if(!dfile
) return nullptr;
195 alure::Context ctx
= alure::Context::GetCurrent();
196 alure::SampleType stype
= alure::SampleType::Float32
;
197 if(!ctx
.isSupported(alure::ChannelConfig::Stereo
, stype
))
198 stype
= alure::SampleType::Int16
;
199 ALuint freq
= ctx
.getDevice().getFrequency();
201 for(auto init
: init_funcs
)
204 if((duh
=init(dfile
)) != nullptr)
206 DUH_SIGRENDERER
*renderer
;
207 if((renderer
=duh_start_sigrenderer(duh
, 0, 2, 0)) != nullptr)
208 return alure::MakeShared
<DumbDecoder
>(
209 std::move(file
), std::move(dfs
), dfile
, duh
, renderer
, stype
, freq
216 dumbfile_seek(dfile
, 0, SEEK_SET
);
220 if((duh
=dumb_read_mod(dfile
, 1)) != nullptr)
222 DUH_SIGRENDERER
*renderer
;
223 if((renderer
=duh_start_sigrenderer(duh
, 0, 2, 0)) != nullptr)
224 return alure::MakeShared
<DumbDecoder
>(
225 std::move(file
), std::move(dfs
), dfile
, duh
, renderer
, stype
, freq
232 dumbfile_close(dfile
);
240 int main(int argc
, char *argv
[])
242 // Set our custom factory for decoding modules.
243 alure::RegisterDecoder("dumb", alure::MakeUnique
<DumbFactory
>());
245 alure::DeviceManager devMgr
= alure::DeviceManager::get();
249 if(argc
> 3 && strcmp(argv
[1], "-device") == 0)
252 dev
= devMgr
.openPlayback(argv
[2], std::nothrow
);
254 std::cerr
<< "Failed to open \""<<argv
[2]<<"\" - trying default" <<std::endl
;
257 dev
= devMgr
.openPlayback();
258 std::cout
<< "Opened \""<<dev
.getName()<<"\"" <<std::endl
;
260 alure::Context ctx
= dev
.createContext();
261 alure::Context::MakeCurrent(ctx
);
263 for(int i
= fileidx
;i
< argc
;i
++)
265 alure::SharedPtr
<alure::Decoder
> decoder(ctx
.createDecoder(argv
[i
]));
266 alure::Source source
= ctx
.createSource();
267 source
.play(decoder
, 12000, 4);
268 std::cout
<< "Playing "<<argv
[i
]<<" ("<<alure::GetSampleTypeName(decoder
->getSampleType())<<", "
269 <<alure::GetChannelConfigName(decoder
->getChannelConfig())<<", "
270 <<decoder
->getFrequency()<<"hz)" <<std::endl
;
272 float invfreq
= 1.0f
/ decoder
->getFrequency();
273 while(source
.isPlaying())
275 std::cout
<< "\r "<<std::setiosflags(std::ios::fixed
)<<std::setprecision(2)<<
276 source
.getSecOffset().count()<<" / "<<(decoder
->getLength()*invfreq
);
278 std::this_thread::sleep_for(std::chrono::milliseconds(25));
281 std::cout
<<std::endl
;
287 alure::Context::MakeCurrent(nullptr);