1 /*
2  * libADLMIDI is a free MIDI to WAV conversion 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-2018 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_private.hpp"
25 
26 std::string ADLMIDI_ErrorString;
27 
28 // Generator callback on audio rate ticks
29 
30 #if defined(ADLMIDI_AUDIO_TICK_HANDLER)
adl_audioTickHandler(void * instance,uint32_t chipId,uint32_t rate)31 void adl_audioTickHandler(void *instance, uint32_t chipId, uint32_t rate)
32 {
33     reinterpret_cast<MIDIplay *>(instance)->AudioTick(chipId, rate);
34 }
35 #endif
36 
adlRefreshNumCards(ADL_MIDIPlayer * device)37 int adlRefreshNumCards(ADL_MIDIPlayer *device)
38 {
39     size_t n_fourop[2] = {0, 0}, n_total[2] = {0, 0};
40     MIDIplay *play = reinterpret_cast<MIDIplay *>(device->adl_midiPlayer);
41 
42     //Automatically calculate how much 4-operator channels is necessary
43 #ifndef DISABLE_EMBEDDED_BANKS
44     if(play->m_synth.m_embeddedBank == OPL3::CustomBankTag)
45 #endif
46     {
47         //For custom bank
48         OPL3::BankMap::iterator it = play->m_synth.m_insBanks.begin();
49         OPL3::BankMap::iterator end = play->m_synth.m_insBanks.end();
50         for(; it != end; ++it)
51         {
52             size_t bank = it->first;
53             size_t div = (bank & OPL3::PercussionTag) ? 1 : 0;
54             for(size_t i = 0; i < 128; ++i)
55             {
56                 adlinsdata2 &ins = it->second.ins[i];
57                 if(ins.flags & adlinsdata::Flag_NoSound)
58                     continue;
59                 if((ins.adl[0] != ins.adl[1]) && ((ins.flags & adlinsdata::Flag_Pseudo4op) == 0))
60                     ++n_fourop[div];
61                 ++n_total[div];
62             }
63         }
64     }
65 #ifndef DISABLE_EMBEDDED_BANKS
66     else
67     {
68         //For embedded bank
69         for(size_t  a = 0; a < 256; ++a)
70         {
71             size_t insno = banks[play->m_setup.bankId][a];
72             if(insno == 198)
73                 continue;
74             ++n_total[a / 128];
75             adlinsdata2 ins(adlins[insno]);
76             if(ins.flags & adlinsdata::Flag_Real4op)
77                 ++n_fourop[a / 128];
78         }
79     }
80 #endif
81 
82     size_t numFourOps = 0;
83 
84     // All 2ops (no 4ops)
85     if((n_fourop[0] == 0) && (n_fourop[1] == 0))
86         numFourOps = 0;
87     // All 2op melodics and Some (or All) 4op drums
88     else if((n_fourop[0] == 0) && (n_fourop[1] > 0))
89         numFourOps = 2;
90     // Many 4op melodics
91     else if((n_fourop[0] >= (n_total[0] * 7) / 8))
92         numFourOps = 6;
93     // Few 4op melodics
94     else if(n_fourop[0] > 0)
95         numFourOps = 4;
96 
97 /* //Old formula
98     unsigned NumFourOps = ((n_fourop[0] == 0) && (n_fourop[1] == 0)) ? 0
99         : (n_fourop[0] >= (n_total[0] * 7) / 8) ? play->m_setup.NumCards * 6
100         : (play->m_setup.NumCards == 1 ? 1 : play->m_setup.NumCards * 4);
101 */
102 
103     play->m_synth.m_numFourOps = play->m_setup.numFourOps = static_cast<unsigned>(numFourOps * play->m_setup.numChips);
104     // Update channel categories and set up four-operator channels
105     play->m_synth.updateChannelCategories();
106 
107     return 0;
108 }
109