1 // ---------------------------------------------------------------------------
2 // FM Sound Generator - Core Unit
3 // Copyright (C) cisc 1998, 2003.
4 // ---------------------------------------------------------------------------
5 // $Id: fmgen.cpp,v 1.50 2003/09/10 13:19:34 cisc Exp $
6 // ---------------------------------------------------------------------------
8 // FM sound generator for M.A.M.E., written by Tatsuyuki Satoh.
11 // OPNB
\82Ì CSM
\83\82\81[
\83h(
\8ed
\97l
\82ª
\82æ
\82
\82í
\82©
\82ç
\82È
\82¢)
14 //
\81EAR!=31
\82Å SSGEC
\82ð
\8eg
\82¤
\82Æ
\94g
\8c`
\82ª
\8eÀ
\8dÛ
\82Æ
\88Ù
\82È
\82é
\89Â
\94\
\90«
\82 \82è
17 // Tatsuyuki Satoh
\82³
\82ñ(fm.c)
18 // Hiromitsu Shioya
\82³
\82ñ(ADPCM-A)
19 // DMP-SOFT.
\82³
\82ñ(OPNB)
20 // KAJA
\82³
\82ñ(test program)
21 //
\82Ù
\82©
\8cf
\8e¦
\94Â
\93\99\82Å
\97l
\81X
\82È
\82²
\8f\95\8c¾
\81C
\82²
\8ex
\89\87\82ð
\82¨
\8añ
\82¹
\82¢
\82½
\82¾
\82¢
\82½
\8aF
\97l
\82É
22 // ---------------------------------------------------------------------------
29 #include "../../fileio.h"
31 #define LOGNAME "fmgen"
33 // ---------------------------------------------------------------------------
35 #define FM_EG_BOTTOM 955
37 // ---------------------------------------------------------------------------
42 const uint8 Operator::notetable[128] =
44 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 3, 3, 3, 3, 3, 3,
45 4, 4, 4, 4, 4, 4, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7,
46 8, 8, 8, 8, 8, 8, 8, 9, 10, 11, 11, 11, 11, 11, 11, 11,
47 12, 12, 12, 12, 12, 12, 12, 13, 14, 15, 15, 15, 15, 15, 15, 15,
48 16, 16, 16, 16, 16, 16, 16, 17, 18, 19, 19, 19, 19, 19, 19, 19,
49 20, 20, 20, 20, 20, 20, 20, 21, 22, 23, 23, 23, 23, 23, 23, 23,
50 24, 24, 24, 24, 24, 24, 24, 25, 26, 27, 27, 27, 27, 27, 27, 27,
51 28, 28, 28, 28, 28, 28, 28, 29, 30, 31, 31, 31, 31, 31, 31, 31,
54 const int8 Operator::dttable[256] =
56 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
58 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4,
59 4, 6, 6, 6, 8, 8, 8, 10, 10, 12, 12, 14, 16, 16, 16, 16,
60 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 8, 8, 8, 10,
61 10, 12, 12, 14, 16, 16, 18, 20, 22, 24, 26, 28, 32, 32, 32, 32,
62 4, 4, 4, 4, 4, 6, 6, 6, 8, 8, 8, 10, 10, 12, 12, 14,
63 16, 16, 18, 20, 22, 24, 26, 28, 32, 34, 38, 40, 44, 44, 44, 44,
64 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
65 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
66 0, 0, 0, 0, -2, -2, -2, -2, -2, -2, -2, -2, -4, -4, -4, -4,
67 -4, -6, -6, -6, -8, -8, -8,-10,-10,-12,-12,-14,-16,-16,-16,-16,
68 -2, -2, -2, -2, -4, -4, -4, -4, -4, -6, -6, -6, -8, -8, -8,-10,
69 -10,-12,-12,-14,-16,-16,-18,-20,-22,-24,-26,-28,-32,-32,-32,-32,
70 -4, -4, -4, -4, -4, -6, -6, -6, -8, -8, -8,-10,-10,-12,-12,-14,
71 -16,-16,-18,-20,-22,-24,-26,-28,-32,-34,-38,-40,-44,-44,-44,-44,
74 const int8 Operator::decaytable1[64][8] =
76 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77 1, 1, 1, 1, 1,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 1, 1, 0,
98 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0,
99 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0,
100 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1,
101 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 2, 1, 2, 2, 2, 1,
102 2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 4, 2, 2, 2,
103 4, 2, 4, 2, 4, 2, 4, 2, 4, 4, 4, 2, 4, 4, 4, 2,
104 4, 4, 4, 4, 4, 4, 4, 4, 8, 4, 4, 4, 8, 4, 4, 4,
105 8, 4, 8, 4, 8, 4, 8, 4, 8, 8, 8, 4, 8, 8, 8, 4,
106 16,16,16,16,16,16,16,16, 16,16,16,16,16,16,16,16,
107 16,16,16,16,16,16,16,16, 16,16,16,16,16,16,16,16,
110 const int Operator::decaytable2[16] =
112 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2047, 2047, 2047, 2047, 2047
115 const int8 Operator::attacktable[64][8] =
117 -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
118 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
119 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
120 4, 4, 4,-1, 4, 4, 4,-1, 4, 4, 4,-1, 4, 4, 4,-1,
121 4,-1, 4,-1, 4,-1, 4,-1, 4, 4, 4,-1, 4,-1, 4,-1,
122 4, 4, 4,-1, 4, 4, 4,-1, 4, 4, 4, 4, 4, 4, 4,-1,
123 4,-1, 4,-1, 4,-1, 4,-1, 4, 4, 4,-1, 4,-1, 4,-1,
124 4, 4, 4,-1, 4, 4, 4,-1, 4, 4, 4, 4, 4, 4, 4,-1,
125 4,-1, 4,-1, 4,-1, 4,-1, 4, 4, 4,-1, 4,-1, 4,-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, 2, 1, 1, 1, 2, 1, 1, 1, 2,
147 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0,
148 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0,
151 const int Operator::ssgenvtable[8][2][3][2] =
153 1, 1, 1, 1, 1, 1, // 08
154 0, 1, 1, 1, 1, 1, // 08 56~
155 0, 1, 2, 0, 2, 0, // 09
156 0, 1, 2, 0, 2, 0, // 09
157 1,-1, 0, 1, 1,-1, // 10
158 0, 1, 1,-1, 0, 1, // 10 60~
159 1,-1, 0, 0, 0, 0, // 11
160 0, 1, 0, 0, 0, 0, // 11 60~
161 2,-1, 2,-1, 2,-1, // 12
162 1,-1, 2,-1, 2,-1, // 12 56~
163 1,-1, 0, 0, 0, 0, // 13
164 1,-1, 0, 0, 0, 0, // 13
165 0, 1, 1,-1, 0, 1, // 14
166 1,-1, 0, 1, 1,-1, // 14 60~
167 0, 1, 2, 0, 2, 0, // 15
168 1,-1, 2, 0, 2, 0, // 15 60~
171 // fixed equasion-based tables
172 int pmtable[2][8][FM_LFOENTS];
173 uint volatile amtable[2][4][FM_LFOENTS];
175 static bool tablemade = false;
181 // ---------------------------------------------------------------------------
182 //
\83e
\81[
\83u
\83\8b\8dì
\90¬
193 static const double pms[2][8] =
195 { 0, 1/360., 2/360., 3/360., 4/360., 6/360., 12/360., 24/360., }, // OPNA
196 // { 0, 1/240., 2/240., 4/240., 10/240., 20/240., 80/240., 140/240., }, // OPM
197 { 0, 1/480., 2/480., 4/480., 10/480., 20/480., 80/480., 140/480., }, // OPM
198 // { 0, 1/960., 2/960., 4/960., 10/960., 20/960., 80/960., 140/960., }, // OPM
200 // 3 6, 12 30 60 240 420 / 720
202 // lfofref[level * max * wave];
203 // pre = lfofref[level][pms * wave >> 8];
204 static const volatile uint8 amt[2][4] =
206 { 31, 6, 4, 3 }, // OPNA
207 { 31, 2, 1, 0 }, // OPM
210 for (int type = 0; type < 2; type++)
214 double pmb = pms[type][i];
215 for (int j=0; j<FM_LFOENTS; j++)
217 // double v = pow(2.0, pmb * (2 * j - FM_LFOENTS+1) / (FM_LFOENTS-1));
218 double w = 0.6 * pmb * sin(2 * j * 3.14159265358979323846 / FM_LFOENTS) + 1;
219 // pmtable[type][i][j] = int(0x10000 * (v - 1));
221 pmtable[type][i][j] = int(0x10000 * (w - 1));
223 // pmtable[type][i][j] = int(0x10000 * (v - 1));
225 // printf("pmtable[%d][%d][%.2x] = %5d %7.5f %7.5f\n", type, i, j, pmtable[type][i][j], v, w);
230 for (int j=0; j<FM_LFOENTS; j++)
232 amtable[type][i][j] = (((j * 4) >> amt[type][i]) * 2) << 2;
239 // ---------------------------------------------------------------------------
240 //
\83`
\83b
\83v
\93à
\82Å
\8b¤
\92Ê
\82È
\95\94\95ª
243 : ratio_(0), aml_(0), pml_(0), pmv_(0), optype_(typeN)
247 //
\83N
\83\8d\83b
\83N
\81E
\83T
\83\93\83v
\83\8a\83\93\83O
\83\8c\81[
\83g
\94ä
\82É
\88Ë
\91¶
\82·
\82é
\83e
\81[
\83u
\83\8b\82ð
\8dì
\90¬
248 void Chip::SetRatio(uint ratio)
257 void Chip::MakeTable()
262 static const float dt2lv[4] = { 1.f, 1.414f, 1.581f, 1.732f };
265 assert(2 + FM_RATIOBITS - FM_PGBITS >= 0);
266 double rr = dt2lv[h] * double(ratio_);
269 int mul = l ? l * 2 : 1;
270 multable_[h][l] = uint(mul * rr);
275 // ---------------------------------------------------------------------------
276 //
\83X
\83e
\81[
\83g
\83Z
\81[
\83u
278 #define CHIP_STATE_VERSION 1
280 void Chip::SaveState(void *f)
282 FILEIO *state_fio = (FILEIO *)f;
284 state_fio->FputUint32(CHIP_STATE_VERSION);
286 state_fio->FputUint32(ratio_);
287 state_fio->FputUint32(aml_);
288 state_fio->FputUint32(pml_);
289 state_fio->FputInt32(pmv_);
292 bool Chip::LoadState(void *f)
294 FILEIO *state_fio = (FILEIO *)f;
296 if(state_fio->FgetUint32() != CHIP_STATE_VERSION) {
299 ratio_ = state_fio->FgetUint32();
300 aml_ = state_fio->FgetUint32();
301 pml_ = state_fio->FgetUint32();
302 pmv_ = state_fio->FgetInt32();
307 // ---------------------------------------------------------------------------
310 bool FM::Operator::tablehasmade = false;
311 uint FM::Operator::sinetable[1024];
312 int32 FM::Operator::cltable[FM_CLENTS];
315 FM::Operator::Operator()
322 ar_ = dr_ = sr_ = rr_ = key_scale_rate_ = 0;
323 ams_ = amtable[0][0];
341 void FM::Operator::Reset()
344 tl_ = tl_latch_ = 127;
356 param_changed_ = true;
360 void Operator::MakeTable()
362 //
\91Î
\90\94\83e
\81[
\83u
\83\8b\82Ì
\8dì
\90¬
363 assert(FM_CLENTS >= 256);
367 for (i=0; i<256; i++)
369 int v = int(floor(pow(2., 13. - i / 256.)));
374 while (p < cltable + FM_CLENTS)
380 // for (i=0; i<13*256; i++)
381 // printf("%4d, %d, %d\n", i, cltable[i*2], cltable[i*2+1]);
383 //
\83T
\83C
\83\93\83e
\81[
\83u
\83\8b\82Ì
\8dì
\90¬
384 double log2 = log(2.);
385 for (i=0; i<FM_OPSINENTS/2; i++)
387 double r = (i * 2 + 1) * FM_PI / FM_OPSINENTS;
388 double q = -256 * log(sin(r)) / log2;
389 uint s = (int) (floor(q + 0.5)) + 1;
390 // printf("%d, %d\n", s, cltable[s * 2] / 8);
391 sinetable[i] = s * 2 ;
392 sinetable[FM_OPSINENTS / 2 + i] = s * 2 + 1;
395 ::FM::MakeLFOTable();
402 inline void FM::Operator::SetDPBN(uint dp, uint bn)
404 dp_ = dp, bn_ = bn; param_changed_ = true;
410 void Operator::Prepare()
414 param_changed_ = false;
416 pg_diff_ = (dp_ + dttable[detune_ + bn_]) * chip_->GetMulValue(detune2_, multiple_);
417 pg_diff_ >>= (2 + FM_RATIOBITS - FM_PGBITS);
418 pg_diff_lfo_ = pg_diff_ >> 11;
421 key_scale_rate_ = bn_ >> (3-ks_);
422 tl_out_ = mute_ ? 0x3ff : tl_ * 8;
427 SetEGRate(ar_ ? Min(63, ar_ + key_scale_rate_) : 0);
430 SetEGRate(dr_ ? Min(63, dr_ + key_scale_rate_) : 0);
431 eg_level_on_next_phase_ = sl_ * 8;
434 SetEGRate(sr_ ? Min(63, sr_ + key_scale_rate_) : 0);
437 SetEGRate(Min(63, rr_ + key_scale_rate_));
442 if (ssg_type_ && (eg_phase_ != release))
444 int m = ar_ >= (uint)((ssg_type_ == 8 || ssg_type_ == 12) ? 56 : 60);
446 if (ssg_phase_ == -1) // XXX quick fix
448 assert(0 <= ssg_phase_ && ssg_phase_ <= 2);
449 const int* table = ssgenvtable[ssg_type_ & 7][m][ssg_phase_];
451 ssg_offset_ = table[0] * 0x200;
452 ssg_vector_ = table[1];
455 ams_ = amtable[type_][amon_ ? (ms_ >> 4) & 3 : 0];
461 // envelop
\82Ì eg_phase_
\95Ï
\8dX
462 void Operator::ShiftPhase(EGPhase nextphase)
466 case attack: // Attack Phase
470 ssg_phase_ = ssg_phase_ + 1;
474 int m = ar_ >= (uint)((ssg_type_ == 8 || ssg_type_ == 12) ? 56 : 60);
476 assert(0 <= ssg_phase_ && ssg_phase_ <= 2);
477 const int* table = ssgenvtable[ssg_type_ & 7][m][ssg_phase_];
479 ssg_offset_ = table[0] * 0x200;
480 ssg_vector_ = table[1];
482 if ((ar_ + key_scale_rate_) < 62)
484 SetEGRate(ar_ ? Min(63, ar_ + key_scale_rate_) : 0);
488 case decay: // Decay Phase
492 eg_level_on_next_phase_ = ssg_type_ ? Min(sl_ * 8, 0x200) : sl_ * 8;
494 SetEGRate(dr_ ? Min(63, dr_ + key_scale_rate_) : 0);
498 case sustain: // Sustain Phase
500 eg_level_on_next_phase_ = ssg_type_ ? 0x200 : 0x400;
502 SetEGRate(sr_ ? Min(63, sr_ + key_scale_rate_) : 0);
506 case release: // Release Phase
509 eg_level_ = eg_level_ * ssg_vector_ + ssg_offset_;
513 if (eg_phase_ == attack || (eg_level_ < FM_EG_BOTTOM)) //0x400/* && eg_phase_ != off*/))
515 eg_level_on_next_phase_ = 0x400;
516 SetEGRate(Min(63, rr_ + key_scale_rate_));
522 eg_level_ = FM_EG_BOTTOM;
523 eg_level_on_next_phase_ = FM_EG_BOTTOM;
532 void Operator::SetFNum(uint f)
534 dp_ = (f & 2047) << ((f >> 11) & 7);
535 bn_ = notetable[(f >> 7) & 127];
536 param_changed_ = true;
540 //
\82P
\83T
\83\93\83v
\83\8b\8d\87\90¬
542 // ISample
\82ð envelop count (2
\83Î)
\82É
\95Ï
\8a·
\82·
\82é
\83V
\83t
\83g
\97Ê
543 #define IS2EC_SHIFT ((20 + FM_PGBITS) - 13)
546 //
\93ü
\97Í: s = 20+FM_PGBITS = 29
547 #define Sine(s) sinetable[((s) >> (20+FM_PGBITS-FM_OPSINBITS))&(FM_OPSINENTS-1)]
548 #define SINE(s) sinetable[(s) & (FM_OPSINENTS-1)]
550 inline FM::ISample Operator::LogToLin(uint a)
552 #if 1 // FM_CLENTS < 0xc00 // 400 for TL, 400 for ENV, 400 for LFO.
553 return (a < FM_CLENTS) ? cltable[a] : 0;
559 inline void Operator::EGUpdate()
563 eg_out_ = Min(tl_out_ + eg_level_, 0x3ff) << (1 + 2);
567 eg_out_ = Min(tl_out_ + eg_level_ * ssg_vector_ + ssg_offset_, 0x3ff) << (1 + 2);
571 inline void Operator::SetEGRate(uint rate)
574 eg_count_diff_ = decaytable2[rate / 4] * chip_->GetRatio();
578 void FM::Operator::EGCalc()
580 eg_count_ = (2047 * 3) << FM_RATIOBITS; // ##
\82±
\82Ì
\8eè
\94²
\82«
\82Í
\8dÄ
\8c»
\90«
\82ð
\92á
\89º
\82³
\82¹
\82é
582 if (eg_phase_ == attack)
584 int c = attacktable[eg_rate_][eg_curve_count_ & 7];
587 eg_level_ -= 1 + (eg_level_ >> c);
597 eg_level_ += decaytable1[eg_rate_][eg_curve_count_ & 7];
598 if (eg_level_ >= eg_level_on_next_phase_)
599 ShiftPhase(EGPhase(eg_phase_+1));
604 eg_level_ += 4 * decaytable1[eg_rate_][eg_curve_count_ & 7];
605 if (eg_level_ >= eg_level_on_next_phase_)
626 inline void FM::Operator::EGStep()
628 eg_count_ -= eg_count_diff_;
630 // EG
\82Ì
\95Ï
\89»
\82Í
\91S
\83X
\83\8d\83b
\83g
\82Å
\93¯
\8aú
\82µ
\82Ä
\82¢
\82é
\82Æ
\82¢
\82¤
\89\
\82à
\82 \82é
636 // ret:2^(20+PGBITS) / cycle
637 inline uint32 FM::Operator::PGCalc()
639 uint32 ret = pg_count_;
640 pg_count_ += pg_diff_;
645 inline uint32 FM::Operator::PGCalcL()
647 uint32 ret = pg_count_;
648 pg_count_ += pg_diff_ + ((pg_diff_lfo_ * chip_->GetPMV()) >> 5);// & -(1 << (2+IS2EC_SHIFT)));
650 return ret /* + pmv * pg_diff_;*/;
654 // in: ISample (
\8dÅ
\91å 8
\83Î)
655 inline FM::ISample FM::Operator::Calc(ISample in)
660 int pgin = PGCalc() >> (20+FM_PGBITS-FM_OPSINBITS);
661 pgin += in >> (20+FM_PGBITS-FM_OPSINBITS-(2+IS2EC_SHIFT));
662 out_ = LogToLin(eg_out_ + SINE(pgin));
668 inline FM::ISample FM::Operator::CalcL(ISample in)
672 int pgin = PGCalcL() >> (20+FM_PGBITS-FM_OPSINBITS);
673 pgin += in >> (20+FM_PGBITS-FM_OPSINBITS-(2+IS2EC_SHIFT));
674 out_ = LogToLin(eg_out_ + SINE(pgin) + ams_[chip_->GetAML()]);
680 inline FM::ISample FM::Operator::CalcN(uint noise)
684 int lv = Max(0, 0x3ff - (tl_out_ + eg_level_)) << 1;
686 // noise & 1 ? lv : -lv
\82Æ
\93\99\89¿
687 noise = (noise & 1) - 1;
688 out_ = (lv + noise) ^ noise;
695 // Self Feedback
\82Ì
\95Ï
\92²
\8dÅ
\91å = 4
\83Î
696 inline FM::ISample FM::Operator::CalcFB(uint fb)
700 ISample in = out_ + out2_;
703 int pgin = PGCalc() >> (20+FM_PGBITS-FM_OPSINBITS);
706 pgin += ((in << (1 + IS2EC_SHIFT)) >> fb) >> (20+FM_PGBITS-FM_OPSINBITS);
708 out_ = LogToLin(eg_out_ + SINE(pgin));
714 inline FM::ISample FM::Operator::CalcFBL(uint fb)
718 ISample in = out_ + out2_;
721 int pgin = PGCalcL() >> (20+FM_PGBITS-FM_OPSINBITS);
724 pgin += ((in << (1 + IS2EC_SHIFT)) >> fb) >> (20+FM_PGBITS-FM_OPSINBITS);
727 out_ = LogToLin(eg_out_ + SINE(pgin) + ams_[chip_->GetAML()]);
733 // ---------------------------------------------------------------------------
734 //
\83X
\83e
\81[
\83g
\83Z
\81[
\83u
736 #define OPERATOR_STATE_VERSION 1
738 void Operator::SaveState(void *f)
740 FILEIO *state_fio = (FILEIO *)f;
742 state_fio->FputUint32(OPERATOR_STATE_VERSION);
744 state_fio->FputInt32(out_);
745 state_fio->FputInt32(out2_);
746 state_fio->FputInt32(in2_);
747 state_fio->FputUint32(dp_);
748 state_fio->FputUint32(detune_);
749 state_fio->FputUint32(detune2_);
750 state_fio->FputUint32(multiple_);
751 state_fio->FputUint32(pg_count_);
752 state_fio->FputUint32(pg_diff_);
753 state_fio->FputInt32(pg_diff_lfo_);
754 state_fio->FputUint32(bn_);
755 state_fio->FputInt32(eg_level_);
756 state_fio->FputInt32(eg_level_on_next_phase_);
757 state_fio->FputInt32(eg_count_);
758 state_fio->FputInt32(eg_count_diff_);
759 state_fio->FputInt32(eg_out_);
760 state_fio->FputInt32(tl_out_);
761 state_fio->FputInt32(eg_rate_);
762 state_fio->FputInt32(eg_curve_count_);
763 state_fio->FputInt32(ssg_offset_);
764 state_fio->FputInt32(ssg_vector_);
765 state_fio->FputInt32(ssg_phase_);
766 state_fio->FputUint32(key_scale_rate_);
767 state_fio->FputInt32(static_cast<int>(eg_phase_));
768 state_fio->FputInt32((int)(ams_ - &amtable[0][0][0]));
769 state_fio->FputUint32(ms_);
770 state_fio->FputUint32(tl_);
771 state_fio->FputUint32(tl_latch_);
772 state_fio->FputUint32(ar_);
773 state_fio->FputUint32(dr_);
774 state_fio->FputUint32(sr_);
775 state_fio->FputUint32(sl_);
776 state_fio->FputUint32(rr_);
777 state_fio->FputUint32(ks_);
778 state_fio->FputUint32(ssg_type_);
779 state_fio->FputBool(keyon_);
780 state_fio->FputBool(amon_);
781 state_fio->FputBool(param_changed_);
782 state_fio->FputBool(mute_);
783 state_fio->FputInt32(dbgopout_);
784 state_fio->FputInt32(dbgpgout_);
787 bool Operator::LoadState(void *f)
789 FILEIO *state_fio = (FILEIO *)f;
791 if(state_fio->FgetUint32() != OPERATOR_STATE_VERSION) {
794 out_ = state_fio->FgetInt32();
795 out2_ = state_fio->FgetInt32();
796 in2_ = state_fio->FgetInt32();
797 dp_ = state_fio->FgetUint32();
798 detune_ = state_fio->FgetUint32();
799 detune2_ = state_fio->FgetUint32();
800 multiple_ = state_fio->FgetUint32();
801 pg_count_ = state_fio->FgetUint32();
802 pg_diff_ = state_fio->FgetUint32();
803 pg_diff_lfo_ = state_fio->FgetInt32();
804 bn_ = state_fio->FgetUint32();
805 eg_level_ = state_fio->FgetInt32();
806 eg_level_on_next_phase_ = state_fio->FgetInt32();
807 eg_count_ = state_fio->FgetInt32();
808 eg_count_diff_ = state_fio->FgetInt32();
809 eg_out_ = state_fio->FgetInt32();
810 tl_out_ = state_fio->FgetInt32();
811 eg_rate_ = state_fio->FgetInt32();
812 eg_curve_count_ = state_fio->FgetInt32();
813 ssg_offset_ = state_fio->FgetInt32();
814 ssg_vector_ = state_fio->FgetInt32();
815 ssg_phase_ = state_fio->FgetInt32();
816 key_scale_rate_ = state_fio->FgetUint32();
817 eg_phase_ = static_cast<EGPhase>(state_fio->FgetInt32());
818 ams_ = &amtable[0][0][0] + state_fio->FgetInt32();
819 ms_ = state_fio->FgetUint32();
820 tl_ = state_fio->FgetUint32();
821 tl_latch_ = state_fio->FgetUint32();
822 ar_ = state_fio->FgetUint32();
823 dr_ = state_fio->FgetUint32();
824 sr_ = state_fio->FgetUint32();
825 sl_ = state_fio->FgetUint32();
826 rr_ = state_fio->FgetUint32();
827 ks_ = state_fio->FgetUint32();
828 ssg_type_ = state_fio->FgetUint32();
829 keyon_ = state_fio->FgetBool();
830 amon_ = state_fio->FgetBool();
831 param_changed_ = state_fio->FgetBool();
832 mute_ = state_fio->FgetBool();
833 dbgopout_ = state_fio->FgetInt32();
834 dbgpgout_ = state_fio->FgetInt32();
840 // ---------------------------------------------------------------------------
843 const uint8 Channel4::fbtable[8] = { 31, 7, 6, 5, 4, 3, 2, 1 };
844 int Channel4::kftable[64];
846 bool Channel4::tablehasmade = false;
858 void Channel4::MakeTable()
860 // 100/64 cent = 2^(i*100/64*1200)
861 for (int i=0; i<64; i++)
863 kftable[i] = int(0x10000 * pow(2., i / 768.) );
867 //
\83\8a\83Z
\83b
\83g
868 void Channel4::Reset()
877 int Channel4::Prepare()
884 pms = pmtable[op[0].type_][op[0].ms_ & 7];
885 int key = (op[0].IsOn() | op[1].IsOn() | op[2].IsOn() | op[3].IsOn()) ? 1 : 0;
886 int lfo = op[0].ms_ & (op[0].amon_ | op[1].amon_ | op[2].amon_ | op[3].amon_ ? 0x37 : 7) ? 2 : 0;
890 // F-Number/BLOCK
\82ð
\90Ý
\92è
891 void Channel4::SetFNum(uint f)
893 for (int i=0; i<4; i++)
897 // KC/KF
\82ð
\90Ý
\92è
898 void Channel4::SetKCKF(uint kc, uint kf)
900 const static uint kctable[16] =
902 5197, 5506, 5833, 6180, 6180, 6547, 6937, 7349,
903 7349, 7786, 8249, 8740, 8740, 9259, 9810, 10394,
906 int oct = 19 - ((kc >> 4) & 7);
908 //printf("%p", this);
909 uint kcv = kctable[kc & 0x0f];
910 kcv = (kcv + 2) / 4 * 4;
911 //printf(" %.4x", kcv);
912 uint dp = kcv * kftable[kf & 0x3f];
913 //printf(" %.4x %.4x %.8x", kcv, kftable[kf & 0x3f], dp >> oct);
917 uint bn = (kc >> 2) & 31;
918 op[0].SetDPBN(dp, bn);
919 op[1].SetDPBN(dp, bn);
920 op[2].SetDPBN(dp, bn);
921 op[3].SetDPBN(dp, bn);
922 //printf(" %.8x\n", dp);
926 void Channel4::KeyControl(uint key)
928 if (key & 0x1) op[0].KeyOn(); else op[0].KeyOff();
929 if (key & 0x2) op[1].KeyOn(); else op[1].KeyOff();
930 if (key & 0x4) op[2].KeyOn(); else op[2].KeyOff();
931 if (key & 0x8) op[3].KeyOn(); else op[3].KeyOff();
934 //
\83A
\83\8b\83S
\83\8a\83Y
\83\80\82ð
\90Ý
\92è
935 void Channel4::SetAlgorithm(uint algo)
937 static const uint8 table1[8][6] =
939 { 0, 1, 1, 2, 2, 3 }, { 1, 0, 0, 1, 1, 2 },
940 { 1, 1, 1, 0, 0, 2 }, { 0, 1, 2, 1, 1, 2 },
941 { 0, 1, 2, 2, 2, 1 }, { 0, 1, 0, 1, 0, 1 },
942 { 0, 1, 2, 1, 2, 1 }, { 1, 0, 1, 0, 1, 0 },
945 in [0] = &buf[table1[algo][0]];
946 out[0] = &buf[table1[algo][1]];
947 in [1] = &buf[table1[algo][2]];
948 out[1] = &buf[table1[algo][3]];
949 in [2] = &buf[table1[algo][4]];
950 out[2] = &buf[table1[algo][5]];
957 ISample Channel4::Calc()
963 op[2].Calc(op[1].Out());
964 op[1].Calc(op[0].Out());
965 r = op[3].Calc(op[2].Out());
969 op[2].Calc(op[0].Out() + op[1].Out());
971 r = op[3].Calc(op[2].Out());
975 op[2].Calc(op[1].Out());
977 r = op[3].Calc(op[0].Out() + op[2].Out());
982 op[1].Calc(op[0].Out());
983 r = op[3].Calc(op[1].Out() + op[2].Out());
988 r = op[1].Calc(op[0].Out());
989 r += op[3].Calc(op[2].Out());
993 r = op[2].Calc(op[0].Out());
994 r += op[1].Calc(op[0].Out());
995 r += op[3].Calc(op[0].Out());
1000 r += op[1].Calc(op[0].Out());
1008 r += op[0].CalcFB(fb);
1015 ISample Channel4::CalcL()
1017 chip_->SetPMV(pms[chip_->GetPML()]);
1023 op[2].CalcL(op[1].Out());
1024 op[1].CalcL(op[0].Out());
1025 r = op[3].CalcL(op[2].Out());
1029 op[2].CalcL(op[0].Out() + op[1].Out());
1031 r = op[3].CalcL(op[2].Out());
1035 op[2].CalcL(op[1].Out());
1037 r = op[3].CalcL(op[0].Out() + op[2].Out());
1042 op[1].CalcL(op[0].Out());
1043 r = op[3].CalcL(op[1].Out() + op[2].Out());
1048 r = op[1].CalcL(op[0].Out());
1049 r += op[3].CalcL(op[2].Out());
1053 r = op[2].CalcL(op[0].Out());
1054 r += op[1].CalcL(op[0].Out());
1055 r += op[3].CalcL(op[0].Out());
1060 r += op[1].CalcL(op[0].Out());
1061 r += op[3].CalcL(0);
1066 r += op[1].CalcL(0);
1067 r += op[3].CalcL(0);
1068 r += op[0].CalcFBL(fb);
1075 ISample Channel4::CalcN(uint noise)
1077 buf[1] = buf[2] = buf[3] = 0;
1079 buf[0] = op[0].out_; op[0].CalcFB(fb);
1080 *out[0] += op[1].Calc(*in[0]);
1081 *out[1] += op[2].Calc(*in[1]);
1088 ISample Channel4::CalcLN(uint noise)
1090 chip_->SetPMV(pms[chip_->GetPML()]);
1091 buf[1] = buf[2] = buf[3] = 0;
1093 buf[0] = op[0].out_; op[0].CalcFBL(fb);
1094 *out[0] += op[1].CalcL(*in[0]);
1095 *out[1] += op[2].CalcL(*in[1]);
1101 // ---------------------------------------------------------------------------
1102 //
\83X
\83e
\81[
\83g
\83Z
\81[
\83u
1104 #define CHANNEL4_STATE_VERSION 1
1106 void Channel4::SaveState(void *f)
1108 FILEIO *state_fio = (FILEIO *)f;
1110 state_fio->FputUint32(CHANNEL4_STATE_VERSION);
1112 state_fio->FputUint32(fb);
1113 state_fio->Fwrite(buf, sizeof(buf), 1);
1114 for(int i = 0; i < 3; i++) {
1115 state_fio->FputInt32((int)(in [i] - &buf[0]));
1116 state_fio->FputInt32((int)(out[i] - &buf[0]));
1118 state_fio->FputInt32((int)(pms - &pmtable[0][0][0]));
1119 state_fio->FputInt32(algo_);
1120 for(int i = 0; i < 4; i++) {
1125 bool Channel4::LoadState(void *f)
1127 FILEIO *state_fio = (FILEIO *)f;
1129 if(state_fio->FgetUint32() != CHANNEL4_STATE_VERSION) {
1132 fb = state_fio->FgetUint32();
1133 state_fio->Fread(buf, sizeof(buf), 1);
1134 for(int i = 0; i < 3; i++) {
1135 in [i] = &buf[0] + state_fio->FgetInt32();
1136 out[i] = &buf[0] + state_fio->FgetInt32();
1138 pms = &pmtable[0][0][0] + state_fio->FgetInt32();
1139 algo_ = state_fio->FgetInt32();
1140 for(int i = 0; i < 4; i++) {
1141 if(!op[i].LoadState(f)) {