DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
include/mixer.h
00001 /*
00002  *  Copyright (C) 2002-2015  The DOSBox Team
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 
00020 #ifndef DOSBOX_MIXER_H
00021 #define DOSBOX_MIXER_H
00022 
00023 #include <sstream>
00024 
00025 #ifndef DOSBOX_DOSBOX_H
00026 #include "dosbox.h"
00027 #endif
00028 
00029 typedef void (*MIXER_MixHandler)(Bit8u * sampdate,Bit32u len);
00030 typedef void (*MIXER_Handler)(Bitu len);
00031 
00032 #define MIXER_BUFSIZE (16*1024)
00033 #define MIXER_BUFMASK (MIXER_BUFSIZE-1)
00034 extern Bit8u MixTemp[MIXER_BUFSIZE];
00035 
00036 #define MAX_AUDIO ((1<<(16-1))-1)
00037 #define MIN_AUDIO -(1<<(16-1))
00038 
00039 #define LOWPASS_ORDER 8
00040 
00041 class MixerChannel {
00042 public:
00043         void SetVolume(float _left,float _right);
00044         void SetScale( float f );
00045         void UpdateVolume(void);
00046         void SetLowpassFreq(Bitu _freq,unsigned int order=2); // _freq / 1 Hz. call with _freq == 0 to disable
00047         void SetSlewFreq(Bitu _freq); // denominator provided by call to SetFreq. call with _freq == 0 to disable
00048         void SetFreq(Bitu _freq,Bitu _den=1U);
00049         void Mix(Bitu whole,Bitu frac);
00050         void AddSilence(void);                  //Fill up until needed
00051         void EndFrame(Bitu samples);
00052 
00053         void lowpassUpdate();
00054         Bit32s lowpassStep(Bit32s in,const unsigned int iteration,const unsigned int channel);
00055         void lowpassProc(Bit32s ch[2]);
00056 
00057         template<class Type,bool stereo,bool signeddata,bool nativeorder,bool lowpass>
00058         void loadCurrentSample(Bitu &len, const Type* &data);
00059 
00060         template<class Type,bool stereo,bool signeddata,bool nativeorder>
00061         void AddSamples(Bitu len, const Type* data);
00062         double timeSinceLastSample(void);
00063 
00064         bool runSampleInterpolation(const Bitu upto);
00065 
00066         void updateSlew(void);
00067         void padFillSampleInterpolation(const Bitu upto);
00068         void finishSampleInterpolation(const Bitu upto);
00069         void AddSamples_m8(Bitu len, const Bit8u * data);
00070         void AddSamples_s8(Bitu len, const Bit8u * data);
00071         void AddSamples_m8s(Bitu len, const Bit8s * data);
00072         void AddSamples_s8s(Bitu len, const Bit8s * data);
00073         void AddSamples_m16(Bitu len, const Bit16s * data);
00074         void AddSamples_s16(Bitu len, const Bit16s * data);
00075         void AddSamples_m16u(Bitu len, const Bit16u * data);
00076         void AddSamples_s16u(Bitu len, const Bit16u * data);
00077         void AddSamples_m32(Bitu len, const Bit32s * data);
00078         void AddSamples_s32(Bitu len, const Bit32s * data);
00079         void AddSamples_m16_nonnative(Bitu len, const Bit16s * data);
00080         void AddSamples_s16_nonnative(Bitu len, const Bit16s * data);
00081         void AddSamples_m16u_nonnative(Bitu len, const Bit16u * data);
00082         void AddSamples_s16u_nonnative(Bitu len, const Bit16u * data);
00083         void AddSamples_m32_nonnative(Bitu len, const Bit32s * data);
00084         void AddSamples_s32_nonnative(Bitu len, const Bit32s * data);
00085 
00086         void FillUp(void);
00087         void Enable(bool _yesno);
00088 
00089         void SaveState( std::ostream& stream );
00090         void LoadState( std::istream& stream );
00091 
00092         MIXER_Handler handler;
00093         float volmain[2];
00094         float scale;
00095         Bit32s volmul[2];
00096         Bit32s lowpass[LOWPASS_ORDER][2];       // lowpass filter
00097         Bit32s lowpass_alpha;                   // "alpha" multiplier for lowpass (16.16 fixed point)
00098         Bitu lowpass_freq;
00099         unsigned int lowpass_order;
00100         bool lowpass_on_load;                   // apply lowpass on sample load (if source rate > mixer rate)
00101         bool lowpass_on_out;                    // apply lowpass on rendered output (if source rate <= mixer rate)
00102         unsigned int freq_f,freq_fslew;
00103         unsigned int freq_nslew,freq_nslew_want;
00104         unsigned int rendering_to_n,rendering_to_d;
00105         unsigned int rend_n,rend_d;
00106         unsigned int freq_n,freq_d,freq_d_orig;
00107         bool current_loaded;
00108         Bit32s current[2],last[2],delta[2],max_change;
00109         Bit32s msbuffer[2048][2];               // more than enough for 1ms of audio, at mixer sample rate
00110         Bits last_sample_write;
00111         Bitu msbuffer_o;
00112         Bitu msbuffer_i;
00113         const char * name;
00114         bool enabled;
00115         MixerChannel * next;
00116 };
00117 
00118 MixerChannel * MIXER_AddChannel(MIXER_Handler handler,Bitu freq,const char * name);
00119 MixerChannel * MIXER_FindChannel(const char * name);
00120 /* Find the device you want to delete with findchannel "delchan gets deleted" */
00121 void MIXER_DelChannel(MixerChannel* delchan); 
00122 
00123 /* Object to maintain a mixerchannel; As all objects it registers itself with create
00124  * and removes itself when destroyed. */
00125 class MixerObject{
00126 private:
00127         bool installed;
00128         char m_name[32];
00129 public:
00130         MixerObject():installed(false){};
00131         MixerChannel* Install(MIXER_Handler handler,Bitu freq,const char * name);
00132         ~MixerObject();
00133 };
00134 
00135 
00136 /* PC Speakers functions, tightly related to the timer functions */
00137 void PCSPEAKER_SetCounter(Bitu cntr,Bitu mode);
00138 void PCSPEAKER_SetType(bool pit_clock_gate_enabled, bool pit_output_enabled);
00139 void PCSPEAKER_SetPITControl(Bitu mode);
00140 
00141 #endif