1 #ifndef _PLAY_HVL_PLAYER_H
2 #define _PLAY_HVL_PLAYER_H 1
3 
4 // Woohoo!
5 #define MAX_CHANNELS 16
6 
7 // Some handy constants. Thanks eightbitbubsy.
8 #define AMIGA_PAL_XTAL            28375160
9 #define AMIGA_NTSC_XTAL           28636360
10 #define AMIGA_CPU_PAL_CLK         ((AMIGA_PAL_XTAL / 4))
11 #define AMIGA_CPU_NTSC_CLK        ((AMIGA_NTSC_XTAL / 4))
12 #define AMIGA_CIA_PAL_CLK         ((AMIGA_CPU_PAL_CLK / 10))
13 #define AMIGA_CIA_NTSC_CLK        ((AMIGA_CPU_NTSC_CLK / 10))
14 #define AMIGA_PAULA_PAL_CLK       ((AMIGA_CPU_PAL_CLK / 2))
15 #define AMIGA_PAULA_NTSC_CLK      ((AMIGA_CPU_NTSC_CLK / 2))
16 
17 #define Period2Freq(period) ((AMIGA_PAULA_PAL_CLK * 65536.f) / (period))
18 
19 extern int32_t __attribute__ ((visibility ("internal"))) stereopan_left[5];
20 extern int32_t __attribute__ ((visibility ("internal"))) stereopan_right[5];
21 
22 
23 #define WHITENOISELEN (0x280*3)
24 #define WO_LOWPASSES   0
25 #define WO_TRIANGLE_04 (WO_LOWPASSES+((0xfc+0xfc+0x80*0x1f+0x80+3*0x280)*31))
26 #define WO_TRIANGLE_08 (WO_TRIANGLE_04+0x04)
27 #define WO_TRIANGLE_10 (WO_TRIANGLE_08+0x08)
28 #define WO_TRIANGLE_20 (WO_TRIANGLE_10+0x10)
29 #define WO_TRIANGLE_40 (WO_TRIANGLE_20+0x20)
30 #define WO_TRIANGLE_80 (WO_TRIANGLE_40+0x40)
31 #define WO_SAWTOOTH_04 (WO_TRIANGLE_80+0x80)
32 #define WO_SAWTOOTH_08 (WO_SAWTOOTH_04+0x04)
33 #define WO_SAWTOOTH_10 (WO_SAWTOOTH_08+0x08)
34 #define WO_SAWTOOTH_20 (WO_SAWTOOTH_10+0x10)
35 #define WO_SAWTOOTH_40 (WO_SAWTOOTH_20+0x20)
36 #define WO_SAWTOOTH_80 (WO_SAWTOOTH_40+0x40)
37 #define WO_SQUARES     (WO_SAWTOOTH_80+0x80)
38 #define WO_WHITENOISE  (WO_SQUARES+(0x80*0x20))
39 #define WO_HIGHPASSES  (WO_WHITENOISE+WHITENOISELEN)
40 #define WAVES_SIZE     (WO_HIGHPASSES+((0xfc+0xfc+0x80*0x1f+0x80+3*0x280)*31))
41 extern int8_t __attribute__ ((visibility ("internal"))) waves[WAVES_SIZE];
42 
43 struct hvl_envelope
44 {
45 	int16_t aFrames, aVolume;
46 	int16_t dFrames, dVolume;
47 	int16_t sFrames;
48 	int16_t rFrames, rVolume;
49 	int16_t pad;
50 };
51 
52 struct hvl_plsentry
53 {
54 	uint8_t ple_Note;
55 	uint8_t ple_Waveform;
56 	int16_t ple_Fixed;
57 	int8_t  ple_FX[2];
58 	int8_t  ple_FXParam[2];
59 };
60 
61 struct hvl_plist
62 {
63 	int16_t              pls_Speed;
64 	int16_t              pls_Length;
65 	struct hvl_plsentry *pls_Entries;
66 };
67 
68 struct hvl_instrument
69 {
70 	char                ins_Name[128];
71 	uint8_t             ins_Volume;
72 	uint8_t             ins_WaveLength;
73 	uint8_t             ins_FilterLowerLimit;
74 	uint8_t             ins_FilterUpperLimit;
75 	uint8_t             ins_FilterSpeed;
76 	uint8_t             ins_SquareLowerLimit;
77 	uint8_t             ins_SquareUpperLimit;
78 	uint8_t             ins_SquareSpeed;
79 	uint8_t             ins_VibratoDelay;
80 	uint8_t             ins_VibratoSpeed;
81 	uint8_t             ins_VibratoDepth;
82 	uint8_t             ins_HardCutRelease;
83 	uint8_t             ins_HardCutReleaseFrames;
84 	struct hvl_envelope ins_Envelope;
85 	struct hvl_plist    ins_PList;
86 };
87 
88 struct hvl_position
89 {
90 	uint8_t pos_Track[MAX_CHANNELS];
91 	int8_t  pos_Transpose[MAX_CHANNELS];
92 };
93 
94 struct hvl_step
95 {
96 	uint8_t stp_Note;
97 	uint8_t stp_Instrument;
98 	uint8_t stp_FX;
99 	uint8_t stp_FXParam;
100 	uint8_t stp_FXb;
101 	uint8_t stp_FXbParam;
102 };
103 
104 struct hvl_voice
105 {
106 	int16_t                vc_Track;
107 	int16_t                vc_NextTrack;
108 	int16_t                vc_Transpose;
109 	int16_t                vc_NextTranspose;
110 	int16_t                vc_OverrideTranspose; // 1.5
111 	int32_t                vc_ADSRVolume;
112 	struct hvl_envelope    vc_ADSR;
113 	struct hvl_instrument *vc_Instrument;
114 	uint32_t               vc_SamplePos;
115 	uint32_t               vc_Delta;
116 	uint16_t               vc_InstrPeriod;
117 	uint16_t               vc_TrackPeriod;
118 	uint16_t               vc_VibratoPeriod;
119 	uint16_t               vc_WaveLength;
120 	int16_t                vc_NoteMaxVolume;
121 	uint16_t               vc_PerfSubVolume;
122 	uint8_t                vc_NewWaveform;
123 	uint8_t                vc_Waveform;
124 	uint8_t                vc_PlantPeriod;
125 	uint8_t                vc_VoiceVolume;
126 	uint8_t                vc_PlantSquare;
127 	uint8_t                vc_IgnoreSquare;
128 	uint8_t                vc_FixedNote;
129 	int16_t                vc_VolumeSlideUp;
130 	int16_t                vc_VolumeSlideDown;
131 	int16_t                vc_HardCut;
132 	uint8_t                vc_HardCutRelease;
133 	int16_t                vc_HardCutReleaseF;
134 	uint8_t                vc_PeriodSlideOn;
135 	int16_t                vc_PeriodSlideSpeed;
136 	int16_t                vc_PeriodSlidePeriod;
137 	int16_t                vc_PeriodSlideLimit;
138 	int16_t                vc_PeriodSlideWithLimit;
139 	int16_t                vc_PeriodPerfSlideSpeed;
140 	int16_t                vc_PeriodPerfSlidePeriod;
141 	uint8_t                vc_PeriodPerfSlideOn;
142 	int16_t                vc_VibratoDelay;
143 	int16_t                vc_VibratoSpeed;
144 	int16_t                vc_VibratoCurrent;
145 	int16_t                vc_VibratoDepth;
146 	int16_t                vc_SquareOn;
147 	int16_t                vc_SquareInit;
148 	int16_t                vc_SquareWait;
149 	int16_t                vc_SquareLowerLimit;
150 	int16_t                vc_SquareUpperLimit;
151 	int16_t                vc_SquarePos;
152 	int16_t                vc_SquareSign;
153 	int16_t                vc_SquareSlidingIn;
154 	int16_t                vc_SquareReverse;
155 	uint8_t                vc_FilterOn;
156 	uint8_t                vc_FilterInit;
157 	int16_t                vc_FilterWait;
158 	int16_t                vc_FilterSpeed;
159 	int16_t                vc_FilterUpperLimit;
160 	int16_t                vc_FilterLowerLimit;
161 	int16_t                vc_FilterPos;
162 	int16_t                vc_FilterSign;
163 	int16_t                vc_FilterSlidingIn;
164 	int16_t                vc_IgnoreFilter;
165 	int16_t                vc_PerfCurrent;
166 	int16_t                vc_PerfSpeed;
167 	int16_t                vc_PerfWait;
168 	struct hvl_plist      *vc_PerfList;
169 	const int8_t          *vc_AudioPointer;
170 	const int8_t          *vc_AudioSource;
171 	uint8_t                vc_NoteDelayOn;
172 	uint8_t                vc_NoteCutOn;
173 	int16_t                vc_NoteDelayWait;
174 	int16_t                vc_NoteCutWait;
175 	int16_t                vc_AudioPeriod;
176 	int16_t                vc_AudioVolume;
177 	int32_t                vc_WNRandom;
178 	const int8_t          *vc_MixSource;
179 	int8_t                 vc_SquareTempBuffer[0x80];
180 	int8_t                 vc_VoiceBuffer[0x282*4];
181 	uint8_t                vc_VoiceNum;
182 	uint8_t                vc_TrackMasterVolume;
183 	uint8_t                vc_TrackOn;
184 	int16_t                vc_VoicePeriod;
185 	uint32_t               vc_Pan;
186 	uint32_t               vc_SetPan;   // New for 1.4
187 	uint32_t               vc_PanMultLeft;
188 	uint32_t               vc_PanMultRight;
189 	uint32_t               vc_RingSamplePos;
190 	uint32_t               vc_RingDelta;
191 	const int8_t          *vc_RingMixSource;
192 	uint8_t                vc_RingPlantPeriod;
193 	int16_t                vc_RingInstrPeriod;
194 	int16_t                vc_RingBasePeriod;
195 	int16_t                vc_RingAudioPeriod;
196 	const int8_t          *vc_RingAudioSource;
197 	uint8_t                vc_RingNewWaveform;
198 	uint8_t                vc_RingWaveform;
199 	uint8_t                vc_RingFixedPeriod;
200 	int8_t                 vc_RingVoiceBuffer[0x282*4];
201 };
202 
203 struct hvl_tune
204 {
205 	char                   ht_Name[128];
206 	uint16_t               ht_SongNum;         /* Current SubSong */
207 	uint32_t               ht_Frequency;
208 	double                 ht_FreqF;
209 	const int8_t          *ht_WaveformTab[MAX_CHANNELS];
210 	uint16_t               ht_Restart;
211 	uint16_t               ht_PositionNr;      /* Number of orders(positions) */
212 	uint8_t                ht_SpeedMultiplier; /* 1 = 50 ticks per second, 2 = 100, 3 = 150 ... */
213 	uint8_t                ht_TrackLength;     /* How many rows per order(track) */
214 	uint8_t                ht_TrackNr;         /* Number of tracks (order is made of parallel tracks) ? */
215 	uint8_t                ht_InstrumentNr;
216 	uint8_t                ht_SubsongNr;       /* Number of SubSongs */
217 	uint16_t               ht_PosJump;         /* Target order(position) */
218 	uint32_t               ht_PlayingTime;
219 	int16_t                ht_Tempo;           /* Ticks per row */
220 	int16_t                ht_PosNr;           /* Current order(position)*/
221 	int16_t                ht_StepWaitFrames;  /* How many ticks left until next row */
222 	int16_t                ht_NoteNr;          /* Current row(note) */
223 	uint16_t               ht_PosJumpNote;     /* Target row(note), if break occurs */
224 	uint8_t                ht_GetNewPosition;
225 	uint8_t                ht_PatternBreak;
226 	uint8_t                ht_SongEndReached;
227 	uint8_t                ht_Stereo;
228 	uint16_t              *ht_Subsongs;
229 	uint16_t               ht_Channels;
230 	struct hvl_position   *ht_Positions;
231 	struct hvl_step        ht_Tracks[256][64];
232 	struct hvl_instrument *ht_Instruments;
233 	struct hvl_voice       ht_Voices[MAX_CHANNELS];
234 	int32_t                ht_defstereo;
235 	int32_t                ht_defpanleft;
236 	int32_t                ht_defpanright;
237 	int32_t                ht_mixgain;
238 	uint8_t                ht_Version;
239 };
240 
241 /* buflen is expected to be ht->ht_Frequency/50, but can be scaled to adjust pitch.... */
242 /* buf is expected to be malloc (MAX_CHANNELS * sizeof(int16) * STEREO_IS_2 * buflen */
243 void __attribute__ ((visibility ("internal"))) hvl_DecodeFrame (struct hvl_tune *ht, int16_t *buf, size_t buflen);
244 void __attribute__ ((visibility ("internal"))) hvl_InitReplayer (void);
245 int __attribute__ ((visibility ("internal"))) hvl_InitSubsong (struct hvl_tune *ht, uint32_t nr);
246 #if 0
247 int32_t __attribute__ ((visibility ("internal"))) hvl_FindLoudest (struct hvl_tune *ht, int32_t maxframes, int usesongend);
248 #endif
249 
250 #endif
251