dosbox-wii/src/hardware/pcspeaker.cpp

148 lines
3.5 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: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:27:47 +02:00
#define SPKR_SHIFT 16
2009-05-02 23:03:37 +02:00
2009-05-02 23:27:47 +02:00
#define SIN_ENT 1024
#define SIN_MAX (SIN_ENT << SPKR_SHIFT)
2009-05-02 23:03:37 +02:00
2009-05-02 23:27:47 +02:00
#define FREQ_MAX (2 << SPKR_SHIFT)
#define FREQ_HALF (FREQ_MAX >> 1)
2009-05-02 23:03:37 +02:00
struct Speaker {
2009-05-02 23:27:47 +02:00
Bitu freq_add;
Bitu freq_pos;
2009-05-02 23:03:37 +02:00
Bit16s volume;
MIXER_Channel * chan;
bool enabled;
2009-05-02 23:20:05 +02:00
bool realsound;
2009-05-02 23:27:47 +02:00
bool sinewave;
Bitu mode;
2009-05-02 23:20:05 +02:00
Bit16u buffer[SPKR_BUF];
2009-05-02 23:27:47 +02:00
Bit16s table[SIN_ENT];
2009-05-02 23:20:05 +02:00
Bitu buf_pos;
2009-05-02 23:03:37 +02:00
};
static Speaker spkr;
2009-05-02 23:20:05 +02:00
void PCSPEAKER_SetCounter(Bitu cntr,Bitu mode) {
2009-05-02 23:27:47 +02:00
spkr.mode=mode;
2009-05-02 23:20:05 +02:00
switch (mode) {
case 0:
if (cntr>72) cntr=72;
spkr.realsound=true;
if (spkr.buf_pos<SPKR_BUF) spkr.buffer[spkr.buf_pos++]=(cntr-36)*600;
break;
case 3:
2009-05-02 23:27:47 +02:00
if (spkr.sinewave) {
spkr.freq_add=(Bitu)(SIN_MAX/((float)SPKR_RATE/(PIT_TICK_RATE/(float)cntr)));
} else {
spkr.freq_add=(Bitu)(FREQ_MAX/((float)SPKR_RATE/(PIT_TICK_RATE/(float)cntr)));
}
2009-05-02 23:20:05 +02:00
break;
2009-05-02 23:27:47 +02:00
case 4:
spkr.freq_pos=(Bitu)(SPKR_RATE/(PIT_TICK_RATE/(float)cntr));
2009-05-02 23:20:05 +02:00
}
2009-05-02 23:03:37 +02:00
}
void PCSPEAKER_Enable(bool enable) {
spkr.enabled=enable;
MIXER_Enable(spkr.chan,enable);
}
static void PCSPEAKER_CallBack(Bit8u * stream,Bit32u len) {
2009-05-02 23:27:47 +02:00
switch (spkr.mode) {
case 0:
/* Generate the "RealSound" */
{
Bitu buf_add=(spkr.buf_pos<<16)/len;
Bitu buf_pos=0;
spkr.buf_pos=0;spkr.realsound=0;
while (len-->0) {
*(Bit16s*)(stream)=spkr.buffer[buf_pos >> 16];
buf_pos+=buf_add;
stream+=2;
}
break;
2009-05-02 23:20:05 +02:00
}
2009-05-02 23:27:47 +02:00
case 3:
if (spkr.sinewave) while (len-->0) {
spkr.freq_pos+=spkr.freq_add;
spkr.freq_pos&=(SIN_MAX-1);
*(Bit16s*)(stream)=spkr.table[spkr.freq_pos>>SPKR_SHIFT];
stream+=2;
} else while (len-->0) {
2009-05-02 23:20:05 +02:00
spkr.freq_pos+=spkr.freq_add;
if (spkr.freq_pos>=FREQ_MAX) spkr.freq_pos-=FREQ_MAX;
if (spkr.freq_pos>=FREQ_HALF) {
*(Bit16s*)(stream)=spkr.volume;
} else {
*(Bit16s*)(stream)=-spkr.volume;
}
stream+=2;
2009-05-02 23:03:37 +02:00
}
2009-05-02 23:27:47 +02:00
break;
case 4:
while (len-->0) {
if (spkr.freq_pos) {
*(Bit16s*)(stream)=spkr.volume;
spkr.freq_pos--;
} else {
*(Bit16s*)(stream)=-spkr.volume;
}
stream+=2;
}
break;
2009-05-02 23:03:37 +02:00
}
}
2009-05-02 23:20:05 +02:00
void PCSPEAKER_Init(Section* sec) {
MSG_Add("SPEAKER_CONFIGFILE_HELP","pcspeaker related options.\n");
2009-05-02 23:27:47 +02:00
Section_prop * section=static_cast<Section_prop *>(sec);
if(!section->Get_bool("enabled")) return;
spkr.sinewave=section->Get_bool("sinewave");
2009-05-02 23:03:37 +02:00
spkr.chan=MIXER_AddChannel(&PCSPEAKER_CallBack,SPKR_RATE,"PC-SPEAKER");
MIXER_Enable(spkr.chan,false);
MIXER_SetMode(spkr.chan,MIXER_16MONO);
spkr.volume=SPKR_VOLUME;
spkr.enabled=false;
2009-05-02 23:20:05 +02:00
spkr.realsound=false;
spkr.buf_pos=0;
2009-05-02 23:27:47 +02:00
/* Generate the sine wave */
for (Bitu i=0;i<SIN_ENT;i++) {
spkr.table[i]=(Bit16s)(sin(2*PI/SIN_ENT*i)*SPKR_VOLUME*1.5);
}
2009-05-02 23:03:37 +02:00
}