Skip to content

Commit

Permalink
Merge branch 'master' into stable
Browse files Browse the repository at this point in the history
  • Loading branch information
Wohlstand committed Sep 30, 2018
2 parents 204c483 + 77e8b86 commit 299f461
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 64 deletions.
16 changes: 15 additions & 1 deletion include/adlmidi.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ extern ADLMIDI_DECLSPEC int adl_setNumChips(struct ADL_MIDIPlayer *device, int n
*/
extern ADLMIDI_DECLSPEC int adl_getNumChips(struct ADL_MIDIPlayer *device);

/**
* @brief Get obtained number of emulated chips
* @param device Instance of the library
* @return Count of working chip emulators
*/
extern ADLMIDI_DECLSPEC int adl_getNumChipsObtained(struct ADL_MIDIPlayer *device);

/**
* @brief Sets a number of the patches bank from 0 to N banks.
*
Expand Down Expand Up @@ -340,10 +347,17 @@ extern ADLMIDI_DECLSPEC int adl_setNumFourOpsChn(struct ADL_MIDIPlayer *device,
/**
* @brief Get current total count of 4-operator channels between all chips
* @param device Instance of the library
* @return 0 on success, <0 when any error has occurred
* @return 0 on success, <-1 when any error has occurred, but, -1 - "auto"
*/
extern ADLMIDI_DECLSPEC int adl_getNumFourOpsChn(struct ADL_MIDIPlayer *device);

/**
* @brief Get obtained total count of 4-operator channels between all chips
* @param device Instance of the library
* @return 0 on success, <0 when any error has occurred
*/
extern ADLMIDI_DECLSPEC int adl_getNumFourOpsChnObtained(struct ADL_MIDIPlayer *device);

/**
* @brief Override Enable(1) or Disable(0) AdLib percussion mode. -1 - use bank default AdLib percussion mode
*
Expand Down
150 changes: 100 additions & 50 deletions src/adlmidi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@

#include "adlmidi_private.hpp"

#ifdef ADLMIDI_HW_OPL
#define MaxChips 1
#define MaxChips_STR "1" //Why not just "#MaxCards" ? Watcom fails to pass this with "syntax error" :-P
#else
#define MaxChips 100
#define MaxChips_STR "100"
#endif

/* Unify MIDI player casting and interface between ADLMIDI and OPNMIDI */
#define GET_MIDI_PLAYER(device) reinterpret_cast<MIDIplay *>((device)->adl_midiPlayer)
typedef MIDIplay MidiPlayer;
Expand Down Expand Up @@ -68,7 +60,7 @@ ADLMIDI_EXPORT struct ADL_MIDIPlayer *adl_init(long sample_rate)
return NULL;
}
midi_device->adl_midiPlayer = player;
adlRefreshNumCards(midi_device);
adlCalculateFourOpChannels(player);
return midi_device;
}

Expand Down Expand Up @@ -107,16 +99,31 @@ ADLMIDI_EXPORT int adl_setNumChips(ADL_MIDIPlayer *device, int numChips)
#else
play->m_setup.numChips = static_cast<unsigned int>(numChips);
#endif
if(play->m_setup.numChips < 1 || play->m_setup.numChips > MaxChips)
if(play->m_setup.numChips < 1 || play->m_setup.numChips > ADL_MAX_CHIPS)
{
play->setErrorString("number of chips may only be 1.." MaxChips_STR ".\n");
play->setErrorString("number of chips may only be 1.." ADL_MAX_CHIPS_STR ".\n");
return -1;
}

play->m_synth.m_numChips = play->m_setup.numChips;
play->partialReset();
int maxFourOps = static_cast<int>(play->m_setup.numChips * 6);

if(play->m_setup.numFourOps > maxFourOps)
play->m_setup.numFourOps = maxFourOps;
else if(play->m_setup.numFourOps < -1)
play->m_setup.numFourOps = -1;

if(!play->m_synth.setupLocked())
{
play->m_synth.m_numChips = play->m_setup.numChips;
if(play->m_setup.numFourOps < 0)
adlCalculateFourOpChannels(play, true);
else
play->m_synth.m_numFourOps = static_cast<uint32_t>(play->m_setup.numFourOps);
play->partialReset();
return 0;
}

return adlRefreshNumCards(device);
return 0;
}

ADLMIDI_EXPORT int adl_getNumChips(struct ADL_MIDIPlayer *device)
Expand All @@ -128,6 +135,15 @@ ADLMIDI_EXPORT int adl_getNumChips(struct ADL_MIDIPlayer *device)
return (int)play->m_setup.numChips;
}

