Skip to content

Commit

Permalink
add SFX access function
Browse files Browse the repository at this point in the history
  • Loading branch information
jpcima committed Oct 16, 2019
1 parent e31f280 commit 1e08d36
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
5 changes: 3 additions & 2 deletions sources/dunelegacy/FileClasses/adl/sound_adlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2534,8 +2534,9 @@ Mix_Chunk* SoundAdlibPC::getSubsong(int Num) {
SoundAdlibPC::callback(this, buf + bufSize - 1024, 1024);

bSilent = true;
for(Uint8* p = buf + bufSize - 1024; p < buf + bufSize; p++) {
if(*p != 0) {
for(Sint16* p = (Sint16 *)(buf + bufSize - 1024); p < (Sint16 *)(buf + bufSize); p++) {
/* prudence against OPL emulators which produce ±1 in silent state */
if(std::abs(*p) > 1) {
bSilent = false;
break;
}
Expand Down
32 changes: 32 additions & 0 deletions sources/dunemusic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,38 @@ void DuneMusic_ChangeMusicEx(DuneMusicType musicType, const char *filename, int
sPlayer->changeMusicEx((MUSICTYPE)musicType, filename, musicNum);
}

DUNEMUSIC_EXPORT
int16_t *DuneMusic_SynthesizeAudio(const char *filename, int musicNum, int volume, size_t *numFramesReturned)
{
sdl2::RWops_ptr rwop = pFileManager->openFile(filename);
if (!rwop)
return nullptr;

std::unique_ptr<SoundAdlibPC> adlib{new SoundAdlibPC(rwop.get())};

if (volume < 0)
volume = 64;

adlib->setVolume(volume);

sdl2::mix_chunk_ptr chunk{adlib->getSubsong(musicNum)};
if (!chunk)
return nullptr;

chunk->allocated = 0;

if (numFramesReturned)
*numFramesReturned = chunk->alen / (2 * sizeof(int16_t));

return (int16_t *)chunk->abuf;
}

DUNEMUSIC_EXPORT
void DuneMusic_FreeAudio(int16_t *audioBuffer)
{
SDL_free(audioBuffer);
}

DUNEMUSIC_EXPORT
int DuneMusic_IsMusicEnabled()
{
Expand Down
3 changes: 3 additions & 0 deletions sources/dunemusic.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ typedef enum DuneMusicType {
void DuneMusic_ChangeMusic(DuneMusicType musicType);
void DuneMusic_ChangeMusicEx(DuneMusicType musicType, const char *filename, int musicNum);

int16_t *DuneMusic_SynthesizeAudio(const char *filename, int musicNum, int volume, size_t *numFramesReturned);
void DuneMusic_FreeAudio(int16_t *audioBuffer);

int DuneMusic_IsMusicEnabled();
void DuneMusic_SetMusicEnabled(int enabled);
int DuneMusic_IsMusicPlaying();
Expand Down

0 comments on commit 1e08d36

Please sign in to comment.