1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 // Allow use of stuff in <nds.h>
24 #define FORBIDDEN_SYMBOL_EXCEPTION_printf
25 #define FORBIDDEN_SYMBOL_EXCEPTION_unistd_h
26 
27 #include "common/scummsys.h"
28 
29 #if defined(__DS__)
30 
31 #include "backends/mixer/maxmod/maxmod-mixer.h"
32 #include "common/system.h"
33 
34 #include <nds.h>
35 #include <maxmod9.h>
36 
MaxModMixerManager(int freq,int bufSize)37 MaxModMixerManager::MaxModMixerManager(int freq, int bufSize)
38 	:
39 	_freq(freq),
40 	_bufSize(bufSize) {
41 
42 }
43 
~MaxModMixerManager()44 MaxModMixerManager::~MaxModMixerManager() {
45 	// HACK: This is called during the OSystem destructor, but Audio::MixerImpl calls
46 	// mutex functions from OSystem during its destructor, which causes a crash.
47 	// _mixer->setReady(false);
48 	_mixer = 0;
49 	mmStreamClose();
50 }
51 
on_stream_request(mm_word length,mm_addr dest,mm_stream_formats format)52 mm_word on_stream_request( mm_word length, mm_addr dest, mm_stream_formats format ) {
53 	Audio::MixerImpl *mixer = (Audio::MixerImpl *)g_system->getMixer();
54 	assert(mixer);
55 	mixer->mixCallback((byte *)dest, length * 4);
56 	return length;
57 }
58 
init()59 void MaxModMixerManager::init() {
60 	_mixer = new Audio::MixerImpl(_freq);
61 	assert(_mixer);
62 
63 	mm_ds_system sys;
64 	sys.mod_count 			= 0;
65 	sys.samp_count			= 0;
66 	sys.mem_bank			= 0;
67 	sys.fifo_channel		= FIFO_MAXMOD;
68 	mmInit( &sys );
69 
70 	_stream.sampling_rate = _freq;
71 	_stream.buffer_length = _bufSize / 4;
72 	_stream.callback = on_stream_request;
73 	_stream.format = MM_STREAM_16BIT_STEREO;
74 	_stream.timer = MM_TIMER2;
75 	_stream.manual = 1;
76 
77 	mmStreamOpen( &_stream );
78 
79 	_mixer->setReady(true);
80 }
81 
suspendAudio()82 void MaxModMixerManager::suspendAudio() {
83 	mmStreamClose();
84 	_audioSuspended = true;
85 }
86 
resumeAudio()87 int MaxModMixerManager::resumeAudio() {
88 	if (!_audioSuspended)
89 		return -2;
90 
91 	mmStreamOpen( &_stream );
92 	_audioSuspended = false;
93 	return 0;
94 }
95 
updateAudio()96 void MaxModMixerManager::updateAudio() {
97 	if (!_audioSuspended)
98 		mmStreamUpdate();
99 }
100 
101 #endif
102