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

lua_v1_sound.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM 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/savefile.h"
00024 
00025 #include "engines/grim/debug.h"
00026 #include "engines/grim/grim.h"
00027 #include "engines/grim/lua_v1.h"
00028 #include "engines/grim/actor.h"
00029 #include "engines/grim/savegame.h"
00030 #include "engines/grim/set.h"
00031 
00032 #include "engines/grim/imuse/imuse.h"
00033 
00034 #include "engines/grim/lua/lauxlib.h"
00035 
00036 namespace Grim {
00037 
00038 enum ImuseParam {
00039     IM_SOUND_PLAY_COUNT = 0x100,
00040     IM_SOUND_PEND_COUNT = 0x200,
00041     IM_SOUND_GROUP = 0x400,
00042     IM_SOUND_PRIORITY = 0x500,
00043     IM_SOUND_VOL = 0x600,
00044     IM_SOUND_PAN = 0x700
00045 };
00046 
00047 static float ImToMixer = 2.f;
00048 
00049 void Lua_V1::ImStartSound() {
00050     lua_Object nameObj = lua_getparam(1);
00051     lua_Object priorityObj = lua_getparam(2);
00052     lua_Object groupObj = lua_getparam(3);
00053 
00054     if (!lua_isstring(nameObj) && !lua_isnumber(nameObj))
00055         return;
00056     if (!lua_isnumber(priorityObj) || !lua_isnumber(groupObj))
00057         return;
00058 
00059     const char *soundName = lua_getstring(nameObj);
00060     int priority = (int)lua_getnumber(priorityObj);
00061     int group = (int)lua_getnumber(groupObj);
00062 
00063     // Start the sound with the appropriate settings
00064     if (g_imuse->startSound(soundName, group, 0, 127, 64, priority, nullptr)) {
00065         // FIXME actually it's pushnumber from result of startSound
00066         lua_pushstring(soundName);
00067     }
00068 }
00069 
00070 void Lua_V1::ImStopSound() {
00071     lua_Object nameObj = lua_getparam(1);
00072     if (lua_isnumber(nameObj))
00073         error("ImStopsound: name from value not supported");
00074 
00075     const char *soundName = lua_getstring(nameObj);
00076     g_imuse->stopSound(soundName);
00077 }
00078 
00079 void Lua_V1::ImStopAllSounds() {
00080     g_imuse->stopAllSounds();
00081 }
00082 
00083 void Lua_V1::ImPause() {
00084     g_imuse->pause(true);
00085 }
00086 
00087 void Lua_V1::ImResume() {
00088     g_imuse->pause(false);
00089 }
00090 
00091 void Lua_V1::ImSetVoiceEffect() {
00092     const char *effectName;
00093 
00094     effectName = luaL_check_string(1);
00095     Debug::warning(Debug::Sound, "ImSetVoiceEffect(%s) Voice effects are not yet supported", effectName);
00096 }
00097 
00098 void Lua_V1::ImSetMusicVol() {
00099     lua_Object volObj = lua_getparam(1);
00100     if (!lua_isnumber(volObj))
00101         return;
00102     g_system->getMixer()->setVolumeForSoundType(Audio::Mixer::kMusicSoundType, (int)(lua_getnumber(volObj) * ImToMixer));
00103 }
00104 
00105 void Lua_V1::ImGetMusicVol() {
00106     lua_pushnumber(g_system->getMixer()->getVolumeForSoundType(Audio::Mixer::kMusicSoundType) / ImToMixer);
00107 }
00108 
00109 void Lua_V1::ImSetVoiceVol() {
00110     lua_Object volObj = lua_getparam(1);
00111     if (!lua_isnumber(volObj))
00112         return;
00113     g_system->getMixer()->setVolumeForSoundType(Audio::Mixer::kSpeechSoundType, (int)(lua_getnumber(volObj) * ImToMixer));
00114 }
00115 
00116 void Lua_V1::ImGetVoiceVol() {
00117     lua_pushnumber(g_system->getMixer()->getVolumeForSoundType(Audio::Mixer::kSpeechSoundType) / ImToMixer);
00118 }
00119 
00120 void Lua_V1::ImSetSfxVol() {
00121     lua_Object volObj = lua_getparam(1);
00122     if (!lua_isnumber(volObj))
00123         return;
00124     g_system->getMixer()->setVolumeForSoundType(Audio::Mixer::kSFXSoundType, (int)(lua_getnumber(volObj) * ImToMixer));
00125 }
00126 
00127 void Lua_V1::ImGetSfxVol() {
00128     lua_pushnumber(g_system->getMixer()->getVolumeForSoundType(Audio::Mixer::kSFXSoundType) / ImToMixer);
00129 }
00130 
00131 void Lua_V1::ImSetParam() {
00132     lua_Object nameObj = lua_getparam(1);
00133     lua_Object paramObj = lua_getparam(2);
00134     lua_Object valueObj = lua_getparam(3);
00135 
00136     if (lua_isnumber(nameObj))
00137         error("ImSetParam: getting name from number is not supported");
00138     if (!lua_isstring(nameObj)) {
00139         lua_pushnumber(-1.0);
00140         return;
00141     }
00142 
00143     const char *soundName = lua_getstring(nameObj);
00144     int param = (int)lua_getnumber(paramObj);
00145     int value = (int)lua_getnumber(valueObj);
00146     if (value < 0)
00147         value = 0;
00148     switch (param) {
00149     case IM_SOUND_VOL:
00150         g_imuse->setVolume(soundName, value);
00151         break;
00152     case IM_SOUND_PAN:
00153         g_imuse->setPan(soundName, value);
00154         break;
00155     default:
00156         error("ImSetParam() Unimplemented %d", param);
00157     }
00158 }
00159 
00160 void Lua_V1::ImGetParam() {
00161     lua_Object nameObj = lua_getparam(1);
00162     lua_Object paramObj = lua_getparam(2);
00163 
00164     if (lua_isnumber(nameObj))
00165         error("ImGetParam: getting name from number is not supported");
00166     if (!lua_isstring(nameObj)) {
00167         lua_pushnumber(-1.0);
00168         return;
00169     }
00170 
00171     const char *soundName = lua_getstring(nameObj);
00172     int param = (int)lua_getnumber(paramObj);
00173     switch (param) {
00174     case IM_SOUND_PLAY_COUNT:
00175         lua_pushnumber(g_imuse->getCountPlayedTracks(soundName));
00176         break;
00177     case IM_SOUND_VOL:
00178         lua_pushnumber(g_imuse->getVolume(soundName));
00179         break;
00180     default:
00181         error("ImGetParam() Unimplemented %d", param);
00182     }
00183 }
00184 
00185 void Lua_V1::ImFadeParam() {
00186     lua_Object nameObj = lua_getparam(1);
00187     lua_Object opcodeObj = lua_getparam(2);
00188     lua_Object valueObj = lua_getparam(3);
00189     lua_Object durationObj = lua_getparam(4);
00190 
00191     if (!lua_isstring(nameObj) && !lua_isnumber(nameObj)) {
00192         lua_pushnumber(0);
00193         return;
00194     }
00195     if (!lua_isnumber(opcodeObj) || !lua_isnumber(valueObj) || !lua_isnumber(durationObj))
00196         return;
00197 
00198     if (lua_isnumber(nameObj)) {
00199         error("ImFadeParam: getting name from number is not supported");
00200     }
00201     const char *soundName = lua_getstring(nameObj);
00202     int opcode = (int)lua_getnumber(opcodeObj);
00203     int value = (int)lua_getnumber(valueObj);
00204     if (value < 0)
00205         value = 0;
00206     int duration = (int)lua_getnumber(durationObj);
00207     switch (opcode) {
00208     case IM_SOUND_PAN:
00209         g_imuse->setFadePan(soundName, value, duration);
00210         break;
00211     default:
00212         error("ImFadeParam(%s, %x, %d, %d)", soundName, opcode, value, duration);
00213         break;
00214     }
00215 }
00216 
00217 void Lua_V1::ImSetState() {
00218     lua_Object stateObj = lua_getparam(1);
00219     if (!lua_isnumber(stateObj))
00220         return;
00221 
00222     g_imuseState = (int)lua_getnumber(stateObj);
00223 }
00224 
00225 void Lua_V1::ImSetSequence() {
00226     lua_Object stateObj = lua_getparam(1);
00227     if (!lua_isnumber(stateObj))
00228         return;
00229 
00230     int state = (int)lua_getnumber(stateObj);
00231     lua_pushnumber(g_imuse->setMusicSequence(state));
00232 }
00233 
00234 void Lua_V1::SaveIMuse() {
00235     SaveGame *savedIMuse = SaveGame::openForSaving("grim.tmp");
00236     if (!savedIMuse)
00237         return;
00238     g_imuse->saveState(savedIMuse);
00239     delete savedIMuse;
00240 }
00241 
00242 void Lua_V1::RestoreIMuse() {
00243     SaveGame *savedIMuse = SaveGame::openForLoading("grim.tmp");
00244     if (!savedIMuse)
00245         return;
00246     g_imuse->stopAllSounds();
00247     g_imuse->resetState();
00248     g_imuse->restoreState(savedIMuse);
00249     delete savedIMuse;
00250     g_system->getSavefileManager()->removeSavefile("grim.tmp");
00251 }
00252 
00253 void Lua_V1::SetSoundPosition() {
00254     Math::Vector3d pos;
00255     int minVolume = 10;
00256     int maxVolume = 127;
00257     float someParam = 0;
00258     int argId = 1;
00259     lua_Object paramObj;
00260 
00261     if (g_grim->getCurrSet()) {
00262         g_grim->getCurrSet()->getSoundParameters(&minVolume, &maxVolume);
00263     }
00264 
00265     lua_Object nameObj = lua_getparam(argId++);
00266     if (!lua_isnumber(nameObj) && !lua_isstring(nameObj))
00267         return;
00268 
00269     lua_Object actorObj = lua_getparam(argId++);
00270     if (lua_isuserdata(actorObj) && lua_tag(actorObj) == MKTAG('A','C','T','R')) {
00271         Actor *actor = getactor(actorObj);
00272         if (!actor)
00273             return;
00274         pos = actor->getPos();
00275     } else if (lua_isnumber(actorObj)) {
00276         float x = lua_getnumber(actorObj);
00277         float y = lua_getnumber(argId++);
00278         float z = lua_getnumber(argId++);
00279         pos.set(x, y, z);
00280     }
00281 
00282     paramObj = (int)lua_getparam(argId++);
00283     if (lua_isnumber(paramObj)) {
00284         minVolume = (int)lua_getnumber(paramObj);
00285         if (minVolume > 127)
00286             minVolume = 127;
00287     }
00288     paramObj = lua_getparam(argId++);
00289     if (lua_isnumber(paramObj)) {
00290         maxVolume = (int)lua_getnumber(paramObj);
00291         if (maxVolume > 127)
00292             maxVolume = 127;
00293         else if (maxVolume < minVolume)
00294             maxVolume = minVolume;
00295     }
00296 
00297     paramObj = lua_getparam(argId++);
00298     if (lua_isnumber(paramObj)) {
00299         someParam = (int)lua_getnumber(paramObj);
00300         if (someParam < 0.0)
00301             someParam = 0.0;
00302     }
00303 
00304     if (g_grim->getCurrSet()) {
00305         if (lua_isnumber(nameObj))
00306             error("SetSoundPosition: number is not yet supported");
00307         else {
00308             const char *soundName = lua_getstring(nameObj);
00309             g_grim->getCurrSet()->setSoundPosition(soundName, pos, minVolume, maxVolume);
00310         }
00311     }
00312 }
00313 
00314 void Lua_V1::IsSoundPlaying() {
00315     // dummy
00316 }
00317 
00318 void Lua_V1::PlaySoundAt() {
00319     // dummy
00320 }
00321 
00322 void Lua_V1::LoadBundle() {
00323     // loading grimdemo.mus is allready handled
00324 }
00325 
00326 void Lua_V1::PlaySound() {
00327     // dummy
00328 }
00329 
00330 } // end of namespace Grim


Generated on Sat Feb 16 2019 05:00:56 for ResidualVM by doxygen 1.7.1
curved edge   curved edge