14static constexpr unsigned DUMMY_INPUT_RATE = 44100;
16[[nodiscard]]
static auto loadSamples(
17 std::string_view name,
const DeviceConfig& config,
18 std::string_view baseName, std::string_view alternativeName,
23 bool alreadyWarned =
false;
25 for (
auto i :
xrange(numSamples)) {
27 auto filename =
tmpStrCat(baseName, i,
".wav");
28 result[i] = WavData(
context.resolve(filename));
29 }
catch (MSXException& e1) {
31 if (alternativeName.empty())
throw;
33 alternativeName, i,
".wav");
34 result[i] = WavData(
context.resolve(filename));
35 }
catch (MSXException& ) {
39 config.getCliComm().printWarning(
40 "Couldn't read ", name,
" sample data: ",
42 ". Continuing without sample data.");
52 std::string_view samplesBaseName,
unsigned numSamples,
53 std::string_view alternativeName)
55 , samples(loadSamples(name_, config, samplesBaseName, alternativeName, numSamples))
68 currentSampleNum = unsigned(-1);
74 assert(sampleNum < samples.size());
75 currentSampleNum = sampleNum;
80void SamplePlayer::setWavParams()
82 if ((currentSampleNum < samples.size()) &&
83 samples[currentSampleNum].getSize()) {
84 const auto& wav = samples[currentSampleNum];
85 bufferSize = narrow<unsigned>(wav.getSize());
87 unsigned freq = wav.getFreq();
102 assert(sampleNum < samples.size());
103 nextSampleNum = sampleNum;
109void SamplePlayer::generateChannels(std::span<float*> bufs,
unsigned num)
112 assert(bufs.size() == 1);
118 const auto& wav = samples[currentSampleNum];
119 for (
auto i :
xrange(num)) {
120 if (index >= bufferSize) {
121 if (nextSampleNum !=
unsigned(-1)) {
124 currentSampleNum = unsigned(-1);
132 bufs[0][i] = narrow<float>(3 * wav.getSample(index++));
136void SamplePlayer::doRepeat()
141template<
typename Archive>
144 ar.serialize(
"index", index,
145 "currentSampleNum", currentSampleNum,
146 "nextSampleNum", nextSampleNum);
147 if constexpr (Archive::IS_LOADER) {
bool isPlaying() const
Is there currently playing a sample.
void serialize(Archive &ar, unsigned version)
SamplePlayer(const std::string &name, static_string_view desc, const DeviceConfig &config, std::string_view samplesBaseName, unsigned numSamples, std::string_view alternativeName={})
void play(unsigned sampleNum)
Start playing a (new) sample.
void repeat(unsigned sampleNum)
Keep on repeating the given sample data.
void stopRepeat()
Stop repeat mode.
unsigned getInputRate() const
void setInputRate(unsigned sampleRate)
void unregisterSound()
Unregisters this sound device with the Mixer.
void registerSound(const DeviceConfig &config)
Registers this sound device with the Mixer.
std::optional< Context > context
This file implemented 3 utility functions:
const FileContext & systemFileContext()
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
TemporaryString tmpStrCat(Ts &&... ts)
constexpr auto xrange(T e)