1 module pxtone.overdrive;
2 // '12/03/03
3 
4 import pxtone.pxtn;
5 import pxtone.error;
6 import pxtone.descriptor;
7 
8 enum TUNEOVERDRIVE_CUT_MAX = 99.9f;
9 enum TUNEOVERDRIVE_CUT_MIN = 50.0f;
10 enum TUNEOVERDRIVE_AMP_MAX = 8.0f;
11 enum TUNEOVERDRIVE_AMP_MIN = 0.1f;
12 enum TUNEOVERDRIVE_DEFAULT_CUT = 90.0f;
13 enum TUNEOVERDRIVE_DEFAULT_AMP = 2.0f;
14 
15 struct pxtnOverDrive {
16 	bool _b_played = true;
17 
18 	int _group;
19 	float _cut_f;
20 	float _amp_f;
21 
22 	int _cut_16bit_top;
23 
24 	float get_cut() const nothrow @safe {
25 		return _cut_f;
26 	}
27 
28 	float get_amp() const nothrow @safe {
29 		return _amp_f;
30 	}
31 
32 	int get_group() const nothrow @safe {
33 		return _group;
34 	}
35 
36 	void Set(float cut, float amp, int group) nothrow @safe {
37 		_cut_f = cut;
38 		_amp_f = amp;
39 		_group = group;
40 	}
41 
42 	bool get_played() const nothrow @safe {
43 		return _b_played;
44 	}
45 
46 	void set_played(bool b) nothrow @safe {
47 		_b_played = b;
48 	}
49 
50 	bool switch_played() nothrow @safe {
51 		_b_played = _b_played ? false : true;
52 		return _b_played;
53 	}
54 
55 	void Tone_Ready() nothrow @safe {
56 		_cut_16bit_top = cast(int)(32767 * (100 - _cut_f) / 100);
57 	}
58 
59 	void Tone_Supple(int[] group_smps) const nothrow @safe {
60 		if (!_b_played) {
61 			return;
62 		}
63 		int work = group_smps[_group];
64 		if (work > _cut_16bit_top) {
65 			work = _cut_16bit_top;
66 		} else if (work < -_cut_16bit_top) {
67 			work = -_cut_16bit_top;
68 		}
69 		group_smps[_group] = cast(int)(cast(float) work * _amp_f);
70 	}
71 
72 	void Write(ref pxtnDescriptor p_doc) const @system {
73 		_OVERDRIVESTRUCT over;
74 		int size;
75 
76 		over.cut = _cut_f;
77 		over.amp = _amp_f;
78 		over.group = cast(ushort) _group;
79 
80 		// dela ----------
81 		size = _OVERDRIVESTRUCT.sizeof;
82 		p_doc.w_asfile(size);
83 		p_doc.w_asfile(over);
84 	}
85 
86 	void Read(ref pxtnDescriptor p_doc) @system {
87 		_OVERDRIVESTRUCT over;
88 		int size = 0;
89 
90 		p_doc.r(size);
91 		p_doc.r(over);
92 
93 		if (over.xxx) {
94 			throw new PxtoneException("fmt unknown");
95 		}
96 		if (over.yyy) {
97 			throw new PxtoneException("fmt unknown");
98 		}
99 		if (over.cut > TUNEOVERDRIVE_CUT_MAX || over.cut < TUNEOVERDRIVE_CUT_MIN) {
100 			throw new PxtoneException("fmt unknown");
101 		}
102 		if (over.amp > TUNEOVERDRIVE_AMP_MAX || over.amp < TUNEOVERDRIVE_AMP_MIN) {
103 			throw new PxtoneException("fmt unknown");
104 		}
105 
106 		_cut_f = over.cut;
107 		_amp_f = over.amp;
108 		_group = over.group;
109 	}
110 }
111 
112 // (8byte) =================
113 struct _OVERDRIVESTRUCT {
114 	ushort xxx;
115 	ushort group;
116 	float cut = 0.0;
117 	float amp = 0.0;
118 	float yyy = 0.0;
119 }