aboutsummaryrefslogtreecommitdiff
path: root/morphos/morphos_sound.cpp
blob: 4077561ad73f409569f1f97cc45cefc389508b8e (plain)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2002 R�diger Hanke
 *
 * This program 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.

 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * MorphOS sound support
 *
 * $Header$
 *
 */

#include <devices/timer.h>

#include "stdafx.h"
#include "scumm.h"
#include "sound.h"

#include <dos/dos.h>
#include <exec/memory.h>
#include <devices/ahi.h>
#include <devices/amidi.h>

#define NO_PPCINLINE_STDARG
#define NO_PPCINLINE_VARARGS
#include <clib/alib_protos.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/ahi.h>

extern int GetTicks();
extern Scumm scumm;
extern SoundEngine sound;
extern SOUND_DRIVER_TYPE snd_driv;

#define AHI_BUF_SIZE	 (8*1024)

struct SignalSemaphore ScummMusicThreadRunning;

static struct MsgPort     *ahiPort    		 = NULL;
static struct AHIRequest  *ahiReq[ 2 ]		 = { NULL, NULL };
static UWORD					ahiCurBuf		 = 0;
static BOOL						ahiReqSent[ 2 ] = { FALSE, FALSE };
static BYTE						ahiDevice		 = -1;
		 UBYTE					ahiUnit  		 = AHI_DEFAULT_UNIT;
static char					  *ahiBuf[ 2 ]		 = { NULL, NULL };

static struct MsgPort 		 *ScummMidiPort = NULL;
		 struct IOMidiRequest *ScummMidiRequest = NULL;

bool init_morphos_sound( ULONG MidiUnit, bool NoMusic )
{
	if( !NoMusic )
	{
		ScummMidiPort = CreateMsgPort();
		if( ScummMidiPort )
		{
			ScummMidiRequest = (struct IOMidiRequest *)CreateIORequest( ScummMidiPort, sizeof( struct IOMidiRequest ) );
			if( ScummMidiRequest )
			{
				ScummMidiRequest->amr_Version = 2;
				if( OpenDevice( "amidi.device", MidiUnit, (struct IORequest *)ScummMidiRequest, AMIDIF_MIDISERVER ) )
				{
					DeleteIORequest( (struct IORequest *)ScummMidiRequest );
					DeleteMsgPort( ScummMidiPort );
					ScummMidiRequest = NULL;
					ScummMidiPort = NULL;
				}
			}
			else
			{
				DeleteMsgPort( ScummMidiPort );
				ScummMidiPort = NULL;
			}
		}

		if( !ScummMidiRequest )
			warning( "Could not open AMidi - music will not play" );
	}

	if( !(ahiPort = CreateMsgPort()) )
		return false;

	if( !(ahiReq[ 0 ] = (struct AHIRequest *)CreateIORequest( ahiPort, sizeof( struct AHIRequest ) )) )
	{
		DeleteMsgPort( ahiPort );
		ahiPort = NULL;
		return false;
	}

	if( !(ahiReq[ 1 ] = (struct AHIRequest *)AllocVec( sizeof( struct AHIRequest ), MEMF_ANY | MEMF_PUBLIC )) )
	{
		DeleteIORequest( ahiReq[ 0 ] );
		DeleteMsgPort( ahiPort );
		ahiReq[ 0 ] = NULL;
		ahiPort = NULL;
		return false;
	}

	if( !(ahiBuf[ 0 ] = (char *)AllocVec( 2*AHI_BUF_SIZE, MEMF_ANY | MEMF_PUBLIC )) )
	{
		FreeVec( ahiReq[ 1 ] );
		DeleteIORequest( ahiReq[ 0 ] );
		DeleteMsgPort( ahiPort );
		ahiReq[ 0 ] = NULL;
		ahiReq[ 1 ] = NULL;
		ahiPort = NULL;
		return false;
	}
	ahiBuf[ 1 ] = &ahiBuf[ 0 ][ AHI_BUF_SIZE ];

	ahiReq[ 0 ]->ahir_Version = 4;
	if( ahiDevice = OpenDevice( AHINAME, 0, (struct IORequest *)ahiReq[ 0 ], 0 ) )
	{
		FreeVec( ahiBuf[ 0 ] );
		FreeVec( ahiReq[ 1 ] );
		DeleteIORequest( ahiReq[ 0 ] );
		DeleteMsgPort( ahiPort );
		ahiBuf[ 0 ] = NULL;
		ahiReq[ 0 ] = NULL;
		ahiReq[ 1 ] = NULL;
		ahiPort = NULL;
		return false;
	}

	CopyMem( ahiReq[ 0 ], ahiReq[ 1 ], sizeof( struct AHIRequest ) );

	return true;
}


