Codechange: Implement SoundLoader interface and cache loaded sounds in memory.
Sounds are loaded into memory on first use, using the SoundLoader interface to support format conversion. Sounds are retained in memory to avoid reloading every time a sound is played. This deduplicates WAV header parsing between NewGRF and baseset sounds, and will allow different audio formats to be supported.
This commit is contained in:
committed by
Peter Nelson
parent
7e8bcf44f7
commit
ce5279a8dc
+9
-6
@@ -19,7 +19,7 @@
|
||||
|
||||
struct MixerChannel {
|
||||
/* pointer to allocated buffer memory */
|
||||
int8_t *memory;
|
||||
std::shared_ptr<std::vector<uint8_t>> memory;
|
||||
|
||||
/* current position in memory */
|
||||
uint32_t pos;
|
||||
@@ -76,7 +76,7 @@ static void mix_int16(MixerChannel *sc, int16_t *buffer, uint samples, uint8_t e
|
||||
sc->samples_left -= samples;
|
||||
assert(samples > 0);
|
||||
|
||||
const T *b = (const T *)sc->memory + sc->pos;
|
||||
const T *b = reinterpret_cast<const T *>(sc->memory->data()) + sc->pos;
|
||||
uint32_t frac_pos = sc->frac_pos;
|
||||
uint32_t frac_speed = sc->frac_speed;
|
||||
int volume_left = sc->volume_left * effect_vol / 255;
|
||||
@@ -103,7 +103,7 @@ static void mix_int16(MixerChannel *sc, int16_t *buffer, uint samples, uint8_t e
|
||||
}
|
||||
|
||||
sc->frac_pos = frac_pos;
|
||||
sc->pos = b - (const T *)sc->memory;
|
||||
sc->pos = b - reinterpret_cast<const T *>(sc->memory->data());
|
||||
}
|
||||
|
||||
static void MxCloseChannel(uint8_t channel_index)
|
||||
@@ -176,20 +176,22 @@ MixerChannel *MxAllocateChannel()
|
||||
uint8_t channel_index = FindFirstBit(available);
|
||||
|
||||
MixerChannel *mc = &_channels[channel_index];
|
||||
free(mc->memory);
|
||||
mc->memory = nullptr;
|
||||
return mc;
|
||||
}
|
||||
|
||||
void MxSetChannelRawSrc(MixerChannel *mc, int8_t *mem, size_t size, uint rate, bool is16bit)
|
||||
void MxSetChannelRawSrc(MixerChannel *mc, const std::shared_ptr<std::vector<uint8_t>> &mem, uint rate, bool is16bit)
|
||||
{
|
||||
mc->memory = mem;
|
||||
mc->frac_pos = 0;
|
||||
mc->pos = 0;
|
||||
|
||||
mc->frac_speed = (rate << 16) / _play_rate;
|
||||
mc->frac_speed = (rate << 16U) / _play_rate;
|
||||
|
||||
size_t size = mc->memory->size();
|
||||
if (is16bit) size /= 2;
|
||||
/* Less 1 to allow for padding sample for the resampler. */
|
||||
size -= 1;
|
||||
|
||||
/* adjust the magnitude to prevent overflow */
|
||||
while (size >= _max_size) {
|
||||
@@ -197,6 +199,7 @@ void MxSetChannelRawSrc(MixerChannel *mc, int8_t *mem, size_t size, uint rate, b
|
||||
rate = (rate >> 1) + 1;
|
||||
}
|
||||
|
||||
/* Scale number of samples by play rate. */
|
||||
mc->samples_left = (uint)size * _play_rate / rate;
|
||||
mc->is16bit = is16bit;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user