1 /*
2  * libADLMIDI is a free Software MIDI synthesizer library with OPL3 emulation
3  *
4  * Original ADLMIDI code: Copyright (c) 2010-2014 Joel Yliluoma <bisqwit@iki.fi>
5  * ADLMIDI Library API:   Copyright (c) 2015-2020 Vitaly Novichkov <admin@wohlnet.ru>
6  *
7  * Library is based on the ADLMIDI, a MIDI player for Linux and Windows with OPL3 emulation:
8  * http://iki.fi/bisqwit/source/adlmidi.html
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "adlmidi_midiplay.hpp"
25 #include "adlmidi_opl3.hpp"
26 #include "adlmidi_private.hpp"
27 #include "wopl/wopl_file.h"
28 
29 
30 std::string ADLMIDI_ErrorString;
31 
32 // Generator callback on audio rate ticks
33 
34 #if defined(ADLMIDI_AUDIO_TICK_HANDLER)
adl_audioTickHandler(void * instance,uint32_t chipId,uint32_t rate)35 void adl_audioTickHandler(void *instance, uint32_t chipId, uint32_t rate)
36 {
37     reinterpret_cast<MIDIplay *>(instance)->AudioTick(chipId, rate);
38 }
39 #endif
40 
adlCalculateFourOpChannels(MIDIplay * play,bool silent)41 int adlCalculateFourOpChannels(MIDIplay *play, bool silent)
42 {
43     Synth &synth = *play->m_synth;
44     size_t n_fourop[2] = {0, 0}, n_total[2] = {0, 0};
45     bool rhythmModeNeeded = false;
46     size_t numFourOps = 0;
47 
48     //Automatically calculate how much 4-operator channels is necessary
49     {
50         //For custom bank
51         Synth::BankMap::iterator it = synth.m_insBanks.begin();
52         Synth::BankMap::iterator end = synth.m_insBanks.end();
53         for(; it != end; ++it)
54         {
55             size_t bank = it->first;
56             size_t div = (bank & Synth::PercussionTag) ? 1 : 0;
57             for(size_t i = 0; i < 128; ++i)
58             {
59                 OplInstMeta &ins = it->second.ins[i];
60                 if(ins.flags & OplInstMeta::Flag_NoSound)
61                     continue;
62                 if((ins.flags & OplInstMeta::Flag_Real4op) != 0)
63                     ++n_fourop[div];
64                 ++n_total[div];
65                 if(div && ((ins.flags & OplInstMeta::Mask_RhythmMode) != 0))
66                     rhythmModeNeeded = true;
67             }
68         }
69     }
70 
71     // All 2ops (no 4ops)
72     if((n_fourop[0] == 0) && (n_fourop[1] == 0))
73         numFourOps = 0;
74     // All 2op melodics and Some (or All) 4op drums
75     else if((n_fourop[0] == 0) && (n_fourop[1] > 0))
76         numFourOps = 2;
77     // Many 4op melodics
78     else if((n_fourop[0] >= (n_total[0] * 7) / 8))
79         numFourOps = 6;
80     // Few 4op melodics
81     else if(n_fourop[0] > 0)
82         numFourOps = 4;
83 
84     synth.m_numFourOps = static_cast<unsigned>(numFourOps * synth.m_numChips);
85 
86     // Update channel categories and set up four-operator channels
87     if(!silent)
88         synth.updateChannelCategories();
89 
90     // Set rhythm mode when it needed
91     synth.m_rhythmMode = rhythmModeNeeded;
92 
93     return 0;
94 }
95 
96 #ifndef DISABLE_EMBEDDED_BANKS
adlFromInstrument(const BanksDump::InstrumentEntry & instIn,OplInstMeta & instOut)97 void adlFromInstrument(const BanksDump::InstrumentEntry &instIn, OplInstMeta &instOut)
98 {
99     instOut.voice2_fine_tune = 0.0;
100     if(instIn.secondVoiceDetune != 0)
101         instOut.voice2_fine_tune = (double)((((int)instIn.secondVoiceDetune + 128) >> 1) - 64) / 32.0;
102 
103     instOut.midiVelocityOffset = instIn.midiVelocityOffset;
104     instOut.drumTone = instIn.percussionKeyNumber;
105     instOut.flags = (instIn.instFlags & WOPL_Ins_4op) && (instIn.instFlags & WOPL_Ins_Pseudo4op) ? OplInstMeta::Flag_Pseudo4op : 0;
106     instOut.flags|= (instIn.instFlags & WOPL_Ins_4op) && ((instIn.instFlags & WOPL_Ins_Pseudo4op) == 0) ? OplInstMeta::Flag_Real4op : 0;
107     instOut.flags|= (instIn.instFlags & WOPL_Ins_IsBlank) ? OplInstMeta::Flag_NoSound : 0;
108     instOut.flags|= instIn.instFlags & WOPL_RhythmModeMask;
109 
110     for(size_t op = 0; op < 2; op++)
111     {
112         if((instIn.ops[(op * 2) + 0] < 0) || (instIn.ops[(op * 2) + 1] < 0))
113             break;
114         const BanksDump::Operator &op1 = g_embeddedBanksOperators[instIn.ops[(op * 2) + 0]];
115         const BanksDump::Operator &op2 = g_embeddedBanksOperators[instIn.ops[(op * 2) + 1]];
116         instOut.op[op].modulator_E862 = op1.d_E862;
117         instOut.op[op].modulator_40   = op1.d_40;
118         instOut.op[op].carrier_E862 = op2.d_E862;
119         instOut.op[op].carrier_40   = op2.d_40;
120         instOut.op[op].feedconn = (instIn.fbConn >> (op * 8)) & 0xFF;
121         instOut.op[op].noteOffset = static_cast<int8_t>(op == 0 ? instIn.noteOffset1 : instIn.noteOffset2);
122     }
123 
124     instOut.soundKeyOnMs  = instIn.delay_on_ms;
125     instOut.soundKeyOffMs = instIn.delay_off_ms;
126 }
127 #endif
128