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;
184 // ---------------------------------------------------------------------------
185 //
\83e
\81[
\83u
\83\8b\8dì
\90¬
196 static const double pms[2][8] =
198 { 0, 1/360., 2/360., 3/360., 4/360., 6/360., 12/360., 24/360., }, // OPNA
199 // { 0, 1/240., 2/240., 4/240., 10/240., 20/240., 80/240., 140/240., }, // OPM
200 { 0, 1/480., 2/480., 4/480., 10/480., 20/480., 80/480., 140/480., }, // OPM
201 // { 0, 1/960., 2/960., 4/960., 10/960., 20/960., 80/960., 140/960., }, // OPM
203 // 3 6, 12 30 60 240 420 / 720
205 // lfofref[level * max * wave];
206 // pre = lfofref[level][pms * wave >> 8];
207 static const volatile uint8 amt[2][4] =
209 { 31, 6, 4, 3 }, // OPNA
210 { 31, 2, 1, 0 }, // OPM
213 for (int type = 0; type < 2; type++)
217 double pmb = pms[type][i];
218 for (int j=0; j<FM_LFOENTS; j++)
220 // double v = pow(2.0, pmb * (2 * j - FM_LFOENTS+1) / (FM_LFOENTS-1));
221 double w = 0.6 * pmb * sin(2 * j * 3.14159265358979323846 / FM_LFOENTS) + 1;
222 // pmtable[type][i][j] = int(0x10000 * (v - 1));
224 pmtable[type][i][j] = int(0x10000 * (w - 1));
226 // pmtable[type][i][j] = int(0x10000 * (v - 1));
228 // printf("pmtable[%d][%d][%.2x] = %5d %7.5f %7.5f\n", type, i, j, pmtable[type][i][j], v, w);
233 for (int j=0; j<FM_LFOENTS; j++)
235 amtable[type][i][j] = (((j * 4) >> amt[type][i]) * 2) << 2;
242 // ---------------------------------------------------------------------------
243 //
\83`
\83b
\83v
\93à
\82Å
\8b¤
\92Ê
\82È
\95\94\95ª
246 : ratio_(0), aml_(0), pml_(0), pmv_(0), optype_(typeN)
248 static int __num = 0;
252 //
\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¬
253 void Chip::SetRatio(uint ratio)
262 void Chip::MakeTable()
267 static const float dt2lv[4] = { 1.f, 1.414f, 1.581f, 1.732f };
270 assert(2 + FM_RATIOBITS - FM_PGBITS >= 0);
271 double rr = dt2lv[h] * double(ratio_);
274 int mul = l ? l * 2 : 1;
275 multable_[h][l] = uint(mul * rr);
280 // ---------------------------------------------------------------------------
281 //
\83X
\83e
\81[
\83g
\83Z
\81[
\83u
283 #define CHIP_STATE_VERSION 2
285 void Chip::DeclState(void *f)
287 p_logger = (CSP_Logger *)f;
288 state_entry = new csp_state_utils(CHIP_STATE_VERSION, chip_num, _T("FMGEN::Chip::"), p_logger);
290 DECL_STATE_ENTRY_UINT32(ratio_);
291 DECL_STATE_ENTRY_UINT32(aml_);
292 DECL_STATE_ENTRY_UINT32(pml_);
293 DECL_STATE_ENTRY_INT32(pmv_);
296 void Chip::SaveState(void *f)
298 FILEIO *state_fio = (FILEIO *)f;
300 if(state_entry != NULL) {
301 state_entry->save_state(state_fio);
303 // state_fio->FputUint32_BE(CHIP_STATE_VERSION);
305 // state_fio->FputUint32_BE(ratio_);
306 // state_fio->FputUint32_BE(aml_);
307 // state_fio->FputUint32_BE(pml_);
308 // state_fio->FputInt32_BE(pmv_);
311 bool Chip::LoadState(void *f)
314 FILEIO *state_fio = (FILEIO *)f;
316 if(state_entry != NULL) {
317 mb = state_entry->load_state(state_fio);
319 if(!mb) return false;
320 // if(state_fio->FgetUint32_BE() != CHIP_STATE_VERSION) {
323 // ratio_ = state_fio->FgetUint32_BE();
324 // aml_ = state_fio->FgetUint32_BE();
325 // pml_ = state_fio->FgetUint32_BE();
326 // pmv_ = state_fio->FgetInt32_BE();
331 // ---------------------------------------------------------------------------
334 bool FM::Operator::tablehasmade = false;
335 uint FM::Operator::sinetable[1024];
336 int32 FM::Operator::cltable[FM_CLENTS];
339 FM::Operator::Operator()
345 static int __num = 0;
346 operators_num = __num++;
348 ar_ = dr_ = sr_ = rr_ = key_scale_rate_ = 0;
349 ams_ = amtable[0][0];
367 void FM::Operator::Reset()
370 tl_ = tl_latch_ = 127;
382 param_changed_ = true;
386 void Operator::MakeTable()
388 //
\91Î
\90\94\83e
\81[
\83u
\83\8b\82Ì
\8dì
\90¬
389 assert(FM_CLENTS >= 256);
393 for (i=0; i<256; i++)
395 int v = int(floor(pow(2., 13. - i / 256.)));
400 while (p < cltable + FM_CLENTS)
406 // for (i=0; i<13*256; i++)
407 // printf("%4d, %d, %d\n", i, cltable[i*2], cltable[i*2+1]);
409 //
\83T
\83C
\83\93\83e
\81[
\83u
\83\8b\82Ì
\8dì
\90¬
410 double log2 = log(2.);
411 for (i=0; i<FM_OPSINENTS/2; i++)
413 double r = (i * 2 + 1) * FM_PI / FM_OPSINENTS;
414 double q = -256 * log(sin(r)) / log2;
415 uint s = (int) (floor(q + 0.5)) + 1;
416 // printf("%d, %d\n", s, cltable[s * 2] / 8);
417 sinetable[i] = s * 2 ;
418 sinetable[FM_OPSINENTS / 2 + i] = s * 2 + 1;
421 ::FM::MakeLFOTable();
428 inline void FM::Operator::SetDPBN(uint dp, uint bn)
430 dp_ = dp, bn_ = bn; param_changed_ = true;
436 void Operator::Prepare()
440 param_changed_ = false;
442 pg_diff_ = (dp_ + dttable[detune_ + bn_]) * chip_->GetMulValue(detune2_, multiple_);
443 pg_diff_ >>= (2 + FM_RATIOBITS - FM_PGBITS);
444 pg_diff_lfo_ = pg_diff_ >> 11;
447 key_scale_rate_ = bn_ >> (3-ks_);
448 tl_out_ = mute_ ? 0x3ff : tl_ * 8;
453 SetEGRate(ar_ ? Min(63, ar_ + key_scale_rate_) : 0);
456 SetEGRate(dr_ ? Min(63, dr_ + key_scale_rate_) : 0);
457 eg_level_on_next_phase_ = sl_ * 8;
460 SetEGRate(sr_ ? Min(63, sr_ + key_scale_rate_) : 0);
463 SetEGRate(Min(63, rr_ + key_scale_rate_));
468 if (ssg_type_ && (eg_phase_ != release))
470 int m = ar_ >= (uint)((ssg_type_ == 8 || ssg_type_ == 12) ? 56 : 60);
472 if (ssg_phase_ == -1) // XXX quick fix
474 assert(0 <= ssg_phase_ && ssg_phase_ <= 2);
475 const int* table = ssgenvtable[ssg_type_ & 7][m][ssg_phase_];
477 ssg_offset_ = table[0] * 0x200;
478 ssg_vector_ = table[1];
481 ams_ = amtable[type_][amon_ ? (ms_ >> 4) & 3 : 0];
487 // envelop
\82Ì eg_phase_
\95Ï
\8dX
488 void Operator::ShiftPhase(EGPhase nextphase)
492 case attack: // Attack Phase
496 ssg_phase_ = ssg_phase_ + 1;
500 int m = ar_ >= (uint)((ssg_type_ == 8 || ssg_type_ == 12) ? 56 : 60);
502 assert(0 <= ssg_phase_ && ssg_phase_ <= 2);
503 const int* table = ssgenvtable[ssg_type_ & 7][m][ssg_phase_];
505 ssg_offset_ = table[0] * 0x200;
506 ssg_vector_ = table[1];
508 if ((ar_ + key_scale_rate_) < 62)
510 SetEGRate(ar_ ? Min(63, ar_ + key_scale_rate_) : 0);
514 case decay: // Decay Phase
518 eg_level_on_next_phase_ = ssg_type_ ? Min(sl_ * 8, 0x200) : sl_ * 8;
520 SetEGRate(dr_ ? Min(63, dr_ + key_scale_rate_) : 0);
524 case sustain: // Sustain Phase
526 eg_level_on_next_phase_ = ssg_type_ ? 0x200 : 0x400;
528 SetEGRate(sr_ ? Min(63, sr_ + key_scale_rate_) : 0);
532 case release: // Release Phase
535 eg_level_ = eg_level_ * ssg_vector_ + ssg_offset_;
539 if (eg_phase_ == attack || (eg_level_ < FM_EG_BOTTOM)) //0x400/* && eg_phase_ != off*/))
541 eg_level_on_next_phase_ = 0x400;
542 SetEGRate(Min(63, rr_ + key_scale_rate_));
548 eg_level_ = FM_EG_BOTTOM;
549 eg_level_on_next_phase_ = FM_EG_BOTTOM;
558 void Operator::SetFNum(uint f)
560 dp_ = (f & 2047) << ((f >> 11) & 7);
561 bn_ = notetable[(f >> 7) & 127];
562 param_changed_ = true;
566 //
\82P
\83T
\83\93\83v
\83\8b\8d\87\90¬
568 // ISample
\82ð envelop count (2
\83Î)
\82É
\95Ï
\8a·
\82·
\82é
\83V
\83t
\83g
\97Ê
569 #define IS2EC_SHIFT ((20 + FM_PGBITS) - 13)
572 //
\93ü
\97Í: s = 20+FM_PGBITS = 29
573 #define Sine(s) sinetable[((s) >> (20+FM_PGBITS-FM_OPSINBITS))&(FM_OPSINENTS-1)]
574 #define SINE(s) sinetable[(s) & (FM_OPSINENTS-1)]
576 inline FM::ISample Operator::LogToLin(uint a)
578 #if 1 // FM_CLENTS < 0xc00 // 400 for TL, 400 for ENV, 400 for LFO.
579 return (a < FM_CLENTS) ? cltable[a] : 0;
585 inline void Operator::EGUpdate()
589 eg_out_ = Min(tl_out_ + eg_level_, 0x3ff) << (1 + 2);
593 eg_out_ = Min(tl_out_ + eg_level_ * ssg_vector_ + ssg_offset_, 0x3ff) << (1 + 2);
597 inline void Operator::SetEGRate(uint rate)
600 eg_count_diff_ = decaytable2[rate / 4] * chip_->GetRatio();
604 void FM::Operator::EGCalc()
606 eg_count_ = (2047 * 3) << FM_RATIOBITS; // ##
\82±
\82Ì
\8eè
\94²
\82«
\82Í
\8dÄ
\8c»
\90«
\82ð
\92á
\89º
\82³
\82¹
\82é
608 if (eg_phase_ == attack)
610 int c = attacktable[eg_rate_][eg_curve_count_ & 7];
613 eg_level_ -= 1 + (eg_level_ >> c);
623 eg_level_ += decaytable1[eg_rate_][eg_curve_count_ & 7];
624 if (eg_level_ >= eg_level_on_next_phase_)
625 ShiftPhase(EGPhase(eg_phase_+1));
630 eg_level_ += 4 * decaytable1[eg_rate_][eg_curve_count_ & 7];
631 if (eg_level_ >= eg_level_on_next_phase_)
652 inline void FM::Operator::EGStep()
654 eg_count_ -= eg_count_diff_;
656 // 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é
662 // ret:2^(20+PGBITS) / cycle
663 inline uint32 FM::Operator::PGCalc()
665 uint32 ret = pg_count_;
666 pg_count_ += pg_diff_;
671 inline uint32 FM::Operator::PGCalcL()
673 uint32 ret = pg_count_;
674 pg_count_ += pg_diff_ + ((pg_diff_lfo_ * chip_->GetPMV()) >> 5);// & -(1 << (2+IS2EC_SHIFT)));
676 return ret /* + pmv * pg_diff_;*/;
680 // in: ISample (
\8dÅ
\91å 8
\83Î)
681 inline FM::ISample FM::Operator::Calc(ISample in)
686 int pgin = PGCalc() >> (20+FM_PGBITS-FM_OPSINBITS);
687 pgin += in >> (20+FM_PGBITS-FM_OPSINBITS-(2+IS2EC_SHIFT));
688 out_ = LogToLin(eg_out_ + SINE(pgin));
694 inline FM::ISample FM::Operator::CalcL(ISample in)
698 int pgin = PGCalcL() >> (20+FM_PGBITS-FM_OPSINBITS);
699 pgin += in >> (20+FM_PGBITS-FM_OPSINBITS-(2+IS2EC_SHIFT));
700 out_ = LogToLin(eg_out_ + SINE(pgin) + ams_[chip_->GetAML()]);
706 inline FM::ISample FM::Operator::CalcN(uint noise)
710 int lv = Max(0, 0x3ff - (tl_out_ + eg_level_)) << 1;
712 // noise & 1 ? lv : -lv
\82Æ
\93\99\89¿
713 noise = (noise & 1) - 1;
714 out_ = (lv + noise) ^ noise;
721 // Self Feedback
\82Ì
\95Ï
\92²
\8dÅ
\91å = 4
\83Î
722 inline FM::ISample FM::Operator::CalcFB(uint fb)
726 ISample in = out_ + out2_;
729 int pgin = PGCalc() >> (20+FM_PGBITS-FM_OPSINBITS);
732 pgin += ((in << (1 + IS2EC_SHIFT)) >> fb) >> (20+FM_PGBITS-FM_OPSINBITS);
734 out_ = LogToLin(eg_out_ + SINE(pgin));
740 inline FM::ISample FM::Operator::CalcFBL(uint fb)
744 ISample in = out_ + out2_;
747 int pgin = PGCalcL() >> (20+FM_PGBITS-FM_OPSINBITS);
750 pgin += ((in << (1 + IS2EC_SHIFT)) >> fb) >> (20+FM_PGBITS-FM_OPSINBITS);
753 out_ = LogToLin(eg_out_ + SINE(pgin) + ams_[chip_->GetAML()]);
759 // ---------------------------------------------------------------------------
760 //
\83X
\83e
\81[
\83g
\83Z
\81[
\83u
762 #define OPERATOR_STATE_VERSION 2
764 void Operator::DeclState(void *f)
766 p_logger = (CSP_Logger *)f;
767 state_entry = new csp_state_utils(OPERATOR_STATE_VERSION, operators_num, _T("FMGEN::Operator::"), p_logger);
769 DECL_STATE_ENTRY_INT32(out_);
770 DECL_STATE_ENTRY_INT32(out2_);
771 DECL_STATE_ENTRY_INT32(in2_);
772 DECL_STATE_ENTRY_UINT32(dp_);
773 DECL_STATE_ENTRY_UINT32(detune_);
774 DECL_STATE_ENTRY_UINT32(detune2_);
775 DECL_STATE_ENTRY_UINT32(multiple_);
776 DECL_STATE_ENTRY_UINT32(pg_count_);
777 DECL_STATE_ENTRY_UINT32(pg_diff_);
778 DECL_STATE_ENTRY_INT32(pg_diff_lfo_);
779 DECL_STATE_ENTRY_UINT32(bn_);
780 DECL_STATE_ENTRY_INT32(eg_level_);
781 DECL_STATE_ENTRY_INT32(eg_level_on_next_phase_);
782 DECL_STATE_ENTRY_INT32(eg_count_);
783 DECL_STATE_ENTRY_INT32(eg_count_diff_);
784 DECL_STATE_ENTRY_INT32(eg_out_);
785 DECL_STATE_ENTRY_INT32(tl_out_);
786 DECL_STATE_ENTRY_INT32(eg_rate_);
787 DECL_STATE_ENTRY_INT32(eg_curve_count_);
788 DECL_STATE_ENTRY_INT32(ssg_offset_);
789 DECL_STATE_ENTRY_INT32(ssg_vector_);
790 DECL_STATE_ENTRY_INT32(ssg_phase_);
791 DECL_STATE_ENTRY_UINT32(key_scale_rate_);
792 DECL_STATE_ENTRY_INT32(tmp_eg_phase);
793 DECL_STATE_ENTRY_INT32(tmp_ams);
794 DECL_STATE_ENTRY_UINT32(ms_);
795 DECL_STATE_ENTRY_UINT32(tl_);
796 DECL_STATE_ENTRY_UINT32(tl_latch_);
797 DECL_STATE_ENTRY_UINT32(ar_);
798 DECL_STATE_ENTRY_UINT32(dr_);
799 DECL_STATE_ENTRY_UINT32(sr_);
800 DECL_STATE_ENTRY_UINT32(sl_);
801 DECL_STATE_ENTRY_UINT32(rr_);
802 DECL_STATE_ENTRY_UINT32(ks_);
803 DECL_STATE_ENTRY_UINT32(ssg_type_);
804 DECL_STATE_ENTRY_BOOL(keyon_);
805 DECL_STATE_ENTRY_BOOL(amon_);
806 DECL_STATE_ENTRY_BOOL(param_changed_);
807 DECL_STATE_ENTRY_BOOL(mute_);
808 DECL_STATE_ENTRY_INT32(dbgopout_);
809 DECL_STATE_ENTRY_INT32(dbgpgout_);
812 void Operator::SaveState(void *f)
814 FILEIO *state_fio = (FILEIO *)f;
816 tmp_eg_phase = static_cast<int>(eg_phase_);
817 tmp_ams = (int)(ams_ - &amtable[0][0][0]);
818 if(state_entry != NULL) {
819 state_entry->save_state(state_fio);
821 // state_fio->FputUint32_BE(OPERATOR_STATE_VERSION);
823 // state_fio->FputInt32_BE(out_);
824 // state_fio->FputInt32_BE(out2_);
825 // state_fio->FputInt32_BE(in2_);
826 // state_fio->FputUint32_BE(dp_);
827 // state_fio->FputUint32_BE(detune_);
828 // state_fio->FputUint32_BE(detune2_);
829 // state_fio->FputUint32_BE(multiple_);
830 // state_fio->FputUint32_BE(pg_count_);
831 // state_fio->FputUint32_BE(pg_diff_);
832 // state_fio->FputInt32_BE(pg_diff_lfo_);
833 // state_fio->FputUint32_BE(bn_);
834 // state_fio->FputInt32_BE(eg_level_);
835 // state_fio->FputInt32_BE(eg_level_on_next_phase_);
836 // state_fio->FputInt32_BE(eg_count_);
837 // state_fio->FputInt32_BE(eg_count_diff_);
838 // state_fio->FputInt32_BE(eg_out_);
839 // state_fio->FputInt32_BE(tl_out_);
840 // state_fio->FputInt32_BE(eg_rate_);
841 // state_fio->FputInt32_BE(eg_curve_count_);
842 // state_fio->FputInt32_BE(ssg_offset_);
843 // state_fio->FputInt32_BE(ssg_vector_);
844 // state_fio->FputInt32_BE(ssg_phase_);
845 // state_fio->FputUint32_BE(key_scale_rate_);
846 // state_fio->FputInt32_BE(static_cast<int>(eg_phase_));
847 // state_fio->FputInt32_BE((int)(ams_ - &amtable[0][0][0]));
848 // state_fio->FputUint32_BE(ms_);
849 // state_fio->FputUint32_BE(tl_);
850 // state_fio->FputUint32_BE(tl_latch_);
851 // state_fio->FputUint32_BE(ar_);
852 // state_fio->FputUint32_BE(dr_);
853 // state_fio->FputUint32_BE(sr_);
854 // state_fio->FputUint32_BE(sl_);
855 // state_fio->FputUint32_BE(rr_);
856 // state_fio->FputUint32_BE(ks_);
857 // state_fio->FputUint32_BE(ssg_type_);
858 // state_fio->FputBool(keyon_);
859 // state_fio->FputBool(amon_);
860 // state_fio->FputBool(param_changed_);
861 // state_fio->FputBool(mute_);
862 // state_fio->FputInt32_BE(dbgopout_);
863 // state_fio->FputInt32_BE(dbgpgout_);
866 bool Operator::LoadState(void *f)
868 FILEIO *state_fio = (FILEIO *)f;
870 if(state_entry != NULL) {
871 mb = state_entry->load_state(state_fio);
873 if(!mb) return false;
874 eg_phase_ = static_cast<EGPhase>(tmp_eg_phase);
875 ams_ = &amtable[0][0][0] + tmp_ams;
877 // if(state_fio->FgetUint32_BE() != OPERATOR_STATE_VERSION) {
880 // out_ = state_fio->FgetInt32_BE();
881 // out2_ = state_fio->FgetInt32_BE();
882 // in2_ = state_fio->FgetInt32_BE();
883 // dp_ = state_fio->FgetUint32_BE();
884 // detune_ = state_fio->FgetUint32_BE();
885 // detune2_ = state_fio->FgetUint32_BE();
886 // multiple_ = state_fio->FgetUint32_BE();
887 // pg_count_ = state_fio->FgetUint32_BE();
888 // pg_diff_ = state_fio->FgetUint32_BE();
889 // pg_diff_lfo_ = state_fio->FgetInt32_BE();
890 // bn_ = state_fio->FgetUint32_BE();
891 // eg_level_ = state_fio->FgetInt32_BE();
892 // eg_level_on_next_phase_ = state_fio->FgetInt32_BE();
893 // eg_count_ = state_fio->FgetInt32_BE();
894 // eg_count_diff_ = state_fio->FgetInt32_BE();
895 // eg_out_ = state_fio->FgetInt32_BE();
896 // tl_out_ = state_fio->FgetInt32_BE();
897 // eg_rate_ = state_fio->FgetInt32_BE();
898 // eg_curve_count_ = state_fio->FgetInt32_BE();
899 // ssg_offset_ = state_fio->FgetInt32_BE();
900 // ssg_vector_ = state_fio->FgetInt32_BE();
901 // ssg_phase_ = state_fio->FgetInt32_BE();
902 // key_scale_rate_ = state_fio->FgetUint32_BE();
903 // eg_phase_ = static_cast<EGPhase>(state_fio->FgetInt32_BE());
904 // ams_ = &amtable[0][0][0] + state_fio->FgetInt32_BE();
905 // ms_ = state_fio->FgetUint32_BE();
906 // tl_ = state_fio->FgetUint32_BE();
907 // tl_latch_ = state_fio->FgetUint32_BE();
908 // ar_ = state_fio->FgetUint32_BE();
909 // dr_ = state_fio->FgetUint32_BE();
910 // sr_ = state_fio->FgetUint32_BE();
911 // sl_ = state_fio->FgetUint32_BE();
912 // rr_ = state_fio->FgetUint32_BE();
913 // ks_ = state_fio->FgetUint32_BE();
914 // ssg_type_ = state_fio->FgetUint32_BE();
915 // keyon_ = state_fio->FgetBool();
916 // amon_ = state_fio->FgetBool();
917 // param_changed_ = state_fio->FgetBool();
918 // mute_ = state_fio->FgetBool();
919 // dbgopout_ = state_fio->FgetInt32_BE();
920 // dbgpgout_ = state_fio->FgetInt32_BE();
926 // ---------------------------------------------------------------------------
929 const uint8 Channel4::fbtable[8] = { 31, 7, 6, 5, 4, 3, 2, 1 };
930 int Channel4::kftable[64];
932 bool Channel4::tablehasmade = false;
940 static int __num = 0;
941 channel4s_num = __num++;
947 void Channel4::MakeTable()
949 // 100/64 cent = 2^(i*100/64*1200)
950 for (int i=0; i<64; i++)
952 kftable[i] = int(0x10000 * pow(2., i / 768.) );
956 //
\83\8a\83Z
\83b
\83g
957 void Channel4::Reset()
966 int Channel4::Prepare()
973 pms = pmtable[op[0].type_][op[0].ms_ & 7];
974 int key = (op[0].IsOn() | op[1].IsOn() | op[2].IsOn() | op[3].IsOn()) ? 1 : 0;
975 int lfo = op[0].ms_ & (op[0].amon_ | op[1].amon_ | op[2].amon_ | op[3].amon_ ? 0x37 : 7) ? 2 : 0;
979 // F-Number/BLOCK
\82ð
\90Ý
\92è
980 void Channel4::SetFNum(uint f)
982 for (int i=0; i<4; i++)
986 // KC/KF
\82ð
\90Ý
\92è
987 void Channel4::SetKCKF(uint kc, uint kf)
989 const static uint kctable[16] =
991 5197, 5506, 5833, 6180, 6180, 6547, 6937, 7349,
992 7349, 7786, 8249, 8740, 8740, 9259, 9810, 10394,
995 int oct = 19 - ((kc >> 4) & 7);
997 //printf("%p", this);
998 uint kcv = kctable[kc & 0x0f];
999 kcv = (kcv + 2) / 4 * 4;
1000 //printf(" %.4x", kcv);
1001 uint dp = kcv * kftable[kf & 0x3f];
1002 //printf(" %.4x %.4x %.8x", kcv, kftable[kf & 0x3f], dp >> oct);
1006 uint bn = (kc >> 2) & 31;
1007 op[0].SetDPBN(dp, bn);
1008 op[1].SetDPBN(dp, bn);
1009 op[2].SetDPBN(dp, bn);
1010 op[3].SetDPBN(dp, bn);
1011 //printf(" %.8x\n", dp);
1015 void Channel4::KeyControl(uint key)
1017 if (key & 0x1) op[0].KeyOn(); else op[0].KeyOff();
1018 if (key & 0x2) op[1].KeyOn(); else op[1].KeyOff();
1019 if (key & 0x4) op[2].KeyOn(); else op[2].KeyOff();
1020 if (key & 0x8) op[3].KeyOn(); else op[3].KeyOff();
1023 //
\83A
\83\8b\83S
\83\8a\83Y
\83\80\82ð
\90Ý
\92è
1024 void Channel4::SetAlgorithm(uint algo)
1026 static const uint8 table1[8][6] =
1028 { 0, 1, 1, 2, 2, 3 }, { 1, 0, 0, 1, 1, 2 },
1029 { 1, 1, 1, 0, 0, 2 }, { 0, 1, 2, 1, 1, 2 },
1030 { 0, 1, 2, 2, 2, 1 }, { 0, 1, 0, 1, 0, 1 },
1031 { 0, 1, 2, 1, 2, 1 }, { 1, 0, 1, 0, 1, 0 },
1034 in [0] = &buf[table1[algo][0]];
1035 out[0] = &buf[table1[algo][1]];
1036 in [1] = &buf[table1[algo][2]];
1037 out[1] = &buf[table1[algo][3]];
1038 in [2] = &buf[table1[algo][4]];
1039 out[2] = &buf[table1[algo][5]];
1046 ISample Channel4::Calc()
1052 op[2].Calc(op[1].Out());
1053 op[1].Calc(op[0].Out());
1054 r = op[3].Calc(op[2].Out());
1058 op[2].Calc(op[0].Out() + op[1].Out());
1060 r = op[3].Calc(op[2].Out());
1064 op[2].Calc(op[1].Out());
1066 r = op[3].Calc(op[0].Out() + op[2].Out());
1071 op[1].Calc(op[0].Out());
1072 r = op[3].Calc(op[1].Out() + op[2].Out());
1077 r = op[1].Calc(op[0].Out());
1078 r += op[3].Calc(op[2].Out());
1082 r = op[2].Calc(op[0].Out());
1083 r += op[1].Calc(op[0].Out());
1084 r += op[3].Calc(op[0].Out());
1089 r += op[1].Calc(op[0].Out());
1097 r += op[0].CalcFB(fb);
1104 ISample Channel4::CalcL()
1106 chip_->SetPMV(pms[chip_->GetPML()]);
1112 op[2].CalcL(op[1].Out());
1113 op[1].CalcL(op[0].Out());
1114 r = op[3].CalcL(op[2].Out());
1118 op[2].CalcL(op[0].Out() + op[1].Out());
1120 r = op[3].CalcL(op[2].Out());
1124 op[2].CalcL(op[1].Out());
1126 r = op[3].CalcL(op[0].Out() + op[2].Out());
1131 op[1].CalcL(op[0].Out());
1132 r = op[3].CalcL(op[1].Out() + op[2].Out());
1137 r = op[1].CalcL(op[0].Out());
1138 r += op[3].CalcL(op[2].Out());
1142 r = op[2].CalcL(op[0].Out());
1143 r += op[1].CalcL(op[0].Out());
1144 r += op[3].CalcL(op[0].Out());
1149 r += op[1].CalcL(op[0].Out());
1150 r += op[3].CalcL(0);
1155 r += op[1].CalcL(0);
1156 r += op[3].CalcL(0);
1157 r += op[0].CalcFBL(fb);
1164 ISample Channel4::CalcN(uint noise)
1166 buf[1] = buf[2] = buf[3] = 0;
1168 buf[0] = op[0].out_; op[0].CalcFB(fb);
1169 *out[0] += op[1].Calc(*in[0]);
1170 *out[1] += op[2].Calc(*in[1]);
1177 ISample Channel4::CalcLN(uint noise)
1179 chip_->SetPMV(pms[chip_->GetPML()]);
1180 buf[1] = buf[2] = buf[3] = 0;
1182 buf[0] = op[0].out_; op[0].CalcFBL(fb);
1183 *out[0] += op[1].CalcL(*in[0]);
1184 *out[1] += op[2].CalcL(*in[1]);
1190 // ---------------------------------------------------------------------------
1191 //
\83X
\83e
\81[
\83g
\83Z
\81[
\83u
1193 #define CHANNEL4_STATE_VERSION 2
1194 void Channel4::DeclState(void *f)
1196 p_logger = (CSP_Logger *)f;
1197 state_entry = new csp_state_utils(CHIP_STATE_VERSION, channel4s_num, _T("FMGEN::Channel4::"), p_logger);
1199 DECL_STATE_ENTRY_UINT32(fb);
1200 DECL_STATE_ENTRY_1D_ARRAY(buf, sizeof(buf) / sizeof(int));
1201 for(int i = 0; i < 3; i++) {
1202 DECL_STATE_ENTRY_INT32_MEMBER((tmp_in_bufptr[i]), i);
1203 DECL_STATE_ENTRY_INT32_MEMBER((tmp_out_bufptr[i]), i);
1205 DECL_STATE_ENTRY_INT32(tmp_pms);
1206 DECL_STATE_ENTRY_INT32(algo_);
1207 for(int i = 0; i < 4; i++) {
1212 void Channel4::SaveState(void *f)
1214 FILEIO *state_fio = (FILEIO *)f;
1216 for(int i = 0; i < 3; i++) {
1217 tmp_in_bufptr[i] = (int)(in[i] - &buf[0]);
1218 tmp_out_bufptr[i] = (int)(out[i] - &buf[0]);
1220 tmp_pms = (int)(pms - &pmtable[0][0][0]);
1221 if(state_entry != NULL) {
1222 state_entry->save_state(state_fio);
1224 // state_fio->FputUint32_BE(CHANNEL4_STATE_VERSION);
1226 // state_fio->FputUint32_BE(fb);
1227 // state_fio->Fwrite(buf, sizeof(buf), 1);
1228 // for(int i = 0; i < 3; i++) {
1229 // tmp_in_buffer[i] = (int)(in[i] - &buf[0])
1230 // tmp_out_buffer[i] = (int)(out[i] - &buf[0])
1231 // state_fio->FputInt32_BE((int)(in [i] - &buf[0]));
1232 // state_fio->FputInt32_BE((int)(out[i] - &buf[0]));
1234 // tmp_pms = (int)(pms - &pmtable[0][0][0]);
1235 // state_fio->FputInt32_BE((int)(pms - &pmtable[0][0][0]));
1236 // state_fio->FputInt32_BE(algo_);
1237 for(int i = 0; i < 4; i++) {
1242 bool Channel4::LoadState(void *f)
1244 FILEIO *state_fio = (FILEIO *)f;
1247 if(state_entry != NULL) {
1248 mb = state_entry->load_state(state_fio);
1250 if(!mb) return false;
1251 // if(state_fio->FgetUint32_BE() != CHANNEL4_STATE_VERSION) {
1254 //fb = state_fio->FgetUint32_BE();
1255 //state_fio->Fread(buf, sizeof(buf), 1);
1256 //for(int i = 0; i < 3; i++) {
1257 // in [i] = &buf[0] + state_fio->FgetInt32_BE();
1258 // out[i] = &buf[0] + state_fio->FgetInt32_BE();
1260 //pms = &pmtable[0][0][0] + state_fio->FgetInt32_BE();
1261 //algo_ = state_fio->FgetInt32_BE();
1263 for(int i = 0; i < 3; i++) {
1264 in[i] = &buf[0] + tmp_in_bufptr[i];
1265 out[i] = &buf[0] + tmp_in_bufptr[i];
1267 pms =&pmtable[0][0][0] + tmp_pms;
1268 for(int i = 0; i < 4; i++) {
1269 if(!op[i].LoadState(f)) {