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;
177 const char *get_libfmgen_version(void)
179 #if defined(__LIBFMGEN_VERSION)
180 return (const char *)__LIBFMGEN_VERSION;
182 return (const char *)"\0";
190 // ---------------------------------------------------------------------------
191 //
\83e
\81[
\83u
\83\8b\8dì
\90¬
202 static const double pms[2][8] =
204 { 0, 1/360., 2/360., 3/360., 4/360., 6/360., 12/360., 24/360., }, // OPNA
205 // { 0, 1/240., 2/240., 4/240., 10/240., 20/240., 80/240., 140/240., }, // OPM
206 { 0, 1/480., 2/480., 4/480., 10/480., 20/480., 80/480., 140/480., }, // OPM
207 // { 0, 1/960., 2/960., 4/960., 10/960., 20/960., 80/960., 140/960., }, // OPM
209 // 3 6, 12 30 60 240 420 / 720
211 // lfofref[level * max * wave];
212 // pre = lfofref[level][pms * wave >> 8];
213 static const volatile uint8 amt[2][4] =
215 { 31, 6, 4, 3 }, // OPNA
216 { 31, 2, 1, 0 }, // OPM
219 for (int type = 0; type < 2; type++)
223 double pmb = pms[type][i];
224 for (int j=0; j<FM_LFOENTS; j++)
226 // double v = pow(2.0, pmb * (2 * j - FM_LFOENTS+1) / (FM_LFOENTS-1));
227 double w = 0.6 * pmb * sin(2 * j * 3.14159265358979323846 / FM_LFOENTS) + 1;
228 // pmtable[type][i][j] = int(0x10000 * (v - 1));
230 pmtable[type][i][j] = int(0x10000 * (w - 1));
232 // pmtable[type][i][j] = int(0x10000 * (v - 1));
234 // printf("pmtable[%d][%d][%.2x] = %5d %7.5f %7.5f\n", type, i, j, pmtable[type][i][j], v, w);
239 for (int j=0; j<FM_LFOENTS; j++)
241 amtable[type][i][j] = (((j * 4) >> amt[type][i]) * 2) << 2;
248 // ---------------------------------------------------------------------------
249 //
\83`
\83b
\83v
\93à
\82Å
\8b¤
\92Ê
\82È
\95\94\95ª
252 : ratio_(0), aml_(0), pml_(0), pmv_(0), optype_(typeN)
254 static int __num = 0;
258 //
\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¬
259 void Chip::SetRatio(uint ratio)
268 void Chip::MakeTable()
273 static const float dt2lv[4] = { 1.f, 1.414f, 1.581f, 1.732f };
276 assert(2 + FM_RATIOBITS - FM_PGBITS >= 0);
277 double rr = dt2lv[h] * double(ratio_);
280 int mul = l ? l * 2 : 1;
281 multable_[h][l] = uint(mul * rr);
286 // ---------------------------------------------------------------------------
287 //
\83X
\83e
\81[
\83g
\83Z
\81[
\83u
289 #define CHIP_STATE_VERSION 1
291 bool Chip::ProcessState(void *f, bool loading)
293 FILEIO *state_fio = (FILEIO *)f;
295 if(!state_fio->StateCheckUint32(CHIP_STATE_VERSION)) {
298 state_fio->StateValue(ratio_);
299 state_fio->StateValue(aml_);
300 state_fio->StateValue(pml_);
301 state_fio->StateValue(pmv_);
306 // ---------------------------------------------------------------------------
309 bool FM::Operator::tablehasmade = false;
310 uint FM::Operator::sinetable[1024];
311 int32 FM::Operator::cltable[FM_CLENTS];
314 FM::Operator::Operator()
320 static int __num = 0;
321 operators_num = __num++;
323 ar_ = dr_ = sr_ = rr_ = key_scale_rate_ = 0;
324 ams_ = amtable[0][0];
342 void FM::Operator::Reset()
345 tl_ = tl_latch_ = 127;
357 param_changed_ = true;
361 void Operator::MakeTable()
363 //
\91Î
\90\94\83e
\81[
\83u
\83\8b\82Ì
\8dì
\90¬
364 assert(FM_CLENTS >= 256);
368 for (i=0; i<256; i++)
370 int v = int(floor(pow(2., 13. - i / 256.)));
375 while (p < cltable + FM_CLENTS)
381 // for (i=0; i<13*256; i++)
382 // printf("%4d, %d, %d\n", i, cltable[i*2], cltable[i*2+1]);
384 //
\83T
\83C
\83\93\83e
\81[
\83u
\83\8b\82Ì
\8dì
\90¬
385 double log2 = log(2.);
386 for (i=0; i<FM_OPSINENTS/2; i++)
388 double r = (i * 2 + 1) * FM_PI / FM_OPSINENTS;
389 double q = -256 * log(sin(r)) / log2;
390 uint s = (int) (floor(q + 0.5)) + 1;
391 // printf("%d, %d\n", s, cltable[s * 2] / 8);
392 sinetable[i] = s * 2 ;
393 sinetable[FM_OPSINENTS / 2 + i] = s * 2 + 1;
396 ::FM::MakeLFOTable();
403 inline void FM::Operator::SetDPBN(uint dp, uint bn)
405 dp_ = dp, bn_ = bn; param_changed_ = true;
411 void Operator::Prepare()
415 param_changed_ = false;
417 pg_diff_ = (dp_ + dttable[detune_ + bn_]) * chip_->GetMulValue(detune2_, multiple_);
418 pg_diff_ >>= (2 + FM_RATIOBITS - FM_PGBITS);
419 pg_diff_lfo_ = pg_diff_ >> 11;
422 key_scale_rate_ = bn_ >> (3-ks_);
423 tl_out_ = mute_ ? 0x3ff : tl_ * 8;
428 SetEGRate(ar_ ? Min(63, ar_ + key_scale_rate_) : 0);
431 SetEGRate(dr_ ? Min(63, dr_ + key_scale_rate_) : 0);
432 eg_level_on_next_phase_ = sl_ * 8;
435 SetEGRate(sr_ ? Min(63, sr_ + key_scale_rate_) : 0);
438 SetEGRate(Min(63, rr_ + key_scale_rate_));
443 if (ssg_type_ && (eg_phase_ != release))
445 int m = ar_ >= (uint)((ssg_type_ == 8 || ssg_type_ == 12) ? 56 : 60);
447 if (ssg_phase_ == -1) // XXX quick fix
449 assert(0 <= ssg_phase_ && ssg_phase_ <= 2);
450 const int* table = ssgenvtable[ssg_type_ & 7][m][ssg_phase_];
452 ssg_offset_ = table[0] * 0x200;
453 ssg_vector_ = table[1];
456 ams_ = amtable[type_][amon_ ? (ms_ >> 4) & 3 : 0];
462 // envelop
\82Ì eg_phase_
\95Ï
\8dX
463 void Operator::ShiftPhase(EGPhase nextphase)
467 case attack: // Attack Phase
471 ssg_phase_ = ssg_phase_ + 1;
475 int m = ar_ >= (uint)((ssg_type_ == 8 || ssg_type_ == 12) ? 56 : 60);
477 assert(0 <= ssg_phase_ && ssg_phase_ <= 2);
478 const int* table = ssgenvtable[ssg_type_ & 7][m][ssg_phase_];
480 ssg_offset_ = table[0] * 0x200;
481 ssg_vector_ = table[1];
483 if ((ar_ + key_scale_rate_) < 62)
485 SetEGRate(ar_ ? Min(63, ar_ + key_scale_rate_) : 0);
489 case decay: // Decay Phase
493 eg_level_on_next_phase_ = ssg_type_ ? Min(sl_ * 8, 0x200) : sl_ * 8;
495 SetEGRate(dr_ ? Min(63, dr_ + key_scale_rate_) : 0);
499 case sustain: // Sustain Phase
501 eg_level_on_next_phase_ = ssg_type_ ? 0x200 : 0x400;
503 SetEGRate(sr_ ? Min(63, sr_ + key_scale_rate_) : 0);
507 case release: // Release Phase
510 eg_level_ = eg_level_ * ssg_vector_ + ssg_offset_;
514 if (eg_phase_ == attack || (eg_level_ < FM_EG_BOTTOM)) //0x400/* && eg_phase_ != off*/))
516 eg_level_on_next_phase_ = 0x400;
517 SetEGRate(Min(63, rr_ + key_scale_rate_));
523 eg_level_ = FM_EG_BOTTOM;
524 eg_level_on_next_phase_ = FM_EG_BOTTOM;
533 void Operator::SetFNum(uint f)
535 dp_ = (f & 2047) << ((f >> 11) & 7);
536 bn_ = notetable[(f >> 7) & 127];
537 param_changed_ = true;
541 //
\82P
\83T
\83\93\83v
\83\8b\8d\87\90¬
543 // ISample
\82ð envelop count (2
\83Î)
\82É
\95Ï
\8a·
\82·
\82é
\83V
\83t
\83g
\97Ê
544 #define IS2EC_SHIFT ((20 + FM_PGBITS) - 13)
547 //
\93ü
\97Í: s = 20+FM_PGBITS = 29
548 #define Sine(s) sinetable[((s) >> (20+FM_PGBITS-FM_OPSINBITS))&(FM_OPSINENTS-1)]
549 #define SINE(s) sinetable[(s) & (FM_OPSINENTS-1)]
551 inline FM::ISample Operator::LogToLin(uint a)
553 #if 1 // FM_CLENTS < 0xc00 // 400 for TL, 400 for ENV, 400 for LFO.
554 return (a < FM_CLENTS) ? cltable[a] : 0;
560 inline void Operator::EGUpdate()
564 eg_out_ = Min(tl_out_ + eg_level_, 0x3ff) << (1 + 2);
568 eg_out_ = Min(tl_out_ + eg_level_ * ssg_vector_ + ssg_offset_, 0x3ff) << (1 + 2);
572 inline void Operator::SetEGRate(uint rate)
575 eg_count_diff_ = decaytable2[rate / 4] * chip_->GetRatio();
579 void FM::Operator::EGCalc()
581 eg_count_ = (2047 * 3) << FM_RATIOBITS; // ##
\82±
\82Ì
\8eè
\94²
\82«
\82Í
\8dÄ
\8c»
\90«
\82ð
\92á
\89º
\82³
\82¹
\82é
583 if (eg_phase_ == attack)
585 int c = attacktable[eg_rate_][eg_curve_count_ & 7];
588 eg_level_ -= 1 + (eg_level_ >> c);
598 eg_level_ += decaytable1[eg_rate_][eg_curve_count_ & 7];
599 if (eg_level_ >= eg_level_on_next_phase_)
600 ShiftPhase(EGPhase(eg_phase_+1));
605 eg_level_ += 4 * decaytable1[eg_rate_][eg_curve_count_ & 7];
606 if (eg_level_ >= eg_level_on_next_phase_)
627 inline void FM::Operator::EGStep()
629 eg_count_ -= eg_count_diff_;
631 // 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é
637 // ret:2^(20+PGBITS) / cycle
638 inline uint32 FM::Operator::PGCalc()
640 uint32 ret = pg_count_;
641 pg_count_ += pg_diff_;
646 inline uint32 FM::Operator::PGCalcL()
648 uint32 ret = pg_count_;
649 pg_count_ += pg_diff_ + ((pg_diff_lfo_ * chip_->GetPMV()) >> 5);// & -(1 << (2+IS2EC_SHIFT)));
651 return ret /* + pmv * pg_diff_;*/;
655 // in: ISample (
\8dÅ
\91å 8
\83Î)
656 inline FM::ISample FM::Operator::Calc(ISample in)
661 int pgin = PGCalc() >> (20+FM_PGBITS-FM_OPSINBITS);
662 pgin += in >> (20+FM_PGBITS-FM_OPSINBITS-(2+IS2EC_SHIFT));
663 out_ = LogToLin(eg_out_ + SINE(pgin));
669 inline FM::ISample FM::Operator::CalcL(ISample in)
673 int pgin = PGCalcL() >> (20+FM_PGBITS-FM_OPSINBITS);
674 pgin += in >> (20+FM_PGBITS-FM_OPSINBITS-(2+IS2EC_SHIFT));
675 out_ = LogToLin(eg_out_ + SINE(pgin) + ams_[chip_->GetAML()]);
681 inline FM::ISample FM::Operator::CalcN(uint noise)
685 int lv = Max(0, 0x3ff - (tl_out_ + eg_level_)) << 1;
687 // noise & 1 ? lv : -lv
\82Æ
\93\99\89¿
688 noise = (noise & 1) - 1;
689 out_ = (lv + noise) ^ noise;
696 // Self Feedback
\82Ì
\95Ï
\92²
\8dÅ
\91å = 4
\83Î
697 inline FM::ISample FM::Operator::CalcFB(uint fb)
701 ISample in = out_ + out2_;
704 int pgin = PGCalc() >> (20+FM_PGBITS-FM_OPSINBITS);
707 pgin += ((in << (1 + IS2EC_SHIFT)) >> fb) >> (20+FM_PGBITS-FM_OPSINBITS);
709 out_ = LogToLin(eg_out_ + SINE(pgin));
715 inline FM::ISample FM::Operator::CalcFBL(uint fb)
719 ISample in = out_ + out2_;
722 int pgin = PGCalcL() >> (20+FM_PGBITS-FM_OPSINBITS);
725 pgin += ((in << (1 + IS2EC_SHIFT)) >> fb) >> (20+FM_PGBITS-FM_OPSINBITS);
728 out_ = LogToLin(eg_out_ + SINE(pgin) + ams_[chip_->GetAML()]);
734 // ---------------------------------------------------------------------------
735 //
\83X
\83e
\81[
\83g
\83Z
\81[
\83u
737 #define OPERATOR_STATE_VERSION 1
739 bool Operator::ProcessState(void *f, bool loading)
741 FILEIO *state_fio = (FILEIO *)f;
743 if(!state_fio->StateCheckUint32(OPERATOR_STATE_VERSION)) {
746 state_fio->StateValue(out_);
747 state_fio->StateValue(out2_);
748 state_fio->StateValue(in2_);
749 state_fio->StateValue(dp_);
750 state_fio->StateValue(detune_);
751 state_fio->StateValue(detune2_);
752 state_fio->StateValue(multiple_);
753 state_fio->StateValue(pg_count_);
754 state_fio->StateValue(pg_diff_);
755 state_fio->StateValue(pg_diff_lfo_);
756 state_fio->StateValue(bn_);
757 state_fio->StateValue(eg_level_);
758 state_fio->StateValue(eg_level_on_next_phase_);
759 state_fio->StateValue(eg_count_);
760 state_fio->StateValue(eg_count_diff_);
761 state_fio->StateValue(eg_out_);
762 state_fio->StateValue(tl_out_);
763 state_fio->StateValue(eg_rate_);
764 state_fio->StateValue(eg_curve_count_);
765 state_fio->StateValue(ssg_offset_);
766 state_fio->StateValue(ssg_vector_);
767 state_fio->StateValue(ssg_phase_);
768 state_fio->StateValue(key_scale_rate_);
770 eg_phase_ = static_cast<EGPhase>(state_fio->FgetInt32_LE());
771 ams_ = &amtable[0][0][0] + state_fio->FgetInt32_LE();
773 state_fio->FputInt32_LE(static_cast<int>(eg_phase_));
774 state_fio->FputInt32_LE((int)(ams_ - &amtable[0][0][0]));
776 state_fio->StateValue(ms_);
777 state_fio->StateValue(tl_);
778 state_fio->StateValue(tl_latch_);
779 state_fio->StateValue(ar_);
780 state_fio->StateValue(dr_);
781 state_fio->StateValue(sr_);
782 state_fio->StateValue(sl_);
783 state_fio->StateValue(rr_);
784 state_fio->StateValue(ks_);
785 state_fio->StateValue(ssg_type_);
786 state_fio->StateValue(keyon_);
787 state_fio->StateValue(amon_);
788 state_fio->StateValue(param_changed_);
789 state_fio->StateValue(mute_);
790 state_fio->StateValue(dbgopout_);
791 state_fio->StateValue(dbgpgout_);
797 // ---------------------------------------------------------------------------
800 const uint8 Channel4::fbtable[8] = { 31, 7, 6, 5, 4, 3, 2, 1 };
801 int Channel4::kftable[64];
803 bool Channel4::tablehasmade = false;
811 static int __num = 0;
812 channel4s_num = __num++;
818 void Channel4::MakeTable()
820 // 100/64 cent = 2^(i*100/64*1200)
821 for (int i=0; i<64; i++)
823 kftable[i] = int(0x10000 * pow(2., i / 768.) );
827 //
\83\8a\83Z
\83b
\83g
828 void Channel4::Reset()
837 int Channel4::Prepare()
844 pms = pmtable[op[0].type_][op[0].ms_ & 7];
845 int key = (op[0].IsOn() | op[1].IsOn() | op[2].IsOn() | op[3].IsOn()) ? 1 : 0;
846 int lfo = op[0].ms_ & (op[0].amon_ | op[1].amon_ | op[2].amon_ | op[3].amon_ ? 0x37 : 7) ? 2 : 0;
850 // F-Number/BLOCK
\82ð
\90Ý
\92è
851 void Channel4::SetFNum(uint f)
853 for (int i=0; i<4; i++)
857 // KC/KF
\82ð
\90Ý
\92è
858 void Channel4::SetKCKF(uint kc, uint kf)
860 const static uint kctable[16] =
862 5197, 5506, 5833, 6180, 6180, 6547, 6937, 7349,
863 7349, 7786, 8249, 8740, 8740, 9259, 9810, 10394,
866 int oct = 19 - ((kc >> 4) & 7);
868 //printf("%p", this);
869 uint kcv = kctable[kc & 0x0f];
870 kcv = (kcv + 2) / 4 * 4;
871 //printf(" %.4x", kcv);
872 uint dp = kcv * kftable[kf & 0x3f];
873 //printf(" %.4x %.4x %.8x", kcv, kftable[kf & 0x3f], dp >> oct);
877 uint bn = (kc >> 2) & 31;
878 op[0].SetDPBN(dp, bn);
879 op[1].SetDPBN(dp, bn);
880 op[2].SetDPBN(dp, bn);
881 op[3].SetDPBN(dp, bn);
882 //printf(" %.8x\n", dp);
886 void Channel4::KeyControl(uint key)
888 if (key & 0x1) op[0].KeyOn(); else op[0].KeyOff();
889 if (key & 0x2) op[1].KeyOn(); else op[1].KeyOff();
890 if (key & 0x4) op[2].KeyOn(); else op[2].KeyOff();
891 if (key & 0x8) op[3].KeyOn(); else op[3].KeyOff();
894 //
\83A
\83\8b\83S
\83\8a\83Y
\83\80\82ð
\90Ý
\92è
895 void Channel4::SetAlgorithm(uint algo)
897 static const uint8 table1[8][6] =
899 { 0, 1, 1, 2, 2, 3 }, { 1, 0, 0, 1, 1, 2 },
900 { 1, 1, 1, 0, 0, 2 }, { 0, 1, 2, 1, 1, 2 },
901 { 0, 1, 2, 2, 2, 1 }, { 0, 1, 0, 1, 0, 1 },
902 { 0, 1, 2, 1, 2, 1 }, { 1, 0, 1, 0, 1, 0 },
905 in [0] = &buf[table1[algo][0]];
906 out[0] = &buf[table1[algo][1]];
907 in [1] = &buf[table1[algo][2]];
908 out[1] = &buf[table1[algo][3]];
909 in [2] = &buf[table1[algo][4]];
910 out[2] = &buf[table1[algo][5]];
917 ISample Channel4::Calc()
923 op[2].Calc(op[1].Out());
924 op[1].Calc(op[0].Out());
925 r = op[3].Calc(op[2].Out());
929 op[2].Calc(op[0].Out() + op[1].Out());
931 r = op[3].Calc(op[2].Out());
935 op[2].Calc(op[1].Out());
937 r = op[3].Calc(op[0].Out() + op[2].Out());
942 op[1].Calc(op[0].Out());
943 r = op[3].Calc(op[1].Out() + op[2].Out());
948 r = op[1].Calc(op[0].Out());
949 r += op[3].Calc(op[2].Out());
953 r = op[2].Calc(op[0].Out());
954 r += op[1].Calc(op[0].Out());
955 r += op[3].Calc(op[0].Out());
960 r += op[1].Calc(op[0].Out());
968 r += op[0].CalcFB(fb);
975 ISample Channel4::CalcL()
977 chip_->SetPMV(pms[chip_->GetPML()]);
983 op[2].CalcL(op[1].Out());
984 op[1].CalcL(op[0].Out());
985 r = op[3].CalcL(op[2].Out());
989 op[2].CalcL(op[0].Out() + op[1].Out());
991 r = op[3].CalcL(op[2].Out());
995 op[2].CalcL(op[1].Out());
997 r = op[3].CalcL(op[0].Out() + op[2].Out());
1002 op[1].CalcL(op[0].Out());
1003 r = op[3].CalcL(op[1].Out() + op[2].Out());
1008 r = op[1].CalcL(op[0].Out());
1009 r += op[3].CalcL(op[2].Out());
1013 r = op[2].CalcL(op[0].Out());
1014 r += op[1].CalcL(op[0].Out());
1015 r += op[3].CalcL(op[0].Out());
1020 r += op[1].CalcL(op[0].Out());
1021 r += op[3].CalcL(0);
1026 r += op[1].CalcL(0);
1027 r += op[3].CalcL(0);
1028 r += op[0].CalcFBL(fb);
1035 ISample Channel4::CalcN(uint noise)
1037 buf[1] = buf[2] = buf[3] = 0;
1039 buf[0] = op[0].out_; op[0].CalcFB(fb);
1040 *out[0] += op[1].Calc(*in[0]);
1041 *out[1] += op[2].Calc(*in[1]);
1048 ISample Channel4::CalcLN(uint noise)
1050 chip_->SetPMV(pms[chip_->GetPML()]);
1051 buf[1] = buf[2] = buf[3] = 0;
1053 buf[0] = op[0].out_; op[0].CalcFBL(fb);
1054 *out[0] += op[1].CalcL(*in[0]);
1055 *out[1] += op[2].CalcL(*in[1]);
1061 // ---------------------------------------------------------------------------
1062 //
\83X
\83e
\81[
\83g
\83Z
\81[
\83u
1064 #define CHANNEL4_STATE_VERSION 1
1066 bool Channel4::ProcessState(void *f, bool loading)
1068 FILEIO *state_fio = (FILEIO *)f;
1070 if(!state_fio->StateCheckUint32(CHANNEL4_STATE_VERSION)) {
1073 state_fio->StateValue(fb);
1074 state_fio->StateArray(buf, sizeof(buf), 1);
1076 for(int i = 0; i < 3; i++) {
1077 in [i] = &buf[0] + state_fio->FgetInt32_LE();
1078 out[i] = &buf[0] + state_fio->FgetInt32_LE();
1080 pms = &pmtable[0][0][0] + state_fio->FgetInt32_LE();
1082 for(int i = 0; i < 3; i++) {
1083 state_fio->FputInt32_LE((int)(in [i] - &buf[0]));
1084 state_fio->FputInt32_LE((int)(out[i] - &buf[0]));
1086 state_fio->FputInt32_LE((int)(pms - &pmtable[0][0][0]));
1088 state_fio->StateValue(algo_);
1089 for(int i = 0; i < 4; i++) {
1090 if(!op[i].ProcessState(f, loading)) {