ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

mididrv.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "common/config-manager.h"
00024 #include "common/error.h"
00025 #include "common/gui_options.h"
00026 #include "common/str.h"
00027 #include "common/system.h"
00028 #include "common/textconsole.h"
00029 #include "common/translation.h"
00030 #include "common/util.h"
00031 #include "gui/message.h"
00032 #include "audio/mididrv.h"
00033 #include "audio/musicplugin.h"
00034 
00035 const byte MidiDriver::_mt32ToGm[128] = {
00036 //    0    1    2    3    4    5    6    7    8    9    A    B    C    D    E    F
00037       0,   1,   0,   2,   4,   4,   5,   3,  16,  17,  18,  16,  16,  19,  20,  21, // 0x
00038       6,   6,   6,   7,   7,   7,   8, 112,  62,  62,  63,  63,  38,  38,  39,  39, // 1x
00039      88,  95,  52,  98,  97,  99,  14,  54, 102,  96,  53, 102,  81, 100,  14,  80, // 2x
00040      48,  48,  49,  45,  41,  40,  42,  42,  43,  46,  45,  24,  25,  28,  27, 104, // 3x
00041      32,  32,  34,  33,  36,  37,  35,  35,  79,  73,  72,  72,  74,  75,  64,  65, // 4x
00042      66,  67,  71,  71,  68,  69,  70,  22,  56,  59,  57,  57,  60,  60,  58,  61, // 5x
00043      61,  11,  11,  98,  14,   9,  14,  13,  12, 107, 107,  77,  78,  78,  76,  76, // 6x
00044      47, 117, 127, 118, 118, 116, 115, 119, 115, 112,  55, 124, 123,   0,  14, 117  // 7x
00045 };
00046 
00047 const byte MidiDriver::_gmToMt32[128] = {
00048 //    0    1    2    3    4    5    6    7    8    9    A    B    C    D    E    F
00049       5,   1,   2,   7,   3,   5,  16,  21,  22, 101, 101,  97, 104, 103, 102,  20, // 0x
00050       8,   9,  11,  12,  14,  15,  87,  15,  59,  60,  61,  62,  67,  44,  79,  23, // 1x
00051      64,  67,  66,  70,  68,  69,  28,  31,  52,  54,  55,  56,  49,  51,  57, 112, // 2x
00052      48,  50,  45,  26,  34,  35,  45, 122,  89,  90,  94,  81,  92,  95,  24,  25, // 3x
00053      80,  78,  79,  78,  84,  85,  86,  82,  74,  72,  76,  77, 110, 107, 108,  76, // 4x
00054      47,  44, 111,  45,  44,  34,  44,  30,  32,  33,  88,  34,  35,  35,  38,  33, // 5x
00055      41,  36, 100,  37,  40,  34,  43,  40,  63,  21,  99, 105, 103,  86,  55,  84, // 6x
00056     101, 103, 100, 120, 117, 113,  99, 128, 128, 128, 128, 124, 123, 128, 128, 128, // 7x
00057 };
00058 
00059 static const struct {
00060     uint32      type;
00061     const char *guio;
00062 } GUIOMapping[] = {
00063     { MT_PCSPK,     GUIO_MIDIPCSPK },
00064     { MT_CMS,       GUIO_MIDICMS },
00065     { MT_PCJR,      GUIO_MIDIPCJR },
00066     { MT_ADLIB,     GUIO_MIDIADLIB },
00067     { MT_C64,       GUIO_MIDIC64 },
00068     { MT_AMIGA,     GUIO_MIDIAMIGA },
00069     { MT_APPLEIIGS, GUIO_MIDIAPPLEIIGS },
00070     { MT_TOWNS,     GUIO_MIDITOWNS },
00071     { MT_PC98,      GUIO_MIDIPC98 },
00072     { MT_GM,        GUIO_MIDIGM },
00073     { MT_MT32,      GUIO_MIDIMT32 },
00074     { 0,            0 },
00075 };
00076 
00077 Common::String MidiDriver::musicType2GUIO(uint32 musicType) {
00078     Common::String res;
00079 
00080     for (int i = 0; GUIOMapping[i].guio; i++) {
00081         if (musicType == GUIOMapping[i].type || musicType == (uint32)-1)
00082             res += GUIOMapping[i].guio;
00083     }
00084 
00085     return res;
00086 }
00087 
00088 bool MidiDriver::_forceTypeMT32 = false;
00089 
00090 MusicType MidiDriver::getMusicType(MidiDriver::DeviceHandle handle) {
00091     if (_forceTypeMT32)
00092         return MT_MT32;
00093 
00094     if (handle) {
00095         const PluginList p = MusicMan.getPlugins();
00096         for (PluginList::const_iterator m = p.begin(); m != p.end(); m++) {
00097             MusicDevices i = (*m)->get<MusicPluginObject>().getDevices();
00098             for (MusicDevices::iterator d = i.begin(); d != i.end(); d++) {
00099                 if (handle == d->getHandle())
00100                     return d->getMusicType();
00101             }
00102         }
00103     }
00104 
00105     return MT_INVALID;
00106 }
00107 
00108 Common::String MidiDriver::getDeviceString(DeviceHandle handle, DeviceStringType type) {
00109     if (handle) {
00110         const PluginList p = MusicMan.getPlugins();
00111         for (PluginList::const_iterator m = p.begin(); m != p.end(); m++) {
00112             MusicDevices i = (*m)->get<MusicPluginObject>().getDevices();
00113             for (MusicDevices::iterator d = i.begin(); d != i.end(); d++) {
00114                 if (handle == d->getHandle()) {
00115                     if (type == kDriverName)
00116                         return d->getMusicDriverName();
00117                     else if (type == kDriverId)
00118                         return d->getMusicDriverId();
00119                     else if (type == kDeviceName)
00120                         return d->getCompleteName();
00121                     else if (type == kDeviceId)
00122                         return d->getCompleteId();
00123                     else
00124                         return Common::String("auto");
00125                 }
00126             }
00127         }
00128     }
00129 
00130     return Common::String("auto");
00131 }
00132 
00133 MidiDriver::DeviceHandle MidiDriver::detectDevice(int flags) {
00134     // Query the selected music device (defaults to MT_AUTO device).
00135     Common::String selDevStr = ConfMan.hasKey("music_driver") ? ConfMan.get("music_driver") : Common::String("auto");
00136     DeviceHandle hdl = getDeviceHandle(selDevStr.empty() ? Common::String("auto") : selDevStr);
00137     DeviceHandle reslt = 0;
00138 
00139     _forceTypeMT32 = false;
00140 
00141     // Check whether the selected music driver is compatible with the
00142     // given flags.
00143     switch (getMusicType(hdl)) {
00144     case MT_PCSPK:
00145         if (flags & MDT_PCSPK)
00146             reslt = hdl;
00147         break;
00148 
00149     case MT_PCJR:
00150         if (flags & MDT_PCJR)
00151             reslt = hdl;
00152         break;
00153 
00154     case MT_CMS:
00155         if (flags & MDT_CMS)
00156             reslt = hdl;
00157         break;
00158 
00159     case MT_ADLIB:
00160         if (flags & MDT_ADLIB)
00161             reslt = hdl;
00162         break;
00163 
00164     case MT_C64:
00165         if (flags & MDT_C64)
00166             reslt = hdl;
00167         break;
00168 
00169     case MT_AMIGA:
00170         if (flags & MDT_AMIGA)
00171             reslt = hdl;
00172         break;
00173 
00174     case MT_APPLEIIGS:
00175         if (flags & MDT_APPLEIIGS)
00176             reslt = hdl;
00177         break;
00178 
00179     case MT_TOWNS:
00180         if (flags & MDT_TOWNS)
00181             reslt = hdl;
00182         break;
00183 
00184     case MT_PC98:
00185         if (flags & MDT_PC98)
00186             reslt = hdl;
00187         break;
00188 
00189     case MT_GM:
00190     case MT_GS:
00191     case MT_MT32:
00192         if (flags & MDT_MIDI)
00193             reslt = hdl;
00194         break;
00195 
00196     case MT_NULL:
00197         reslt = hdl;
00198         break;
00199 
00200     default:
00201         break;
00202     }
00203 
00204     Common::String failedDevStr;
00205     if (getMusicType(hdl) == MT_INVALID) {
00206         // If the expressly selected driver or device cannot be found (no longer compiled in, turned off, etc.)
00207         // we display a warning and continue.
00208         failedDevStr = selDevStr;
00209         Common::String warningMsg = Common::String::format(_("The selected audio device '%s' was not found (e.g. might be turned off or disconnected)."), failedDevStr.c_str()) + " " + _("Attempting to fall back to the next available device...");
00210         GUI::MessageDialog dialog(warningMsg);
00211         dialog.runModal();
00212     }
00213 
00214     MusicType tp = getMusicType(reslt);
00215     if (tp != MT_INVALID && tp != MT_AUTO) {
00216         if (checkDevice(reslt)) {
00217             return reslt;
00218         } else {
00219             // If the expressly selected device cannot be used we display a warning and continue.
00220             failedDevStr = getDeviceString(hdl, MidiDriver::kDeviceName);
00221             Common::String warningMsg = Common::String::format(_("The selected audio device '%s' cannot be used. See log file for more information."), failedDevStr.c_str()) + " " + _("Attempting to fall back to the next available device...");
00222             GUI::MessageDialog dialog(warningMsg);
00223             dialog.runModal();
00224         }
00225     }
00226 
00227     // If the selected driver did not match the flags setting,
00228     // we try to determine a suitable and "optimal" music driver.
00229     const PluginList p = MusicMan.getPlugins();
00230     // If only MDT_MIDI but not MDT_PREFER_MT32 or MDT_PREFER_GM is set we prefer the other devices (which will always be
00231     // detected since they are hard coded and cannot be disabled).
00232     bool skipMidi = !(flags & (MDT_PREFER_GM | MDT_PREFER_MT32));
00233     while (flags != MDT_NONE) {
00234         if ((flags & MDT_MIDI) && !skipMidi) {
00235             // If a preferred MT32 or GM device has been selected that device gets returned if available.
00236             Common::String devStr;
00237             if (flags & MDT_PREFER_MT32)
00238                 devStr = ConfMan.hasKey("mt32_device") ? ConfMan.get("mt32_device") : Common::String("null");
00239             else if (flags & MDT_PREFER_GM)
00240                 devStr = ConfMan.hasKey("gm_device") ? ConfMan.get("gm_device") : Common::String("null");
00241             else
00242                 devStr = "auto";
00243 
00244             // Default to Null device here, since we also register a default null setting for
00245             // the MT32 or GM device in the config manager.
00246             hdl = getDeviceHandle(devStr.empty() ? Common::String("null") : devStr);
00247             const MusicType type = getMusicType(hdl);
00248 
00249             // If we have a "Don't use GM/MT-32" setting we skip this part and jump
00250             // to AdLib, PC Speaker etc. detection right away.
00251             if (type != MT_NULL) {
00252                 if (type == MT_INVALID) {
00253                     // If the preferred (expressly requested) selected driver or device cannot be found (no longer compiled in, turned off, etc.)
00254                     // we display a warning and continue. Don't warn about the missing device if we did already (this becomes relevant if the
00255                     // missing device is selected as preferred device and also as GM or MT-32 device).
00256                     if (failedDevStr != devStr) {
00257                         Common::String warningMsg = Common::String::format(_("The preferred audio device '%s' was not found (e.g. might be turned off or disconnected)."), devStr.c_str()) + " " + _("Attempting to fall back to the next available device...");
00258                         GUI::MessageDialog dialog(warningMsg);
00259                         dialog.runModal();
00260                     }
00261                 } else if (type != MT_AUTO) {
00262                     if (checkDevice(hdl)) {
00263                         if (flags & MDT_PREFER_MT32)
00264                             // If we have a preferred MT32 device we disable the gm/mt32 mapping (more about this in mididrv.h).
00265                             _forceTypeMT32 = true;
00266                         return hdl;
00267                     } else {
00268                         // If the preferred (expressly requested) device cannot be used we display a warning and continue.
00269                         // Don't warn about the failing device if we did already (this becomes relevant if the failing
00270                         // device is selected as preferred device and also as GM or MT-32 device).
00271                         if (failedDevStr != getDeviceString(hdl, MidiDriver::kDeviceName)) {
00272                             Common::String warningMsg = Common::String::format(_("The preferred audio device '%s' cannot be used. See log file for more information."), getDeviceString(hdl, MidiDriver::kDeviceName).c_str()) + " " + _("Attempting to fall back to the next available device...");
00273                             GUI::MessageDialog dialog(warningMsg);
00274                             dialog.runModal();
00275                         }
00276                     }
00277                 }
00278 
00279                 // If no specific device is selected (neither in the scummvm nor in the game domain)
00280                 // and there is no preferred MT32 or GM device selected either or if the detected device is unavailable we arrive here.
00281                 // If MT32 is preferred we try for the first available device with music type 'MT_MT32' (usually the mt32 emulator).
00282                 if (flags & MDT_PREFER_MT32) {
00283                     for (PluginList::const_iterator m = p.begin(); m != p.end(); ++m) {
00284                         MusicDevices i = (*m)->get<MusicPluginObject>().getDevices();
00285                         for (MusicDevices::iterator d = i.begin(); d != i.end(); ++d) {
00286                             if (d->getMusicType() == MT_MT32) {
00287                                 hdl = d->getHandle();
00288                                 if (checkDevice(hdl))
00289                                     return hdl;
00290                             }
00291                         }
00292                     }
00293                 }
00294 
00295                 // Now we default to the first available device with music type 'MT_GM' if not
00296                 // MT-32 is preferred or if MT-32 is preferred but all other devices have failed.
00297                 if (!(flags & MDT_PREFER_MT32) || flags == (MDT_PREFER_MT32 | MDT_MIDI)) {
00298                     for (PluginList::const_iterator m = p.begin(); m != p.end(); ++m) {
00299                         MusicDevices i = (*m)->get<MusicPluginObject>().getDevices();
00300                         for (MusicDevices::iterator d = i.begin(); d != i.end(); ++d) {
00301                             if (d->getMusicType() == MT_GM || d->getMusicType() == MT_GS) {
00302                                 hdl = d->getHandle();
00303                                 if (checkDevice(hdl))
00304                                     return hdl;
00305                             }
00306                         }
00307                     }
00308                     // Detection flags get removed after final detection attempt to avoid further attempts.
00309                     flags &= ~(MDT_MIDI | MDT_PREFER_GM | MDT_PREFER_MT32);
00310                 }
00311             }
00312         }
00313 
00314         // The order in this list is important, since this is the order of preference
00315         // (e.g. MT_ADLIB is checked before MT_PCJR and MT_PCSPK for a good reason).
00316         // Detection flags get removed after detection attempt to avoid further attempts.
00317         if (flags & MDT_TOWNS) {
00318             tp = MT_TOWNS;
00319             flags &= ~MDT_TOWNS;
00320         } else if (flags & MDT_PC98) {
00321             tp = MT_PC98;
00322             flags &= ~MDT_PC98;
00323         } else if (flags & MDT_ADLIB) {
00324             tp = MT_ADLIB;
00325             flags &= ~MDT_ADLIB;
00326         } else if (flags & MDT_PCJR) {
00327             tp = MT_PCJR;
00328             flags &= ~MDT_PCJR;
00329         } else if (flags & MDT_PCSPK) {
00330             tp = MT_PCSPK;
00331             flags &= ~MDT_PCSPK;
00332         } else if (flags & MDT_C64) {
00333             tp = MT_C64;
00334             flags &= ~MDT_C64;
00335         } else if (flags & MDT_AMIGA) {
00336             tp = MT_AMIGA;
00337             flags &= ~MDT_AMIGA;
00338         } else if (flags & MDT_APPLEIIGS) {
00339             tp = MT_APPLEIIGS;
00340             flags &= ~MDT_APPLEIIGS;
00341         } else if (flags & MDT_MIDI) {
00342             // If we haven't tried to find a MIDI device yet we do this now.
00343             skipMidi = false;
00344             continue;
00345         } else if (flags) {
00346             // Invalid flags. Set them to MDT_NONE to leave detection loop.
00347             flags = MDT_NONE;
00348             tp = MT_AUTO;
00349         }
00350 
00351         for (PluginList::const_iterator m = p.begin(); m != p.end(); ++m) {
00352             MusicDevices i = (*m)->get<MusicPluginObject>().getDevices();
00353             for (MusicDevices::iterator d = i.begin(); d != i.end(); ++d) {
00354                 if (d->getMusicType() == tp) {
00355                     hdl = d->getHandle();
00356                     if (checkDevice(hdl))
00357                         return hdl;
00358                 }
00359             }
00360         }
00361     }
00362 
00363     return 0;
00364 }
00365 
00366 MidiDriver *MidiDriver::createMidi(MidiDriver::DeviceHandle handle) {
00367     MidiDriver *driver = 0;
00368     const PluginList p = MusicMan.getPlugins();
00369     for (PluginList::const_iterator m = p.begin(); m != p.end(); m++) {
00370         const MusicPluginObject &musicPlugin = (*m)->get<MusicPluginObject>();
00371         if (getDeviceString(handle, MidiDriver::kDriverId).equals(musicPlugin.getId()))
00372             musicPlugin.createInstance(&driver, handle);
00373     }
00374 
00375     return driver;
00376 }
00377 
00378 bool MidiDriver::checkDevice(MidiDriver::DeviceHandle handle) {
00379     const PluginList p = MusicMan.getPlugins();
00380     for (PluginList::const_iterator m = p.begin(); m != p.end(); m++) {
00381         const MusicPluginObject &musicPlugin = (*m)->get<MusicPluginObject>();
00382         if (getDeviceString(handle, MidiDriver::kDriverId).equals(musicPlugin.getId()))
00383             return musicPlugin.checkDevice(handle);
00384     }
00385 
00386     return false;
00387 }
00388 
00389 MidiDriver::DeviceHandle MidiDriver::getDeviceHandle(const Common::String &identifier) {
00390     const PluginList p = MusicMan.getPlugins();
00391 
00392     if (p.begin() == p.end())
00393         error("MidiDriver::getDeviceHandle: Music plugins must be loaded prior to calling this method");
00394 
00395     for (PluginList::const_iterator m = p.begin(); m != p.end(); m++) {
00396         MusicDevices i = (*m)->get<MusicPluginObject>().getDevices();
00397         for (MusicDevices::iterator d = i.begin(); d != i.end(); d++) {
00398             // The music driver id isn't unique, but it will match
00399             // driver's first device. This is useful when selecting
00400             // the driver from the command line.
00401             if (identifier.equals(d->getMusicDriverId()) || identifier.equals(d->getCompleteId()) || identifier.equals(d->getCompleteName())) {
00402                 return d->getHandle();
00403             }
00404         }
00405     }
00406 
00407     return 0;
00408 }
00409 
00410 void MidiDriver::sendMT32Reset() {
00411     static const byte resetSysEx[] = { 0x41, 0x10, 0x16, 0x12, 0x7F, 0x00, 0x00, 0x01, 0x00 };
00412     sysEx(resetSysEx, sizeof(resetSysEx));
00413     g_system->delayMillis(100);
00414 }
00415 
00416 void MidiDriver::sendGMReset() {
00417     static const byte resetSysEx[] = { 0x7E, 0x7F, 0x09, 0x01 };
00418     sysEx(resetSysEx, sizeof(resetSysEx));
00419     g_system->delayMillis(100);
00420 }


Generated on Sat Jul 20 2019 05:00:43 for ResidualVM by doxygen 1.7.1
curved edge   curved edge