all repos — mgba @ bdc72bb73b1988de6d96132f40e887918d75d539

mGBA Game Boy Advance Emulator

src/platform/qt/GameController.cpp (view raw)

  1/* Copyright (c) 2013-2014 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "GameController.h"
  7
  8#include "AudioProcessor.h"
  9#include "InputController.h"
 10#include "MultiplayerController.h"
 11
 12#include <QDateTime>
 13#include <QThread>
 14
 15#include <ctime>
 16
 17extern "C" {
 18#include "gba/audio.h"
 19#include "gba/gba.h"
 20#include "gba/serialize.h"
 21#include "gba/sharkport.h"
 22#include "gba/renderers/video-software.h"
 23#include "gba/supervisor/config.h"
 24#include "util/vfs.h"
 25}
 26
 27using namespace QGBA;
 28using namespace std;
 29
 30const int GameController::LUX_LEVELS[10] = { 5, 11, 18, 27, 42, 62, 84, 109, 139, 183 };
 31
 32GameController::GameController(QObject* parent)
 33	: QObject(parent)
 34	, m_drawContext(new uint32_t[256 * 256])
 35	, m_threadContext()
 36	, m_activeKeys(0)
 37	, m_inactiveKeys(0)
 38	, m_logLevels(0)
 39	, m_gameOpen(false)
 40	, m_audioThread(new QThread(this))
 41	, m_audioProcessor(AudioProcessor::create())
 42	, m_videoSync(VIDEO_SYNC)
 43	, m_audioSync(AUDIO_SYNC)
 44	, m_fpsTarget(-1)
 45	, m_turbo(false)
 46	, m_turboForced(false)
 47	, m_turboSpeed(-1)
 48	, m_inputController(nullptr)
 49	, m_multiplayer(nullptr)
 50	, m_stateSlot(1)
 51{
 52	m_renderer = new GBAVideoSoftwareRenderer;
 53	GBAVideoSoftwareRendererCreate(m_renderer);
 54	m_renderer->outputBuffer = (color_t*) m_drawContext;
 55	m_renderer->outputBufferStride = 256;
 56
 57	GBACheatDeviceCreate(&m_cheatDevice);
 58
 59	m_threadContext.state = THREAD_INITIALIZED;
 60	m_threadContext.debugger = 0;
 61	m_threadContext.frameskip = 0;
 62	m_threadContext.bios = 0;
 63	m_threadContext.renderer = &m_renderer->d;
 64	m_threadContext.userData = this;
 65	m_threadContext.rewindBufferCapacity = 0;
 66	m_threadContext.cheats = &m_cheatDevice;
 67	m_threadContext.logLevel = GBA_LOG_ALL;
 68
 69	m_lux.p = this;
 70	m_lux.sample = [] (GBALuminanceSource* context) {
 71		GameControllerLux* lux = static_cast<GameControllerLux*>(context);
 72		lux->value = 0xFF - lux->p->m_luxValue;
 73	};
 74
 75	m_lux.readLuminance = [] (GBALuminanceSource* context) {
 76		GameControllerLux* lux = static_cast<GameControllerLux*>(context);
 77		return lux->value;
 78	};
 79	setLuminanceLevel(0);
 80
 81	m_rtc.p = this;
 82	m_rtc.override = GameControllerRTC::NO_OVERRIDE;
 83	m_rtc.sample = [] (GBARTCSource* context) { };
 84	m_rtc.unixTime = [] (GBARTCSource* context) -> time_t {
 85		GameControllerRTC* rtc = static_cast<GameControllerRTC*>(context);
 86		switch (rtc->override) {
 87		case GameControllerRTC::NO_OVERRIDE:
 88		default:
 89			return time(nullptr);
 90		case GameControllerRTC::FIXED:
 91			return rtc->value;
 92		case GameControllerRTC::FAKE_EPOCH:
 93			return rtc->value + rtc->p->m_threadContext.gba->video.frameCounter * (int64_t) VIDEO_TOTAL_LENGTH / GBA_ARM7TDMI_FREQUENCY;
 94		}
 95	};
 96
 97	m_threadContext.startCallback = [] (GBAThread* context) {
 98		GameController* controller = static_cast<GameController*>(context->userData);
 99		controller->m_audioProcessor->setInput(context);
100		context->gba->luminanceSource = &controller->m_lux;
101		context->gba->rtcSource = &controller->m_rtc;
102		context->gba->rumble = controller->m_inputController->rumble();
103		context->gba->rotationSource = controller->m_inputController->rotationSource();
104		controller->m_fpsTarget = context->fpsTarget;
105		controller->gameStarted(context);
106	};
107
108	m_threadContext.cleanCallback = [] (GBAThread* context) {
109		GameController* controller = static_cast<GameController*>(context->userData);
110		controller->gameStopped(context);
111	};
112
113	m_threadContext.frameCallback = [] (GBAThread* context) {
114		GameController* controller = static_cast<GameController*>(context->userData);
115		controller->m_pauseMutex.lock();
116		if (controller->m_pauseAfterFrame) {
117			GBAThreadPauseFromThread(context);
118			controller->m_pauseAfterFrame = false;
119			controller->gamePaused(&controller->m_threadContext);
120		}
121		controller->m_pauseMutex.unlock();
122		if (GBASyncDrawingFrame(&controller->m_threadContext.sync)) {
123			controller->frameAvailable(controller->m_drawContext);
124		} else {
125			controller->frameAvailable(nullptr);
126		}
127	};
128
129	m_threadContext.logHandler = [] (GBAThread* context, enum GBALogLevel level, const char* format, va_list args) {
130		static const char* stubMessage = "Stub software interrupt";
131		if (!context) {
132			return;
133		}
134		GameController* controller = static_cast<GameController*>(context->userData);
135		if (level == GBA_LOG_STUB && strncmp(stubMessage, format, strlen(stubMessage)) == 0) {
136			va_list argc;
137			va_copy(argc, args);
138			int immediate = va_arg(argc, int);
139			controller->unimplementedBiosCall(immediate);
140		}
141		if (level == GBA_LOG_FATAL) {
142			QMetaObject::invokeMethod(controller, "crashGame", Q_ARG(const QString&, QString().vsprintf(format, args)));
143		} else if (!(controller->m_logLevels & level)) {
144			return;
145		}
146		QString message(QString().vsprintf(format, args));
147		if (level == GBA_LOG_STATUS) {
148			controller->statusPosted(message);
149		}
150		controller->postLog(level, message);
151	};
152
153	m_audioThread->start(QThread::TimeCriticalPriority);
154	m_audioProcessor->moveToThread(m_audioThread);
155	connect(this, SIGNAL(gameStarted(GBAThread*)), m_audioProcessor, SLOT(start()));
156	connect(this, SIGNAL(gameStopped(GBAThread*)), m_audioProcessor, SLOT(pause()));
157	connect(this, SIGNAL(gamePaused(GBAThread*)), m_audioProcessor, SLOT(pause()));
158	connect(this, SIGNAL(gameUnpaused(GBAThread*)), m_audioProcessor, SLOT(start()));
159	connect(this, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(pollEvents()));
160}
161
162GameController::~GameController() {
163	m_audioThread->quit();
164	m_audioThread->wait();
165	disconnect();
166	clearMultiplayerController();
167	closeGame();
168	GBACheatDeviceDestroy(&m_cheatDevice);
169	delete m_renderer;
170	delete[] m_drawContext;
171}
172
173void GameController::setMultiplayerController(std::shared_ptr<MultiplayerController> controller) {
174	if (controller == m_multiplayer) {
175		return;
176	}
177	clearMultiplayerController();
178	m_multiplayer = controller;
179	controller->attachGame(this);
180}
181
182void GameController::clearMultiplayerController() {
183	if (!m_multiplayer) {
184		return;
185	}
186	m_multiplayer->detachGame(this);
187	m_multiplayer.reset();
188}
189
190void GameController::setOverride(const GBACartridgeOverride& override) {
191	m_threadContext.override = override;
192	m_threadContext.hasOverride = true;
193}
194
195void GameController::setOptions(const GBAOptions* opts) {
196	setFrameskip(opts->frameskip);
197	setAudioSync(opts->audioSync);
198	setVideoSync(opts->videoSync);
199	setSkipBIOS(opts->skipBios);
200	setUseBIOS(opts->useBios);
201	setRewind(opts->rewindEnable, opts->rewindBufferCapacity, opts->rewindBufferInterval);
202	setVolume(opts->volume);
203	setMute(opts->mute);
204
205	threadInterrupt();
206	m_threadContext.idleOptimization = opts->idleOptimization;
207	threadContinue();
208}
209
210#ifdef USE_GDB_STUB
211ARMDebugger* GameController::debugger() {
212	return m_threadContext.debugger;
213}
214
215void GameController::setDebugger(ARMDebugger* debugger) {
216	threadInterrupt();
217	if (m_threadContext.debugger && GBAThreadIsActive(&m_threadContext)) {
218		GBADetachDebugger(m_threadContext.gba);
219	}
220	m_threadContext.debugger = debugger;
221	if (m_threadContext.debugger && GBAThreadIsActive(&m_threadContext)) {
222		GBAAttachDebugger(m_threadContext.gba, m_threadContext.debugger);
223	}
224	threadContinue();
225}
226#endif
227
228void GameController::loadGame(const QString& path, bool dirmode) {
229	closeGame();
230	if (!dirmode) {
231		QFile file(path);
232		if (!file.open(QIODevice::ReadOnly)) {
233			return;
234		}
235		file.close();
236	}
237
238	m_fname = path;
239	m_dirmode = dirmode;
240	openGame();
241}
242
243void GameController::openGame() {
244	m_gameOpen = true;
245
246	m_pauseAfterFrame = false;
247
248	if (m_turbo) {
249		m_threadContext.sync.videoFrameWait = false;
250		m_threadContext.sync.audioWait = false;
251	} else {
252		m_threadContext.sync.videoFrameWait = m_videoSync;
253		m_threadContext.sync.audioWait = m_audioSync;
254	}
255
256	m_threadContext.gameDir = 0;
257	m_threadContext.fname = strdup(m_fname.toLocal8Bit().constData());
258	if (m_dirmode) {
259		m_threadContext.gameDir = VDirOpen(m_threadContext.fname);
260		m_threadContext.stateDir = m_threadContext.gameDir;
261	} else {
262		m_threadContext.rom = VFileOpen(m_threadContext.fname, O_RDONLY);
263#if USE_LIBZIP
264		if (!m_threadContext.gameDir) {
265			m_threadContext.gameDir = VDirOpenZip(m_threadContext.fname, 0);
266		}
267#endif
268#if USE_LZMA
269		if (!m_threadContext.gameDir) {
270			m_threadContext.gameDir = VDirOpen7z(m_threadContext.fname, 0);
271		}
272#endif
273	}
274
275	if (!m_bios.isNull() &&m_useBios) {
276		m_threadContext.bios = VFileOpen(m_bios.toLocal8Bit().constData(), O_RDONLY);
277	} else {
278		m_threadContext.bios = nullptr;
279	}
280
281	if (!m_patch.isNull()) {
282		m_threadContext.patch = VFileOpen(m_patch.toLocal8Bit().constData(), O_RDONLY);
283	}
284
285	m_inputController->recalibrateAxes();
286
287	if (!GBAThreadStart(&m_threadContext)) {
288		m_gameOpen = false;
289		emit gameFailed();
290	}
291}
292
293void GameController::loadBIOS(const QString& path) {
294	if (m_bios == path) {
295		return;
296	}
297	m_bios = path;
298	if (m_gameOpen) {
299		closeGame();
300		openGame();
301	}
302}
303
304void GameController::loadPatch(const QString& path) {
305	if (m_gameOpen) {
306		closeGame();
307		m_patch = path;
308		openGame();
309	} else {
310		m_patch = path;
311	}
312}
313
314void GameController::importSharkport(const QString& path) {
315	if (!m_gameOpen) {
316		return;
317	}
318	VFile* vf = VFileOpen(path.toLocal8Bit().constData(), O_RDONLY);
319	if (!vf) {
320		return;
321	}
322	threadInterrupt();
323	GBASavedataImportSharkPort(m_threadContext.gba, vf, false);
324	threadContinue();
325	vf->close(vf);
326}
327
328void GameController::exportSharkport(const QString& path) {
329	if (!m_gameOpen) {
330		return;
331	}
332	VFile* vf = VFileOpen(path.toLocal8Bit().constData(), O_WRONLY | O_CREAT | O_TRUNC);
333	if (!vf) {
334		return;
335	}
336	threadInterrupt();
337	GBASavedataExportSharkPort(m_threadContext.gba, vf);
338	threadContinue();
339	vf->close(vf);
340}
341
342void GameController::closeGame() {
343	if (!m_gameOpen) {
344		return;
345	}
346	if (GBAThreadIsPaused(&m_threadContext)) {
347		GBAThreadUnpause(&m_threadContext);
348	}
349	GBAThreadEnd(&m_threadContext);
350	GBAThreadJoin(&m_threadContext);
351	if (m_threadContext.fname) {
352		free(const_cast<char*>(m_threadContext.fname));
353		m_threadContext.fname = nullptr;
354	}
355
356	m_patch = QString();
357
358	for (size_t i = 0; i < GBACheatSetsSize(&m_cheatDevice.cheats); ++i) {
359		GBACheatSet* set = *GBACheatSetsGetPointer(&m_cheatDevice.cheats, i);
360		GBACheatSetDeinit(set);
361		delete set;
362	}
363	GBACheatSetsClear(&m_cheatDevice.cheats);
364
365	m_gameOpen = false;
366	emit gameStopped(&m_threadContext);
367}
368
369void GameController::crashGame(const QString& crashMessage) {
370	closeGame();
371	emit gameCrashed(crashMessage);
372	emit gameStopped(&m_threadContext);
373}
374
375bool GameController::isPaused() {
376	if (!m_gameOpen) {
377		return false;
378	}
379	return GBAThreadIsPaused(&m_threadContext);
380}
381
382void GameController::setPaused(bool paused) {
383	if (!m_gameOpen || paused == GBAThreadIsPaused(&m_threadContext)) {
384		return;
385	}
386	if (paused) {
387		GBAThreadPause(&m_threadContext);
388		emit gamePaused(&m_threadContext);
389	} else {
390		GBAThreadUnpause(&m_threadContext);
391		emit gameUnpaused(&m_threadContext);
392	}
393}
394
395void GameController::reset() {
396	GBAThreadReset(&m_threadContext);
397}
398
399void GameController::threadInterrupt() {
400	if (m_gameOpen) {
401		GBAThreadInterrupt(&m_threadContext);
402	}
403}
404
405void GameController::threadContinue() {
406	if (m_gameOpen) {
407		GBAThreadContinue(&m_threadContext);
408	}
409}
410
411void GameController::frameAdvance() {
412	m_pauseMutex.lock();
413	m_pauseAfterFrame = true;
414	setPaused(false);
415	m_pauseMutex.unlock();
416}
417
418void GameController::setRewind(bool enable, int capacity, int interval) {
419	if (m_gameOpen) {
420		threadInterrupt();
421		GBARewindSettingsChanged(&m_threadContext, enable ? capacity : 0, enable ? interval : 0);
422		threadContinue();
423	} else {
424		if (enable) {
425			m_threadContext.rewindBufferInterval = interval;
426			m_threadContext.rewindBufferCapacity = capacity;
427		} else {
428			m_threadContext.rewindBufferInterval = 0;
429			m_threadContext.rewindBufferCapacity = 0;
430		}
431	}
432}
433
434void GameController::rewind(int states) {
435	threadInterrupt();
436	if (!states) {
437		GBARewindAll(&m_threadContext);
438	} else {
439		GBARewind(&m_threadContext, states);
440	}
441	threadContinue();
442	emit rewound(&m_threadContext);
443	emit frameAvailable(m_drawContext);
444}
445
446void GameController::keyPressed(int key) {
447	int mappedKey = 1 << key;
448	m_activeKeys |= mappedKey;
449	if (!m_inputController->allowOpposing()) {
450		if ((m_activeKeys & 0x30) == 0x30) {
451			m_inactiveKeys |= mappedKey ^ 0x30;
452			m_activeKeys ^= mappedKey ^ 0x30;
453		}
454		if ((m_activeKeys & 0xC0) == 0xC0) {
455			m_inactiveKeys |= mappedKey ^ 0xC0;
456			m_activeKeys ^= mappedKey ^ 0xC0;
457		}
458	}
459	updateKeys();
460}
461
462void GameController::keyReleased(int key) {
463	int mappedKey = 1 << key;
464	m_activeKeys &= ~mappedKey;
465	if (!m_inputController->allowOpposing()) {
466		if (mappedKey & 0x30) {
467			m_activeKeys |= m_inactiveKeys & (0x30 ^ mappedKey);
468			m_inactiveKeys &= ~0x30;
469		}
470		if (mappedKey & 0xC0) {
471			m_activeKeys |= m_inactiveKeys & (0xC0 ^ mappedKey);
472			m_inactiveKeys &= ~0xC0;
473		}
474	}
475	updateKeys();
476}
477
478void GameController::clearKeys() {
479	m_activeKeys = 0;
480	m_inactiveKeys = 0;
481	updateKeys();
482}
483
484void GameController::setAudioBufferSamples(int samples) {
485	threadInterrupt();
486	redoSamples(samples);
487	threadContinue();
488	QMetaObject::invokeMethod(m_audioProcessor, "setBufferSamples", Q_ARG(int, samples));
489}
490
491void GameController::setFPSTarget(float fps) {
492	threadInterrupt();
493	m_fpsTarget = fps;
494	m_threadContext.fpsTarget = fps;
495	if (m_turbo && m_turboSpeed > 0) {
496		m_threadContext.fpsTarget *= m_turboSpeed;
497	}
498	redoSamples(m_audioProcessor->getBufferSamples());
499	threadContinue();
500	QMetaObject::invokeMethod(m_audioProcessor, "inputParametersChanged");
501}
502
503void GameController::setSkipBIOS(bool set) {
504	threadInterrupt();
505	m_threadContext.skipBios = set;
506	threadContinue();
507}
508
509void GameController::setUseBIOS(bool use) {
510	threadInterrupt();
511	m_useBios = use;
512	threadContinue();
513}
514
515void GameController::loadState(int slot) {
516	if (slot > 0) {
517		m_stateSlot = slot;
518	}
519	GBARunOnThread(&m_threadContext, [](GBAThread* context) {
520		GameController* controller = static_cast<GameController*>(context->userData);
521		GBALoadState(context, context->stateDir, controller->m_stateSlot);
522		controller->stateLoaded(context);
523		controller->frameAvailable(controller->m_drawContext);
524	});
525}
526
527void GameController::saveState(int slot) {
528	if (slot > 0) {
529		m_stateSlot = slot;
530	}
531	GBARunOnThread(&m_threadContext, [](GBAThread* context) {
532		GameController* controller = static_cast<GameController*>(context->userData);
533		GBASaveState(context, context->stateDir, controller->m_stateSlot, true);
534	});
535}
536
537void GameController::setVideoSync(bool set) {
538	m_videoSync = set;
539	if (!m_turbo) {
540		threadInterrupt();
541		m_threadContext.sync.videoFrameWait = set;
542		threadContinue();
543	}
544}
545
546void GameController::setAudioSync(bool set) {
547	m_audioSync = set;
548	if (!m_turbo) {
549		threadInterrupt();
550		m_threadContext.sync.audioWait = set;
551		threadContinue();
552	}
553}
554
555void GameController::setFrameskip(int skip) {
556	m_threadContext.frameskip = skip;
557}
558
559void GameController::setVolume(int volume) {
560	threadInterrupt();
561	m_threadContext.volume = volume;
562	if (m_gameOpen) {
563		m_threadContext.gba->audio.masterVolume = volume;
564	}
565	threadContinue();
566}
567
568void GameController::setMute(bool mute) {
569	threadInterrupt();
570	m_threadContext.mute = mute;
571	if (m_gameOpen) {
572		m_threadContext.gba->audio.masterVolume = mute ? 0 : m_threadContext.volume;
573	}
574	threadContinue();
575}
576
577void GameController::setTurbo(bool set, bool forced) {
578	if (m_turboForced && !forced) {
579		return;
580	}
581	if (m_turbo == set && m_turboForced == forced) {
582		// Don't interrupt the thread if we don't need to
583		return;
584	}
585	m_turbo = set;
586	m_turboForced = set && forced;
587	enableTurbo();
588}
589
590void GameController::setTurboSpeed(float ratio) {
591	m_turboSpeed = ratio;
592	enableTurbo();
593}
594
595void GameController::enableTurbo() {
596	threadInterrupt();
597	if (!m_turbo) {
598		m_threadContext.fpsTarget = m_fpsTarget;
599		m_threadContext.sync.audioWait = m_audioSync;
600		m_threadContext.sync.videoFrameWait = m_videoSync;
601		redoSamples(m_audioProcessor->getBufferSamples());
602	} else if (m_turboSpeed <= 0) {
603		m_threadContext.sync.audioWait = false;
604		m_threadContext.sync.videoFrameWait = false;
605	} else {
606		m_threadContext.fpsTarget = m_fpsTarget * m_turboSpeed;
607		m_threadContext.sync.audioWait = true;
608		m_threadContext.sync.videoFrameWait = false;
609		redoSamples(m_audioProcessor->getBufferSamples());
610	}
611	threadContinue();
612}
613
614void GameController::setAVStream(GBAAVStream* stream) {
615	threadInterrupt();
616	m_threadContext.stream = stream;
617	if (m_gameOpen) {
618		m_threadContext.gba->stream = stream;
619	}
620	threadContinue();
621}
622
623void GameController::clearAVStream() {
624	threadInterrupt();
625	m_threadContext.stream = nullptr;
626	if (m_gameOpen) {
627		m_threadContext.gba->stream = nullptr;
628	}
629	threadContinue();
630}
631
632#ifdef USE_PNG
633void GameController::screenshot() {
634	GBARunOnThread(&m_threadContext, GBAThreadTakeScreenshot);
635}
636#endif
637
638void GameController::reloadAudioDriver() {
639	QMetaObject::invokeMethod(m_audioProcessor, "pause", Qt::BlockingQueuedConnection);
640	int samples = m_audioProcessor->getBufferSamples();
641	delete m_audioProcessor;
642	m_audioProcessor = AudioProcessor::create();
643	m_audioProcessor->setBufferSamples(samples);
644	m_audioProcessor->moveToThread(m_audioThread);
645	connect(this, SIGNAL(gameStarted(GBAThread*)), m_audioProcessor, SLOT(start()));
646	connect(this, SIGNAL(gameStopped(GBAThread*)), m_audioProcessor, SLOT(pause()));
647	connect(this, SIGNAL(gamePaused(GBAThread*)), m_audioProcessor, SLOT(pause()));
648	connect(this, SIGNAL(gameUnpaused(GBAThread*)), m_audioProcessor, SLOT(start()));
649	if (isLoaded()) {
650		m_audioProcessor->setInput(&m_threadContext);
651		QMetaObject::invokeMethod(m_audioProcessor, "start");
652	}
653}
654
655void GameController::setLuminanceValue(uint8_t value) {
656	m_luxValue = value;
657	value = std::max<int>(value - 0x16, 0);
658	m_luxLevel = 10;
659	for (int i = 0; i < 10; ++i) {
660		if (value < LUX_LEVELS[i]) {
661			m_luxLevel = i;
662			break;
663		}
664	}
665	emit luminanceValueChanged(m_luxValue);
666}
667
668void GameController::setLuminanceLevel(int level) {
669	int value = 0x16;
670	level = std::max(0, std::min(10, level));
671	if (level > 0) {
672		value += LUX_LEVELS[level - 1];
673	}
674	setLuminanceValue(value);
675}
676
677void GameController::setRealTime() {
678	m_rtc.override = GameControllerRTC::NO_OVERRIDE;
679}
680
681void GameController::setFixedTime(const QDateTime& time) {
682	m_rtc.override = GameControllerRTC::FIXED;
683	m_rtc.value = time.toMSecsSinceEpoch() / 1000;
684}
685
686void GameController::setFakeEpoch(const QDateTime& time) {
687	m_rtc.override = GameControllerRTC::FAKE_EPOCH;
688	m_rtc.value = time.toMSecsSinceEpoch() / 1000;
689}
690
691void GameController::updateKeys() {
692	int activeKeys = m_activeKeys;
693	activeKeys |= m_activeButtons;
694	activeKeys &= ~m_inactiveKeys;
695	m_threadContext.activeKeys = activeKeys;
696}
697
698void GameController::redoSamples(int samples) {
699#if RESAMPLE_LIBRARY != RESAMPLE_BLIP_BUF
700	float sampleRate = 0x8000;
701	float ratio;
702	if (m_threadContext.gba) {
703		sampleRate = m_threadContext.gba->audio.sampleRate;
704	}
705	ratio = GBAAudioCalculateRatio(sampleRate, m_threadContext.fpsTarget, 44100);
706	m_threadContext.audioBuffers = ceil(samples / ratio);
707#else
708	m_threadContext.audioBuffers = samples;
709#endif
710	if (m_threadContext.gba) {
711		GBAAudioResizeBuffer(&m_threadContext.gba->audio, m_threadContext.audioBuffers);
712	}
713}
714
715void GameController::setLogLevel(int levels) {
716	threadInterrupt();
717	m_logLevels = levels;
718	threadContinue();
719}
720
721void GameController::enableLogLevel(int levels) {
722	threadInterrupt();
723	m_logLevels |= levels;
724	threadContinue();
725}
726
727void GameController::disableLogLevel(int levels) {
728	threadInterrupt();
729	m_logLevels &= ~levels;
730	threadContinue();
731}
732
733void GameController::pollEvents() {
734	if (!m_inputController) {
735		return;
736	}
737
738	m_activeButtons = m_inputController->pollEvents();
739	updateKeys();
740}