ADLMIDI_EXPORT int adl_getNumChipsObtained(struct ADL_MIDIPlayer *device)
{
if(device == NULL)
return -2;
MidiPlayer *play = GET_MIDI_PLAYER(device);
assert(play);
return (int)play->m_synth.m_numChips;
}

ADLMIDI_EXPORT int adl_setBank(ADL_MIDIPlayer *device, int bank)
{
#ifdef DISABLE_EMBEDDED_BANKS
Expand Down Expand Up @@ -159,7 +175,7 @@ ADLMIDI_EXPORT int adl_setBank(ADL_MIDIPlayer *device, int bank)
play->m_synth.setEmbeddedBank(play->m_setup.bankId);
play->applySetup();

return adlRefreshNumCards(device);
return 0;
#endif
}

Expand Down Expand Up @@ -357,45 +373,61 @@ ADLMIDI_EXPORT int adl_setNumFourOpsChn(ADL_MIDIPlayer *device, int ops4)
if(!device)
return -1;

if(ops4 == -1)
return adlRefreshNumCards(device);

MidiPlayer *play = GET_MIDI_PLAYER(device);
assert(play);
if((unsigned int)ops4 > 6 * play->m_setup.numChips)
if(ops4 > 6 * static_cast<int>(play->m_setup.numChips))
{
char errBuff[250];
snprintf(errBuff, 250, "number of four-op channels may only be 0..%u when %u OPL3 cards are used.\n", (6 * (play->m_setup.numChips)), play->m_setup.numChips);
play->setErrorString(errBuff);
return -1;
}

play->m_setup.numFourOps = static_cast<unsigned int>(ops4);
play->m_synth.m_numFourOps = play->m_setup.numFourOps;
play->m_synth.updateChannelCategories();
play->m_setup.numFourOps = ops4;
if(!play->m_synth.setupLocked())
{
if(play->m_setup.numFourOps < 0)
adlCalculateFourOpChannels(play, true);
else
play->m_synth.m_numFourOps = static_cast<uint32_t>(play->m_setup.numFourOps);
play->m_synth.updateChannelCategories();
}

return 0;
}

ADLMIDI_EXPORT int adl_getNumFourOpsChn(struct ADL_MIDIPlayer *device)
{
if(!device)
return -1;
return -2;
MidiPlayer *play = GET_MIDI_PLAYER(device);
assert(play);
return (int)play->m_setup.numFourOps;
return play->m_setup.numFourOps;
}

ADLMIDI_EXPORT int adl_getNumFourOpsChnObtained(struct ADL_MIDIPlayer *device)
{
if(!device)
return -2;
MidiPlayer *play = GET_MIDI_PLAYER(device);
assert(play);
return (int)play->m_synth.m_numFourOps;
}


ADLMIDI_EXPORT void adl_setPercMode(ADL_MIDIPlayer *device, int percmod)
{
if(!device) return;
MidiPlayer *play = GET_MIDI_PLAYER(device);
assert(play);
play->m_setup.rhythmMode = percmod;
play->m_synth.m_rhythmMode = play->m_setup.rhythmMode < 0 ?
(play->m_synth.m_insBankSetup.adLibPercussions) :
(play->m_setup.rhythmMode != 0);
play->m_synth.updateChannelCategories();
if(!play->m_synth.setupLocked())
{
play->m_synth.m_rhythmMode = play->m_setup.rhythmMode < 0 ?
(play->m_synth.m_insBankSetup.adLibPercussions) :
(play->m_setup.rhythmMode != 0);
play->m_synth.updateChannelCategories();
}
}

ADLMIDI_EXPORT void adl_setHVibrato(ADL_MIDIPlayer *device, int hvibro)
Expand All @@ -404,10 +436,13 @@ ADLMIDI_EXPORT void adl_setHVibrato(ADL_MIDIPlayer *device, int hvibro)
MidiPlayer *play = GET_MIDI_PLAYER(device);
assert(play);
play->m_setup.deepVibratoMode = hvibro;
play->m_synth.m_deepVibratoMode = play->m_setup.deepVibratoMode < 0 ?
play->m_synth.m_insBankSetup.deepVibrato :
(play->m_setup.deepVibratoMode != 0);
play->m_synth.commitDeepFlags();
if(!play->m_synth.setupLocked())
{
play->m_synth.m_deepVibratoMode = play->m_setup.deepVibratoMode < 0 ?
play->m_synth.m_insBankSetup.deepVibrato :
(play->m_setup.deepVibratoMode != 0);
play->m_synth.commitDeepFlags();
}
}

