aboutsummaryrefslogtreecommitdiff
path: root/macosx/plugins/DFSound/src/macosx.c
blob: f329ffcd097ebcd1d64398e88af369628e4af84c (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
/***************************************************************************
                        macosx.c  -  description
                             -------------------
    begin                : Wed May 15 2002
    copyright            : (C) 2002 by Pete Bernert
    email                : BlackDove@addcom.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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. See also the license.txt file for *
 *   additional informations.                                              *
 *                                                                         *
 ***************************************************************************/

#include "stdafx.h"

#define _IN_MACOSX

#ifdef _MACOSX

#include <Carbon/Carbon.h>
#include "externals.h"

#define kMaxSoundBuffers	20

//static int				macBufferSize = 2, macBufferCount = 36;
//static float			macSoundPitch = 1.0;
static long			   macSoundVolume = 100;
volatile int			soundBufferAt = -1, soundPlayAt = -1, soundQueued = 0;
char			*soundBuffer[kMaxSoundBuffers+1], *emptyBuffer;
SndChannelPtr	sndChannel;
//ExtSoundHeader	sndHeader;
CmpSoundHeader	sndHeader;
SndCallBackUPP	callBackUPP;
static int bufferIndex;

////////////////////////////////////////////////////////////////////////
// small linux time helper... only used for watchdog
////////////////////////////////////////////////////////////////////////

unsigned long timeGetTime()
{
 struct timeval tv;
 gettimeofday(&tv, 0);                                 // well, maybe there are better ways
 return tv.tv_sec * 1000 + tv.tv_usec/1000;            // to do that, but at least it works
}

pascal void MacProcessSound(SndChannelPtr chan, SndCommand *cmd)
{
	#pragma unused (chan, cmd)

	if (soundQueued <= 0)
		sndHeader.samplePtr = emptyBuffer;
	else
	{
		sndHeader.samplePtr = soundBuffer[soundPlayAt];
                soundPlayAt++;
		if (soundPlayAt >= kMaxSoundBuffers/*macBufferCount*/)
			soundPlayAt = 0;
		soundQueued--;
	}
	
	SndCommand buffer   = { bufferCmd, 0, (long) &sndHeader };
	SndDoImmediate(sndChannel, &buffer);

	SndCommand callback = { callBackCmd, 0, 0 };
	SndDoCommand(sndChannel, &callback, true);
}

////////////////////////////////////////////////////////////////////////
// SETUP SOUND
////////////////////////////////////////////////////////////////////////

static int buffer_size;
void SetupSound(void)
{
	int	count;
	
   callBackUPP = NewSndCallBackUPP(MacProcessSound);
   
	if (sndChannel)
	{
		SndDisposeChannel(sndChannel, true);
		sndChannel = nil;
	}
   
   buffer_size = 1;
   while (buffer_size < (44100 / 60))
      buffer_size <<= 1;
	
  	memset(&sndHeader, 0, sizeof(sndHeader));
	sndHeader.numChannels   = (iDisStereo ? 1 : 2);
	sndHeader.sampleRate    = 44100 << 16;
	sndHeader.encode        = cmpSH;
	sndHeader.baseFrequency = kMiddleC;
	sndHeader.numFrames     = buffer_size;
	sndHeader.sampleSize    = 16;
#ifdef __POWERPC__
    sndHeader.format        = k16BitBigEndianFormat;
#else
    sndHeader.format        = k16BitLittleEndianFormat;
#endif
    sndHeader.compressionID = fixedCompression;
   
	if (soundBufferAt != -1)
	{
      free(soundBuffer[0]);
		free(emptyBuffer);
	}
   
   soundBuffer[0] = (char *) calloc(buffer_size << 2, kMaxSoundBuffers);
	for (count = 1; count <= kMaxSoundBuffers; count++)
		soundBuffer[count] = soundBuffer[count-1] + (buffer_size << 2);
	emptyBuffer = (char *) calloc(buffer_size << 2, 1);
	
	soundBufferAt = soundPlayAt = soundQueued = 0;
        bufferIndex = 0;
	
	SndNewChannel(&sndChannel, sampledSynth, initStereo, callBackUPP);

	SndCommand	sndcmd;
	UInt32		volume;
	
	volume = (UInt32) (256.0 * (float) macSoundVolume / 100.0);
	
	sndcmd.cmd = volumeCmd;
   sndcmd.param1 = 0;
   sndcmd.param2 = (volume << 16) | volume;
   SndDoCommand(sndChannel, &sndcmd, true);

   sndcmd.cmd = callBackCmd;
   sndcmd.param1 = 0;
   sndcmd.param2 = 0;	
	SndDoCommand(sndChannel, &sndcmd, true);
}

////////////////////////////////////////////////////////////////////////
// REMOVE SOUND
////////////////////////////////////////////////////////////////////////

void RemoveSound(void)
{
   DisposeSndCallBackUPP(callBackUPP);
}

////////////////////////////////////////////////////////////////////////
// GET BYTES BUFFERED
////////////////////////////////////////////////////////////////////////

unsigned long SoundGetBytesBuffered(void)
{
	int bytes;
	int playAt = soundPlayAt;
	
   if (soundBufferAt < playAt) {
      bytes = (soundBuffer[kMaxSoundBuffers]-soundBuffer[playAt])+
      (soundBuffer[soundBufferAt]-soundBuffer[0]);
   } else {
		bytes = soundBuffer[soundBufferAt]-soundBuffer[playAt];
	}
	//printf("sb=%i\n", bytes);
	
//	if (bytes < SOUNDSIZE/2)
//		return 0;
	
	return bytes;
}

////////////////////////////////////////////////////////////////////////
// FEED SOUND DATA
////////////////////////////////////////////////////////////////////////

void SoundFeedStreamData(unsigned char* pSound,long lBytes)
{
    int rem;
    
   if (lBytes > (buffer_size<<2)*kMaxSoundBuffers) {
      printf("sound feed overflow!\n");
      return;
   }

   rem = soundBuffer[kMaxSoundBuffers]-(soundBuffer[soundBufferAt]+bufferIndex);
   if (lBytes > rem) {
      memcpy(soundBuffer[soundBufferAt]+bufferIndex, pSound, rem);
      lBytes -= rem; pSound += rem;
      soundQueued += kMaxSoundBuffers-soundBufferAt;
      soundBufferAt = 0; bufferIndex = 0;
   }
   memcpy(soundBuffer[soundBufferAt]+bufferIndex, pSound, lBytes);
   soundBufferAt += (lBytes+bufferIndex)/(buffer_size<<2);
   soundQueued += (lBytes+bufferIndex)/(buffer_size<<2);
   bufferIndex = (lBytes+bufferIndex)%(buffer_size<<2);
   
   if (soundQueued >= kMaxSoundBuffers) {
      printf("sound buffer overflow!\n");
   }
}

#endif