aboutsummaryrefslogtreecommitdiffstats
path: root/code/macosx/macosx_snddma.m
blob: e7451bb71a72d23859cc719bf9bf7df4c027dc7d (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
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.

This file is part of Quake III Arena source code.

Quake III Arena source code 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.

Quake III Arena source code 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 Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===========================================================================
*/

// mac_snddma.c
// all other sound mixing is portable

#include "../client/snd_local.h"
#include <Carbon/Carbon.h>

// For 'ri'
#include "../renderer/tr_local.h"

#import <Foundation/NSZone.h>

// TJW - Different versions of SoundManager have different DMA buffer sizes.  On MacOS X DP2,
// the buffer size is 8K.  On MacOS 9 it is much smaller.  The SoundManager guy at Apple says
// that the size of the buffer will be decreasing for final release to help get rid of latency.
//#define	MAX_MIXED_SAMPLES	(0x8000 * 64)
//#define	SUBMISSION_CHUNK	 (0x100 * 64)

// Original MacOS 9 sizes
//#define	MAX_MIXED_SAMPLES	0x8000
//#define	SUBMISSION_CHUNK	 0x100 


static unsigned int submissionChunk;
static unsigned int maxMixedSamples;

static	short		*s_mixedSamples;
static	int				 s_chunkCount;		// number of chunks submitted
static	SndChannel		*s_sndChan;
static	ExtSoundHeader	s_sndHeader;

/*
===============
S_Callback
===============
*/
void S_Callback( SndChannel *sc, SndCommand *cmd )
{
    SndCommand		mySndCmd;
    SndCommand		mySndCmd2;
    int				offset;

    offset = ( s_chunkCount * submissionChunk ) & (maxMixedSamples-1);

    // queue up another sound buffer
    memset( &s_sndHeader, 0, sizeof( s_sndHeader ) );
    s_sndHeader.samplePtr = (void *)(s_mixedSamples + offset);
    s_sndHeader.numChannels = 2;
    s_sndHeader.sampleRate = rate22khz;
    s_sndHeader.loopStart = 0;
    s_sndHeader.loopEnd = 0;
    s_sndHeader.encode = extSH;
    s_sndHeader.baseFrequency = 1;
    s_sndHeader.numFrames = submissionChunk / 2;
    s_sndHeader.markerChunk = NULL;
    s_sndHeader.instrumentChunks = NULL;
    s_sndHeader.AESRecording = NULL;
    s_sndHeader.sampleSize = 16;

    mySndCmd.cmd = bufferCmd;
    mySndCmd.param1 = 0;
    mySndCmd.param2 = (int)&s_sndHeader;
    SndDoCommand( sc, &mySndCmd, true );

    // and another callback
    mySndCmd2.cmd = callBackCmd;
    mySndCmd2.param1 = 0;
    mySndCmd2.param2 = 0;
    SndDoCommand( sc, &mySndCmd2, true );

    s_chunkCount++;		// this is the next buffer we will submit
}

/*
===============
S_MakeTestPattern
===============
*/
void S_MakeTestPattern( void ) {
	int		i;
	float	v;
	int		sample;
	
	for ( i = 0 ; i < dma.samples / 2 ; i ++ ) {
		v = sin( M_PI * 2 * i / 64 );
		sample = v * 0x4000;
		((short *)dma.buffer)[i*2] = sample;	
		((short *)dma.buffer)[i*2+1] = sample;	
	}
}

/*
===============
SNDDMA_Init
===============
*/
qboolean SNDDMA_Init(void)
{
    int		err;
    cvar_t *bufferSize;
    cvar_t *chunkSize;

    chunkSize = ri.Cvar_Get( "s_chunksize", "8192", CVAR_ARCHIVE );
    bufferSize = ri.Cvar_Get( "s_buffersize", "65536", CVAR_ARCHIVE );

    if (!chunkSize->integer) {
        ri.Error(ERR_FATAL, "snd_chunkSize must be non-zero\n");
    }

    if (!bufferSize->integer) {
        ri.Error(ERR_FATAL, "snd_bufferSize must be non-zero\n");
    }

    if (chunkSize->integer >= bufferSize->integer) {
        ri.Error(ERR_FATAL, "snd_chunkSize must be less than snd_bufferSize\n");
    }

    if (bufferSize->integer % chunkSize->integer) {
        ri.Error(ERR_FATAL, "snd_bufferSize must be an even multiple of snd_chunkSize\n");
    }

    // create a sound channel
    s_sndChan = NULL;
    err = SndNewChannel( &s_sndChan, sampledSynth, initStereo, NewSndCallBackProc(S_Callback) );
    if ( err ) {
        return false;
    }

    submissionChunk = chunkSize->integer;
    maxMixedSamples = bufferSize->integer;

    s_mixedSamples = NSZoneMalloc(NULL, sizeof(*s_mixedSamples) * maxMixedSamples);
    
    dma.channels = 2;
    dma.samples = maxMixedSamples;
    dma.submission_chunk = submissionChunk;
    dma.samplebits = 16;
    dma.speed = 22050;
    dma.buffer = (byte *)s_mixedSamples;

    // que up the first submission-chunk sized buffer
    s_chunkCount = 0;

    S_Callback( s_sndChan, NULL );

    return qtrue;
}

/*
===============
SNDDMA_GetDMAPos
===============
*/
int	SNDDMA_GetDMAPos(void) {
    return s_chunkCount * submissionChunk;
}

/*
===============
SNDDMA_Shutdown
===============
*/
void SNDDMA_Shutdown(void) {
	if ( s_sndChan ) {
		SndDisposeChannel( s_sndChan, true );
		s_sndChan = NULL;
	}
}

/*
===============
SNDDMA_BeginPainting
===============
*/
void SNDDMA_BeginPainting(void) {
}

/*
===============
SNDDMA_Submit
===============
*/
void SNDDMA_Submit(void) {
}