From d5b44e44e1eba73ee08b8fc869bbe3041900cc18 Mon Sep 17 00:00:00 2001 From: Muzychenko Andrey <33288308+k4zmu2a@users.noreply.github.com> Date: Wed, 18 Aug 2021 12:44:26 +0300 Subject: [PATCH] Improved wav duration calculation - now supports sample rates other than 11025. Bumped build tool version to VS2019. --- SpaceCadetPinball/SpaceCadetPinball.vcxproj | 10 +++--- SpaceCadetPinball/loader.cpp | 7 +++-- SpaceCadetPinball/loader.h | 34 +++++++++++++++++++++ SpaceCadetPinball/midi.cpp | 6 ++-- SpaceCadetPinball/midi.h | 2 +- 5 files changed, 48 insertions(+), 11 deletions(-) diff --git a/SpaceCadetPinball/SpaceCadetPinball.vcxproj b/SpaceCadetPinball/SpaceCadetPinball.vcxproj index cf961cf..1848094 100644 --- a/SpaceCadetPinball/SpaceCadetPinball.vcxproj +++ b/SpaceCadetPinball/SpaceCadetPinball.vcxproj @@ -23,32 +23,32 @@ {F7B78CC7-6984-4F79-9486-ABCF87DF9F06} Win32Proj SpaceCadetPinball - 10.0.17763.0 + 10.0 Application true - v141 + v142 NotSet Application false - v141 + v142 true NotSet Application true - v141 + v142 NotSet Application false - v141 + v142 true NotSet diff --git a/SpaceCadetPinball/loader.cpp b/SpaceCadetPinball/loader.cpp index 3fbfa66..5447bbc 100644 --- a/SpaceCadetPinball/loader.cpp +++ b/SpaceCadetPinball/loader.cpp @@ -135,6 +135,8 @@ int loader::get_sound_id(int groupIndex) if (!sound_list[soundIndex].Loaded && !sound_list[soundIndex].WavePtr) { + WaveHeader wavHeader{}; + int soundGroupId = sound_list[soundIndex].GroupIndex; sound_list[soundIndex].Duration = 0.0; if (soundGroupId > 0 && !pinball::quickFlag) @@ -150,9 +152,10 @@ int loader::get_sound_id(int groupIndex) pinball::make_path_name(filePath, fileName2); HFILE hFile = _lopen(filePath, 0); - sound_list[soundIndex].Duration = static_cast(static_cast(_llseek(hFile, 0, SEEK_END)) * - 0.0000909090909090909); + _lread(hFile, &wavHeader, sizeof wavHeader); _lclose(hFile); + auto sampleCount = wavHeader.data_size / (wavHeader.channels * (wavHeader.bits_per_sample / 8.0)); + sound_list[soundIndex].Duration = static_cast(sampleCount / wavHeader.sample_rate); sound_list[soundIndex].WavePtr = Sound::LoadWaveFile(filePath); } } diff --git a/SpaceCadetPinball/loader.h b/SpaceCadetPinball/loader.h index caf94f8..0e2ba9a 100644 --- a/SpaceCadetPinball/loader.h +++ b/SpaceCadetPinball/loader.h @@ -48,6 +48,40 @@ struct visualStruct zmap_header_type* ZMap; }; +#pragma pack(push) +#pragma pack(1) +// WAVE file header format +struct WaveHeader +{ + unsigned char riff[4]; // RIFF string + + unsigned int overall_size; // overall size of file in bytes + + unsigned char wave[4]; // WAVE string + + unsigned char fmt_chunk_marker[4]; // fmt string with trailing null char + + unsigned int length_of_fmt; // length of the format data + + unsigned short format_type; // format type. 1-PCM, 3- IEEE float, 6 - 8bit A law, 7 - 8bit mu law + + unsigned short channels; // no.of channels + + unsigned int sample_rate; // sampling rate (blocks per second) + + unsigned int byterate; // SampleRate * NumChannels * BitsPerSample/8 + + unsigned short block_align; // NumChannels * BitsPerSample/8 + + unsigned short bits_per_sample; // bits per sample, 8- 8bits, 16- 16 bits etc + + unsigned char data_chunk_header[4]; // DATA string or FLLR string + + unsigned int data_size; // NumSamples * NumChannels * BitsPerSample/8 - size of the next chunk that will be read +}; +#pragma pack(pop) +static_assert(sizeof(WaveHeader) == 44, "Wrong size of WaveHeader"); + class loader { diff --git a/SpaceCadetPinball/midi.cpp b/SpaceCadetPinball/midi.cpp index 5a6d4e9..92f3e1a 100644 --- a/SpaceCadetPinball/midi.cpp +++ b/SpaceCadetPinball/midi.cpp @@ -6,7 +6,7 @@ #include "pinball.h" tagMCI_OPEN_PARMSA midi::mci_open_info; -char midi::midi_device_type[28]; +char midi::midi_file_name[28]; HWND midi::midi_notify_hwnd; int midi::midi_seq1_open, midi::midi_seq1_playing; @@ -53,8 +53,8 @@ int midi::music_init(HWND hwnd) mci_open_info.wDeviceID = 0; midi_notify_hwnd = hwnd; - lstrcpyA(midi_device_type, pinball::get_rc_string(156, 0)); - mci_open_info.lpstrElementName = midi_device_type; + lstrcpyA(midi_file_name, pinball::get_rc_string(156, 0)); + mci_open_info.lpstrElementName = midi_file_name; mci_open_info.lpstrDeviceType = nullptr; auto result = mciSendCommandA(0, MCI_OPEN, MCI_OPEN_ELEMENT | MCI_NOTIFY_SUPERSEDED, (DWORD_PTR)&mci_open_info); midi_seq1_open = result == 0; diff --git a/SpaceCadetPinball/midi.h b/SpaceCadetPinball/midi.h index a02ee79..cecca49 100644 --- a/SpaceCadetPinball/midi.h +++ b/SpaceCadetPinball/midi.h @@ -59,7 +59,7 @@ public: static void music_shutdown(); private: static tagMCI_OPEN_PARMSA mci_open_info; - static char midi_device_type[28]; + static char midi_file_name[28]; static HWND midi_notify_hwnd; static int midi_seq1_open, midi_seq1_playing;