ADLMIDI_EXPORT int adl_getHVibrato(struct ADL_MIDIPlayer *device)
Expand All @@ -424,10 +459,13 @@ ADLMIDI_EXPORT void adl_setHTremolo(ADL_MIDIPlayer *device, int htremo)
MidiPlayer *play = GET_MIDI_PLAYER(device);
assert(play);
play->m_setup.deepTremoloMode = htremo;
play->m_synth.m_deepTremoloMode = play->m_setup.deepTremoloMode < 0 ?
play->m_synth.m_insBankSetup.deepTremolo :
(play->m_setup.deepTremoloMode != 0);
play->m_synth.commitDeepFlags();
if(!play->m_synth.setupLocked())
{
play->m_synth.m_deepTremoloMode = play->m_setup.deepTremoloMode < 0 ?
play->m_synth.m_insBankSetup.deepTremolo :
(play->m_setup.deepTremoloMode != 0);
play->m_synth.commitDeepFlags();
}
}

ADLMIDI_EXPORT int adl_getHTremolo(struct ADL_MIDIPlayer *device)
Expand All @@ -445,9 +483,12 @@ ADLMIDI_EXPORT void adl_setScaleModulators(ADL_MIDIPlayer *device, int smod)
MidiPlayer *play = GET_MIDI_PLAYER(device);
assert(play);
play->m_setup.scaleModulators = smod;
play->m_synth.m_scaleModulators = play->m_setup.scaleModulators < 0 ?
play->m_synth.m_insBankSetup.scaleModulators :
(play->m_setup.scaleModulators != 0);
if(!play->m_synth.setupLocked())
{
play->m_synth.m_scaleModulators = play->m_setup.scaleModulators < 0 ?
play->m_synth.m_insBankSetup.scaleModulators :
(play->m_setup.scaleModulators != 0);
}
}

ADLMIDI_EXPORT void adl_setFullRangeBrightness(struct ADL_MIDIPlayer *device, int fr_brightness)
Expand Down Expand Up @@ -489,10 +530,13 @@ ADLMIDI_EXPORT void adl_setLogarithmicVolumes(struct ADL_MIDIPlayer *device, int
MidiPlayer *play = GET_MIDI_PLAYER(device);
assert(play);
play->m_setup.logarithmicVolumes = (logvol != 0);
if(play->m_setup.logarithmicVolumes)
play->m_synth.setVolumeScaleModel(ADLMIDI_VolumeModel_NativeOPL3);
else
play->m_synth.setVolumeScaleModel(static_cast<ADLMIDI_VolumeModels>(play->m_synth.m_volumeScale));
if(!play->m_synth.setupLocked())
{
if(play->m_setup.logarithmicVolumes)
play->m_synth.setVolumeScaleModel(ADLMIDI_VolumeModel_NativeOPL3);
else
play->m_synth.setVolumeScaleModel(static_cast<ADLMIDI_VolumeModels>(play->m_synth.m_volumeScale));
}
}

ADLMIDI_EXPORT void adl_setVolumeRangeModel(struct ADL_MIDIPlayer *device, int volumeModel)
Expand All @@ -502,10 +546,13 @@ ADLMIDI_EXPORT void adl_setVolumeRangeModel(struct ADL_MIDIPlayer *device, int v
MidiPlayer *play = GET_MIDI_PLAYER(device);
assert(play);
play->m_setup.volumeScaleModel = volumeModel;
if(play->m_setup.volumeScaleModel == ADLMIDI_VolumeModel_AUTO)//Use bank default volume model
play->m_synth.m_volumeScale = (OPL3::VolumesScale)play->m_synth.m_insBankSetup.volumeModel;
else
play->m_synth.setVolumeScaleModel(static_cast<ADLMIDI_VolumeModels>(volumeModel));
if(!play->m_synth.setupLocked())
{
if(play->m_setup.volumeScaleModel == ADLMIDI_VolumeModel_AUTO)//Use bank default volume model
play->m_synth.m_volumeScale = (OPL3::VolumesScale)play->m_synth.m_insBankSetup.volumeModel;
else
play->m_synth.setVolumeScaleModel(static_cast<ADLMIDI_VolumeModels>(volumeModel));
}
}

ADLMIDI_EXPORT int adl_getVolumeRangeModel(struct ADL_MIDIPlayer *device)
Expand All @@ -531,7 +578,8 @@ ADLMIDI_EXPORT int adl_openBankFile(struct ADL_MIDIPlayer *device, const char *f
play->setErrorString("ADL MIDI: Can't load file");
return -1;
}
else return adlRefreshNumCards(device);
else
return adlCalculateFourOpChannels(play, true);
}

