Expose a static interface for using the sample loader, for any

other sound chips which may need to fetch samples.
This commit is contained in:
Aaron Giles 2012-02-25 19:19:01 +00:00
parent 8d02105004
commit 19d720efc1
2 changed files with 26 additions and 29 deletions

View File

@ -122,10 +122,10 @@ void samples_device::start(UINT8 channel, UINT32 samplenum, bool loop)
chan.stream->update(); chan.stream->update();
// update the parameters // update the parameters
loaded_sample &sample = m_sample[samplenum]; sample_t &sample = m_sample[samplenum];
chan.source = sample.data; chan.source = sample.data;
chan.source_length = sample.length; chan.source_length = sample.data.count();
chan.source_num = (sample.data.count() > 0) ? samplenum : -1; chan.source_num = (chan.source_length > 0) ? samplenum : -1;
chan.pos = 0; chan.pos = 0;
chan.frac = 0; chan.frac = 0;
chan.basefreq = sample.frequency; chan.basefreq = sample.frequency;
@ -330,9 +330,9 @@ void samples_device::device_post_load()
channel_t &chan = m_channel[channel]; channel_t &chan = m_channel[channel];
if (chan.source_num >= 0 && chan.source_num < m_sample.count()) if (chan.source_num >= 0 && chan.source_num < m_sample.count())
{ {
loaded_sample &sample = m_sample[chan.source_num]; sample_t &sample = m_sample[chan.source_num];
chan.source = sample.data; chan.source = sample.data;
chan.source_length = sample.length; chan.source_length = sample.data.count();
if (sample.data == NULL) if (sample.data == NULL)
chan.source_num = -1; chan.source_num = -1;
} }
@ -426,7 +426,7 @@ void samples_device::sound_stream_update(sound_stream &stream, stream_sample_t *
// sample // sample
//------------------------------------------------- //-------------------------------------------------
bool samples_device::read_sample(emu_file &file, loaded_sample &sample) bool samples_device::read_sample(emu_file &file, sample_t &sample)
{ {
// read the core header and make sure it's a proper file // read the core header and make sure it's a proper file
UINT8 buf[4]; UINT8 buf[4];
@ -453,7 +453,7 @@ bool samples_device::read_sample(emu_file &file, loaded_sample &sample)
// read_wav_sample - read a WAV file as a sample // read_wav_sample - read a WAV file as a sample
//------------------------------------------------- //-------------------------------------------------
bool samples_device::read_wav_sample(emu_file &file, loaded_sample &sample) bool samples_device::read_wav_sample(emu_file &file, sample_t &sample)
{ {
// we already read the opening 'WAVE' header // we already read the opening 'WAVE' header
UINT32 offset = 4; UINT32 offset = 4;
@ -548,7 +548,6 @@ bool samples_device::read_wav_sample(emu_file &file, loaded_sample &sample)
// read the data in // read the data in
if (bits == 8) if (bits == 8)
{ {
sample.length = length;
sample.data.resize(length); sample.data.resize(length);
file.read(sample.data, length); file.read(sample.data, length);
@ -560,13 +559,12 @@ bool samples_device::read_wav_sample(emu_file &file, loaded_sample &sample)
else else
{ {
// 16-bit data is fine as-is // 16-bit data is fine as-is
sample.length = length / 2;
sample.data.resize(length / 2); sample.data.resize(length / 2);
file.read(sample.data, length); file.read(sample.data, length);
// swap high/low on big-endian systems // swap high/low on big-endian systems
if (ENDIANNESS_NATIVE != ENDIANNESS_LITTLE) if (ENDIANNESS_NATIVE != ENDIANNESS_LITTLE)
for (UINT32 sindex = 0; sindex < sample.length; sindex++) for (UINT32 sindex = 0; sindex < length / 2; sindex++)
sample.data[sindex] = LITTLE_ENDIANIZE_INT16(sample.data[sindex]); sample.data[sindex] = LITTLE_ENDIANIZE_INT16(sample.data[sindex]);
} }
return true; return true;
@ -577,14 +575,13 @@ bool samples_device::read_wav_sample(emu_file &file, loaded_sample &sample)
// read_flac_sample - read a FLAC file as a sample // read_flac_sample - read a FLAC file as a sample
//------------------------------------------------- //-------------------------------------------------
bool samples_device::read_flac_sample(emu_file &file, loaded_sample &sample) bool samples_device::read_flac_sample(emu_file &file, sample_t &sample)
{ {
// seek back to the start of the file // seek back to the start of the file
file.seek(0, SEEK_SET); file.seek(0, SEEK_SET);
// create the FLAC decoder and fill in the sample data // create the FLAC decoder and fill in the sample data
flac_decoder decoder(file); flac_decoder decoder(file);
sample.length = decoder.total_samples();
sample.frequency = decoder.sample_rate(); sample.frequency = decoder.sample_rate();
// error if more than 1 channel or not 16bpp // error if more than 1 channel or not 16bpp
@ -594,8 +591,8 @@ bool samples_device::read_flac_sample(emu_file &file, loaded_sample &sample)
return false; return false;
// resize the array and read // resize the array and read
sample.data.resize(sample.length); sample.data.resize(decoder.total_samples());
if (!decoder.decode_interleaved(sample.data, sample.length)) if (!decoder.decode_interleaved(sample.data, sample.data.count()))
return false; return false;
// finish up and clean up // finish up and clean up

View File

@ -105,6 +105,17 @@ public:
void set_frequency(UINT8 channel, UINT32 frequency); void set_frequency(UINT8 channel, UINT32 frequency);
void set_volume(UINT8 channel, float volume); void set_volume(UINT8 channel, float volume);
// helpers
struct sample_t
{
// shouldn't need a copy, but in case it happens, catch it here
sample_t &operator=(const sample_t &rhs) { assert(false); return *this; }
UINT32 frequency; // frequency of the sample
dynamic_array<INT16> data; // 16-bit signed data
};
static bool read_sample(emu_file &file, sample_t &sample);
protected: protected:
// subclasses can do it this way // subclasses can do it this way
samples_device(const machine_config &mconfig, device_type type, const char *name, const char *tag, device_t *owner, UINT32 clock); samples_device(const machine_config &mconfig, device_type type, const char *name, const char *tag, device_t *owner, UINT32 clock);
@ -119,16 +130,6 @@ protected:
private: private:
// internal classes // internal classes
struct loaded_sample
{
// shouldn't need a copy, but in case it happens, catch it here
loaded_sample &operator=(const loaded_sample &rhs) { assert(false); return *this; }
UINT32 length; // length in samples
UINT32 frequency; // frequency of the sample
dynamic_array<INT16> data; // 16-bit signed data
};
struct channel_t struct channel_t
{ {
sound_stream * stream; sound_stream * stream;
@ -144,14 +145,13 @@ private:
}; };
// internal helpers // internal helpers
bool read_sample(emu_file &file, loaded_sample &sample); static bool read_wav_sample(emu_file &file, sample_t &sample);
bool read_wav_sample(emu_file &file, loaded_sample &sample); static bool read_flac_sample(emu_file &file, sample_t &sample);
bool read_flac_sample(emu_file &file, loaded_sample &sample);
void load_samples(); void load_samples();
// internal state // internal state
dynamic_array<channel_t> m_channel; dynamic_array<channel_t> m_channel;
dynamic_array<loaded_sample> m_sample; dynamic_array<sample_t> m_sample;
// internal constants // internal constants
static const UINT8 FRAC_BITS = 24; static const UINT8 FRAC_BITS = 24;