comparison Plugins/Input/adplug/core/u6m.h @ 359:8df427a314a8 trunk

[svn] Adlib synthesizer (AdPlug) support.
author chainsaw
date Fri, 30 Dec 2005 16:31:39 -0800
parents
children 6ad7eb96dd26
comparison
equal deleted inserted replaced
358:70075730e187 359:8df427a314a8
1 /*
2 * Adplug - Replayer for many OPL2/OPL3 audio file formats.
3 * Copyright (C) 1999 - 2003 Simon Peter, <dn.tlp@gmx.net>, et al.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * u6m.h - Ultima 6 Music Player by Marc Winterrowd.
20 * This code extends the Adlib Winamp plug-in by Simon Peter <dn.tlp@gmx.net>
21 */
22
23 #include <stack>
24
25 #include "player.h"
26
27 #define default_dict_size 4096 // because maximum codeword size == 12 bits
28 #define max_codeword_length 12 // maximum codeword length in bits
29
30 class Cu6mPlayer: public CPlayer
31 {
32 public:
33 static CPlayer *factory(Copl *newopl);
34
35 Cu6mPlayer(Copl *newopl) : CPlayer(newopl), song_data(0)
36 {
37 };
38
39
40 ~Cu6mPlayer()
41 {
42 if(song_data) delete[] song_data;
43 };
44
45 bool load(const std::string &filename, const CFileProvider &fp);
46 bool update();
47 void rewind(int subsong);
48 float getrefresh();
49
50 std::string gettype()
51 {
52 return std::string("Ultima 6 Music");
53 };
54
55
56 protected:
57
58 struct byte_pair
59 {
60 unsigned char lo;
61 unsigned char hi;
62 };
63
64 struct subsong_info // information about a subsong
65 {
66 int continue_pos;
67 int subsong_repetitions;
68 int subsong_start;
69 };
70
71 struct dict_entry // dictionary entry
72 {
73 unsigned char root;
74 int codeword;
75 };
76
77 struct data_block //
78 {
79 long size;
80 unsigned char *data;
81 };
82
83 class MyDict
84 {
85 private:
86 // The actual number of dictionary entries allocated
87 // is (dictionary_size-256), because there are 256 roots
88 // that do not need to be stored.
89 int contains; // number of entries currently in the dictionary
90 int dict_size; // max number of entries that will fit into the dictionary
91 dict_entry* dictionary;
92
93 public:
94 MyDict(); // use dictionary size of 4096
95 MyDict(int); // let the caller specify a dictionary size
96 void reset(); // re-initializes the dictionary
97 void add(unsigned char, int);
98 unsigned char get_root(int);
99 int get_codeword(int);
100 };
101
102
103 // class variables
104 long played_ticks;
105
106 unsigned char* song_data; // the uncompressed .m file (the "song")
107 bool driver_active; // flag to prevent reentrancy
108 bool songend; // indicates song end
109 int song_pos; // current offset within the song
110 int loop_position; // position of the loop point
111 int read_delay; // delay (in timer ticks) before further song data is read
112 std::stack<subsong_info> subsong_stack;
113
114 int instrument_offsets[9]; // offsets of the adlib instrument data
115 // vibrato ("vb")
116 unsigned char vb_current_value[9];
117 unsigned char vb_double_amplitude[9];
118 unsigned char vb_multiplier[9];
119 unsigned char vb_direction_flag[9];
120 // mute factor ("mf") = not(volume)
121 unsigned char carrier_mf[9];
122 signed char carrier_mf_signed_delta[9];
123 unsigned char carrier_mf_mod_delay_backup[9];
124 unsigned char carrier_mf_mod_delay[9];
125 // frequency
126 byte_pair channel_freq[9]; // adlib freq settings for each channel
127 signed char channel_freq_signed_delta[9];
128
129 // protected functions used by update()
130 void command_loop();
131 unsigned char read_song_byte();
132 signed char read_signed_song_byte();
133 void dec_clip(int&);
134 byte_pair expand_freq_byte(unsigned char);
135 void set_adlib_freq(int channel,byte_pair freq_word);
136 void set_adlib_freq_no_update(int channel,byte_pair freq_word);
137 void set_carrier_mf(int channel,unsigned char mute_factor);
138 void set_modulator_mf(int channel,unsigned char mute_factor);
139 void freq_slide(int channel);
140 void vibrato(int channel);
141 void mf_slide(int channel);
142
143 void command_0(int channel);
144 void command_1(int channel);
145 void command_2(int channel);
146 void command_3(int channel);
147 void command_4(int channel);
148 void command_5(int channel);
149 void command_6(int channel);
150 void command_7(int channel);
151 void command_81();
152 void command_82();
153 void command_83();
154 void command_85();
155 void command_86();
156 void command_E();
157 void command_F();
158
159 void out_adlib(unsigned char adlib_register, unsigned char adlib_data);
160 void out_adlib_opcell(int channel, bool carrier, unsigned char adlib_register, unsigned char out_byte);
161
162 // protected functions used by load()
163 bool lzw_decompress(data_block source, data_block dest);
164 int get_next_codeword (long& bits_read, unsigned char *source, int codeword_size);
165 void output_root(unsigned char root, unsigned char *destination, long& position);
166 void get_string(int codeword, MyDict& dictionary, std::stack<unsigned char>& root_stack);
167 };
168