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"
32 #include "../../statesub.h"
34 #define LOGNAME "fmgen"
36 // ---------------------------------------------------------------------------
38 #define FM_EG_BOTTOM 955
40 // ---------------------------------------------------------------------------
45 const uint8 Operator::notetable[128] =
47 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 3, 3, 3, 3, 3, 3,
48 4, 4, 4, 4, 4, 4, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7,
49 8, 8, 8, 8, 8, 8, 8, 9, 10, 11, 11, 11, 11, 11, 11, 11,
50 12, 12, 12, 12, 12, 12, 12, 13, 14, 15, 15, 15, 15, 15, 15, 15,
51 16, 16, 16, 16, 16, 16, 16, 17, 18, 19, 19, 19, 19, 19, 19, 19,
52 20, 20, 20, 20, 20, 20, 20, 21, 22, 23, 23, 23, 23, 23, 23, 23,
53 24, 24, 24, 24, 24, 24, 24, 25, 26, 27, 27, 27, 27, 27, 27, 27,
54 28, 28, 28, 28, 28, 28, 28, 29, 30, 31, 31, 31, 31, 31, 31, 31,
57 const int8 Operator::dttable[256] =
59 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
60 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
61 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4,
62 4, 6, 6, 6, 8, 8, 8, 10, 10, 12, 12, 14, 16, 16, 16, 16,
63 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 8, 8, 8, 10,
64 10, 12, 12, 14, 16, 16, 18, 20, 22, 24, 26, 28, 32, 32, 32, 32,
65 4, 4, 4, 4, 4, 6, 6, 6, 8, 8, 8, 10, 10, 12, 12, 14,
66 16, 16, 18, 20, 22, 24, 26, 28, 32, 34, 38, 40, 44, 44, 44, 44,
67 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
68 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
69 0, 0, 0, 0, -2, -2, -2, -2, -2, -2, -2, -2, -4, -4, -4, -4,
70 -4, -6, -6, -6, -8, -8, -8,-10,-10,-12,-12,-14,-16,-16,-16,-16,
71 -2, -2, -2, -2, -4, -4, -4, -4, -4, -6, -6, -6, -8, -8, -8,-10,
72 -10,-12,-12,-14,-16,-16,-18,-20,-22,-24,-26,-28,-32,-32,-32,-32,
73 -4, -4, -4, -4, -4, -6, -6, -6, -8, -8, -8,-10,-10,-12,-12,-14,
74 -16,-16,-18,-20,-22,-24,-26,-28,-32,-34,-38,-40,-44,-44,-44,-44,
77 const int8 Operator::decaytable1[64][8] =
79 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
80 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1, 1, 1, 1, 1, 1, 1, 0,
101 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0,
102 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0,
103 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1,
104 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 2, 1, 2, 2, 2, 1,
105 2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 4, 2, 2, 2,
106 4, 2, 4, 2, 4, 2, 4, 2, 4, 4, 4, 2, 4, 4, 4, 2,
107 4, 4, 4, 4, 4, 4, 4, 4, 8, 4, 4, 4, 8, 4, 4, 4,
108 8, 4, 8, 4, 8, 4, 8, 4, 8, 8, 8, 4, 8, 8, 8, 4,
109 16,16,16,16,16,16,16,16, 16,16,16,16,16,16,16,16,
110 16,16,16,16,16,16,16,16, 16,16,16,16,16,16,16,16,
113 const int Operator::decaytable2[16] =
115 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2047, 2047, 2047, 2047, 2047
118 const int8 Operator::attacktable[64][8] =
120 -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
121 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
122 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
123 4, 4, 4,-1, 4, 4, 4,-1, 4, 4, 4,-1, 4, 4, 4,-1,
124 4,-1, 4,-1, 4,-1, 4,-1, 4, 4, 4,-1, 4,-1, 4,-1,
125 4, 4, 4,-1, 4, 4, 4,-1, 4, 4, 4, 4, 4, 4, 4,-1,
126 4,-1, 4,-1, 4,-1, 4,-1, 4, 4, 4,-1, 4,-1, 4,-1,
127 4, 4, 4,-1, 4, 4, 4,-1, 4, 4, 4, 4, 4, 4, 4,-1,
128 4,-1, 4,-1, 4,-1, 4,-1, 4, 4, 4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 1, 2, 1, 2, 1, 2, 1, 1, 1, 2, 1, 1, 1, 2,
150 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0,
151 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0,
154 const int Operator::ssgenvtable[8][2][3][2] =
156 1, 1, 1, 1, 1, 1, // 08
157 0, 1, 1, 1, 1, 1, // 08 56~
158 0, 1, 2, 0, 2, 0, // 09
159 0, 1, 2, 0, 2, 0, // 09
160 1,-1, 0, 1, 1,-1, // 10
161 0, 1, 1,-1, 0, 1, // 10 60~
162 1,-1, 0, 0, 0, 0, // 11
163 0, 1, 0, 0, 0, 0, // 11 60~
164 2,-1, 2,-1, 2,-1, // 12
165 1,-1, 2,-1, 2,-1, // 12 56~
166 1,-1, 0, 0, 0, 0, // 13
167 1,-1, 0, 0, 0, 0, // 13
168 0, 1, 1,-1, 0, 1, // 14
169 1,-1, 0, 1, 1,-1, // 14 60~
170 0, 1, 2, 0, 2, 0, // 15
171 1,-1, 2, 0, 2, 0, // 15 60~
174 // fixed equasion-based tables
175 int pmtable[2][8][FM_LFOENTS];
176 uint volatile amtable[2][4][FM_LFOENTS];
178 static bool tablemade = false;
180 const char *get_libfmgen_version(void)
182 #if defined(__LIBFMGEN_VERSION)
183 return (const char *)__LIBFMGEN_VERSION;
185 return (const char *)"\0";
193 // ---------------------------------------------------------------------------
194 //
\83e
\81[
\83u
\83\8b\8dì
\90¬
205 static const double pms[2][8] =
207 { 0, 1/360., 2/360., 3/360., 4/360., 6/360., 12/360., 24/360., }, // OPNA
208 // { 0, 1/240., 2/240., 4/240., 10/240., 20/240., 80/240., 140/240., }, // OPM
209 { 0, 1/480., 2/480., 4/480., 10/480., 20/480., 80/480., 140/480., }, // OPM
210 // { 0, 1/960., 2/960., 4/960., 10/960., 20/960., 80/960., 140/960., }, // OPM
212 // 3 6, 12 30 60 240 420 / 720
214 // lfofref[level * max * wave];
215 // pre = lfofref[level][pms * wave >> 8];
216 static const volatile uint8 amt[2][4] =
218 { 31, 6, 4, 3 }, // OPNA
219 { 31, 2, 1, 0 }, // OPM
222 for (int type = 0; type < 2; type++)
226 double pmb = pms[type][i];
227 for (int j=0; j<FM_LFOENTS; j++)
229 // double v = pow(2.0, pmb * (2 * j - FM_LFOENTS+1) / (FM_LFOENTS-1));
230 double w = 0.6 * pmb * sin(2 * j * 3.14159265358979323846 / FM_LFOENTS) + 1;
231 // pmtable[type][i][j] = int(0x10000 * (v - 1));
233 pmtable[type][i][j] = int(0x10000 * (w - 1));
235 // pmtable[type][i][j] = int(0x10000 * (v - 1));
237 // printf("pmtable[%d][%d][%.2x] = %5d %7.5f %7.5f\n", type, i, j, pmtable[type][i][j], v, w);
242 for (int j=0; j<FM_LFOENTS; j++)
244 amtable[type][i][j] = (((j * 4) >> amt[type][i]) * 2) << 2;
251 // ---------------------------------------------------------------------------
252 //
\83`
\83b
\83v
\93à
\82Å
\8b¤
\92Ê
\82È
\95\94\95ª
255 : ratio_(0), aml_(0), pml_(0), pmv_(0), optype_(typeN)
257 static int __num = 0;
261 //
\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¬
262 void Chip::SetRatio(uint ratio)
271 void Chip::MakeTable()
276 static const float dt2lv[4] = { 1.f, 1.414f, 1.581f, 1.732f };
279 assert(2 + FM_RATIOBITS - FM_PGBITS >= 0);
280 double rr = dt2lv[h] * double(ratio_);
283 int mul = l ? l * 2 : 1;
284 multable_[h][l] = uint(mul * rr);
289 // ---------------------------------------------------------------------------
290 //
\83X
\83e
\81[
\83g
\83Z
\81[
\83u
292 #define CHIP_STATE_VERSION 2
294 void Chip::DeclState(void *f)
296 p_logger = (CSP_Logger *)f;
297 state_entry = new csp_state_utils(CHIP_STATE_VERSION, chip_num, _T("FMGEN::Chip::"), p_logger);
299 DECL_STATE_ENTRY_UINT32(ratio_);
300 DECL_STATE_ENTRY_UINT32(aml_);
301 DECL_STATE_ENTRY_UINT32(pml_);
302 DECL_STATE_ENTRY_INT32(pmv_);
305 void Chip::SaveState(void *f)
307 FILEIO *state_fio = (FILEIO *)f;
309 if(state_entry != NULL) {
310 state_entry->save_state(state_fio);
312 // state_fio->FputUint32_BE(CHIP_STATE_VERSION);
314 // state_fio->FputUint32_BE(ratio_);
315 // state_fio->FputUint32_BE(aml_);
316 // state_fio->FputUint32_BE(pml_);
317 // state_fio->FputInt32_BE(pmv_);
320 bool Chip::LoadState(void *f)
323 FILEIO *state_fio = (FILEIO *)f;
325 if(state_entry != NULL) {
326 mb = state_entry->load_state(state_fio);
328 if(!mb) return false;
329 // if(state_fio->FgetUint32_BE() != CHIP_STATE_VERSION) {
332 // ratio_ = state_fio->FgetUint32_BE();
333 // aml_ = state_fio->FgetUint32_BE();
334 // pml_ = state_fio->FgetUint32_BE();
335 // pmv_ = state_fio->FgetInt32_BE();
340 // ---------------------------------------------------------------------------
343 bool FM::Operator::tablehasmade = false;
344 uint FM::Operator::sinetable[1024];
345 int32 FM::Operator::cltable[FM_CLENTS];
348 FM::Operator::Operator()
354 static int __num = 0;
355 operators_num = __num++;
357 ar_ = dr_ = sr_ = rr_ = key_scale_rate_ = 0;
358 ams_ = amtable[0][0];
376 void FM::Operator::Reset()
379 tl_ = tl_latch_ = 127;
391 param_changed_ = true;
395 void Operator::MakeTable()
397 //
\91Î
\90\94\83e
\81[
\83u
\83\8b\82Ì
\8dì
\90¬
398 assert(FM_CLENTS >= 256);
402 for (i=0; i<256; i++)
404 int v = int(floor(pow(2., 13. - i / 256.)));
409 while (p < cltable + FM_CLENTS)
415 // for (i=0; i<13*256; i++)
416 // printf("%4d, %d, %d\n", i, cltable[i*2], cltable[i*2+1]);
418 //
\83T
\83C
\83\93\83e
\81[
\83u
\83\8b\82Ì
\8dì
\90¬
419 double log2 = log(2.);
420 for (i=0; i<FM_OPSINENTS/2; i++)
422 double r = (i * 2 + 1) * FM_PI / FM_OPSINENTS;
423 double q = -256 * log(sin(r)) / log2;
424 uint s = (int) (floor(q + 0.5)) + 1;
425 // printf("%d, %d\n", s, cltable[s * 2] / 8);
426 sinetable[i] = s * 2 ;
427 sinetable[FM_OPSINENTS / 2 + i] = s * 2 + 1;
430 ::FM::MakeLFOTable();
437 inline void FM::Operator::SetDPBN(uint dp, uint bn)
439 dp_ = dp, bn_ = bn; param_changed_ = true;
445 void Operator::Prepare()
449 param_changed_ = false;
451 pg_diff_ = (dp_ + dttable[detune_ + bn_]) * chip_->GetMulValue(detune2_, multiple_);
452 pg_diff_ >>= (2 + FM_RATIOBITS - FM_PGBITS);
453 pg_diff_lfo_ = pg_diff_ >> 11;
456 key_scale_rate_ = bn_ >> (3-ks_);
457 tl_out_ = mute_ ? 0x3ff : tl_ * 8;
462 SetEGRate(ar_ ? Min(63, ar_ + key_scale_rate_) : 0);
465 SetEGRate(dr_ ? Min(63, dr_ + key_scale_rate_) : 0);
466 eg_level_on_next_phase_ = sl_ * 8;
469 SetEGRate(sr_ ? Min(63, sr_ + key_scale_rate_) : 0);
472 SetEGRate(Min(63, rr_ + key_scale_rate_));
477 if (ssg_type_ && (eg_phase_ != release))
479 int m = ar_ >= (uint)((ssg_type_ == 8 || ssg_type_ == 12) ? 56 : 60);
481 if (ssg_phase_ == -1) // XXX quick fix
483 assert(0 <= ssg_phase_ && ssg_phase_ <= 2);
484 const int* table = ssgenvtable[ssg_type_ & 7][m][ssg_phase_];
486 ssg_offset_ = table[0] * 0x200;
487 ssg_vector_ = table[1];
490 ams_ = amtable[type_][amon_ ? (ms_ >> 4) & 3 : 0];
496 // envelop
\82Ì eg_phase_
\95Ï
\8dX
497 void Operator::ShiftPhase(EGPhase nextphase)
501 case attack: // Attack Phase
505 ssg_phase_ = ssg_phase_ + 1;
509 int m = ar_ >= (uint)((ssg_type_ == 8 || ssg_type_ == 12) ? 56 : 60);
511 assert(0 <= ssg_phase_ && ssg_phase_ <= 2);
512 const int* table = ssgenvtable[ssg_type_ & 7][m][ssg_phase_];
514 ssg_offset_ = table[0] * 0x200;
515 ssg_vector_ = table[1];
517 if ((ar_ + key_scale_rate_) < 62)
519 SetEGRate(ar_ ? Min(63, ar_ + key_scale_rate_) : 0);
523 case decay: // Decay Phase
527 eg_level_on_next_phase_ = ssg_type_ ? Min(sl_ * 8, 0x200) : sl_ * 8;
529 SetEGRate(dr_ ? Min(63, dr_ + key_scale_rate_) : 0);
533 case sustain: // Sustain Phase
535 eg_level_on_next_phase_ = ssg_type_ ? 0x200 : 0x400;
537 SetEGRate(sr_ ? Min(63, sr_ + key_scale_rate_) : 0);
541 case release: // Release Phase
544 eg_level_ = eg_level_ * ssg_vector_ + ssg_offset_;
548 if (eg_phase_ == attack || (eg_level_ < FM_EG_BOTTOM)) //0x400/* && eg_phase_ != off*/))
550 eg_level_on_next_phase_ = 0x400;
551 SetEGRate(Min(63, rr_ + key_scale_rate_));
557 eg_level_ = FM_EG_BOTTOM;
558 eg_level_on_next_phase_ = FM_EG_BOTTOM;
567 void Operator::SetFNum(uint f)
569 dp_ = (f & 2047) << ((f >> 11) & 7);
570 bn_ = notetable[(f >> 7) & 127];
571 param_changed_ = true;
575 //
\82P
\83T
\83\93\83v
\83\8b\8d\87\90¬
577 // ISample
\82ð envelop count (2
\83Î)
\82É
\95Ï
\8a·
\82·
\82é
\83V
\83t
\83g
\97Ê
578 #define IS2EC_SHIFT ((20 + FM_PGBITS) - 13)
581 //
\93ü
\97Í: s = 20+FM_PGBITS = 29
582 #define Sine(s) sinetable[((s) >> (20+FM_PGBITS-FM_OPSINBITS))&(FM_OPSINENTS-1)]
583 #define SINE(s) sinetable[(s) & (FM_OPSINENTS-1)]
585 inline FM::ISample Operator::LogToLin(uint a)
587 #if 1 // FM_CLENTS < 0xc00 // 400 for TL, 400 for ENV, 400 for LFO.
588 return (a < FM_CLENTS) ? cltable[a] : 0;
594 inline void Operator::EGUpdate()
598 eg_out_ = Min(tl_out_ + eg_level_, 0x3ff) << (1 + 2);
602 eg_out_ = Min(tl_out_ + eg_level_ * ssg_vector_ + ssg_offset_, 0x3ff) << (1 + 2);
606 inline void Operator::SetEGRate(uint rate)
609 eg_count_diff_ = decaytable2[rate / 4] * chip_->GetRatio();
613 void FM::Operator::EGCalc()
615 eg_count_ = (2047 * 3) << FM_RATIOBITS; // ##
\82±
\82Ì
\8eè
\94²
\82«
\82Í
\8dÄ
\8c»
\90«
\82ð
\92á
\89º
\82³
\82¹
\82é
617 if (eg_phase_ == attack)
619 int c = attacktable[eg_rate_][eg_curve_count_ & 7];
622 eg_level_ -= 1 + (eg_level_ >> c);
632 eg_level_ += decaytable1[eg_rate_][eg_curve_count_ & 7];
633 if (eg_level_ >= eg_level_on_next_phase_)
634 ShiftPhase(EGPhase(eg_phase_+1));
639 eg_level_ += 4 * decaytable1[eg_rate_][eg_curve_count_ & 7];
640 if (eg_level_ >= eg_level_on_next_phase_)
661 inline void FM::Operator::EGStep()
663 eg_count_ -= eg_count_diff_;
665 // 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é
671 // ret:2^(20+PGBITS) / cycle
672 inline uint32 FM::Operator::PGCalc()
674 uint32 ret = pg_count_;
675 pg_count_ += pg_diff_;
680 inline uint32 FM::Operator::PGCalcL()
682 uint32 ret = pg_count_;
683 pg_count_ += pg_diff_ + ((pg_diff_lfo_ * chip_->GetPMV()) >> 5);// & -(1 << (2+IS2EC_SHIFT)));
685 return ret /* + pmv * pg_diff_;*/;
689 // in: ISample (
\8dÅ
\91å 8
\83Î)
690 inline FM::ISample FM::Operator::Calc(ISample in)
695 int pgin = PGCalc() >> (20+FM_PGBITS-FM_OPSINBITS);
696 pgin += in >> (20+FM_PGBITS-FM_OPSINBITS-(2+IS2EC_SHIFT));
697 out_ = LogToLin(eg_out_ + SINE(pgin));
703 inline FM::ISample FM::Operator::CalcL(ISample in)
707 int pgin = PGCalcL() >> (20+FM_PGBITS-FM_OPSINBITS);
708 pgin += in >> (20+FM_PGBITS-FM_OPSINBITS-(2+IS2EC_SHIFT));
709 out_ = LogToLin(eg_out_ + SINE(pgin) + ams_[chip_->GetAML()]);
715 inline FM::ISample FM::Operator::CalcN(uint noise)
719 int lv = Max(0, 0x3ff - (tl_out_ + eg_level_)) << 1;
721 // noise & 1 ? lv : -lv
\82Æ
\93\99\89¿
722 noise = (noise & 1) - 1;
723 out_ = (lv + noise) ^ noise;
730 // Self Feedback
\82Ì
\95Ï
\92²
\8dÅ
\91å = 4
\83Î
731 inline FM::ISample FM::Operator::CalcFB(uint fb)
735 ISample in = out_ + out2_;
738 int pgin = PGCalc() >> (20+FM_PGBITS-FM_OPSINBITS);
741 pgin += ((in << (1 + IS2EC_SHIFT)) >> fb) >> (20+FM_PGBITS-FM_OPSINBITS);
743 out_ = LogToLin(eg_out_ + SINE(pgin));
749 inline FM::ISample FM::Operator::CalcFBL(uint fb)
753 ISample in = out_ + out2_;
756 int pgin = PGCalcL() >> (20+FM_PGBITS-FM_OPSINBITS);
759 pgin += ((in << (1 + IS2EC_SHIFT)) >> fb) >> (20+FM_PGBITS-FM_OPSINBITS);
762 out_ = LogToLin(eg_out_ + SINE(pgin) + ams_[chip_->GetAML()]);
768 // ---------------------------------------------------------------------------
769 //
\83X
\83e
\81[
\83g
\83Z
\81[
\83u
771 #define OPERATOR_STATE_VERSION 2
773 void Operator::DeclState(void *f)
775 p_logger = (CSP_Logger *)f;
776 state_entry = new csp_state_utils(OPERATOR_STATE_VERSION, operators_num, _T("FMGEN::Operator::"), p_logger);
778 DECL_STATE_ENTRY_INT32(out_);
779 DECL_STATE_ENTRY_INT32(out2_);
780 DECL_STATE_ENTRY_INT32(in2_);
781 DECL_STATE_ENTRY_UINT32(dp_);
782 DECL_STATE_ENTRY_UINT32(detune_);
783 DECL_STATE_ENTRY_UINT32(detune2_);
784 DECL_STATE_ENTRY_UINT32(multiple_);
785 DECL_STATE_ENTRY_UINT32(pg_count_);
786 DECL_STATE_ENTRY_UINT32(pg_diff_);
787 DECL_STATE_ENTRY_INT32(pg_diff_lfo_);
788 DECL_STATE_ENTRY_UINT32(bn_);
789 DECL_STATE_ENTRY_INT32(eg_level_);
790 DECL_STATE_ENTRY_INT32(eg_level_on_next_phase_);
791 DECL_STATE_ENTRY_INT32(eg_count_);
792 DECL_STATE_ENTRY_INT32(eg_count_diff_);
793 DECL_STATE_ENTRY_INT32(eg_out_);
794 DECL_STATE_ENTRY_INT32(tl_out_);
795 DECL_STATE_ENTRY_INT32(eg_rate_);
796 DECL_STATE_ENTRY_INT32(eg_curve_count_);
797 DECL_STATE_ENTRY_INT32(ssg_offset_);
798 DECL_STATE_ENTRY_INT32(ssg_vector_);
799 DECL_STATE_ENTRY_INT32(ssg_phase_);
800 DECL_STATE_ENTRY_UINT32(key_scale_rate_);
801 DECL_STATE_ENTRY_INT32(tmp_eg_phase);
802 DECL_STATE_ENTRY_INT32(tmp_ams);
803 DECL_STATE_ENTRY_UINT32(ms_);
804 DECL_STATE_ENTRY_UINT32(tl_);
805 DECL_STATE_ENTRY_UINT32(tl_latch_);
806 DECL_STATE_ENTRY_UINT32(ar_);
807 DECL_STATE_ENTRY_UINT32(dr_);
808 DECL_STATE_ENTRY_UINT32(sr_);
809 DECL_STATE_ENTRY_UINT32(sl_);
810 DECL_STATE_ENTRY_UINT32(rr_);
811 DECL_STATE_ENTRY_UINT32(ks_);
812 DECL_STATE_ENTRY_UINT32(ssg_type_);
813 DECL_STATE_ENTRY_BOOL(keyon_);
814 DECL_STATE_ENTRY_BOOL(amon_);
815 DECL_STATE_ENTRY_BOOL(param_changed_);
816 DECL_STATE_ENTRY_BOOL(mute_);
817 DECL_STATE_ENTRY_INT32(dbgopout_);
818 DECL_STATE_ENTRY_INT32(dbgpgout_);
821 void Operator::SaveState(void *f)
823 FILEIO *state_fio = (FILEIO *)f;
825 tmp_eg_phase = static_cast<int>(eg_phase_);
826 tmp_ams = (int)(ams_ - &amtable[0][0][0]);
827 if(state_entry != NULL) {
828 state_entry->save_state(state_fio);
830 // state_fio->FputUint32_BE(OPERATOR_STATE_VERSION);
832 // state_fio->FputInt32_BE(out_);
833 // state_fio->FputInt32_BE(out2_);
834 // state_fio->FputInt32_BE(in2_);
835 // state_fio->FputUint32_BE(dp_);
836 // state_fio->FputUint32_BE(detune_);
837 // state_fio->FputUint32_BE(detune2_);
838 // state_fio->FputUint32_BE(multiple_);
839 // state_fio->FputUint32_BE(pg_count_);
840 // state_fio->FputUint32_BE(pg_diff_);
841 // state_fio->FputInt32_BE(pg_diff_lfo_);
842 // state_fio->FputUint32_BE(bn_);
843 // state_fio->FputInt32_BE(eg_level_);
844 // state_fio->FputInt32_BE(eg_level_on_next_phase_);
845 // state_fio->FputInt32_BE(eg_count_);
846 // state_fio->FputInt32_BE(eg_count_diff_);
847 // state_fio->FputInt32_BE(eg_out_);
848 // state_fio->FputInt32_BE(tl_out_);
849 // state_fio->FputInt32_BE(eg_rate_);
850 // state_fio->FputInt32_BE(eg_curve_count_);
851 // state_fio->FputInt32_BE(ssg_offset_);
852 // state_fio->FputInt32_BE(ssg_vector_);
853 // state_fio->FputInt32_BE(ssg_phase_);
854 // state_fio->FputUint32_BE(key_scale_rate_);
855 // state_fio->FputInt32_BE(static_cast<int>(eg_phase_));
856 // state_fio->FputInt32_BE((int)(ams_ - &amtable[0][0][0]));
857 // state_fio->FputUint32_BE(ms_);
858 // state_fio->FputUint32_BE(tl_);
859 // state_fio->FputUint32_BE(tl_latch_);
860 // state_fio->FputUint32_BE(ar_);
861 // state_fio->FputUint32_BE(dr_);
862 // state_fio->FputUint32_BE(sr_);
863 // state_fio->FputUint32_BE(sl_);
864 // state_fio->FputUint32_BE(rr_);
865 // state_fio->FputUint32_BE(ks_);
866 // state_fio->FputUint32_BE(ssg_type_);
867 // state_fio->FputBool(keyon_);
868 // state_fio->FputBool(amon_);
869 // state_fio->FputBool(param_changed_);
870 // state_fio->FputBool(mute_);
871 // state_fio->FputInt32_BE(dbgopout_);
872 // state_fio->FputInt32_BE(dbgpgout_);
875 bool Operator::LoadState(void *f)
877 FILEIO *state_fio = (FILEIO *)f;
879 if(state_entry != NULL) {
880 mb = state_entry->load_state(state_fio);
882 if(!mb) return false;
883 eg_phase_ = static_cast<EGPhase>(tmp_eg_phase);
884 ams_ = &amtable[0][0][0] + tmp_ams;
886 // if(state_fio->FgetUint32_BE() != OPERATOR_STATE_VERSION) {
889 // out_ = state_fio->FgetInt32_BE();
890 // out2_ = state_fio->FgetInt32_BE();
891 // in2_ = state_fio->FgetInt32_BE();
892 // dp_ = state_fio->FgetUint32_BE();
893 // detune_ = state_fio->FgetUint32_BE();
894 // detune2_ = state_fio->FgetUint32_BE();
895 // multiple_ = state_fio->FgetUint32_BE();
896 // pg_count_ = state_fio->FgetUint32_BE();
897 // pg_diff_ = state_fio->FgetUint32_BE();
898 // pg_diff_lfo_ = state_fio->FgetInt32_BE();
899 // bn_ = state_fio->FgetUint32_BE();
900 // eg_level_ = state_fio->FgetInt32_BE();
901 // eg_level_on_next_phase_ = state_fio->FgetInt32_BE();
902 // eg_count_ = state_fio->FgetInt32_BE();
903 // eg_count_diff_ = state_fio->FgetInt32_BE();
904 // eg_out_ = state_fio->FgetInt32_BE();
905 // tl_out_ = state_fio->FgetInt32_BE();
906 // eg_rate_ = state_fio->FgetInt32_BE();
907 // eg_curve_count_ = state_fio->FgetInt32_BE();
908 // ssg_offset_ = state_fio->FgetInt32_BE();
909 // ssg_vector_ = state_fio->FgetInt32_BE();
910 // ssg_phase_ = state_fio->FgetInt32_BE();
911 // key_scale_rate_ = state_fio->FgetUint32_BE();
912 // eg_phase_ = static_cast<EGPhase>(state_fio->FgetInt32_BE());
913 // ams_ = &amtable[0][0][0] + state_fio->FgetInt32_BE();
914 // ms_ = state_fio->FgetUint32_BE();
915 // tl_ = state_fio->FgetUint32_BE();
916 // tl_latch_ = state_fio->FgetUint32_BE();
917 // ar_ = state_fio->FgetUint32_BE();
918 // dr_ = state_fio->FgetUint32_BE();
919 // sr_ = state_fio->FgetUint32_BE();
920 // sl_ = state_fio->FgetUint32_BE();
921 // rr_ = state_fio->FgetUint32_BE();
922 // ks_ = state_fio->FgetUint32_BE();
923 // ssg_type_ = state_fio->FgetUint32_BE();
924 // keyon_ = state_fio->FgetBool();
925 // amon_ = state_fio->FgetBool();
926 // param_changed_ = state_fio->FgetBool();
927 // mute_ = state_fio->FgetBool();
928 // dbgopout_ = state_fio->FgetInt32_BE();
929 // dbgpgout_ = state_fio->FgetInt32_BE();
935 // ---------------------------------------------------------------------------
938 const uint8 Channel4::fbtable[8] = { 31, 7, 6, 5, 4, 3, 2, 1 };
939 int Channel4::kftable[64];
941 bool Channel4::tablehasmade = false;
949 static int __num = 0;
950 channel4s_num = __num++;
956 void Channel4::MakeTable()
958 // 100/64 cent = 2^(i*100/64*1200)
959 for (int i=0; i<64; i++)
961 kftable[i] = int(0x10000 * pow(2., i / 768.) );
965 //
\83\8a\83Z
\83b
\83g
966 void Channel4::Reset()
975 int Channel4::Prepare()
982 pms = pmtable[op[0].type_][op[0].ms_ & 7];
983 int key = (op[0].IsOn() | op[1].IsOn() | op[2].IsOn() | op[3].IsOn()) ? 1 : 0;
984 int lfo = op[0].ms_ & (op[0].amon_ | op[1].amon_ | op[2].amon_ | op[3].amon_ ? 0x37 : 7) ? 2 : 0;
988 // F-Number/BLOCK
\82ð
\90Ý
\92è
989 void Channel4::SetFNum(uint f)
991 for (int i=0; i<4; i++)
995 // KC/KF
\82ð
\90Ý
\92è
996 void Channel4::SetKCKF(uint kc, uint kf)
998 const static uint kctable[16] =
1000 5197, 5506, 5833, 6180, 6180, 6547, 6937, 7349,
1001 7349, 7786, 8249, 8740, 8740, 9259, 9810, 10394,
1004 int oct = 19 - ((kc >> 4) & 7);
1006 //printf("%p", this);
1007 uint kcv = kctable[kc & 0x0f];
1008 kcv = (kcv + 2) / 4 * 4;
1009 //printf(" %.4x", kcv);
1010 uint dp = kcv * kftable[kf & 0x3f];
1011 //printf(" %.4x %.4x %.8x", kcv, kftable[kf & 0x3f], dp >> oct);
1015 uint bn = (kc >> 2) & 31;
1016 op[0].SetDPBN(dp, bn);
1017 op[1].SetDPBN(dp, bn);
1018 op[2].SetDPBN(dp, bn);
1019 op[3].SetDPBN(dp, bn);
1020 //printf(" %.8x\n", dp);
1024 void Channel4::KeyControl(uint key)
1026 if (key & 0x1) op[0].KeyOn(); else op[0].KeyOff();
1027 if (key & 0x2) op[1].KeyOn(); else op[1].KeyOff();
1028 if (key & 0x4) op[2].KeyOn(); else op[2].KeyOff();
1029 if (key & 0x8) op[3].KeyOn(); else op[3].KeyOff();
1032 //
\83A
\83\8b\83S
\83\8a\83Y
\83\80\82ð
\90Ý
\92è
1033 void Channel4::SetAlgorithm(uint algo)
1035 static const uint8 table1[8][6] =
1037 { 0, 1, 1, 2, 2, 3 }, { 1, 0, 0, 1, 1, 2 },
1038 { 1, 1, 1, 0, 0, 2 }, { 0, 1, 2, 1, 1, 2 },
1039 { 0, 1, 2, 2, 2, 1 }, { 0, 1, 0, 1, 0, 1 },
1040 { 0, 1, 2, 1, 2, 1 }, { 1, 0, 1, 0, 1, 0 },
1043 in [0] = &buf[table1[algo][0]];
1044 out[0] = &buf[table1[algo][1]];
1045 in [1] = &buf[table1[algo][2]];
1046 out[1] = &buf[table1[algo][3]];
1047 in [2] = &buf[table1[algo][4]];
1048 out[2] = &buf[table1[algo][5]];
1055 ISample Channel4::Calc()
1061 op[2].Calc(op[1].Out());
1062 op[1].Calc(op[0].Out());
1063 r = op[3].Calc(op[2].Out());
1067 op[2].Calc(op[0].Out() + op[1].Out());
1069 r = op[3].Calc(op[2].Out());
1073 op[2].Calc(op[1].Out());
1075 r = op[3].Calc(op[0].Out() + op[2].Out());
1080 op[1].Calc(op[0].Out());
1081 r = op[3].Calc(op[1].Out() + op[2].Out());
1086 r = op[1].Calc(op[0].Out());
1087 r += op[3].Calc(op[2].Out());
1091 r = op[2].Calc(op[0].Out());
1092 r += op[1].Calc(op[0].Out());
1093 r += op[3].Calc(op[0].Out());
1098 r += op[1].Calc(op[0].Out());
1106 r += op[0].CalcFB(fb);
1113 ISample Channel4::CalcL()
1115 chip_->SetPMV(pms[chip_->GetPML()]);
1121 op[2].CalcL(op[1].Out());
1122 op[1].CalcL(op[0].Out());
1123 r = op[3].CalcL(op[2].Out());
1127 op[2].CalcL(op[0].Out() + op[1].Out());
1129 r = op[3].CalcL(op[2].Out());
1133 op[2].CalcL(op[1].Out());
1135 r = op[3].CalcL(op[0].Out() + op[2].Out());
1140 op[1].CalcL(op[0].Out());
1141 r = op[3].CalcL(op[1].Out() + op[2].Out());
1146 r = op[1].CalcL(op[0].Out());
1147 r += op[3].CalcL(op[2].Out());
1151 r = op[2].CalcL(op[0].Out());
1152 r += op[1].CalcL(op[0].Out());
1153 r += op[3].CalcL(op[0].Out());
1158 r += op[1].CalcL(op[0].Out());
1159 r += op[3].CalcL(0);
1164 r += op[1].CalcL(0);
1165 r += op[3].CalcL(0);
1166 r += op[0].CalcFBL(fb);
1173 ISample Channel4::CalcN(uint noise)
1175 buf[1] = buf[2] = buf[3] = 0;
1177 buf[0] = op[0].out_; op[0].CalcFB(fb);
1178 *out[0] += op[1].Calc(*in[0]);
1179 *out[1] += op[2].Calc(*in[1]);
1186 ISample Channel4::CalcLN(uint noise)
1188 chip_->SetPMV(pms[chip_->GetPML()]);
1189 buf[1] = buf[2] = buf[3] = 0;
1191 buf[0] = op[0].out_; op[0].CalcFBL(fb);
1192 *out[0] += op[1].CalcL(*in[0]);
1193 *out[1] += op[2].CalcL(*in[1]);
1199 // ---------------------------------------------------------------------------
1200 //
\83X
\83e
\81[
\83g
\83Z
\81[
\83u
1202 #define CHANNEL4_STATE_VERSION 2
1203 void Channel4::DeclState(void *f)
1205 p_logger = (CSP_Logger *)f;
1206 state_entry = new csp_state_utils(CHIP_STATE_VERSION, channel4s_num, _T("FMGEN::Channel4::"), p_logger);
1208 DECL_STATE_ENTRY_UINT32(fb);
1209 DECL_STATE_ENTRY_1D_ARRAY(buf, sizeof(buf) / sizeof(int));
1210 for(int i = 0; i < 3; i++) {
1211 DECL_STATE_ENTRY_INT32_MEMBER((tmp_in_bufptr[i]), i);
1212 DECL_STATE_ENTRY_INT32_MEMBER((tmp_out_bufptr[i]), i);
1214 DECL_STATE_ENTRY_INT32(tmp_pms);
1215 DECL_STATE_ENTRY_INT32(algo_);
1216 for(int i = 0; i < 4; i++) {
1221 void Channel4::SaveState(void *f)
1223 FILEIO *state_fio = (FILEIO *)f;
1225 for(int i = 0; i < 3; i++) {
1226 tmp_in_bufptr[i] = (int)(in[i] - &buf[0]);
1227 tmp_out_bufptr[i] = (int)(out[i] - &buf[0]);
1229 tmp_pms = (int)(pms - &pmtable[0][0][0]);
1230 if(state_entry != NULL) {
1231 state_entry->save_state(state_fio);
1233 // state_fio->FputUint32_BE(CHANNEL4_STATE_VERSION);
1235 // state_fio->FputUint32_BE(fb);
1236 // state_fio->Fwrite(buf, sizeof(buf), 1);
1237 // for(int i = 0; i < 3; i++) {
1238 // tmp_in_buffer[i] = (int)(in[i] - &buf[0])
1239 // tmp_out_buffer[i] = (int)(out[i] - &buf[0])
1240 // state_fio->FputInt32_BE((int)(in [i] - &buf[0]));
1241 // state_fio->FputInt32_BE((int)(out[i] - &buf[0]));
1243 // tmp_pms = (int)(pms - &pmtable[0][0][0]);
1244 // state_fio->FputInt32_BE((int)(pms - &pmtable[0][0][0]));
1245 // state_fio->FputInt32_BE(algo_);
1246 for(int i = 0; i < 4; i++) {
1251 bool Channel4::LoadState(void *f)
1253 FILEIO *state_fio = (FILEIO *)f;
1256 if(state_entry != NULL) {
1257 mb = state_entry->load_state(state_fio);
1259 if(!mb) return false;
1260 // if(state_fio->FgetUint32_BE() != CHANNEL4_STATE_VERSION) {
1263 //fb = state_fio->FgetUint32_BE();
1264 //state_fio->Fread(buf, sizeof(buf), 1);
1265 //for(int i = 0; i < 3; i++) {
1266 // in [i] = &buf[0] + state_fio->FgetInt32_BE();
1267 // out[i] = &buf[0] + state_fio->FgetInt32_BE();
1269 //pms = &pmtable[0][0][0] + state_fio->FgetInt32_BE();
1270 //algo_ = state_fio->FgetInt32_BE();
1272 for(int i = 0; i < 3; i++) {
1273 in[i] = &buf[0] + tmp_in_bufptr[i];
1274 out[i] = &buf[0] + tmp_in_bufptr[i];
1276 pms =&pmtable[0][0][0] + tmp_pms;
1277 for(int i = 0; i < 4; i++) {
1278 if(!op[i].LoadState(f)) {