Use automatic memory management for sound stuff (#1771)

This commit is contained in:
Vladimir Olteanu 2021-04-30 10:02:07 +03:00 committed by GitHub
commit 7aad72f96d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 54 additions and 103 deletions

View file

@ -109,56 +109,39 @@ static int CapVolume(int volume)
return volume - volume % 100;
}
bool snd_playing(TSnd *pSnd)
{
if (pSnd == nullptr || pSnd->DSB == nullptr)
return false;
return pSnd->DSB->IsPlaying();
}
void snd_play_snd(TSnd *pSnd, int lVolume, int lPan)
{
SoundSample *DSB;
DWORD tc;
if (pSnd == nullptr || !gbSoundOn) {
return;
}
DSB = pSnd->DSB;
if (DSB == nullptr) {
return;
}
tc = SDL_GetTicks();
if (tc - pSnd->start_tc < 80) {
return;
}
lVolume = CapVolume(lVolume + sgOptions.Audio.nSoundVolume);
DSB->Play(lVolume, lPan);
pSnd->DSB.Play(lVolume, lPan);
pSnd->start_tc = tc;
}
TSnd *sound_file_load(const char *path, bool stream)
std::unique_ptr<TSnd> sound_file_load(const char *path, bool stream)
{
HANDLE file;
TSnd *pSnd;
int error = 0;
if (!SFileOpenFile(path, &file)) {
ErrDlg("SFileOpenFile failed", path, __FILE__, __LINE__);
}
pSnd = (TSnd *)DiabloAllocPtr(sizeof(TSnd));
memset(pSnd, 0, sizeof(TSnd));
pSnd->sound_path = path;
pSnd->start_tc = SDL_GetTicks() - 80 - 1;
pSnd->DSB = new SoundSample();
auto snd = std::make_unique<TSnd>();
snd->sound_path = std::string(path);
snd->start_tc = SDL_GetTicks() - 80 - 1;
if (stream) {
pSnd->file_handle = file;
error = pSnd->DSB->SetChunkStream(file);
snd->file_handle = file;
error = snd->DSB.SetChunkStream(file);
if (error != 0) {
SFileCloseFile(file);
ErrSdl();
@ -167,31 +150,25 @@ TSnd *sound_file_load(const char *path, bool stream)
DWORD dwBytes = SFileGetFileSize(file, nullptr);
auto wave_file = std::make_unique<std::uint8_t[]>(dwBytes);
SFileReadFile(file, wave_file.get(), dwBytes, nullptr, nullptr);
error = pSnd->DSB->SetChunk(std::move(wave_file), dwBytes);
error = snd->DSB.SetChunk(std::move(wave_file), dwBytes);
SFileCloseFile(file);
}
if (error != 0) {
ErrSdl();
}
return pSnd;
return snd;
}
void sound_file_cleanup(TSnd *sound_file)
#ifndef NOSOUND
TSnd::~TSnd()
{
if (sound_file != nullptr) {
if (sound_file->DSB != nullptr) {
sound_file->DSB->Stop();
sound_file->DSB->Release();
delete sound_file->DSB;
sound_file->DSB = nullptr;
}
if (sound_file->file_handle != nullptr)
SFileCloseFile(sound_file->file_handle);
mem_free_dbg(sound_file);
}
DSB.Stop();
DSB.Release();
if (file_handle != nullptr)
SFileCloseFile(file_handle);
}
#endif
void snd_init()
{