9c29ada7eb7a1e2c23a46ee350e4ec6234fda8af
[jelmer/ptabtools.git] / ptb.h
1 /*
2    Functions for writing and reading PowerTab (.ptb) files
3    (c) 2004-2005 Jelmer Vernooij <jelmer@samba.org>
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program 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
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18    */
19
20 #ifndef __PTB_H__
21 #define __PTB_H__
22
23 #include <sys/stat.h>
24 #include <stdlib.h>
25
26 #if defined(_MSC_VER) && !defined(PTB_CORE)
27 #pragma comment(lib,"ptb.lib")
28 #endif
29
30 #ifdef _MSC_VER
31 typedef unsigned char uint8_t;
32 typedef unsigned short uint16_t;
33 typedef unsigned long uint32_t;
34 #else
35 #include <stdint.h>
36 #endif
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41
42 typedef uint8_t ptb_chord;
43 typedef uint8_t ptb_tone;
44 typedef uint8_t ptb_note;
45
46 struct ptb_hdr {
47         enum { CLASSIFICATION_SONG = 0, CLASSIFICATION_LESSON} classification;
48
49         union {
50                 struct {
51                         enum { RELEASE_TYPE_PR_AUDIO = 0, RELEASE_TYPE_PR_VIDEO, RELEASE_TYPE_BOOTLEG, RELEASE_TYPE_UNRELEASED } release_type;
52
53                         union {
54                                 struct {
55                                         enum { AUDIO_TYPE_SINGLE = 0, AUDIO_TYPE_EP, AUDIO_TYPE_ALBUM, AUDIO_TYPE_DOUBLE_ALBUM, AUDIO_TYPE_TRIPLE_ALBUM, AUDIO_TYPE_BOXSET } type;
56                                         char *album_title;
57                                         uint16_t year;
58                                         uint8_t is_live_recording;
59                                 } pr_audio;
60                                 struct {
61                                         char *video_title;
62                                         uint16_t year;
63                                         uint8_t is_live_recording;
64                                 } pr_video;
65                                 struct {
66                                         char *title;
67                                         uint16_t day;
68                                         uint16_t month;
69                                         uint16_t year;
70                                 } bootleg;
71                                 struct {
72                                         char empty;
73                                 } unreleased;
74                         } release_info;
75                         uint8_t is_original_author_unknown;
76                         char *title;
77                         char *artist;
78                         char *words_by;
79                         char *music_by;
80                         char *arranged_by;
81                         char *guitar_transcribed_by;
82                         char *bass_transcribed_by;
83                         char *lyrics;
84                         char *copyright;
85                 } song;
86
87                 struct  {
88                         char *title;
89                         char *artist;
90                         uint16_t style;
91                         enum { LEVEL_BEGINNER = 0, LEVEL_INTERMEDIATE, LEVEL_ADVANCED} level;
92                         char *author;
93                         char *copyright;
94                 } lesson;
95         } class_info;
96
97         char *guitar_notes;
98         char *bass_notes;
99         char *drum_notes;
100         uint16_t version;
101 };
102
103 struct ptb_guitar {
104         struct ptb_guitar *prev, *next;
105
106         uint8_t index;
107         char *title;
108         char *type;
109         uint8_t nr_strings;
110         uint8_t *strings;
111         uint8_t reverb;
112         uint8_t chorus;
113         uint8_t tremolo;
114         uint8_t pan;
115         uint8_t capo;
116         uint8_t initial_volume;
117         uint8_t midi_instrument;
118         uint8_t half_up;
119         uint8_t simulate;
120 };
121
122 struct ptb_dynamic {
123         struct ptb_dynamic *prev, *next;
124
125         uint8_t offset;
126         uint8_t staff;
127         uint8_t volume;
128 };
129
130 struct ptb_guitarin {
131         struct ptb_guitarin *prev, *next;
132
133         uint8_t offset; 
134         uint8_t section;
135         uint8_t staff;
136
137         /* OR'd numbers of guitars 
138          * (0x01 = guitar1, 0x02 = guitar2, 0x04 = guitar3, etc) */
139         uint8_t rhythm_slash;
140         uint8_t staff_in;
141 };
142
143 struct ptb_font {
144         uint8_t size;
145         uint8_t thickness;
146         uint8_t underlined;
147         uint8_t italic;
148         char *family;
149 };
150
151 struct ptb_floatingtext {
152         struct ptb_floatingtext *prev, *next;
153
154         char *text;
155         uint8_t beginpos;
156 #define ALIGN_LEFT              1
157 #define ALIGN_CENTER    2
158 #define ALIGN_RIGHT             3
159 #define ALIGN_TIMESTAMP 8
160         uint8_t alignment;
161         struct ptb_font font;
162 };
163
164 struct ptb_tempomarker {
165         struct ptb_tempomarker *prev, *next;
166
167         char *description;
168         uint16_t type; 
169         uint8_t bpm;
170         uint8_t section;
171         uint8_t offset;
172 };
173
174 struct ptb_chorddiagram {
175         struct ptb_chorddiagram *prev, *next;
176
177         ptb_chord name[2];
178         uint8_t frets;
179         uint8_t nr_strings;
180         uint8_t type;
181         ptb_tone *tones;
182 };
183
184 struct ptb_chordtext {
185         struct ptb_chordtext *prev, *next;
186
187         ptb_chord name[2];
188 #define CHORDTEXT_PROPERTY_NOCHORD                      0x10
189 #define CHORDTEXT_PROPERTY_PARENTHESES          0x20
190 #define CHORDTEXT_PROPERTY_FORMULA_M            0x01
191 #define CHORDTEXT_PROPERTY_FORMULA_MAJ7         0x08
192         uint8_t properties;
193         uint8_t offset;
194 #define CHORDTEXT_ADD_9                                         0x40
195         uint8_t additions;
196         uint8_t alterations;
197 #define CHORDTEXT_VII                                           0x08
198         uint8_t VII;
199 };
200
201 struct ptb_position {
202         struct ptb_position *prev, *next;
203
204         uint8_t offset;
205 #define POSITION_PALM_MUTE                                              0x20
206 #define POSITION_STACCATO                                               0x02
207 #define POSITION_ACCENT                                                 0x04
208         uint8_t palm_mute;
209         uint8_t length;
210 #define POSITION_DOTS_1                                                 0x01
211 #define POSITION_DOTS_2                                                 0x02
212 #define POSITION_DOTS_REST                                              0x04
213 #define POSITION_DOTS_ARPEGGIO_UP                               0x10
214         uint8_t dots;
215 #define POSITION_PROPERTY_IN_SINGLE_BEAM                0x0080
216 #define POSITION_PROPERTY_IN_DOUBLE_BEAM                0x0100
217 #define POSITION_PROPERTY_FIRST_IN_BEAM                 0x0400
218 #define POSITION_PROPERTY_LAST_IN_BEAM                  0x2000
219         uint16_t properties;
220         uint8_t let_ring;
221         uint8_t fermenta;
222 #define POSITION_FERMENTA_LET_RING                              0x08
223 #define POSITION_FERMENTA_FERMENTA                              0x10
224 #define POSITION_FERMENTA_TRIPLET_1                             0x20
225 #define POSITION_FERMENTA_TRIPLET_2                             0x40
226 #define POSITION_FERMENTA_TRIPLET_3                             0x80
227         
228         uint8_t nr_additional_data;
229         struct ptb_position_additional
230         {
231                 uint8_t start_volume;
232                 uint8_t end_volume;
233                 uint8_t duration; /* Number of following positions */
234                 uint8_t properties;
235 #define POSITION_ADDITIONAL_VOLUMESWELL                 0x61
236 #define POSITION_ADDITIONAL_TREMOLOBAR                  0x63
237         } *additional;
238
239         struct ptb_linedata *linedatas;
240 };
241
242
243
244 #define STAFF_TYPE_BASS_KEY     0x10
245
246 struct ptb_staff {
247         struct ptb_staff *prev, *next;
248
249         /* Number of strings OR-ed with some settings */
250         uint8_t properties;
251         uint8_t highest_note;
252         uint8_t lowest_note;
253         struct ptb_position *positions[2];
254 };
255
256 struct ptb_bend
257 {
258         uint8_t bend_pitch:4;
259         uint8_t release_pitch:4;
260         uint8_t bend1;
261         uint8_t bend2;
262         uint8_t bend3;
263 };
264
265 struct ptb_linedata {
266         struct ptb_linedata *prev, *next;
267
268         union {
269                 struct {
270                         unsigned int fret:5;
271                         unsigned int string:3;
272                 } detailed;
273                 uint8_t tone; 
274         }; 
275 #define LINEDATA_PROPERTY_TIE                                   0x01
276 #define LINEDATA_PROPERTY_MUTED                                 0x02
277 #define LINEDATA_PROPERTY_HAMMERON_FROM                 0x08
278 #define LINEDATA_PROPERTY_PULLOFF_FROM                  0x10
279 #define LINEDATA_PROPERTY_NATURAL_HARMONIC              0x40
280 #define LINEDATA_PROPERTY_GHOST_NOTE                    0x80
281         uint8_t properties;
282         uint8_t transcribe;
283 #define LINEDATA_TRANSCRIBE_8VA                                 0x01
284 #define LINEDATA_TRANSCRIBE_15MA                                0x02
285 #define LINEDATA_TRANSCRIBE_8VB                                 0x03
286 #define LINEDATA_TRANSCRIBE_15MB                                0x04
287         uint8_t conn_to_next;
288         struct ptb_bend *bends;
289 };
290
291 #define METER_TYPE_BEAM_2       0x0080  
292 #define METER_TYPE_BEAM_4       0x0100
293 #define METER_TYPE_BEAM_3       0x0180
294 #define METER_TYPE_BEAM_6       0x0200
295 #define METER_TYPE_BEAM_5       0x0280
296 #define METER_TYPE_COMMON       0x4000
297 #define METER_TYPE_CUT          0x8000
298 #define METER_TYPE_SHOW         0x1000
299
300 #define END_MARK_TYPE_NORMAL     0x00
301 #define END_MARK_TYPE_DOUBLELINE 0x20
302 #define END_MARK_TYPE_REPEAT     0x80
303
304 struct ptb_section {
305         struct ptb_section *prev, *next;
306
307         char letter;
308         struct ptb_staff *staffs;
309         struct ptb_chordtext *chordtexts;
310         struct ptb_rhythmslash *rhythmslashes;
311         struct ptb_direction *directions;
312         struct ptb_musicbar *musicbars;
313         
314         /* Number of times to repeat OR-ed with end mark type */
315         uint8_t end_mark;
316         uint16_t meter_type;
317         union {
318                 uint8_t beat_info;
319                 struct {
320                         unsigned int beat:3;
321                         unsigned int beat_value:5;
322                 } detailed;
323         };
324         uint8_t metronome_pulses_per_measure;
325         uint16_t properties;
326         uint8_t key_extra;
327         uint8_t position_width;
328         char *description;
329 };
330
331 struct ptb_sectionsymbol {
332         struct ptb_sectionsymbol *prev, *next;
333
334         uint16_t repeat_ending;
335 };
336
337 struct ptb_musicbar {
338         struct ptb_musicbar *prev, *next;
339
340         uint8_t offset;
341 #define MUSICBAR_PROPERTY_SINGLE_BAR    0x01
342 #define MUSICBAR_PROPERTY_DOUBLE_BAR    0x20
343 #define MUSICBAR_PROPERTY_FREE_BAR      0x40
344 #define MUSICBAR_PROPERTY_REPEAT_BEGIN  0x60
345 #define MUSICBAR_PROPERTY_REPEAT_END    0x80
346 #define MUSICBAR_PROPERTY_END_BAR       0xA0
347         /* Number of times to repeat OR-ed only with REPEAT_END property */
348         uint8_t properties;
349         char letter;
350         char *description;
351 };
352
353 struct ptb_rhythmslash {
354         struct ptb_rhythmslash *prev, *next;
355
356 #define RHYTHMSLASH_PROPERTY_FIRST_IN_BEAM      0x04
357         uint8_t properties;
358         uint8_t offset;
359         uint8_t dotted;
360         uint8_t length;
361 };
362
363 struct ptb_direction {
364         struct ptb_direction *prev, *next;
365 };
366
367 struct ptbf {
368         char data[3];
369         int fd;
370         int mode;
371         char *filename;
372         struct ptb_hdr hdr;
373         struct ptb_instrument {
374                 struct ptb_guitar *guitars;
375                 struct ptb_section *sections;
376                 struct ptb_guitarin *guitarins;
377                 struct ptb_chorddiagram *chorddiagrams;
378                 struct ptb_tempomarker *tempomarkers;
379                 struct ptb_dynamic *dynamics;
380                 struct ptb_floatingtext *floatingtexts;
381                 struct ptb_sectionsymbol *sectionsymbols;
382         } instrument[2];
383         off_t curpos;
384         struct ptb_font default_font;
385         struct ptb_font chord_name_font;
386         struct ptb_font tablature_font;
387 };
388
389 extern struct ptbf *ptb_read_file(const char *ptb);
390 extern int ptb_write_file(const char *ptb, struct ptbf *);
391 extern void ptb_free(struct ptbf *);
392
393 extern void ptb_set_debug(int level);
394 extern void ptb_set_asserts_fatal(int yes);
395
396 extern const char *ptb_get_note(struct ptb_guitar *guitar, ptb_note);
397 extern const char *ptb_get_tone(ptb_tone);
398 extern const char *ptb_get_tone_full(ptb_tone);
399
400 extern void ptb_get_position_difference(struct ptb_section *, int start, int end, int *bars, int *length);
401
402 /* Reading tuning data files (tunings.dat) */
403
404 struct ptb_tuning_dict {
405         uint16_t nr_tunings;
406
407         struct ptb_tuning {
408                 char *name;
409                 uint8_t capo;
410                 uint8_t nr_strings;
411                 uint8_t *strings;
412         } *tunings;
413 };
414
415 extern struct ptb_tuning_dict *ptb_read_tuning_dict(const char *);
416 extern int ptb_write_tuning_dict(const char *, struct ptb_tuning_dict *);
417 extern void ptb_free_tuning_dict(struct ptb_tuning_dict *);
418 extern const char *ptb_tuning_get_note(char);
419
420 #ifdef __cplusplus
421 }
422 #endif
423
424 #endif /* __PTB_H__ */