Skip to content

Commit

Permalink
fix(DmSynth): don't overwrite sample header unity notes
Browse files Browse the repository at this point in the history
  • Loading branch information
lmichaelis committed May 1, 2024
1 parent 541d34f commit 35805de
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/util/Tsf.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,17 @@ static DmResult Dm_createHydra(DmDls* dls, struct tsf_hydra* res) {
// 6. Count the number of instrument zones and generators required and allocate them
res->ibagNum = 1; // One for the sentinel
res->igenNum = 1; // One for the sentinel
res->shdrNum = 1; // One for the sentinel

for (size_t i = 0; i < dls->instrument_count; ++i) {
DmDlsInstrument* ins = &dls->instruments[i];

// -> We need zone one for each instrument region
// -> We need one zone for each instrument region
res->ibagNum += ins->region_count;

// -> We need one sample for each instrument region
res->shdrNum += ins->region_count;

// -> We need 6 generators for the implicit 'kKeyRange', 'kVelRange', 'kAttackVolEnv',
// 'kInitialAttenuation', 'kSampleModes' and 'kSampleID' for each zone
res->igenNum += ins->region_count * 6;
Expand All @@ -246,6 +250,7 @@ static DmResult Dm_createHydra(DmDls* dls, struct tsf_hydra* res) {

res->ibags = Dm_alloc(sizeof(struct tsf_hydra_ibag) * res->ibagNum);
res->igens = Dm_alloc(sizeof(struct tsf_hydra_igen) * res->igenNum);
res->shdrs = Dm_alloc(sizeof(struct tsf_hydra_shdr) * res->shdrNum);

// 7. Count the number of instrument modulators required and allocate them
// -> We need one sentinel (modulators are not supported)
Expand Down Expand Up @@ -273,7 +278,9 @@ DmResult DmSynth_createTsfForDls(DmDls* dls, tsf** out) {
// Decode all PCM and create the sample headers.
float* pcm = NULL;
int32_t pcm_len = 0;
rv = Dm_createHydraSamplesForDls(dls, &pcm, &pcm_len, &hydra.shdrs, &hydra.shdrNum);
struct tsf_hydra_shdr* default_shdrs = NULL;
int32_t default_shdrs_len = 0;
rv = Dm_createHydraSamplesForDls(dls, &pcm, &pcm_len, &default_shdrs, &default_shdrs_len);
if (rv != DmResult_SUCCESS) {
return rv;
}
Expand All @@ -284,6 +291,7 @@ DmResult DmSynth_createTsfForDls(DmDls* dls, tsf** out) {
uint32_t ibag_ndx = 0;
uint32_t igen_ndx = 0;
uint32_t imod_ndx = 0;
uint32_t shdr_ndx = 0;
for (size_t i = 0; i < dls->instrument_count; ++i) {
DmDlsInstrument* ins = &dls->instruments[i];
uint32_t bank = ins->bank;
Expand Down Expand Up @@ -358,12 +366,14 @@ DmResult DmSynth_createTsfForDls(DmDls* dls, tsf** out) {
igen_ndx++;

hydra.igens[igen_ndx].genOper = kSampleID;
hydra.igens[igen_ndx].genAmount.wordAmount = reg->link_table_index;
hydra.igens[igen_ndx].genAmount.wordAmount = shdr_ndx;
igen_ndx++;

// Additional sample configuration.
// TODO(lmichaelis): Some samples are re-used. Is is okay to change these values for them?
struct tsf_hydra_shdr* hdr = &hydra.shdrs[reg->link_table_index];
struct tsf_hydra_shdr* hdr = &hydra.shdrs[shdr_ndx];
*hdr = default_shdrs[reg->link_table_index];
shdr_ndx++;

if (reg->sample.looping) {
uint32_t offset = hdr->start;
hdr->startLoop = offset + reg->sample.loop_start;
Expand Down Expand Up @@ -433,6 +443,7 @@ DmResult DmSynth_createTsfForDls(DmDls* dls, tsf** out) {
res->fontSamples = pcm;

// Lastly, free up all the hydra stuff
Dm_free(default_shdrs);
Dm_free(hydra.phdrs);
Dm_free(hydra.pbags);
Dm_free(hydra.pgens);
Expand Down

0 comments on commit 35805de

Please sign in to comment.