1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
////////////////////////////////////////////////////////////////////////////////////////
//
// Nestopia - NES/Famicom emulator written in C++
//
// Copyright (C) 2003-2008 Martin Freij
//
// This file is part of Nestopia.
//
// Nestopia is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Nestopia is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Nestopia; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
////////////////////////////////////////////////////////////////////////////////////////
#include "NstIoFile.hpp"
#include "NstIoLog.hpp"
#include "NstIoArchive.hpp"
#include "NstResourceString.hpp"
#include "NstManagerPaths.hpp"
#include "NstWindowUser.hpp"
#include "NstDialogSound.hpp"
#include "NstManagerSound.hpp"
#include "NstManagerSoundRecorder.hpp"
#include "NstManagerPreferences.hpp"
namespace Nestopia
{
namespace Managers
{
struct Sound::Callbacks
{
static bool NST_CALLBACK Lock(Nes::Sound::UserData,Nes::Sound::Output&);
static void NST_CALLBACK Unlock(Nes::Sound::UserData,Nes::Sound::Output&);
};
Sound::Sound
(
Window::Custom& window,
Window::Menu& m,
Emulator& e,
const Paths& p,
const Preferences& r,
const Configuration& cfg
)
:
Manager ( e, m, this, &Sound::OnEmuEvent, IDM_OPTIONS_SOUND, &Sound::OnMenuOptionsSound ),
paths ( p ),
preferences ( r ),
directSound ( window ),
dialog ( new Window::Sound(e,directSound.GetAdapters(),p,cfg) ),
recorder ( new Recorder(m,dialog->GetRecorder(),e) )
{
Nes::Sound::Output::lockCallback.Set( &Callbacks::Lock, this );
Nes::Sound::Output::unlockCallback.Set( &Callbacks::Unlock, this );
UpdateSettings();
}
Sound::~Sound()
{
Nes::Sound::Output::lockCallback.Unset();
Nes::Sound::Output::unlockCallback.Unset();
}
bool Sound::CanRunInBackground() const
{
if (emulator.IsNsf())
return menu[IDM_MACHINE_NSF_OPTIONS_PLAYINBACKGROUND].Checked();
else
return preferences[Managers::Preferences::RUN_IN_BACKGROUND];
}
void Sound::OnEmuEvent(const Emulator::Event event,const Emulator::Data data)
{
switch (event)
{
case Emulator::EVENT_POWER_ON:
case Emulator::EVENT_SPEED:
if (emuOutput && emulator.NetPlayers() == 0)
{
Nes::Sound( emulator ).SetSpeed( emulator.GetSpeed() );
Nes::Sound( emulator ).EmptyBuffer();
}
break;
case Emulator::EVENT_REWINDING_START:
case Emulator::EVENT_REWINDING_STOP:
Nes::Sound(emulator).SetVolume
(
Nes::Sound::CHANNEL_DPCM,
event == Emulator::EVENT_REWINDING_START ? 0 : dialog->GetVolume(Nes::Sound::CHANNEL_DPCM)
);
case Emulator::EVENT_REWINDING_PREPARE:
directSound.StopStream();
break;
case Emulator::EVENT_NETPLAY_MODE:
menu[IDM_OPTIONS_SOUND].Enable( !data );
break;
}
}
void Sound::OnMenuOptionsSound(uint)
{
dialog->Open();
UpdateSettings();
}
void Sound::Save(Configuration& cfg) const
{
dialog->Save( cfg );
}
void Sound::UpdateSettings()
{
cstring errMsg = NULL;
if (dialog->SoundEnabled())
{
Nes::Sound nesSound( emulator );
errMsg = directSound.Update
(
dialog->GetAdapter(),
nesSound.GetSampleRate(),
16,
nesSound.GetSpeaker() == Nes::Sound::SPEAKER_STEREO ? DirectX::DirectSound::STEREO : DirectX::DirectSound::MONO,
emulator.IsNsf() ? 500 : (dialog->GetLatency() + 2) * 21,
dialog->GetPool(),
CanRunInBackground()
);
if (errMsg == NULL)
{
emuOutput = &output;
nesSound.SetSpeed( emulator.GetSpeed() );
recorder->Enable( &directSound.GetWaveFormat() );
return;
}
}
else
{
directSound.Destroy();
}
Disable( errMsg );
}
void Sound::Disable(cstring const errMsg)
{
emuOutput = NULL;
Nes::Sound( emulator ).SetVolume( Nes::Sound::ALL_CHANNELS, 0 );
recorder->Enable( NULL );
if (errMsg)
Window::User::Fail( Resource::String( IDS_ERR_SOUND_FAILED ).Invoke( errMsg ) );
}
void Sound::StartEmulation() const
{
Nes::Sound( emulator ).EmptyBuffer();
}
void Sound::StopEmulation()
{
directSound.StopStream();
}
#ifdef NST_MSVC_OPTIMIZE
#pragma optimize("t", on)
#endif
bool NST_CALLBACK Sound::Callbacks::Lock(Nes::Sound::UserData data,Nes::Sound::Output&)
{
Sound& sound = *static_cast<Sound*>(data);
if (sound.directSound.Streaming())
{
return sound.directSound.LockStream( sound.output.samples, sound.output.length );
}
else
{
if (sound.CanRunInBackground() != sound.directSound.GlobalFocus())
sound.UpdateSettings();
sound.directSound.StartStream();
return false;
}
}
void NST_CALLBACK Sound::Callbacks::Unlock(Nes::Sound::UserData data,Nes::Sound::Output&)
{
Sound& sound = *static_cast<Sound*>(data);
if (sound.recorder->IsRecording())
sound.recorder->Flush( sound.output );
sound.directSound.UnlockStream( sound.output.samples, sound.output.length );
}
#ifdef NST_MSVC_OPTIMIZE
#pragma optimize("", on)
#endif
}
}
|