15 [[nodiscard]]
static auto loadSamples(
16 std::string_view name,
const DeviceConfig& config,
17 std::string_view baseName, std::string_view alternativeName,
22 bool alreadyWarned =
false;
24 for (
auto i :
xrange(numSamples)) {
28 }
catch (MSXException& e1) {
30 if (alternativeName.empty())
throw;
32 alternativeName, i,
".wav");
34 }
catch (MSXException& ) {
38 config.getCliComm().printWarning(
39 "Couldn't read ", name,
" sample data: ",
41 ". Continuing without sample data.");
51 std::string_view samplesBaseName,
unsigned numSamples,
52 std::string_view alternativeName)
54 , samples(loadSamples(name_, config, samplesBaseName, alternativeName, numSamples))
68 currentSampleNum = unsigned(-1);
74 assert(sampleNum < samples.size());
75 currentSampleNum = sampleNum;
80 void SamplePlayer::setWavParams()
82 if ((currentSampleNum < samples.size()) &&
83 samples[currentSampleNum].getSize()) {
84 const auto& wav = samples[currentSampleNum];
85 bufferSize = wav.getSize();
87 unsigned freq = wav.getFreq();
102 assert(sampleNum < samples.size());
103 nextSampleNum = sampleNum;
109 void SamplePlayer::generateChannels(
float** bufs,
unsigned num)
117 const auto& wav = samples[currentSampleNum];
118 for (
auto i :
xrange(num)) {
119 if (index >= bufferSize) {
120 if (nextSampleNum !=
unsigned(-1)) {
123 currentSampleNum = unsigned(-1);
131 bufs[0][i] = 3 * wav.getSample(index++);
135 void SamplePlayer::doRepeat()
140 template<
typename Archive>
143 ar.serialize(
"index", index,
144 "currentSampleNum", currentSampleNum,
145 "nextSampleNum", nextSampleNum);
146 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:
constexpr unsigned DUMMY_INPUT_RATE
const FileContext & systemFileContext()
constexpr const char *const filename
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
TemporaryString tmpStrCat(Ts &&... ts)
constexpr auto xrange(T e)