dosbox-wii/src/hardware/pcspeaker.cpp

231 lines
5.8 KiB
C++
Raw Normal View History

2009-05-02 23:03:37 +02:00
/*
* Copyright (C) 2002 The DOSBox Team
*
* 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 Library 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.
*/
#include <math.h>
2009-05-02 23:20:05 +02:00
#include "dosbox.h"
#include "mixer.h"
#include "timer.h"
2009-05-02 23:27:47 +02:00
#include "setup.h"
2009-05-02 23:35:44 +02:00
#include "pic.h"
2009-05-02 23:03:37 +02:00
#ifndef PI
#define PI 3.14159265358979323846
#endif
2009-05-02 23:20:05 +02:00
#define SPKR_BUF 4096
2009-05-02 23:03:37 +02:00
#define SPKR_RATE 22050
#define SPKR_VOLUME 5000
2009-05-02 23:35:44 +02:00
#define SPKR_SHIFT 10
2009-05-02 23:03:37 +02:00
2009-05-02 23:35:44 +02:00
enum SPKR_MODE {
MODE_NONE,MODE_WAVE,MODE_DELAY,MODE_ONOFF,MODE_REAL,
};
2009-05-02 23:03:37 +02:00
2009-05-02 23:35:44 +02:00
/* TODO
Maybe interpolate at the moment we switch between on/off
Keep track of postion of speaker conus in ONOFF mode
*/
2009-05-02 23:03:37 +02:00
2009-05-02 23:35:44 +02:00
static struct {
2009-05-02 23:03:37 +02:00
Bit16s volume;
MIXER_Channel * chan;
2009-05-02 23:20:05 +02:00
Bit16u buffer[SPKR_BUF];
2009-05-02 23:35:44 +02:00
SPKR_MODE mode;
SPKR_MODE pit_mode;
struct {
Bit16s buf[SPKR_BUF];
Bitu used;
} real;
struct {
Bitu index;
struct {
Bitu max,half;
} count,new_count;
} wave;
struct {
Bit16s buf[SPKR_BUF];
Bitu pos;
} out;
struct {
Bitu index;
Bitu max;
} delay;
struct {
Bitu vol;
Bitu rate;
Bitu rate_conv;
Bitu tick_add;
} hw;
bool onoff,enabled;
2009-05-02 23:20:05 +02:00
Bitu buf_pos;
2009-05-02 23:35:44 +02:00
} spkr;
2009-05-02 23:03:37 +02:00
2009-05-02 23:35:44 +02:00
static void GenerateSound(Bitu size) {
while (spkr.out.pos<size) {
Bitu samples=size-spkr.out.pos;
Bit16s * stream=&spkr.out.buf[spkr.out.pos];
switch (spkr.mode) {
case MODE_NONE:
memset(stream,0,samples*2);
spkr.out.pos+=samples;
break;
case MODE_WAVE:
{
for (Bitu i=0;i<samples;i++) {
spkr.wave.index+=spkr.hw.tick_add;
if (spkr.wave.index>=spkr.wave.count.max) {
*stream++=+spkr.volume;
spkr.wave.index-=spkr.wave.count.max;
spkr.wave.count.max=spkr.wave.new_count.max;
spkr.wave.count.half=spkr.wave.new_count.half;
} else if (spkr.wave.index>spkr.wave.count.half) {
*stream++=-spkr.volume;
} else {
*stream++=+spkr.volume;
}
}
spkr.out.pos+=samples;
}
break;
case MODE_ONOFF:
{
Bit16s val=spkr.onoff ? spkr.volume : -spkr.volume;
for (Bitu i=0;i<samples;i++) *stream++=val;
spkr.out.pos+=samples;
}
break;
case MODE_REAL:
{
Bitu buf_add=(spkr.real.used<<16)/samples;
Bitu buf_pos=0;
spkr.real.used=0;spkr.mode=MODE_NONE;
for (Bitu i=0;i<samples;i++) {
*stream++=spkr.real.buf[buf_pos >> 16];
buf_pos+=buf_add;
}
spkr.out.pos+=samples;
break;
}
case MODE_DELAY:
{
for (Bitu i=0;i<samples;i++) {
spkr.delay.index+=spkr.hw.tick_add;
if (spkr.delay.index<=spkr.delay.max) {
*stream++=+spkr.volume;
} else {
*stream++=-spkr.volume;
}
}
}
spkr.out.pos+=samples;
break;
}
}
}
2009-05-02 23:03:37 +02:00
2009-05-02 23:20:05 +02:00
void PCSPEAKER_SetCounter(Bitu cntr,Bitu mode) {
2009-05-02 23:35:44 +02:00
Bitu index=PIC_Index();
Bitu len=(spkr.hw.rate_conv*index)>>16;
if (spkr.mode==MODE_NONE) MIXER_Enable(spkr.chan,true);
2009-05-02 23:20:05 +02:00
switch (mode) {
2009-05-02 23:35:44 +02:00
case 0: /* Mode 0 one shot, used with realsound */
if (!spkr.enabled) return;
2009-05-02 23:20:05 +02:00
if (cntr>72) cntr=72;
2009-05-02 23:35:44 +02:00
if (spkr.mode!=MODE_REAL) GenerateSound(len);
spkr.pit_mode=MODE_REAL;
if (spkr.real.used<SPKR_BUF) spkr.real.buf[spkr.real.used++]=(cntr-36)*600;
2009-05-02 23:20:05 +02:00
break;
2009-05-02 23:35:44 +02:00
case 3: /* Square wave generator */
GenerateSound(len);
spkr.pit_mode=MODE_WAVE;
spkr.wave.new_count.max=cntr << SPKR_SHIFT;
spkr.wave.new_count.half=(cntr/2) << SPKR_SHIFT;
break;
case 4: /* Keep high till end of count */
GenerateSound(len);
spkr.delay.max=cntr << SPKR_SHIFT;
spkr.delay.index=0;
spkr.pit_mode=MODE_DELAY;
2009-05-02 23:20:05 +02:00
break;
}
2009-05-02 23:35:44 +02:00
if (spkr.enabled) spkr.mode=spkr.pit_mode;
2009-05-02 23:03:37 +02:00
}
2009-05-02 23:35:44 +02:00
void PCSPEAKER_SetType(Bitu mode) {
Bitu index=PIC_Index();
Bitu len=(spkr.hw.rate_conv*index)>>16;
GenerateSound(len);
if (spkr.mode==MODE_NONE) MIXER_Enable(spkr.chan,true);
switch (mode) {
2009-05-02 23:27:47 +02:00
case 0:
2009-05-02 23:35:44 +02:00
if (spkr.mode==MODE_ONOFF && spkr.onoff) spkr.onoff=false;
else spkr.mode=MODE_NONE;
spkr.enabled=false;
2009-05-02 23:27:47 +02:00
break;
2009-05-02 23:35:44 +02:00
case 1:
spkr.mode=MODE_ONOFF;
spkr.enabled=false;
spkr.onoff=false;
2009-05-02 23:27:47 +02:00
break;
2009-05-02 23:35:44 +02:00
case 2:
spkr.enabled=false;
spkr.onoff=true;
spkr.mode=MODE_ONOFF;
break;
case 3:
spkr.onoff=true;
spkr.enabled=true;
spkr.mode=spkr.pit_mode;
break;
};
}
static void PCSPEAKER_CallBack(Bit8u * stream,Bit32u len) {
if (spkr.out.pos<len) GenerateSound(len);
memcpy(stream,spkr.out.buf,len*2);
memcpy(spkr.out.buf,&spkr.out.buf[len],(spkr.out.pos-len)*2);
spkr.out.pos-=len;
if (spkr.mode==MODE_NONE) MIXER_Enable(spkr.chan,false);
2009-05-02 23:03:37 +02:00
}
2009-05-02 23:20:05 +02:00
void PCSPEAKER_Init(Section* sec) {
2009-05-02 23:35:44 +02:00
MSG_Add("SPEAKER_CONFIGFILE_HELP","PC-Speaker and TandySound options.\n");
2009-05-02 23:27:47 +02:00
Section_prop * section=static_cast<Section_prop *>(sec);
if(!section->Get_bool("enabled")) return;
2009-05-02 23:35:44 +02:00
spkr.volume=SPKR_VOLUME;
spkr.mode=MODE_NONE;
spkr.pit_mode=MODE_WAVE;
spkr.real.used=0;
spkr.out.pos=0;
spkr.onoff=false;
// spkr.hw.vol=section->Get_int("volume");
spkr.hw.rate=section->Get_int("pcrate");
spkr.hw.rate_conv=(spkr.hw.rate<<16)/1000000;
spkr.hw.tick_add=(Bitu)((double)PIT_TICK_RATE*(double)(1 << SPKR_SHIFT)/(double)spkr.hw.rate);
spkr.wave.index=0;
spkr.wave.count.max=spkr.wave.new_count.max=0x10000 << SPKR_SHIFT;
spkr.wave.count.half=spkr.wave.new_count.half=(0x10000 << SPKR_SHIFT)/2;
/* Register the sound channel */
spkr.chan=MIXER_AddChannel(&PCSPEAKER_CallBack,spkr.hw.rate,"PC-SPEAKER");
2009-05-02 23:03:37 +02:00
MIXER_Enable(spkr.chan,false);
MIXER_SetMode(spkr.chan,MIXER_16MONO);
}