| 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 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: pcspeaker.cpp,v 1.26 2009-05-27 09:15:41 qbix79 Exp $ */ |
| 20 | |
| 21 | #include <math.h> |
| 22 | #include "dosbox.h" |
| 23 | #include "mixer.h" |
| 24 | #include "timer.h" |
| 25 | #include "setup.h" |
| 26 | #include "pic.h" |
| 27 | |
| 28 | |
| 29 | #ifndef PI |
| 30 | #define PI 3.14159265358979323846 |
| 31 | #endif |
| 32 | |
| 33 | #define SPKR_ENTRIES 1024 |
| 34 | #define SPKR_VOLUME 5000 |
| 35 | //#define SPKR_SHIFT 8 |
| 36 | #define SPKR_SPEED (float)((SPKR_VOLUME*2)/0.070f) |
| 37 | |
| 38 | enum SPKR_MODES { |
| 39 | SPKR_OFF,SPKR_ON,SPKR_PIT_OFF,SPKR_PIT_ON |
| 40 | }; |
| 41 | |
| 42 | struct DelayEntry { |
| 43 | float index; |
| 44 | float vol; |
| 45 | }; |
| 46 | |
| 47 | static struct { |
| 48 | MixerChannel * chan; |
| 49 | SPKR_MODES mode; |
| 50 | Bitu pit_mode; |
| 51 | Bitu rate; |
| 52 | |
| 53 | float pit_last; |
| 54 | float pit_new_max,pit_new_half; |
| 55 | float pit_max,pit_half; |
| 56 | float pit_index; |
| 57 | float volwant,volcur; |
| 58 | Bitu last_ticks; |
| 59 | float last_index; |
| 60 | Bitu min_tr; |
| 61 | DelayEntry entries[SPKR_ENTRIES]; |
| 62 | Bitu used; |
| 63 | } spkr; |
| 64 | |
| 65 | static void AddDelayEntry(float index,float vol) { |
| 66 | if (spkr.used==SPKR_ENTRIES) { |
| 67 | return; |
| 68 | } |
| 69 | spkr.entries[spkr.used].index=index; |
| 70 | spkr.entries[spkr.used].vol=vol; |
| 71 | spkr.used++; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | static void ForwardPIT(float newindex) { |
| 76 | float passed=(newindex-spkr.last_index); |
| 77 | float delay_base=spkr.last_index; |
| 78 | spkr.last_index=newindex; |
| 79 | switch (spkr.pit_mode) { |
| 80 | case 0: |
| 81 | return; |
| 82 | case 1: |
| 83 | return; |
| 84 | case 2: |
| 85 | while (passed>0) { |
| 86 | /* passed the initial low cycle? */ |
| 87 | if (spkr.pit_index>=spkr.pit_half) { |
| 88 | /* Start a new low cycle */ |
| 89 | if ((spkr.pit_index+passed)>=spkr.pit_max) { |
| 90 | float delay=spkr.pit_max-spkr.pit_index; |
| 91 | delay_base+=delay;passed-=delay; |
| 92 | spkr.pit_last=-SPKR_VOLUME; |
| 93 | if (spkr.mode==SPKR_PIT_ON) AddDelayEntry(delay_base,spkr.pit_last); |
| 94 | spkr.pit_index=0; |
| 95 | } else { |
| 96 | spkr.pit_index+=passed; |
| 97 | return; |
| 98 | } |
| 99 | } else { |
| 100 | if ((spkr.pit_index+passed)>=spkr.pit_half) { |
| 101 | float delay=spkr.pit_half-spkr.pit_index; |
| 102 | delay_base+=delay;passed-=delay; |
| 103 | spkr.pit_last=SPKR_VOLUME; |
| 104 | if (spkr.mode==SPKR_PIT_ON) AddDelayEntry(delay_base,spkr.pit_last); |
| 105 | spkr.pit_index=spkr.pit_half; |
| 106 | } else { |
| 107 | spkr.pit_index+=passed; |
| 108 | return; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | break; |
| 113 | //END CASE 2 |
| 114 | case 3: |
| 115 | while (passed>0) { |
| 116 | /* Determine where in the wave we're located */ |
| 117 | if (spkr.pit_index>=spkr.pit_half) { |
| 118 | if ((spkr.pit_index+passed)>=spkr.pit_max) { |
| 119 | float delay=spkr.pit_max-spkr.pit_index; |
| 120 | delay_base+=delay;passed-=delay; |
| 121 | spkr.pit_last=SPKR_VOLUME; |
| 122 | if (spkr.mode==SPKR_PIT_ON) AddDelayEntry(delay_base,spkr.pit_last); |
| 123 | spkr.pit_index=0; |
| 124 | /* Load the new count */ |
| 125 | spkr.pit_half=spkr.pit_new_half; |
| 126 | spkr.pit_max=spkr.pit_new_max; |
| 127 | } else { |
| 128 | spkr.pit_index+=passed; |
| 129 | return; |
| 130 | } |
| 131 | } else { |
| 132 | if ((spkr.pit_index+passed)>=spkr.pit_half) { |
| 133 | float delay=spkr.pit_half-spkr.pit_index; |
| 134 | delay_base+=delay;passed-=delay; |
| 135 | spkr.pit_last=-SPKR_VOLUME; |
| 136 | if (spkr.mode==SPKR_PIT_ON) AddDelayEntry(delay_base,spkr.pit_last); |
| 137 | spkr.pit_index=spkr.pit_half; |
| 138 | /* Load the new count */ |
| 139 | spkr.pit_half=spkr.pit_new_half; |
| 140 | spkr.pit_max=spkr.pit_new_max; |
| 141 | } else { |
| 142 | spkr.pit_index+=passed; |
| 143 | return; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | break; |
| 148 | //END CASE 3 |
| 149 | case 4: |
| 150 | if (spkr.pit_index<spkr.pit_max) { |
| 151 | /* Check if we're gonna pass the end this block */ |
| 152 | if (spkr.pit_index+passed>=spkr.pit_max) { |
| 153 | float delay=spkr.pit_max-spkr.pit_index; |
| 154 | delay_base+=delay;passed-=delay; |
| 155 | spkr.pit_last=-SPKR_VOLUME; |
| 156 | if (spkr.mode==SPKR_PIT_ON) AddDelayEntry(delay_base,spkr.pit_last); //No new events unless reprogrammed |
| 157 | spkr.pit_index=spkr.pit_max; |
| 158 | } else spkr.pit_index+=passed; |
| 159 | } |
| 160 | break; |
| 161 | //END CASE 4 |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void PCSPEAKER_SetCounter(Bitu cntr,Bitu mode) { |
| 166 | if (!spkr.last_ticks) { |
| 167 | if(spkr.chan) spkr.chan->Enable(true); |
| 168 | spkr.last_index=0; |
| 169 | } |
| 170 | spkr.last_ticks=PIC_Ticks; |
| 171 | float newindex=PIC_TickIndex(); |
| 172 | ForwardPIT(newindex); |
| 173 | switch (mode) { |
| 174 | case 0: /* Mode 0 one shot, used with realsound */ |
| 175 | if (spkr.mode!=SPKR_PIT_ON) return; |
| 176 | if (cntr>80) { |
| 177 | cntr=80; |
| 178 | } |
| 179 | spkr.pit_last=((float)cntr-40)*(SPKR_VOLUME/40.0f); |
| 180 | AddDelayEntry(newindex,spkr.pit_last); |
| 181 | spkr.pit_index=0; |
| 182 | break; |
| 183 | case 1: |
| 184 | if (spkr.mode!=SPKR_PIT_ON) return; |
| 185 | spkr.pit_last=SPKR_VOLUME; |
| 186 | AddDelayEntry(newindex,spkr.pit_last); |
| 187 | break; |
| 188 | case 2: /* Single cycle low, rest low high generator */ |
| 189 | spkr.pit_index=0; |
| 190 | spkr.pit_last=-SPKR_VOLUME; |
| 191 | AddDelayEntry(newindex,spkr.pit_last); |
| 192 | spkr.pit_half=(1000.0f/PIT_TICK_RATE)*1; |
| 193 | spkr.pit_max=(1000.0f/PIT_TICK_RATE)*cntr; |
| 194 | break; |
| 195 | case 3: /* Square wave generator */ |
| 196 | if (cntr<spkr.min_tr) { |
| 197 | /* skip frequencies that can't be represented */ |
| 198 | spkr.pit_last=0; |
| 199 | spkr.pit_mode=0; |
| 200 | return; |
| 201 | } |
| 202 | spkr.pit_new_max=(1000.0f/PIT_TICK_RATE)*cntr; |
| 203 | spkr.pit_new_half=spkr.pit_new_max/2; |
| 204 | break; |
| 205 | case 4: /* Software triggered strobe */ |
| 206 | spkr.pit_last=SPKR_VOLUME; |
| 207 | AddDelayEntry(newindex,spkr.pit_last); |
| 208 | spkr.pit_index=0; |
| 209 | spkr.pit_max=(1000.0f/PIT_TICK_RATE)*cntr; |
| 210 | break; |
| 211 | default: |
| 212 | #if C_DEBUG |
| 213 | LOG_MSG("Unhandled speaker mode %d",mode); |
| 214 | #endif |
| 215 | return; |
| 216 | } |
| 217 | spkr.pit_mode=mode; |
| 218 | } |
| 219 | |
| 220 | void PCSPEAKER_SetType(Bitu mode) { |
| 221 | if (!spkr.last_ticks) { |
| 222 | if(spkr.chan) spkr.chan->Enable(true); |
| 223 | spkr.last_index=0; |
| 224 | } |
| 225 | spkr.last_ticks=PIC_Ticks; |
| 226 | float newindex=PIC_TickIndex(); |
| 227 | ForwardPIT(newindex); |
| 228 | switch (mode) { |
| 229 | case 0: |
| 230 | spkr.mode=SPKR_OFF; |
| 231 | AddDelayEntry(newindex,-SPKR_VOLUME); |
| 232 | break; |
| 233 | case 1: |
| 234 | spkr.mode=SPKR_PIT_OFF; |
| 235 | AddDelayEntry(newindex,-SPKR_VOLUME); |
| 236 | break; |
| 237 | case 2: |
| 238 | spkr.mode=SPKR_ON; |
| 239 | AddDelayEntry(newindex,SPKR_VOLUME); |
| 240 | break; |
| 241 | case 3: |
| 242 | if (spkr.mode!=SPKR_PIT_ON) { |
| 243 | AddDelayEntry(newindex,spkr.pit_last); |
| 244 | } |
| 245 | spkr.mode=SPKR_PIT_ON; |
| 246 | break; |
| 247 | }; |
| 248 | } |
| 249 | |
| 250 | static void PCSPEAKER_CallBack(Bitu len) { |
| 251 | Bit16s * stream=(Bit16s*)MixTemp; |
| 252 | ForwardPIT(1); |
| 253 | spkr.last_index=0; |
| 254 | Bitu count=len; |
| 255 | Bitu pos=0; |
| 256 | float sample_base=0; |
| 257 | float sample_add=(1.0001f)/len; |
| 258 | while (count--) { |
| 259 | float index=sample_base; |
| 260 | sample_base+=sample_add; |
| 261 | float end=sample_base; |
| 262 | double value=0; |
| 263 | while(index<end) { |
| 264 | /* Check if there is an upcoming event */ |
| 265 | if (spkr.used && spkr.entries[pos].index<=index) { |
| 266 | spkr.volwant=spkr.entries[pos].vol; |
| 267 | pos++;spkr.used--; |
| 268 | continue; |
| 269 | } |
| 270 | float vol_end; |
| 271 | if (spkr.used && spkr.entries[pos].index<end) { |
| 272 | vol_end=spkr.entries[pos].index; |
| 273 | } else vol_end=end; |
| 274 | float vol_len=vol_end-index; |
| 275 | /* Check if we have to slide the volume */ |
| 276 | float vol_diff=spkr.volwant-spkr.volcur; |
| 277 | if (vol_diff == 0) { |
| 278 | value+=spkr.volcur*vol_len; |
| 279 | index+=vol_len; |
| 280 | } else { |
| 281 | /* Check how long it will take to goto new level */ |
| 282 | float vol_time=fabs(vol_diff)/SPKR_SPEED; |
| 283 | if (vol_time<=vol_len) { |
| 284 | /* Volume reaches endpoint in this block, calc until that point */ |
| 285 | value+=vol_time*spkr.volcur; |
| 286 | value+=vol_time*vol_diff/2; |
| 287 | index+=vol_time; |
| 288 | spkr.volcur=spkr.volwant; |
| 289 | } else { |
| 290 | /* Volume still not reached in this block */ |
| 291 | value+=spkr.volcur*vol_len; |
| 292 | if (vol_diff<0) { |
| 293 | value-=(SPKR_SPEED*vol_len*vol_len)/2; |
| 294 | spkr.volcur-=SPKR_SPEED*vol_len; |
| 295 | } else { |
| 296 | value+=(SPKR_SPEED*vol_len*vol_len)/2; |
| 297 | spkr.volcur+=SPKR_SPEED*vol_len; |
| 298 | } |
| 299 | index+=vol_len; |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | *stream++=(Bit16s)(value/sample_add); |
| 304 | } |
| 305 | if(spkr.chan) spkr.chan->AddSamples_m16(len,(Bit16s*)MixTemp); |
| 306 | |
| 307 | //Turn off speaker after 10 seconds of idle or one second idle when in off mode |
| 308 | bool turnoff = false; |
| 309 | Bitu test_ticks = PIC_Ticks; |
| 310 | if ((spkr.last_ticks + 10000) < test_ticks) turnoff = true; |
| 311 | if((spkr.mode == SPKR_OFF) && ((spkr.last_ticks + 1000) < test_ticks)) turnoff = true; |
| 312 | |
| 313 | if(turnoff){ |
| 314 | if(spkr.volwant == 0) { |
| 315 | spkr.last_ticks = 0; |
| 316 | if(spkr.chan) spkr.chan->Enable(false); |
| 317 | } else { |
| 318 | if(spkr.volwant > 0) spkr.volwant--; else spkr.volwant++; |
| 319 | |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | } |
| 324 | class PCSPEAKER:public Module_base { |
| 325 | private: |
| 326 | MixerObject MixerChan; |
| 327 | public: |
| 328 | PCSPEAKER(Section* configuration):Module_base(configuration){ |
| 329 | spkr.chan=0; |
| 330 | Section_prop * section=static_cast<Section_prop *>(configuration); |
| 331 | if(!section->Get_bool("pcspeaker")) return; |
| 332 | spkr.mode=SPKR_OFF; |
| 333 | spkr.last_ticks=0; |
| 334 | spkr.last_index=0; |
| 335 | spkr.rate=section->Get_int("pcrate"); |
| 336 | spkr.pit_max=(1000.0f/PIT_TICK_RATE)*65535; |
| 337 | spkr.pit_half=spkr.pit_max/2; |
| 338 | spkr.pit_new_max=spkr.pit_max; |
| 339 | spkr.pit_new_half=spkr.pit_half; |
| 340 | spkr.pit_index=0; |
| 341 | spkr.min_tr=(PIT_TICK_RATE+spkr.rate/2-1)/(spkr.rate/2); |
| 342 | spkr.used=0; |
| 343 | /* Register the sound channel */ |
| 344 | spkr.chan=MixerChan.Install(&PCSPEAKER_CallBack,spkr.rate,"SPKR"); |
| 345 | } |
| 346 | ~PCSPEAKER(){ |
| 347 | Section_prop * section=static_cast<Section_prop *>(m_configuration); |
| 348 | if(!section->Get_bool("pcspeaker")) return; |
| 349 | } |
| 350 | }; |
| 351 | static PCSPEAKER* test; |
| 352 | |
| 353 | void PCSPEAKER_ShutDown(Section* sec){ |
| 354 | delete test; |
| 355 | } |
| 356 | |
| 357 | void PCSPEAKER_Init(Section* sec) { |
| 358 | test = new PCSPEAKER(sec); |
| 359 | sec->AddDestroyFunction(&PCSPEAKER_ShutDown,true); |
| 360 | } |