Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed several missing sounds #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 80 additions & 34 deletions PanzerChasm/sound/sounds_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,53 +66,99 @@ class RawMonsterSoundData final : public ISoundData
};


struct RiffHeader
{
uint32_t fourCC;
uint32_t size;
};

struct FormatHeader
{
uint32_t fourCC;
uint32_t size;
uint16_t audioFormat;
uint16_t channels;
uint32_t sampleRate;
uint32_t byteRate;
uint16_t blockAlign;
uint16_t bitsPerSample;
};

struct DataHeader
{
uint32_t fourCC;
uint32_t size;
};

struct WaveHeader
{
RiffHeader riff;
uint32_t wave;
FormatHeader format;
DataHeader data;
};

class WavSoundData final : public ISoundData
{
public:
WavSoundData( const Vfs::FileContent& data )
WavSoundData( Vfs::FileContent& data )
{
bool ok= false;

SDL_AudioSpec spec;
spec.freq= 0;
spec.format= 0;
spec.channels= 0;
Uint32 buffer_length_butes;

SDL_RWops* const rw_ops=
SDL_RWFromConstMem( data.data(), data.size() );

if( rw_ops != nullptr )
if( data.size() > sizeof( WaveHeader ) )
{
const SDL_AudioSpec* const result=
SDL_LoadWAV_RW(
rw_ops, 1, // 1 - means free rw_ops
&spec,
&wav_buffer_, &buffer_length_butes );
WaveHeader* const header= reinterpret_cast<WaveHeader*>( &data[0] );

ok= result != nullptr;
PC_ASSERT( result == nullptr || result == &spec );
}
// Some wave files from CSM.BIN have incorrect size of the format chunk
if( header->format.size != 16 )
{
header->format.size= 16;
}

const unsigned int bits= SDL_AUDIO_BITSIZE( spec.format );
const bool is_signed= SDL_AUDIO_ISSIGNED( spec.format ) != 0;
SDL_AudioSpec spec;
spec.freq= 0;
spec.format= 0;
spec.channels= 0;
Uint32 buffer_length_butes;

if( !( bits == 16u || bits == 8u ) )
ok= false;
if( SDL_AUDIO_ISFLOAT( spec.format ) || spec.channels != 1u )
ok= false;
SDL_RWops* const rw_ops=
SDL_RWFromConstMem( data.data(), data.size() );

if( ok )
{
frequency_= spec.freq;
data_type_=
if( rw_ops != nullptr )
{
const SDL_AudioSpec* const result=
SDL_LoadWAV_RW(
rw_ops, 1, // 1 - means free rw_ops
&spec,
&wav_buffer_, &buffer_length_butes );

ok= result != nullptr;
PC_ASSERT( result == nullptr || result == &spec );
}

const unsigned int bits= SDL_AUDIO_BITSIZE( spec.format );
const bool is_signed= SDL_AUDIO_ISSIGNED( spec.format ) != 0;

if( !( bits == 16u || bits == 8u ) )
ok= false;
if( SDL_AUDIO_ISFLOAT( spec.format ) || spec.channels != 1u )
ok= false;

if( ok )
{
frequency_= spec.freq;
data_type_=
bits == 8u
? ( is_signed ? DataType::Signed8 : DataType::Unsigned8 )
: ( is_signed ? DataType::Signed16 : DataType::Unsigned16 );
data_= wav_buffer_;
sample_count_= buffer_length_butes / ( bits / 8u );
? ( is_signed ? DataType::Signed8 : DataType::Unsigned8 )
: ( is_signed ? DataType::Signed16 : DataType::Unsigned16 );
data_= wav_buffer_;
sample_count_= buffer_length_butes / ( bits / 8u );

return;
}
}
else

if (!ok)
{
// Make dummy
wav_buffer_= nullptr;
Expand Down