libsidplayfp  1.0.1
mixer.h
1 /*
2  * This file is part of libsidplayfp, a SID player engine.
3  *
4  * Copyright 2011-2013 Leandro Nini <drfiemost@users.sourceforge.net>
5  * Copyright 2007-2010 Antti Lankila
6  * Copyright (C) 2000 Simon White
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
24 #ifndef MIXER_H
25 #define MIXER_H
26 
27 #include <stdlib.h>
28 
29 #include "event.h"
30 #include "sidbuilder.h"
31 
35 class Mixer : private Event
36 {
37 private:
41  static const int MIXER_EVENT_RATE = 5000;
42 
43 public:
45  static const int_least32_t VOLUME_MAX = 1024;
46 
47 private:
51  EventContext &event_context;
52 
53  sidemu *m_chip1;
54  sidemu *m_chip2;
55 
56  int oldRandomValue;
57  int m_fastForwardFactor;
58 
59  int_least32_t m_leftVolume;
60  int_least32_t m_rightVolume;
61  bool m_stereo;
62 
63  // Mixer settings
64  uint_least32_t m_sampleCount;
65  uint_least32_t m_sampleIndex;
66  short *m_sampleBuffer;
67 
68 private:
69  int triangularDithering()
70  {
71  const int prevValue = oldRandomValue;
72  oldRandomValue = rand() & (VOLUME_MAX-1);
73  return oldRandomValue - prevValue;
74  }
75 
76 public:
82  Mixer(EventContext *context) :
83  Event("Mixer"),
84  event_context(*context),
85  oldRandomValue(0),
86  m_fastForwardFactor(1),
87  m_sampleCount(0) {}
88 
92  void event();
93 
94  void reset() { event_context.schedule(*this, MIXER_EVENT_RATE, EVENT_CLOCK_PHI2); }
95 
96  void begin(short *buffer, uint_least32_t count);
97 
98  void setSids(sidemu *chip1, sidemu *chip2);
99  bool setFastForward(int ff);
100  void setVolume(int_least32_t left, int_least32_t right);
101  void setStereo(bool stereo) { m_stereo = stereo; }
102 
103  bool notFinished() const { return m_sampleIndex != m_sampleCount; }
104  uint_least32_t samplesGenerated() const { return m_sampleIndex; }
105  uint_least32_t sampleCount() const { return m_sampleCount; }
106 };
107 
108 #endif // MIXER_H