1 | /* $Id: dsmixer.cpp,v 1.3 2000-08-02 15:48:27 bird Exp $ */
|
---|
2 | /*
|
---|
3 | * DirectSound Software Mixer
|
---|
4 | *
|
---|
5 | * Copyright 2000 Michal Necasek
|
---|
6 | * Copyright 2000 Ondrej Necasek
|
---|
7 | *
|
---|
8 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
9 | *
|
---|
10 | */
|
---|
11 |
|
---|
12 |
|
---|
13 | /*@Header***********************************************************************
|
---|
14 | * Header Files *
|
---|
15 | *******************************************************************************/
|
---|
16 | #define INCL_DOSMISC
|
---|
17 | #include <os2win.h>
|
---|
18 |
|
---|
19 | #include <stdlib.h>
|
---|
20 | #include <string.h>
|
---|
21 |
|
---|
22 | #define INITGUID
|
---|
23 | #include <dsound.h>
|
---|
24 |
|
---|
25 | #include "os2dsound.h"
|
---|
26 | #include "os2sndbuffer.h"
|
---|
27 | #include "os2notify.h"
|
---|
28 | #include <misc.h>
|
---|
29 |
|
---|
30 | signed long mixbuf[2300*2]; /* enough for 50 ms at 44100Hz stereo */
|
---|
31 |
|
---|
32 | void MixCallback(ULONG cbMix) {
|
---|
33 | // Check for priority level here; for DSSCL_WRITEPRIMARY just play directly
|
---|
34 | // from the primary buffer; for all others mix the secondary buffers
|
---|
35 | if (OS2IDirectSoundBuffer::primary->parentDS->GetCoopLevel() != DSSCL_WRITEPRIMARY)
|
---|
36 | MixFunc(OS2IDirectSoundBuffer::dsbroot, OS2IDirectSoundBuffer::primary, cbMix);
|
---|
37 | else {
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | void MixOneBuffer(OS2IDirectSoundBuffer *inBuf, int tomix, int outrate)
|
---|
42 | {
|
---|
43 | unsigned char *data8b;
|
---|
44 | signed short *data16b;
|
---|
45 | int inpos, spd, i, j, sample, len, bytespersample, vol1, vol2;
|
---|
46 | int oldpos;
|
---|
47 |
|
---|
48 | bytespersample = inBuf->lpfxFormat->wBitsPerSample *
|
---|
49 | inBuf->lpfxFormat->nChannels / 8;
|
---|
50 | inpos = inBuf->playpos / bytespersample * 1024 + inBuf->frac;
|
---|
51 | spd = 1024 * inBuf->frequency / outrate;
|
---|
52 | if (inBuf->pan <= 0)
|
---|
53 | vol1 = inBuf->volume;
|
---|
54 | else
|
---|
55 | vol1 = inBuf->volume * (256 - inBuf->pan) / 256;
|
---|
56 | if (inBuf->pan >= 0)
|
---|
57 | vol2 = inBuf->volume;
|
---|
58 | else
|
---|
59 | vol2 = inBuf->volume * (256 + inBuf->pan) / 256;
|
---|
60 |
|
---|
61 | len = inBuf->bufferdesc.dwBufferBytes / bytespersample * 1024;
|
---|
62 | if ((inBuf->lpfxFormat->nChannels == 2) && (inBuf->lpfxFormat->wBitsPerSample == 16)) {
|
---|
63 | data16b = (signed short *) inBuf->lpBuffer;
|
---|
64 | for (i = 0; i < tomix; i++) {
|
---|
65 | j = inpos / 1024 * 2;
|
---|
66 | mixbuf[i*2] += data16b[j] * vol1 / 256;
|
---|
67 | mixbuf[i*2+1] += data16b[j+1] * vol2 / 256;
|
---|
68 | inpos += spd;
|
---|
69 | if (inpos >= len) {
|
---|
70 | if (inBuf->fLoop) inpos -= len;
|
---|
71 | else {
|
---|
72 | inBuf->fPlaying = FALSE;
|
---|
73 | if (inBuf->notify != NULL)
|
---|
74 | inBuf->notify->CheckStop();
|
---|
75 | break;
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 | if ((inBuf->lpfxFormat->nChannels == 1) && (inBuf->lpfxFormat->wBitsPerSample == 16)) {
|
---|
81 | data16b = (signed short *) inBuf->lpBuffer;
|
---|
82 | for (i = 0; i < tomix; i++) {
|
---|
83 | j = inpos / 1024;
|
---|
84 | sample = data16b[j];
|
---|
85 | mixbuf[i*2] += sample * vol1 / 256;
|
---|
86 | mixbuf[i*2+1] += sample * vol2 / 256;
|
---|
87 | inpos += spd;
|
---|
88 | if (inpos >= len) {
|
---|
89 | if (inBuf->fLoop) inpos -= len;
|
---|
90 | else {
|
---|
91 | inBuf->fPlaying = FALSE;
|
---|
92 | if (inBuf->notify != NULL)
|
---|
93 | inBuf->notify->CheckStop();
|
---|
94 | break;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 | if ((inBuf->lpfxFormat->nChannels == 2) && (inBuf->lpfxFormat->wBitsPerSample == 8)) {
|
---|
100 | data8b = (unsigned char *) inBuf->lpBuffer;
|
---|
101 | for (i = 0; i < tomix; i++) {
|
---|
102 | j = inpos / 1024 * 2;
|
---|
103 | sample = ((int) data8b[j] - 128) * vol1;
|
---|
104 | mixbuf[i*2] += sample;
|
---|
105 | sample = ((int) data8b[j+1] - 128) * vol2;
|
---|
106 | mixbuf[i*2+1] += sample;
|
---|
107 | inpos += spd;
|
---|
108 | if (inpos >= len) {
|
---|
109 | if (inBuf->fLoop) inpos -= len;
|
---|
110 | else {
|
---|
111 | inBuf->fPlaying = FALSE;
|
---|
112 | if (inBuf->notify != NULL)
|
---|
113 | inBuf->notify->CheckStop();
|
---|
114 | break;
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 | if ((inBuf->lpfxFormat->nChannels == 1) && (inBuf->lpfxFormat->wBitsPerSample == 8)) {
|
---|
120 | data8b = (unsigned char *) inBuf->lpBuffer;
|
---|
121 | for (i = 0; i < tomix; i++) {
|
---|
122 | j = inpos / 1024;
|
---|
123 | sample = (int) data8b[j] - 128;
|
---|
124 | mixbuf[i*2] += sample * vol1;
|
---|
125 | mixbuf[i*2+1] += sample * vol2;
|
---|
126 | inpos += spd;
|
---|
127 | if (inpos >= len) {
|
---|
128 | if (inBuf->fLoop) inpos -= len;
|
---|
129 | else {
|
---|
130 | inBuf->fPlaying = FALSE;
|
---|
131 | if (inBuf->notify != NULL)
|
---|
132 | inBuf->notify->CheckStop();
|
---|
133 | break;
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|
138 | oldpos = inBuf->playpos;
|
---|
139 | inBuf->playpos = inpos / 1024 * bytespersample;
|
---|
140 | inBuf->frac = inpos % 1024;
|
---|
141 |
|
---|
142 | // Check if any notifications are to be signaled
|
---|
143 | if (inBuf->notify != NULL)
|
---|
144 | inBuf->notify->CheckPos(oldpos, inBuf->playpos);
|
---|
145 |
|
---|
146 | // keep the write cursor about 15ms ahead of the play cursor
|
---|
147 | inBuf->writepos = inBuf->playpos + inBuf->frequency * bytespersample / 67;
|
---|
148 | inBuf->writepos %= inBuf->bufferdesc.dwBufferBytes;
|
---|
149 | }
|
---|
150 |
|
---|
151 | void MixFunc (OS2IDirectSoundBuffer *firstBuf, OS2IDirectSoundBuffer *outBuf,
|
---|
152 | ULONG cbMix) {
|
---|
153 | OS2IDirectSoundBuffer *inBuf = firstBuf;
|
---|
154 | int i, outbits, outrate, outnch, tomix, outpos, outlen;
|
---|
155 | unsigned char *data8b;
|
---|
156 | signed short *data16b;
|
---|
157 |
|
---|
158 | outbits = outBuf->lpfxFormat->wBitsPerSample;
|
---|
159 | outrate = outBuf->lpfxFormat->nSamplesPerSec;
|
---|
160 | outnch = outBuf->lpfxFormat->nChannels;
|
---|
161 | tomix = cbMix * 8 / outbits / outnch;
|
---|
162 |
|
---|
163 | memset(&mixbuf, 0, tomix * 2 * sizeof(mixbuf[0]));
|
---|
164 |
|
---|
165 | while (inBuf != NULL) {
|
---|
166 | if (inBuf->fPlaying) {
|
---|
167 | MixOneBuffer(inBuf, tomix, outrate);
|
---|
168 | }
|
---|
169 | inBuf = inBuf->next;
|
---|
170 | }
|
---|
171 |
|
---|
172 | outpos = outBuf->playpos * 8 / outbits;
|
---|
173 | outlen = outBuf->bufferdesc.dwBufferBytes * 8 / outbits;
|
---|
174 | if (outbits == 16) {
|
---|
175 | data16b = (signed short *) outBuf->lpBuffer;
|
---|
176 | for (i = 0; i < tomix * outnch; i++) {
|
---|
177 | if (mixbuf[i] <= -32768) data16b[outpos] = -32768;
|
---|
178 | else if (mixbuf[i] >= 32767) data16b[outpos] = 32767;
|
---|
179 | else data16b[outpos] = mixbuf[i];
|
---|
180 | outpos++;
|
---|
181 | if (outpos >= outlen) outpos = 0;
|
---|
182 | }
|
---|
183 | } else {
|
---|
184 | data8b = (unsigned char *) outBuf->lpBuffer;
|
---|
185 | for (i = 0; i < tomix * outnch; i++) {
|
---|
186 | if (mixbuf[i] <= -32768) data8b[outpos] = 0;
|
---|
187 | else if (mixbuf[i] >= 32767) data8b[outpos] = 255;
|
---|
188 | else data8b[outpos] = mixbuf[i] / 256 + 128;
|
---|
189 | outpos++;
|
---|
190 | if (outpos >= outlen) outpos = 0;
|
---|
191 | }
|
---|
192 | }
|
---|
193 | outBuf->playpos = outpos * outbits / 8;
|
---|
194 | }
|
---|
195 |
|
---|