MakoFM: optionally take ownership of music data buffer

This commit is contained in:
kichikuou
2020-11-23 14:26:39 +09:00
parent b6a47d8db7
commit d6640b338a
6 changed files with 17 additions and 17 deletions
+1 -6
View File
@@ -13,7 +13,6 @@
namespace {
std::unique_ptr<MakoFMgen> fm;
uint8* fmdata;
} // namespace
@@ -45,9 +44,7 @@ void MAKO::play_music(int page)
uint8* data = dri.load(amus, page, &size);
if (!data)
return;
fm = std::make_unique<MakoFMgen>(data);
free(fmdata);
fmdata = data;
fm = std::make_unique<MakoFMgen>(data, true);
EM_ASM({ xsystem35.audio.enable_audio_hook(); });
} else {
auto midi = std::make_unique<MAKOMidi>(nact, amus);
@@ -77,8 +74,6 @@ void MAKO::stop_music()
});
fm = nullptr;
free(fmdata);
fmdata = nullptr;
current_music = 0;
}
+3 -4
View File
@@ -6,8 +6,8 @@
const int SAMPLE_RATE = 44100;
const int OPNA_CLOCK = 3993600 * 2;
MakoFMgen::MakoFMgen(const uint8_t* data) :
MakoFM(data),
MakoFMgen::MakoFMgen(const uint8_t* data, bool free_data) :
MakoFM(data, free_data),
sync_ms(0),
samples_left(0) {
bool ok = opna.Init(OPNA_CLOCK, SAMPLE_RATE, false);
@@ -49,7 +49,7 @@ void MakoFMgen::Mix(int samples) {
samples_left = samples - out_samples;
samples = out_samples;
}
FM_SAMPLETYPE* buf = new FM_SAMPLETYPE[samples * 2];
FM_SAMPLETYPE* buf = (FM_SAMPLETYPE*)alloca(samples * 2 * sizeof(FM_SAMPLETYPE));
memset(buf, 0, samples * sizeof(FM_SAMPLETYPE) * 2);
opna.Mix(buf, samples);
for (int i = 0; i < samples * 2; i += 2) {
@@ -58,5 +58,4 @@ void MakoFMgen::Mix(int samples) {
}
out_samples -= samples;
output += samples * 2;
delete[] buf;
}
+1 -1
View File
@@ -6,7 +6,7 @@
class MakoFMgen : private MakoFM {
public:
MakoFMgen(const uint8_t* data);
MakoFMgen(const uint8_t* data, bool free_data);
void Process(int16* stream, int len);
private:
+8 -1
View File
@@ -1,6 +1,7 @@
#include "makofm.h"
#include <algorithm>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
@@ -42,8 +43,9 @@ const uint8_t PresetFMTone[TONE_DATA_SIZE] = {
} // namespace
MakoFM::MakoFM(const uint8_t* data) :
MakoFM::MakoFM(const uint8_t* data, bool free_data) :
data(data),
free_data(free_data),
tone_offset(GetWord(0)),
ver(GetWord(2)) {
memset(work, 0, sizeof(work));
@@ -57,6 +59,11 @@ MakoFM::MakoFM(const uint8_t* data) :
}
}
MakoFM::~MakoFM() {
if (free_data)
free((void*)data);
}
void MakoFM::MainLoop() {
if (time_ms == 0.0) {
InitOPNA();
+3 -1
View File
@@ -12,7 +12,8 @@ const int MAKO_MAXCH = 9;
// [2] http://mydocuments.g2.xrea.com/html/p6/makop6.html
class MakoFM {
public:
MakoFM(const uint8_t* data);
MakoFM(const uint8_t* data, bool free_data);
~MakoFM();
void MainLoop();
float GetTime() const { return time_ms; }
@@ -116,6 +117,7 @@ private:
Work work[9];
const uint8_t* data;
const bool free_data;
uint16_t tone_offset;
uint16_t ver;
+1 -4
View File
@@ -25,7 +25,6 @@ Mix_Music *mix_music;
Mix_Chunk *mix_chunk;
SDL_mutex* fm_mutex;
std::unique_ptr<MakoFMgen> fm;
uint8* fmdata;
void FMHook(void *udata, Uint8 *stream, int len) {
SDL_LockMutex(fm_mutex);
@@ -114,9 +113,7 @@ void MAKO::play_music(int page)
fm_mutex = SDL_CreateMutex();
SDL_LockMutex(fm_mutex);
free(fmdata);
fmdata = data;
fm = std::make_unique<MakoFMgen>(fmdata);
fm = std::make_unique<MakoFMgen>(data, true);
Mix_HookMusic(&FMHook, this);
SDL_UnlockMutex(fm_mutex);
} else {