ADLMIDI_ErrorString = "Can't load file: ADLMIDI is not initialized";
Expand All @@ -552,7 +600,8 @@ ADLMIDI_EXPORT int adl_openBankData(struct ADL_MIDIPlayer *device, const void *m
play->setErrorString("ADL MIDI: Can't load data from memory");
return -1;
}
else return adlRefreshNumCards(device);
else
return adlCalculateFourOpChannels(play, true);
}

ADLMIDI_ErrorString = "Can't load file: ADL MIDI is not initialized";
Expand Down Expand Up @@ -660,7 +709,8 @@ ADLMIDI_EXPORT int adl_setRunAtPcmRate(ADL_MIDIPlayer *device, int enabled)
MidiPlayer *play = GET_MIDI_PLAYER(device);
assert(play);
play->m_setup.runAtPcmRate = (enabled != 0);
play->partialReset();
if(!play->m_synth.setupLocked())
play->partialReset();
return 0;
}
return -1;
Expand Down
18 changes: 17 additions & 1 deletion src/adlmidi_load.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* libADLMIDI is a free MIDI to WAV conversion library with OPL3 emulation
*
* Original ADLMIDI code: Copyright (c) 2010-2014 Joel Yliluoma <[email protected]>
Expand Down Expand Up @@ -214,20 +214,36 @@ bool MIDIplay::LoadMIDI_post()
m_synth.m_rhythmMode = true;
m_synth.m_musicMode = OPL3::MODE_CMF;
m_synth.m_volumeScale = OPL3::VOLUME_NATIVE;

m_synth.m_numChips = 1;
m_synth.m_numFourOps = 0;
}
else if(format == MidiSequencer::Format_RSXX)
{
//opl.CartoonersVolumes = true;
m_synth.m_musicMode = OPL3::MODE_RSXX;
m_synth.m_volumeScale = OPL3::VOLUME_NATIVE;

m_synth.m_numChips = 1;
m_synth.m_numFourOps = 0;
}
else if(format == MidiSequencer::Format_IMF)
{
//std::fprintf(stderr, "Done reading IMF file\n");
m_synth.m_numFourOps = 0; //Don't use 4-operator channels for IMF playing!
m_synth.m_musicMode = OPL3::MODE_IMF;

m_synth.m_numChips = 1;
m_synth.m_numFourOps = 0;
}
else
{
m_synth.m_numChips = m_setup.numChips;
if(m_setup.numFourOps < 0)
adlCalculateFourOpChannels(this, true);
}

m_setup.tick_skip_samples_delay = 0;
m_synth.reset(m_setup.emulator, m_setup.PCM_RATE, this); // Reset OPL3 chip
//opl.Reset(); // ...twice (just in case someone misprogrammed OPL3 previously)
m_chipChannels.clear();
Expand Down
12 changes: 9 additions & 3 deletions src/adlmidi_midiplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ MIDIplay::MIDIplay(unsigned long sampleRate):
m_setup.maxdelay = 512.0 / (double)m_setup.PCM_RATE;

m_setup.bankId = 0;
m_setup.numFourOps = 7;
m_setup.numFourOps = -1;
m_setup.numChips = 2;
m_setup.deepTremoloMode = -1;
m_setup.deepVibratoMode = -1;
Expand Down Expand Up @@ -198,9 +198,13 @@ void MIDIplay::applySetup()
m_synth.m_volumeScale = (OPL3::VolumesScale)m_synth.m_insBankSetup.volumeModel;

m_synth.m_numChips = m_setup.numChips;
m_synth.m_numFourOps = m_setup.numFourOps;
m_cmfPercussionMode = false;

if(m_setup.numFourOps >= 0)
m_synth.m_numFourOps = m_setup.numFourOps;
else
adlCalculateFourOpChannels(this, true);

m_synth.reset(m_setup.emulator, m_setup.PCM_RATE, this);
m_chipChannels.clear();
m_chipChannels.resize(m_synth.m_numChannels);
Expand Down Expand Up @@ -1461,6 +1465,8 @@ void MIDIplay::killOrEvacuate(size_t from_channel,
AdlChannel::LocationData *j,
MIDIplay::MIDIchannel::activenoteiterator i)
{
uint32_t maxChannels = ADL_MAX_CHIPS * 18;

// Before killing the note, check if it can be
// evacuated to another channel as an arpeggio
// instrument. This helps if e.g. all channels
Expand All @@ -1470,7 +1476,7 @@ void MIDIplay::killOrEvacuate(size_t from_channel,
{
uint16_t cs = static_cast<uint16_t>(c);

if(c > std::numeric_limits<uint32_t>::max())
if(c >= maxChannels)
break;
if(c == from_channel)
continue;
Expand Down
Loading

0 comments on commit 299f461

Please sign in to comment.