libsidplayfp 2.6.0
SID.h
1/*
2 * This file is part of libsidplayfp, a SID player engine.
3 *
4 * Copyright 2011-2016 Leandro Nini <drfiemost@users.sourceforge.net>
5 * Copyright 2007-2010 Antti Lankila
6 * Copyright 2004 Dag Lem <resid@nimrod.no>
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#ifndef SIDFP_H
24#define SIDFP_H
25
26#include <memory>
27
28#include "siddefs-fp.h"
29
30#include "sidcxx11.h"
31
32namespace reSIDfp
33{
34
35class Filter;
36class Filter6581;
37class Filter8580;
38class ExternalFilter;
39class Potentiometer;
40class Voice;
41class Resampler;
42
47{
48private:
49 const char* message;
50
51public:
52 SIDError(const char* msg) :
53 message(msg) {}
54 const char* getMessage() const { return message; }
55};
56
60class SID
61{
62private:
64 Filter* filter;
65
67 std::unique_ptr<Filter6581> const filter6581;
68
70 std::unique_ptr<Filter8580> const filter8580;
71
76 std::unique_ptr<ExternalFilter> const externalFilter;
77
79 std::unique_ptr<Resampler> resampler;
80
82 std::unique_ptr<Potentiometer> const potX;
83
85 std::unique_ptr<Potentiometer> const potY;
86
88 std::unique_ptr<Voice> voice[3];
89
91 int scaleFactor;
92
94 int busValueTtl;
95
97 int modelTTL;
98
100 unsigned int nextVoiceSync;
101
103 ChipModel model;
104
106 unsigned char busValue;
107
109 bool muted[3];
110
116 float envDAC[256];
117
123 float oscDAC[4096];
124
125private:
131 void ageBusValue(unsigned int n);
132
138 int output() const;
139
146 void voiceSync(bool sync);
147
148public:
149 SID();
150 ~SID();
151
158 void setChipModel(ChipModel model);
159
163 ChipModel getChipModel() const { return model; }
164
168 void reset();
169
178 void input(int value);
179
200 unsigned char read(int offset);
201
208 void write(int offset, unsigned char value);
209
216 void mute(int channel, bool enable) { muted[channel] = enable; }
217
243 void setSamplingParameters(double clockFrequency, SamplingMethod method, double samplingFrequency, double highestAccurateFrequency);
244
252 int clock(unsigned int cycles, short* buf);
253
264 void clockSilent(unsigned int cycles);
265
271 void setFilter6581Curve(double filterCurve);
272
278 void setFilter8580Curve(double filterCurve);
279
285 void enableFilter(bool enable);
286};
287
288} // namespace reSIDfp
289
290#if RESID_INLINING || defined(SID_CPP)
291
292#include <algorithm>
293
294#include "Filter.h"
295#include "ExternalFilter.h"
296#include "Voice.h"
297#include "resample/Resampler.h"
298
299namespace reSIDfp
300{
301
302RESID_INLINE
303void SID::ageBusValue(unsigned int n)
304{
305 if (likely(busValueTtl != 0))
306 {
307 busValueTtl -= n;
308
309 if (unlikely(busValueTtl <= 0))
310 {
311 busValue = 0;
312 busValueTtl = 0;
313 }
314 }
315}
316
317RESID_INLINE
318int SID::output() const
319{
320 const int v1 = voice[0]->output(voice[2]->wave());
321 const int v2 = voice[1]->output(voice[0]->wave());
322 const int v3 = voice[2]->output(voice[1]->wave());
323
324 const int input = (scaleFactor * static_cast<unsigned int>(filter->clock(v1, v2, v3))) / 2;
325
326 return externalFilter->clock(input);
327}
328
329
330RESID_INLINE
331int SID::clock(unsigned int cycles, short* buf)
332{
333 ageBusValue(cycles);
334 int s = 0;
335
336 while (cycles != 0)
337 {
338 unsigned int delta_t = std::min(nextVoiceSync, cycles);
339
340 if (likely(delta_t > 0))
341 {
342 for (unsigned int i = 0; i < delta_t; i++)
343 {
344 // clock waveform generators
345 voice[0]->wave()->clock();
346 voice[1]->wave()->clock();
347 voice[2]->wave()->clock();
348
349 // clock envelope generators
350 voice[0]->envelope()->clock();
351 voice[1]->envelope()->clock();
352 voice[2]->envelope()->clock();
353
354 if (unlikely(resampler->input(output())))
355 {
356 buf[s++] = resampler->getOutput();
357 }
358 }
359
360 cycles -= delta_t;
361 nextVoiceSync -= delta_t;
362 }
363
364 if (unlikely(nextVoiceSync == 0))
365 {
366 voiceSync(true);
367 }
368 }
369
370 return s;
371}
372
373} // namespace reSIDfp
374
375#endif
376
377#endif
Definition Filter.h:33
virtual unsigned short clock(int v1, int v2, int v3)=0
Definition SID.h:47
Definition SID.h:61
void setChipModel(ChipModel model)
Definition SID.cpp:208
void input(int value)
Definition SID.cpp:293
unsigned char read(int offset)
Definition SID.cpp:299
void setSamplingParameters(double clockFrequency, SamplingMethod method, double samplingFrequency, double highestAccurateFrequency)
Definition SID.cpp:453
void write(int offset, unsigned char value)
Definition SID.cpp:334
ChipModel getChipModel() const
Definition SID.h:163
void setFilter6581Curve(double filterCurve)
Definition SID.cpp:158
void setFilter8580Curve(double filterCurve)
Definition SID.cpp:163
void enableFilter(bool enable)
Definition SID.cpp:168
void reset()
Definition SID.cpp:272
int clock(unsigned int cycles, short *buf)
Definition SID.h:331
void clockSilent(unsigned int cycles)
Definition SID.cpp:472
void mute(int channel, bool enable)
Definition SID.h:216