libsidplayfp  1.0.3
mixer.h
00001 /*
00002  * This file is part of libsidplayfp, a SID player engine.
00003  *
00004  * Copyright 2011-2013 Leandro Nini <drfiemost@users.sourceforge.net>
00005  * Copyright 2007-2010 Antti Lankila
00006  * Copyright (C) 2000 Simon White
00007  *
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00021  */
00022 
00023 
00024 #ifndef MIXER_H
00025 #define MIXER_H
00026 
00027 #include <stdlib.h>
00028 
00029 #include "event.h"
00030 #include "sidbuilder.h"
00031 
00035 class Mixer : private Event
00036 {
00037 private:
00041     static const int MIXER_EVENT_RATE = 5000;
00042 
00043 public:
00045     static const int_least32_t VOLUME_MAX = 1024;
00046 
00047 private:
00051     EventContext &event_context;
00052 
00053     sidemu *m_chip1;
00054     sidemu *m_chip2;
00055 
00056     int oldRandomValue;
00057     int m_fastForwardFactor;
00058 
00059     int_least32_t  m_leftVolume;
00060     int_least32_t  m_rightVolume;
00061     bool           m_stereo;
00062 
00063     // Mixer settings
00064     uint_least32_t m_sampleCount;
00065     uint_least32_t m_sampleIndex;
00066     short         *m_sampleBuffer;
00067 
00068 private:
00069     int triangularDithering()
00070     {
00071         const int prevValue = oldRandomValue;
00072         oldRandomValue = rand() & (VOLUME_MAX-1);
00073         return oldRandomValue - prevValue;
00074     }
00075 
00076 public:
00082     Mixer(EventContext *context) :
00083         Event("Mixer"),
00084         event_context(*context),
00085         oldRandomValue(0),
00086         m_fastForwardFactor(1),
00087         m_sampleCount(0) {}
00088 
00092     void event();
00093 
00094     void reset() { event_context.schedule(*this, MIXER_EVENT_RATE, EVENT_CLOCK_PHI1); }
00095 
00096     void begin(short *buffer, uint_least32_t count);
00097 
00098     void setSids(sidemu *chip1, sidemu *chip2);
00099     bool setFastForward(int ff);
00100     void setVolume(int_least32_t left, int_least32_t right);
00101     void setStereo(bool stereo) { m_stereo = stereo; }
00102 
00103     bool notFinished() const { return m_sampleIndex != m_sampleCount; }
00104     uint_least32_t samplesGenerated() const { return m_sampleIndex; }
00105     uint_least32_t sampleCount() const { return m_sampleCount; }
00106 };
00107 
00108 #endif // MIXER_H