void exit_morphos_sound()
{
	if( ScummMidiRequest )
	{
		CloseDevice( (struct IORequest *)ScummMidiRequest );
		DeleteIORequest( (struct IORequest *)ScummMidiRequest );
		DeleteMsgPort( ScummMidiPort );
	}

	if( ahiReq[ 1 ] )
		FreeVec( ahiReq[ 1 ] );

	if( ahiReq[ 0 ] )
	{
		CloseDevice( (struct IORequest *)ahiReq[ 0 ] );
		DeleteIORequest( ahiReq[ 0 ] );
	}

	if( ahiBuf[ 0 ] )
		FreeVec( (APTR)ahiBuf[ 0 ] );

	if( ahiPort )
		DeleteMsgPort( ahiPort );
}


int morphos_music_thread( Scumm *s, ULONG MidiUnit, bool NoMusic )
{
	int  old_time, cur_time;
	bool initialized;
   bool TimerAvailable = false;
	struct MsgPort     *TimerMsgPort;
	struct timerequest *TimerIORequest;

	ObtainSemaphore( &ScummMusicThreadRunning );

	initialized = init_morphos_sound( MidiUnit, NoMusic );
	if( !initialized )
		warning( "Sound could not be initialized" );

	TimerMsgPort = CreateMsgPort();
	if( TimerMsgPort )
	{
		TimerIORequest = (struct timerequest *)CreateIORequest( TimerMsgPort, sizeof( struct timerequest ) );
		if( TimerIORequest )
		{
			if( OpenDevice( "timer.device", UNIT_MICROHZ, (struct IORequest *)TimerIORequest, 0 ) == 0 )
				TimerAvailable = true;
			else
			{
				DeleteIORequest( (struct IORequest *)TimerIORequest );
				DeleteMsgPort( TimerMsgPort );
			}
		}
		else
			DeleteMsgPort( TimerMsgPort );
	}

	if( !TimerAvailable )
	{
		warning( "ScummVM Music Thread: no timer available! Sound and music will be disabled" );
		Wait( SIGBREAKF_CTRL_F );
	}
	else
	{
		old_time = GetTicks();

		for(;;)
		{
			if( CheckSignal( SIGBREAKF_CTRL_F ) )
				break;

			if( !snd_driv.wave_based() )
			{
				cur_time = GetTicks();
				while( old_time < cur_time )
				{
					old_time += 10;
					sound.on_timer();
				}
/*				  TimerIORequest->tr_time.tv_micro = (old_time-cur_time)*1000;
				if( TimerIORequest->tr_time.tv_micro == 0 )
					TimerIORequest->tr_time.tv_micro = 100;*/
				TimerIORequest->tr_time.tv_micro = 10000;
			}
			else
				TimerIORequest->tr_time.tv_micro = 10000;

			TimerIORequest->tr_node.io_Command  = TR_ADDREQUEST;
			TimerIORequest->tr_time.tv_secs  = 0;
			DoIO( (struct IORequest *)TimerIORequest );

			if( !initialized )
				continue;

			if( !ahiReqSent[ ahiCurBuf ] || CheckIO( (struct IORequest *)ahiReq[ ahiCurBuf ] ) )
			{
				struct AHIRequest *req = ahiReq[ ahiCurBuf ];
				UWORD ahiOtherBuf = !ahiCurBuf;

				if( ahiReqSent[ ahiCurBuf ] )
				{
					WaitIO( (struct IORequest *)req );
					ahiReqSent[ ahiCurBuf ] = FALSE;
				}

				if( CheckSignal( SIGBREAKF_CTRL_F ) )
					break;

				scumm.mixWaves( (int16 *)ahiBuf[ ahiCurBuf ], AHI_BUF_SIZE >> 1 );

				req->ahir_Std.io_Message.mn_Node.ln_Pri = 0;
				req->ahir_Std.io_Command = CMD_WRITE;
				req->ahir_Std.io_Data    = ahiBuf[ ahiCurBuf ];
				req->ahir_Std.io_Length  = AHI_BUF_SIZE;
				req->ahir_Type				 = AHIST_M16S;
				req->ahir_Frequency		 = SAMPLES_PER_SEC;
				req->ahir_Position  		 = 0x8000;
				req->ahir_Volume			 = 0x10000;
				req->ahir_Link				 = (ahiReqSent[ ahiOtherBuf ] && !CheckIO( (struct IORequest *)ahiReq[ ahiOtherBuf ] )) ? ahiReq[ ahiOtherBuf ] : NULL;
				SendIO( (struct IORequest *)req );

				ahiReqSent[ ahiCurBuf ] = TRUE;
				ahiCurBuf = ahiOtherBuf;
			}
		}
	}

	if( ahiReqSent[ ahiCurBuf ] )
	{
		AbortIO( (struct IORequest *)ahiReq[ ahiCurBuf ] );
		WaitIO ( (struct IORequest *)ahiReq[ ahiCurBuf ] );
	}

	if( TimerAvailable )
	{
		CloseDevice( (struct IORequest *)TimerIORequest );
		DeleteIORequest( (struct IORequest *)TimerIORequest );
		DeleteMsgPort( TimerMsgPort );
	}

	exit_morphos_sound();

	ReleaseSemaphore( &ScummMusicThreadRunning );
	return 0;
}