OPL.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * OPL.cpp
  3. * -------
  4. * Purpose: Translate data coming from OpenMPT's mixer into OPL commands to be sent to the Opal emulator.
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * Schism Tracker contributors (bisqwit, JosepMa, Malvineous, code relicensed from GPL to BSD with permission)
  8. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  9. */
  10. #include "stdafx.h"
  11. #include "../common/misc_util.h"
  12. #include "OPL.h"
  13. #include "opal.h"
  14. OPENMPT_NAMESPACE_BEGIN
  15. OPL::OPL(uint32 samplerate)
  16. {
  17. Initialize(samplerate);
  18. }
  19. OPL::OPL(IRegisterLogger &logger)
  20. : m_logger{&logger}
  21. {
  22. Initialize(OPL_BASERATE);
  23. }
  24. OPL::~OPL()
  25. {
  26. // This destructor is put here so that we can forward-declare the Opal emulator class.
  27. }
  28. void OPL::Initialize(uint32 samplerate)
  29. {
  30. if(m_opl == nullptr)
  31. m_opl = std::make_unique<Opal>(samplerate);
  32. else
  33. m_opl->SetSampleRate(samplerate);
  34. Reset();
  35. }
  36. void OPL::Mix(int32 *target, size_t count, uint32 volumeFactorQ16)
  37. {
  38. if(!m_isActive)
  39. return;
  40. // This factor causes a sample voice to be more or less as loud as an OPL voice
  41. const int32 factor = Util::muldiv_unsigned(volumeFactorQ16, 6169, (1 << 16));
  42. while(count--)
  43. {
  44. int16 l, r;
  45. m_opl->Sample(&l, &r);
  46. target[0] += l * factor;
  47. target[1] += r * factor;
  48. target += 2;
  49. }
  50. }
  51. uint16 OPL::ChannelToRegister(uint8 oplCh)
  52. {
  53. if(oplCh < 9)
  54. return oplCh;
  55. else
  56. return (oplCh - 9) | 0x100;
  57. }
  58. // Translate a channel's first operator address into a register
  59. uint16 OPL::OperatorToRegister(uint8 oplCh)
  60. {
  61. static constexpr uint8 OPLChannelToOperator[] = { 0, 1, 2, 8, 9, 10, 16, 17, 18 };
  62. if(oplCh < 9)
  63. return OPLChannelToOperator[oplCh];
  64. else
  65. return OPLChannelToOperator[oplCh - 9] | 0x100;
  66. }
  67. uint8 OPL::GetVoice(CHANNELINDEX c) const
  68. {
  69. if((m_ChanToOPL[c] & OPL_CHANNEL_CUT) || m_ChanToOPL[c] == OPL_CHANNEL_INVALID)
  70. return OPL_CHANNEL_INVALID;
  71. return m_ChanToOPL[c] & OPL_CHANNEL_MASK;
  72. }
  73. uint8 OPL::AllocateVoice(CHANNELINDEX c)
  74. {
  75. // Can we re-use a previous channel?
  76. if(auto oplCh = m_ChanToOPL[c]; oplCh != OPL_CHANNEL_INVALID)
  77. {
  78. if(!(m_ChanToOPL[c] & OPL_CHANNEL_CUT))
  79. return oplCh;
  80. // Check re-use hint
  81. oplCh &= OPL_CHANNEL_MASK;
  82. if(m_OPLtoChan[oplCh] == CHANNELINDEX_INVALID || m_OPLtoChan[oplCh] == c)
  83. {
  84. m_OPLtoChan[oplCh] = c;
  85. m_ChanToOPL[c] = oplCh;
  86. return oplCh;
  87. }
  88. }
  89. // Search for unused channel or channel with released note
  90. uint8 releasedChn = OPL_CHANNEL_INVALID, releasedCutChn = OPL_CHANNEL_INVALID;
  91. for(uint8 oplCh = 0; oplCh < OPL_CHANNELS; oplCh++)
  92. {
  93. if(m_OPLtoChan[oplCh] == CHANNELINDEX_INVALID)
  94. {
  95. m_OPLtoChan[oplCh] = c;
  96. m_ChanToOPL[c] = oplCh;
  97. return oplCh;
  98. } else if(!(m_KeyOnBlock[oplCh] & KEYON_BIT))
  99. {
  100. releasedChn = oplCh;
  101. if(m_ChanToOPL[m_OPLtoChan[oplCh]] & OPL_CHANNEL_CUT)
  102. releasedCutChn = oplCh;
  103. }
  104. }
  105. if(releasedChn != OPL_CHANNEL_INVALID)
  106. {
  107. // Prefer channel that has been marked as cut over channel that has just been released
  108. if(releasedCutChn != OPL_CHANNEL_INVALID)
  109. releasedChn = releasedCutChn;
  110. m_ChanToOPL[m_OPLtoChan[releasedChn]] = OPL_CHANNEL_INVALID;
  111. m_OPLtoChan[releasedChn] = c;
  112. m_ChanToOPL[c] = releasedChn;
  113. }
  114. return GetVoice(c);
  115. }
  116. void OPL::MoveChannel(CHANNELINDEX from, CHANNELINDEX to)
  117. {
  118. uint8 oplCh = GetVoice(from);
  119. if(oplCh == OPL_CHANNEL_INVALID)
  120. return;
  121. m_OPLtoChan[oplCh] = to;
  122. m_ChanToOPL[from] = OPL_CHANNEL_INVALID;
  123. m_ChanToOPL[to] = oplCh;
  124. }
  125. void OPL::NoteOff(CHANNELINDEX c)
  126. {
  127. uint8 oplCh = GetVoice(c);
  128. if(oplCh == OPL_CHANNEL_INVALID || m_opl == nullptr)
  129. return;
  130. m_KeyOnBlock[oplCh] &= ~KEYON_BIT;
  131. Port(c, KEYON_BLOCK | ChannelToRegister(oplCh), m_KeyOnBlock[oplCh]);
  132. }
  133. void OPL::NoteCut(CHANNELINDEX c, bool unassign)
  134. {
  135. uint8 oplCh = GetVoice(c);
  136. if(oplCh == OPL_CHANNEL_INVALID)
  137. return;
  138. NoteOff(c);
  139. Volume(c, 0, false); // Note that a volume of 0 is not complete silence; the release portion of the sound will still be heard at -48dB
  140. if(unassign)
  141. {
  142. m_OPLtoChan[oplCh] = CHANNELINDEX_INVALID;
  143. m_ChanToOPL[c] |= OPL_CHANNEL_CUT;
  144. }
  145. }
  146. void OPL::Frequency(CHANNELINDEX c, uint32 milliHertz, bool keyOff, bool beatingOscillators)
  147. {
  148. uint8 oplCh = GetVoice(c);
  149. if(oplCh == OPL_CHANNEL_INVALID || m_opl == nullptr)
  150. return;
  151. uint16 fnum = 1023;
  152. uint8 block = 7;
  153. if(milliHertz <= 6208431)
  154. {
  155. if(milliHertz > 3104215) block = 7;
  156. else if(milliHertz > 1552107) block = 6;
  157. else if(milliHertz > 776053) block = 5;
  158. else if(milliHertz > 388026) block = 4;
  159. else if(milliHertz > 194013) block = 3;
  160. else if(milliHertz > 97006) block = 2;
  161. else if(milliHertz > 48503) block = 1;
  162. else block = 0;
  163. fnum = static_cast<uint16>(Util::muldivr_unsigned(milliHertz, 1 << (20 - block), OPL_BASERATE * 1000));
  164. MPT_ASSERT(fnum < 1024);
  165. }
  166. // Evil CDFM hack! Composer 670 slightly detunes each note based on the OPL channel number modulo 4.
  167. // We allocate our OPL channels dynamically, which would result in slightly different beating characteristics,
  168. // but we can just take the pattern channel number instead, as the pattern channel layout is always identical.
  169. if(beatingOscillators)
  170. fnum = std::min(static_cast<uint16>(fnum + (c & 3)), uint16(1023));
  171. fnum |= (block << 10);
  172. uint16 channel = ChannelToRegister(oplCh);
  173. m_KeyOnBlock[oplCh] = (keyOff ? 0 : KEYON_BIT) | (fnum >> 8); // Key on bit + Octave (block) + F-number high 2 bits
  174. Port(c, FNUM_LOW | channel, fnum & 0xFF); // F-Number low 8 bits
  175. Port(c, KEYON_BLOCK | channel, m_KeyOnBlock[oplCh]);
  176. m_isActive = true;
  177. }
  178. uint8 OPL::CalcVolume(uint8 trackerVol, uint8 kslVolume)
  179. {
  180. if(trackerVol >= 63u)
  181. return kslVolume;
  182. if(trackerVol > 0)
  183. trackerVol++;
  184. return (kslVolume & KSL_MASK) | (63u - ((63u - (kslVolume & TOTAL_LEVEL_MASK)) * trackerVol) / 64u);
  185. }
  186. void OPL::Volume(CHANNELINDEX c, uint8 vol, bool applyToModulator)
  187. {
  188. uint8 oplCh = GetVoice(c);
  189. if(oplCh == OPL_CHANNEL_INVALID || m_opl == nullptr)
  190. return;
  191. const auto &patch = m_Patches[oplCh];
  192. const uint16 modulator = OperatorToRegister(oplCh), carrier = modulator + 3;
  193. if((patch[10] & CONNECTION_BIT) || applyToModulator)
  194. {
  195. // Set volume of both operators in additive mode
  196. Port(c, KSL_LEVEL + modulator, CalcVolume(vol, patch[2]));
  197. }
  198. if(!applyToModulator)
  199. {
  200. Port(c, KSL_LEVEL + carrier, CalcVolume(vol, patch[3]));
  201. }
  202. }
  203. int8 OPL::Pan(CHANNELINDEX c, int32 pan)
  204. {
  205. uint8 oplCh = GetVoice(c);
  206. if(oplCh == OPL_CHANNEL_INVALID || m_opl == nullptr)
  207. return 0;
  208. const auto &patch = m_Patches[oplCh];
  209. uint8 fbConn = patch[10] & ~STEREO_BITS;
  210. // OPL3 only knows hard left, center and right, so we need to translate our
  211. // continuous panning range into one of those three states.
  212. // 0...84 = left, 85...170 = center, 171...256 = right
  213. if(pan <= 170)
  214. fbConn |= VOICE_TO_LEFT;
  215. if(pan >= 85)
  216. fbConn |= VOICE_TO_RIGHT;
  217. Port(c, FEEDBACK_CONNECTION | ChannelToRegister(oplCh), fbConn);
  218. return ((fbConn & VOICE_TO_LEFT) ? -1 : 0) + ((fbConn & VOICE_TO_RIGHT) ? 1 : 0);
  219. }
  220. void OPL::Patch(CHANNELINDEX c, const OPLPatch &patch)
  221. {
  222. uint8 oplCh = AllocateVoice(c);
  223. if(oplCh == OPL_CHANNEL_INVALID || m_opl == nullptr)
  224. return;
  225. m_Patches[oplCh] = patch;
  226. const uint16 modulator = OperatorToRegister(oplCh), carrier = modulator + 3;
  227. for(uint8 op = 0; op < 2; op++)
  228. {
  229. const auto opReg = op ? carrier : modulator;
  230. Port(c, AM_VIB | opReg, patch[0 + op]);
  231. Port(c, KSL_LEVEL | opReg, patch[2 + op]);
  232. Port(c, ATTACK_DECAY | opReg, patch[4 + op]);
  233. Port(c, SUSTAIN_RELEASE | opReg, patch[6 + op]);
  234. Port(c, WAVE_SELECT | opReg, patch[8 + op]);
  235. }
  236. Port(c, FEEDBACK_CONNECTION | ChannelToRegister(oplCh), patch[10]);
  237. }
  238. void OPL::Reset()
  239. {
  240. if(m_isActive)
  241. {
  242. for(CHANNELINDEX chn = 0; chn < MAX_CHANNELS; chn++)
  243. {
  244. NoteCut(chn);
  245. }
  246. m_isActive = false;
  247. }
  248. m_KeyOnBlock.fill(0);
  249. m_OPLtoChan.fill(CHANNELINDEX_INVALID);
  250. m_ChanToOPL.fill(OPL_CHANNEL_INVALID);
  251. Port(CHANNELINDEX_INVALID, 0x105, 1); // Enable OPL3
  252. Port(CHANNELINDEX_INVALID, 0x104, 0); // No 4-op voices
  253. }
  254. void OPL::Port(CHANNELINDEX c, uint16 reg, uint8 value)
  255. {
  256. if(!m_logger)
  257. m_opl->Port(reg, value);
  258. else
  259. m_logger->Port(c, reg, value);
  260. }
  261. std::vector<uint16> OPL::AllVoiceRegisters()
  262. {
  263. static constexpr uint8 opRegisters[] = {OPL::AM_VIB, OPL::KSL_LEVEL, OPL::ATTACK_DECAY, OPL::SUSTAIN_RELEASE, OPL::WAVE_SELECT};
  264. static constexpr uint8 chnRegisters[] = {OPL::FNUM_LOW, OPL::KEYON_BLOCK, OPL::FEEDBACK_CONNECTION};
  265. std::vector<uint16> result;
  266. result.reserve(234);
  267. for(uint16 chip = 0; chip < 2; chip++)
  268. {
  269. for(uint8 opReg : opRegisters)
  270. {
  271. for(uint8 op = 0; op < 22; op++)
  272. {
  273. if((op & 7) < 6)
  274. result.push_back((chip << 8) | opReg | op);
  275. }
  276. }
  277. for(uint8 chnReg : chnRegisters)
  278. {
  279. for(uint8 chn = 0; chn < 9; chn++)
  280. {
  281. result.push_back((chip << 8) | chnReg | chn);
  282. }
  283. }
  284. }
  285. return result;
  286. }
  287. OPENMPT_NAMESPACE_END