| 1 |
/* |
| 2 |
* Copyright (C) 2002-2010 The DOSBox Team |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU Library General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 |
*/ |
| 18 |
|
| 19 |
/* $Id: mixer.cpp,v 1.54 2009-09-05 11:10:04 qbix79 Exp $ */ |
| 20 |
|
| 21 |
/* |
| 22 |
Remove the sdl code from here and have it handeld in the sdlmain. |
| 23 |
That should call the mixer start from there or something. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <string.h> |
| 27 |
#include <sys/types.h> |
| 28 |
#include <math.h> |
| 29 |
|
| 30 |
#if defined (WIN32) |
| 31 |
//Midi listing |
| 32 |
#ifndef WIN32_LEAN_AND_MEAN |
| 33 |
#define WIN32_LEAN_AND_MEAN |
| 34 |
#endif |
| 35 |
#include <windows.h> |
| 36 |
#include <mmsystem.h> |
| 37 |
#endif |
| 38 |
|
| 39 |
#include "SDL.h" |
| 40 |
#include "mem.h" |
| 41 |
#include "pic.h" |
| 42 |
#include "dosbox.h" |
| 43 |
#include "mixer.h" |
| 44 |
#include "timer.h" |
| 45 |
#include "setup.h" |
| 46 |
#include "cross.h" |
| 47 |
#include "support.h" |
| 48 |
#include "mapper.h" |
| 49 |
#include "hardware.h" |
| 50 |
#include "programs.h" |
| 51 |
|
| 52 |
#define MIXER_SSIZE 4 |
| 53 |
#define MIXER_SHIFT 14 |
| 54 |
#define MIXER_REMAIN ((1<<MIXER_SHIFT)-1) |
| 55 |
#define MIXER_VOLSHIFT 13 |
| 56 |
|
| 57 |
static INLINE Bit16s MIXER_CLIP(Bits SAMP) { |
| 58 |
if (SAMP < MAX_AUDIO) { |
| 59 |
if (SAMP > MIN_AUDIO) |
| 60 |
return SAMP; |
| 61 |
else return MIN_AUDIO; |
| 62 |
} else return MAX_AUDIO; |
| 63 |
} |
| 64 |
|
| 65 |
static struct { |
| 66 |
Bit32s work[MIXER_BUFSIZE][2]; |
| 67 |
Bitu pos,done; |
| 68 |
Bitu needed, min_needed, max_needed; |
| 69 |
Bit32u tick_add,tick_remain; |
| 70 |
float mastervol[2]; |
| 71 |
MixerChannel * channels; |
| 72 |
bool nosound; |
| 73 |
Bit32u freq; |
| 74 |
Bit32u blocksize; |
| 75 |
} mixer; |
| 76 |
|
| 77 |
Bit8u MixTemp[MIXER_BUFSIZE]; |
| 78 |
|
| 79 |
MixerChannel * MIXER_AddChannel(MIXER_Handler handler,Bitu freq,const char * name) { |
| 80 |
MixerChannel * chan=new MixerChannel(); |
| 81 |
chan->scale = 1.0; |
| 82 |
chan->handler=handler; |
| 83 |
chan->name=name; |
| 84 |
chan->SetFreq(freq); |
| 85 |
chan->next=mixer.channels; |
| 86 |
chan->SetVolume(1,1); |
| 87 |
chan->enabled=false; |
| 88 |
mixer.channels=chan; |
| 89 |
return chan; |
| 90 |
} |
| 91 |
|
| 92 |
MixerChannel * MIXER_FindChannel(const char * name) { |
| 93 |
MixerChannel * chan=mixer.channels; |
| 94 |
while (chan) { |
| 95 |
if (!strcasecmp(chan->name,name)) break; |
| 96 |
chan=chan->next; |
| 97 |
} |
| 98 |
return chan; |
| 99 |
} |
| 100 |
|
| 101 |
void MIXER_DelChannel(MixerChannel* delchan) { |
| 102 |
MixerChannel * chan=mixer.channels; |
| 103 |
MixerChannel * * where=&mixer.channels; |
| 104 |
while (chan) { |
| 105 |
if (chan==delchan) { |
| 106 |
*where=chan->next; |
| 107 |
delete delchan; |
| 108 |
return; |
| 109 |
} |
| 110 |
where=&chan->next; |
| 111 |
chan=chan->next; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
void MixerChannel::UpdateVolume(void) { |
| 116 |
volmul[0]=(Bits)((1 << MIXER_VOLSHIFT)*scale*volmain[0]*mixer.mastervol[0]); |
| 117 |
volmul[1]=(Bits)((1 << MIXER_VOLSHIFT)*scale*volmain[1]*mixer.mastervol[1]); |
| 118 |
} |
| 119 |
|
| 120 |
void MixerChannel::SetVolume(float _left,float _right) { |
| 121 |
volmain[0]=_left; |
| 122 |
volmain[1]=_right; |
| 123 |
UpdateVolume(); |
| 124 |
} |
| 125 |
|
| 126 |
void MixerChannel::SetScale( float f ) { |
| 127 |
scale = f; |
| 128 |
UpdateVolume(); |
| 129 |
} |
| 130 |
|
| 131 |
void MixerChannel::Enable(bool _yesno) { |
| 132 |
if (_yesno==enabled) return; |
| 133 |
enabled=_yesno; |
| 134 |
if (enabled) { |
| 135 |
freq_index=MIXER_REMAIN; |
| 136 |
SDL_LockAudio(); |
| 137 |
if (done<mixer.done) done=mixer.done; |
| 138 |
SDL_UnlockAudio(); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
void MixerChannel::SetFreq(Bitu _freq) { |
| 143 |
freq_add=(_freq<<MIXER_SHIFT)/mixer.freq; |
| 144 |
} |
| 145 |
|
| 146 |
void MixerChannel::Mix(Bitu _needed) { |
| 147 |
needed=_needed; |
| 148 |
while (enabled && needed>done) { |
| 149 |
Bitu todo=needed-done; |
| 150 |
todo *= freq_add; |
| 151 |
todo = (todo >> MIXER_SHIFT) + ((todo & MIXER_REMAIN)!=0); |
| 152 |
handler(todo); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
void MixerChannel::AddSilence(void) { |
| 157 |
if (done<needed) { |
| 158 |
done=needed; |
| 159 |
last[0]=last[1]=0; |
| 160 |
freq_index=MIXER_REMAIN; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
template<class Type,bool stereo,bool signeddata,bool nativeorder> |
| 165 |
inline void MixerChannel::AddSamples(Bitu len, const Type* data) { |
| 166 |
Bits diff[2]; |
| 167 |
Bitu mixpos=mixer.pos+done; |
| 168 |
freq_index&=MIXER_REMAIN; |
| 169 |
Bitu pos=0;Bitu new_pos; |
| 170 |
|
| 171 |
goto thestart; |
| 172 |
for (;;) { |
| 173 |
new_pos=freq_index >> MIXER_SHIFT; |
| 174 |
if (pos<new_pos) { |
| 175 |
last[0]+=diff[0]; |
| 176 |
if (stereo) last[1]+=diff[1]; |
| 177 |
pos=new_pos; |
| 178 |
thestart: |
| 179 |
if (pos>=len) return; |
| 180 |
if ( sizeof( Type) == 1) { |
| 181 |
if (!signeddata) { |
| 182 |
if (stereo) { |
| 183 |
diff[0]=(((Bit8s)(data[pos*2+0] ^ 0x80)) << 8)-last[0]; |
| 184 |
diff[1]=(((Bit8s)(data[pos*2+1] ^ 0x80)) << 8)-last[1]; |
| 185 |
} else { |
| 186 |
diff[0]=(((Bit8s)(data[pos] ^ 0x80)) << 8)-last[0]; |
| 187 |
} |
| 188 |
} else { |
| 189 |
if (stereo) { |
| 190 |
diff[0]=(data[pos*2+0] << 8)-last[0]; |
| 191 |
diff[1]=(data[pos*2+1] << 8)-last[1]; |
| 192 |
} else { |
| 193 |
diff[0]=(data[pos] << 8)-last[0]; |
| 194 |
} |
| 195 |
} |
| 196 |
//16bit and 32bit both contain 16bit data internally |
| 197 |
} else { |
| 198 |
if (signeddata) { |
| 199 |
if (stereo) { |
| 200 |
if (nativeorder) { |
| 201 |
diff[0]=data[pos*2+0]-last[0]; |
| 202 |
diff[1]=data[pos*2+1]-last[1]; |
| 203 |
} else { |
| 204 |
if ( sizeof( Type) == 2) { |
| 205 |
diff[0]=(Bit16s)host_readw((HostPt)&data[pos*2+0])-last[0]; |
| 206 |
diff[1]=(Bit16s)host_readw((HostPt)&data[pos*2+1])-last[1]; |
| 207 |
} else { |
| 208 |
diff[0]=(Bit32s)host_readd((HostPt)&data[pos*2+0])-last[0]; |
| 209 |
diff[1]=(Bit32s)host_readd((HostPt)&data[pos*2+1])-last[1]; |
| 210 |
} |
| 211 |
} |
| 212 |
} else { |
| 213 |
if (nativeorder) { |
| 214 |
diff[0]=data[pos]-last[0]; |
| 215 |
} else { |
| 216 |
if ( sizeof( Type) == 2) { |
| 217 |
diff[0]=(Bit16s)host_readw((HostPt)&data[pos])-last[0]; |
| 218 |
} else { |
| 219 |
diff[0]=(Bit32s)host_readd((HostPt)&data[pos])-last[0]; |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
} else { |
| 224 |
if (stereo) { |
| 225 |
if (nativeorder) { |
| 226 |
diff[0]=(Bits)data[pos*2+0]-32768-last[0]; |
| 227 |
diff[1]=(Bits)data[pos*2+1]-32768-last[1]; |
| 228 |
} else { |
| 229 |
if ( sizeof( Type) == 2) { |
| 230 |
diff[0]=(Bits)host_readw((HostPt)&data[pos*2+0])-32768-last[0]; |
| 231 |
diff[1]=(Bits)host_readw((HostPt)&data[pos*2+1])-32768-last[1]; |
| 232 |
} else { |
| 233 |
diff[0]=(Bits)host_readd((HostPt)&data[pos*2+0])-32768-last[0]; |
| 234 |
diff[1]=(Bits)host_readd((HostPt)&data[pos*2+1])-32768-last[1]; |
| 235 |
} |
| 236 |
} |
| 237 |
} else { |
| 238 |
if (nativeorder) { |
| 239 |
diff[0]=(Bits)data[pos]-32768-last[0]; |
| 240 |
} else { |
| 241 |
if ( sizeof( Type) == 2) { |
| 242 |
diff[0]=(Bits)host_readw((HostPt)&data[pos])-32768-last[0]; |
| 243 |
} else { |
| 244 |
diff[0]=(Bits)host_readd((HostPt)&data[pos])-32768-last[0]; |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
Bits diff_mul=freq_index & MIXER_REMAIN; |
| 252 |
freq_index+=freq_add; |
| 253 |
mixpos&=MIXER_BUFMASK; |
| 254 |
Bits sample=last[0]+((diff[0]*diff_mul) >> MIXER_SHIFT); |
| 255 |
mixer.work[mixpos][0]+=sample*volmul[0]; |
| 256 |
if (stereo) sample=last[1]+((diff[1]*diff_mul) >> MIXER_SHIFT); |
| 257 |
mixer.work[mixpos][1]+=sample*volmul[1]; |
| 258 |
mixpos++;done++; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
void MixerChannel::AddStretched(Bitu len,Bit16s * data) { |
| 263 |
if (done>=needed) { |
| 264 |
LOG_MSG("Can't add, buffer full"); |
| 265 |
return; |
| 266 |
} |
| 267 |
Bitu outlen=needed-done;Bits diff; |
| 268 |
freq_index=0; |
| 269 |
Bitu temp_add=(len << MIXER_SHIFT)/outlen; |
| 270 |
Bitu mixpos=mixer.pos+done;done=needed; |
| 271 |
Bitu pos=0; |
| 272 |
diff=data[0]-last[0]; |
| 273 |
while (outlen--) { |
| 274 |
Bitu new_pos=freq_index >> MIXER_SHIFT; |
| 275 |
if (pos<new_pos) { |
| 276 |
pos=new_pos; |
| 277 |
last[0]+=diff; |
| 278 |
diff=data[pos]-last[0]; |
| 279 |
} |
| 280 |
Bits diff_mul=freq_index & MIXER_REMAIN; |
| 281 |
freq_index+=temp_add; |
| 282 |
mixpos&=MIXER_BUFMASK; |
| 283 |
Bits sample=last[0]+((diff*diff_mul) >> MIXER_SHIFT); |
| 284 |
mixer.work[mixpos][0]+=sample*volmul[0]; |
| 285 |
mixer.work[mixpos][1]+=sample*volmul[1]; |
| 286 |
mixpos++; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
void MixerChannel::AddSamples_m8(Bitu len, const Bit8u * data) { |
| 291 |
AddSamples<Bit8u,false,false,true>(len,data); |
| 292 |
} |
| 293 |
void MixerChannel::AddSamples_s8(Bitu len,const Bit8u * data) { |
| 294 |
AddSamples<Bit8u,true,false,true>(len,data); |
| 295 |
} |
| 296 |
void MixerChannel::AddSamples_m8s(Bitu len,const Bit8s * data) { |
| 297 |
AddSamples<Bit8s,false,true,true>(len,data); |
| 298 |
} |
| 299 |
void MixerChannel::AddSamples_s8s(Bitu len,const Bit8s * data) { |
| 300 |
AddSamples<Bit8s,true,true,true>(len,data); |
| 301 |
} |
| 302 |
void MixerChannel::AddSamples_m16(Bitu len,const Bit16s * data) { |
| 303 |
AddSamples<Bit16s,false,true,true>(len,data); |
| 304 |
} |
| 305 |
void MixerChannel::AddSamples_s16(Bitu len,const Bit16s * data) { |
| 306 |
AddSamples<Bit16s,true,true,true>(len,data); |
| 307 |
} |
| 308 |
void MixerChannel::AddSamples_m16u(Bitu len,const Bit16u * data) { |
| 309 |
AddSamples<Bit16u,false,false,true>(len,data); |
| 310 |
} |
| 311 |
void MixerChannel::AddSamples_s16u(Bitu len,const Bit16u * data) { |
| 312 |
AddSamples<Bit16u,true,false,true>(len,data); |
| 313 |
} |
| 314 |
void MixerChannel::AddSamples_m32(Bitu len,const Bit32s * data) { |
| 315 |
AddSamples<Bit32s,false,true,true>(len,data); |
| 316 |
} |
| 317 |
void MixerChannel::AddSamples_s32(Bitu len,const Bit32s * data) { |
| 318 |
AddSamples<Bit32s,true,true,true>(len,data); |
| 319 |
} |
| 320 |
void MixerChannel::AddSamples_m16_nonnative(Bitu len,const Bit16s * data) { |
| 321 |
AddSamples<Bit16s,false,true,false>(len,data); |
| 322 |
} |
| 323 |
void MixerChannel::AddSamples_s16_nonnative(Bitu len,const Bit16s * data) { |
| 324 |
AddSamples<Bit16s,true,true,false>(len,data); |
| 325 |
} |
| 326 |
void MixerChannel::AddSamples_m16u_nonnative(Bitu len,const Bit16u * data) { |
| 327 |
AddSamples<Bit16u,false,false,false>(len,data); |
| 328 |
} |
| 329 |
void MixerChannel::AddSamples_s16u_nonnative(Bitu len,const Bit16u * data) { |
| 330 |
AddSamples<Bit16u,true,false,false>(len,data); |
| 331 |
} |
| 332 |
void MixerChannel::AddSamples_m32_nonnative(Bitu len,const Bit32s * data) { |
| 333 |
AddSamples<Bit32s,false,true,false>(len,data); |
| 334 |
} |
| 335 |
void MixerChannel::AddSamples_s32_nonnative(Bitu len,const Bit32s * data) { |
| 336 |
AddSamples<Bit32s,true,true,false>(len,data); |
| 337 |
} |
| 338 |
|
| 339 |
void MixerChannel::FillUp(void) { |
| 340 |
SDL_LockAudio(); |
| 341 |
if (!enabled || done<mixer.done) { |
| 342 |
SDL_UnlockAudio(); |
| 343 |
return; |
| 344 |
} |
| 345 |
float index=PIC_TickIndex(); |
| 346 |
Mix((Bitu)(index*mixer.needed)); |
| 347 |
SDL_UnlockAudio(); |
| 348 |
} |
| 349 |
|
| 350 |
extern bool ticksLocked; |
| 351 |
static inline bool Mixer_irq_important(void) { |
| 352 |
/* In some states correct timing of the irqs is more important then |
| 353 |
* non stuttering audo */ |
| 354 |
return (ticksLocked || (CaptureState & (CAPTURE_WAVE|CAPTURE_VIDEO))); |
| 355 |
} |
| 356 |
|
| 357 |
/* Mix a certain amount of new samples */ |
| 358 |
static void MIXER_MixData(Bitu needed) { |
| 359 |
MixerChannel * chan=mixer.channels; |
| 360 |
while (chan) { |
| 361 |
chan->Mix(needed); |
| 362 |
chan=chan->next; |
| 363 |
} |
| 364 |
if (CaptureState & (CAPTURE_WAVE|CAPTURE_VIDEO)) { |
| 365 |
Bit16s convert[1024][2]; |
| 366 |
Bitu added=needed-mixer.done; |
| 367 |
if (added>1024) |
| 368 |
added=1024; |
| 369 |
Bitu readpos=(mixer.pos+mixer.done)&MIXER_BUFMASK; |
| 370 |
for (Bitu i=0;i<added;i++) { |
| 371 |
Bits sample=mixer.work[readpos][0] >> MIXER_VOLSHIFT; |
| 372 |
convert[i][0]=MIXER_CLIP(sample); |
| 373 |
sample=mixer.work[readpos][1] >> MIXER_VOLSHIFT; |
| 374 |
convert[i][1]=MIXER_CLIP(sample); |
| 375 |
readpos=(readpos+1)&MIXER_BUFMASK; |
| 376 |
} |
| 377 |
CAPTURE_AddWave( mixer.freq, added, (Bit16s*)convert ); |
| 378 |
} |
| 379 |
//Reset the the tick_add for constant speed |
| 380 |
if( Mixer_irq_important() ) |
| 381 |
mixer.tick_add = ((mixer.freq) << MIXER_SHIFT)/1000; |
| 382 |
mixer.done = needed; |
| 383 |
} |
| 384 |
|
| 385 |
static void MIXER_Mix(void) { |
| 386 |
SDL_LockAudio(); |
| 387 |
MIXER_MixData(mixer.needed); |
| 388 |
mixer.tick_remain+=mixer.tick_add; |
| 389 |
mixer.needed+=(mixer.tick_remain>>MIXER_SHIFT); |
| 390 |
mixer.tick_remain&=MIXER_REMAIN; |
| 391 |
SDL_UnlockAudio(); |
| 392 |
} |
| 393 |
|
| 394 |
static void MIXER_Mix_NoSound(void) { |
| 395 |
MIXER_MixData(mixer.needed); |
| 396 |
/* Clear piece we've just generated */ |
| 397 |
for (Bitu i=0;i<mixer.needed;i++) { |
| 398 |
mixer.work[mixer.pos][0]=0; |
| 399 |
mixer.work[mixer.pos][1]=0; |
| 400 |
mixer.pos=(mixer.pos+1)&MIXER_BUFMASK; |
| 401 |
} |
| 402 |
/* Reduce count in channels */ |
| 403 |
for (MixerChannel * chan=mixer.channels;chan;chan=chan->next) { |
| 404 |
if (chan->done>mixer.needed) chan->done-=mixer.needed; |
| 405 |
else chan->done=0; |
| 406 |
} |
| 407 |
/* Set values for next tick */ |
| 408 |
mixer.tick_remain+=mixer.tick_add; |
| 409 |
mixer.needed=mixer.tick_remain>>MIXER_SHIFT; |
| 410 |
mixer.tick_remain&=MIXER_REMAIN; |
| 411 |
mixer.done=0; |
| 412 |
} |
| 413 |
|
| 414 |
static void SDLCALL MIXER_CallBack(void * userdata, Uint8 *stream, int len) { |
| 415 |
Bitu need=(Bitu)len/MIXER_SSIZE; |
| 416 |
Bit16s * output=(Bit16s *)stream; |
| 417 |
Bitu reduce; |
| 418 |
Bitu pos, index, index_add; |
| 419 |
Bits sample; |
| 420 |
/* Enough room in the buffer ? */ |
| 421 |
if (mixer.done < need) { |
| 422 |
// LOG_MSG("Full underrun need %d, have %d, min %d", need, mixer.done, mixer.min_needed); |
| 423 |
if((need - mixer.done) > (need >>7) ) //Max 1 procent stretch. |
| 424 |
return; |
| 425 |
reduce = mixer.done; |
| 426 |
index_add = (reduce << MIXER_SHIFT) / need; |
| 427 |
mixer.tick_add = ((mixer.freq+mixer.min_needed) << MIXER_SHIFT)/1000; |
| 428 |
} else if (mixer.done < mixer.max_needed) { |
| 429 |
Bitu left = mixer.done - need; |
| 430 |
if (left < mixer.min_needed) { |
| 431 |
if( !Mixer_irq_important() ) { |
| 432 |
Bitu needed = mixer.needed - need; |
| 433 |
Bitu diff = (mixer.min_needed>needed?mixer.min_needed:needed) - left; |
| 434 |
mixer.tick_add = ((mixer.freq+(diff*3)) << MIXER_SHIFT)/1000; |
| 435 |
left = 0; //No stretching as we compensate with the tick_add value |
| 436 |
} else { |
| 437 |
left = (mixer.min_needed - left); |
| 438 |
left = 1 + (2*left) / mixer.min_needed; //left=1,2,3 |
| 439 |
} |
| 440 |
// LOG_MSG("needed underrun need %d, have %d, min %d, left %d", need, mixer.done, mixer.min_needed, left); |
| 441 |
reduce = need - left; |
| 442 |
index_add = (reduce << MIXER_SHIFT) / need; |
| 443 |
} else { |
| 444 |
reduce = need; |
| 445 |
index_add = (1 << MIXER_SHIFT); |
| 446 |
// LOG_MSG("regular run need %d, have %d, min %d, left %d", need, mixer.done, mixer.min_needed, left); |
| 447 |
|
| 448 |
/* Mixer tick value being updated: |
| 449 |
* 3 cases: |
| 450 |
* 1) A lot too high. >division by 5. but maxed by 2* min to prevent too fast drops. |
| 451 |
* 2) A little too high > division by 8 |
| 452 |
* 3) A little to nothing above the min_needed buffer > go to default value |
| 453 |
*/ |
| 454 |
Bitu diff = left - mixer.min_needed; |
| 455 |
if(diff > (mixer.min_needed<<1)) diff = mixer.min_needed<<1; |
| 456 |
if(diff > (mixer.min_needed>>1)) |
| 457 |
mixer.tick_add = ((mixer.freq-(diff/5)) << MIXER_SHIFT)/1000; |
| 458 |
else if (diff > (mixer.min_needed>>2)) |
| 459 |
mixer.tick_add = ((mixer.freq-(diff>>3)) << MIXER_SHIFT)/1000; |
| 460 |
else |
| 461 |
mixer.tick_add = (mixer.freq<< MIXER_SHIFT)/1000; |
| 462 |
} |
| 463 |
} else { |
| 464 |
/* There is way too much data in the buffer */ |
| 465 |
// LOG_MSG("overflow run need %d, have %d, min %d", need, mixer.done, mixer.min_needed); |
| 466 |
if (mixer.done > MIXER_BUFSIZE) |
| 467 |
index_add = MIXER_BUFSIZE - 2*mixer.min_needed; |
| 468 |
else |
| 469 |
index_add = mixer.done - 2*mixer.min_needed; |
| 470 |
index_add = (index_add << MIXER_SHIFT) / need; |
| 471 |
reduce = mixer.done - 2* mixer.min_needed; |
| 472 |
mixer.tick_add = ((mixer.freq-(mixer.min_needed/5)) << MIXER_SHIFT)/1000; |
| 473 |
} |
| 474 |
/* Reduce done count in all channels */ |
| 475 |
for (MixerChannel * chan=mixer.channels;chan;chan=chan->next) { |
| 476 |
if (chan->done>reduce) chan->done-=reduce; |
| 477 |
else chan->done=0; |
| 478 |
} |
| 479 |
|
| 480 |
// Reset mixer.tick_add when irqs are important |
| 481 |
if( Mixer_irq_important() ) |
| 482 |
mixer.tick_add=(mixer.freq<< MIXER_SHIFT)/1000; |
| 483 |
|
| 484 |
mixer.done -= reduce; |
| 485 |
mixer.needed -= reduce; |
| 486 |
pos = mixer.pos; |
| 487 |
mixer.pos = (mixer.pos + reduce) & MIXER_BUFMASK; |
| 488 |
index = 0; |
| 489 |
if(need != reduce) { |
| 490 |
while (need--) { |
| 491 |
Bitu i = (pos + (index >> MIXER_SHIFT )) & MIXER_BUFMASK; |
| 492 |
index += index_add; |
| 493 |
sample=mixer.work[i][0]>>MIXER_VOLSHIFT; |
| 494 |
*output++=MIXER_CLIP(sample); |
| 495 |
sample=mixer.work[i][1]>>MIXER_VOLSHIFT; |
| 496 |
*output++=MIXER_CLIP(sample); |
| 497 |
} |
| 498 |
/* Clean the used buffer */ |
| 499 |
while (reduce--) { |
| 500 |
pos &= MIXER_BUFMASK; |
| 501 |
mixer.work[pos][0]=0; |
| 502 |
mixer.work[pos][1]=0; |
| 503 |
pos++; |
| 504 |
} |
| 505 |
} else { |
| 506 |
while (reduce--) { |
| 507 |
pos &= MIXER_BUFMASK; |
| 508 |
sample=mixer.work[pos][0]>>MIXER_VOLSHIFT; |
| 509 |
*output++=MIXER_CLIP(sample); |
| 510 |
sample=mixer.work[pos][1]>>MIXER_VOLSHIFT; |
| 511 |
*output++=MIXER_CLIP(sample); |
| 512 |
mixer.work[pos][0]=0; |
| 513 |
mixer.work[pos][1]=0; |
| 514 |
pos++; |
| 515 |
} |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
static void MIXER_Stop(Section* sec) { |
| 520 |
} |
| 521 |
|
| 522 |
class MIXER : public Program { |
| 523 |
public: |
| 524 |
void MakeVolume(char * scan,float & vol0,float & vol1) { |
| 525 |
Bitu w=0; |
| 526 |
bool db=(toupper(*scan)=='D'); |
| 527 |
if (db) scan++; |
| 528 |
while (*scan) { |
| 529 |
if (*scan==':') { |
| 530 |
++scan;w=1; |
| 531 |
} |
| 532 |
char * before=scan; |
| 533 |
float val=(float)strtod(scan,&scan); |
| 534 |
if (before==scan) { |
| 535 |
++scan;continue; |
| 536 |
} |
| 537 |
if (!db) val/=100; |
| 538 |
else val=powf(10.0f,(float)val/20.0f); |
| 539 |
if (val<0) val=1.0f; |
| 540 |
if (!w) { |
| 541 |
vol0=val; |
| 542 |
} else { |
| 543 |
vol1=val; |
| 544 |
} |
| 545 |
} |
| 546 |
if (!w) vol1=vol0; |
| 547 |
} |
| 548 |
|
| 549 |
void Run(void) { |
| 550 |
if(cmd->FindExist("/LISTMIDI")) { |
| 551 |
ListMidi(); |
| 552 |
return; |
| 553 |
} |
| 554 |
if (cmd->FindString("MASTER",temp_line,false)) { |
| 555 |
MakeVolume((char *)temp_line.c_str(),mixer.mastervol[0],mixer.mastervol[1]); |
| 556 |
} |
| 557 |
MixerChannel * chan=mixer.channels; |
| 558 |
while (chan) { |
| 559 |
if (cmd->FindString(chan->name,temp_line,false)) { |
| 560 |
MakeVolume((char *)temp_line.c_str(),chan->volmain[0],chan->volmain[1]); |
| 561 |
} |
| 562 |
chan->UpdateVolume(); |
| 563 |
chan=chan->next; |
| 564 |
} |
| 565 |
if (cmd->FindExist("/NOSHOW")) return; |
| 566 |
chan=mixer.channels; |
| 567 |
WriteOut("Channel Main Main(dB)\n"); |
| 568 |
ShowVolume("MASTER",mixer.mastervol[0],mixer.mastervol[1]); |
| 569 |
for (chan=mixer.channels;chan;chan=chan->next) |
| 570 |
ShowVolume(chan->name,chan->volmain[0],chan->volmain[1]); |
| 571 |
} |
| 572 |
private: |
| 573 |
void ShowVolume(const char * name,float vol0,float vol1) { |
| 574 |
WriteOut("%-8s %3.0f:%-3.0f %+3.2f:%-+3.2f \n",name, |
| 575 |
vol0*100,vol1*100, |
| 576 |
20*log(vol0)/log(10.0f),20*log(vol1)/log(10.0f) |
| 577 |
); |
| 578 |
} |
| 579 |
|
| 580 |
void ListMidi(){ |
| 581 |
#if defined (WIN32) |
| 582 |
unsigned int total = midiOutGetNumDevs(); |
| 583 |
for(unsigned int i=0;i<total;i++) { |
| 584 |
MIDIOUTCAPS mididev; |
| 585 |
midiOutGetDevCaps(i, &mididev, sizeof(MIDIOUTCAPS)); |
| 586 |
WriteOut("%2d\t \"%s\"\n",i,mididev.szPname); |
| 587 |
} |
| 588 |
#endif |
| 589 |
return; |
| 590 |
}; |
| 591 |
|
| 592 |
}; |
| 593 |
|
| 594 |
static void MIXER_ProgramStart(Program * * make) { |
| 595 |
*make=new MIXER; |
| 596 |
} |
| 597 |
|
| 598 |
MixerChannel* MixerObject::Install(MIXER_Handler handler,Bitu freq,const char * name){ |
| 599 |
if(!installed) { |
| 600 |
if(strlen(name) > 31) E_Exit("Too long mixer channel name"); |
| 601 |
safe_strncpy(m_name,name,32); |
| 602 |
installed = true; |
| 603 |
return MIXER_AddChannel(handler,freq,name); |
| 604 |
} else { |
| 605 |
E_Exit("allready added mixer channel."); |
| 606 |
return 0; //Compiler happy |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
MixerObject::~MixerObject(){ |
| 611 |
if(!installed) return; |
| 612 |
MIXER_DelChannel(MIXER_FindChannel(m_name)); |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
void MIXER_Init(Section* sec) { |
| 617 |
sec->AddDestroyFunction(&MIXER_Stop); |
| 618 |
|
| 619 |
Section_prop * section=static_cast<Section_prop *>(sec); |
| 620 |
/* Read out config section */ |
| 621 |
mixer.freq=section->Get_int("rate"); |
| 622 |
mixer.nosound=section->Get_bool("nosound"); |
| 623 |
mixer.blocksize=section->Get_int("blocksize"); |
| 624 |
|
| 625 |
/* Initialize the internal stuff */ |
| 626 |
mixer.channels=0; |
| 627 |
mixer.pos=0; |
| 628 |
mixer.done=0; |
| 629 |
memset(mixer.work,0,sizeof(mixer.work)); |
| 630 |
mixer.mastervol[0]=1.0f; |
| 631 |
mixer.mastervol[1]=1.0f; |
| 632 |
|
| 633 |
/* Start the Mixer using SDL Sound at 22 khz */ |
| 634 |
SDL_AudioSpec spec; |
| 635 |
SDL_AudioSpec obtained; |
| 636 |
|
| 637 |
spec.freq=mixer.freq; |
| 638 |
spec.format=AUDIO_S16SYS; |
| 639 |
spec.channels=2; |
| 640 |
spec.callback=MIXER_CallBack; |
| 641 |
spec.userdata=NULL; |
| 642 |
spec.samples=(Uint16)mixer.blocksize; |
| 643 |
|
| 644 |
mixer.tick_remain=0; |
| 645 |
if (mixer.nosound) { |
| 646 |
LOG_MSG("MIXER:No Sound Mode Selected."); |
| 647 |
mixer.tick_add=((mixer.freq) << MIXER_SHIFT)/1000; |
| 648 |
TIMER_AddTickHandler(MIXER_Mix_NoSound); |
| 649 |
} else if (SDL_OpenAudio(&spec, &obtained) <0 ) { |
| 650 |
mixer.nosound = true; |
| 651 |
LOG_MSG("MIXER:Can't open audio: %s , running in nosound mode.",SDL_GetError()); |
| 652 |
mixer.tick_add=((mixer.freq) << MIXER_SHIFT)/1000; |
| 653 |
TIMER_AddTickHandler(MIXER_Mix_NoSound); |
| 654 |
} else { |
| 655 |
if((mixer.freq != obtained.freq) || (mixer.blocksize != obtained.samples)) |
| 656 |
LOG_MSG("MIXER:Got different values from SDL: freq %d, blocksize %d",obtained.freq,obtained.samples); |
| 657 |
mixer.freq=obtained.freq; |
| 658 |
mixer.blocksize=obtained.samples; |
| 659 |
mixer.tick_add=(mixer.freq << MIXER_SHIFT)/1000; |
| 660 |
TIMER_AddTickHandler(MIXER_Mix); |
| 661 |
SDL_PauseAudio(0); |
| 662 |
} |
| 663 |
mixer.min_needed=section->Get_int("prebuffer"); |
| 664 |
if (mixer.min_needed>100) mixer.min_needed=100; |
| 665 |
mixer.min_needed=(mixer.freq*mixer.min_needed)/1000; |
| 666 |
mixer.max_needed=mixer.blocksize * 2 + 2*mixer.min_needed; |
| 667 |
mixer.needed=mixer.min_needed+1; |
| 668 |
PROGRAMS_MakeFile("MIXER.COM",MIXER_ProgramStart); |
| 669 |
} |