Fix compatibility with newer versions of lilypond.
[jelmer/ptabtools.git] / ptb.h
1 /*
2    Functions for writing and reading PowerTab (.ptb) files
3    (c) 2004-2007 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 3 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 #define CONTENT_TYPE_GUITAR             0x01
48 #define CONTENT_TYPE_BASS               0x02
49 #define CONTENT_TYPE_PERCUSSION 0x04
50         enum { CLASSIFICATION_SONG = 0, CLASSIFICATION_LESSON} classification;
51
52         union {
53                 struct {
54                         enum { RELEASE_TYPE_PR_AUDIO = 0, RELEASE_TYPE_PR_VIDEO, RELEASE_TYPE_BOOTLEG, RELEASE_TYPE_UNRELEASED } release_type;
55
56                         union {
57                                 struct {
58                                         enum { AUDIO_TYPE_SINGLE = 0, AUDIO_TYPE_EP, AUDIO_TYPE_ALBUM, AUDIO_TYPE_DOUBLE_ALBUM, AUDIO_TYPE_TRIPLE_ALBUM, AUDIO_TYPE_BOXSET } type;
59                                         char *album_title;
60                                         uint16_t year;
61                                         uint8_t is_live_recording;
62                                 } pr_audio;
63                                 struct {
64                                         char *video_title;
65                                         uint16_t year;
66                                         uint8_t is_live_recording;
67                                 } pr_video;
68                                 struct {
69                                         char *title;
70                                         uint16_t day;
71                                         uint16_t month;
72                                         uint16_t year;
73                                 } bootleg;
74                                 struct {
75                                         char empty;
76                                 } unreleased;
77                         } release_info;
78                         uint8_t is_original_author_unknown;
79                         uint8_t content_type;
80                         char *title;
81                         char *artist;
82                         char *words_by;
83                         char *music_by;
84                         char *arranged_by;
85                         char *guitar_transcribed_by;
86                         char *bass_transcribed_by;
87                         char *lyrics;
88                         char *copyright;
89                 } song;
90
91                 struct  {
92                         char *title;
93                         char *artist;
94 #define MUSICSTYLE_ALTERNATIVE             0
95 #define MUSICSTYLE_BLUEGRASS               1
96 #define MUSICSTYLE_BLUES                   2
97 #define MUSICSTYLE_COUNTRY                 3
98 #define MUSICSTYLE_FINGERPICK              4
99 #define MUSICSTYLE_FLAMENCO                5
100 #define MUSICSTYLE_FOLK                    6
101 #define MUSICSTYLE_FUNK                    7
102 #define MUSICSTYLE_FUSION                  8
103 #define MUSICSTYLE_GENERAL                 9
104 #define MUSICSTYLE_JAZZ                    10
105 #define MUSICSTYLE_METAL                   11
106 #define MUSICSTYLE_OTHER                   12
107 #define MUSICSTYLE_POP                     13
108 #define MUSICSTYLE_PROGRESSIVE             14
109 #define MUSICSTYLE_PUNK                    15
110 #define MUSICSTYLE_REGGAE                  16
111 #define MUSICSTYLE_ROCK                    17
112 #define MUSICSTYLE_SWING                   18
113                         uint16_t style;
114                         enum { LEVEL_BEGINNER = 0, LEVEL_INTERMEDIATE, LEVEL_ADVANCED} level;
115                         char *author;
116                         char *copyright;
117                 } lesson;
118         } class_info;
119
120         char *guitar_notes;
121         char *bass_notes;
122         char *drum_notes;
123         uint16_t version;
124 };
125
126 struct ptb_guitar {
127         struct ptb_guitar *prev, *next;
128
129         uint8_t index;
130         char *title;
131         char *type;
132         uint8_t nr_strings;
133         uint8_t *strings;
134         uint8_t reverb;
135         uint8_t chorus;
136         uint8_t tremolo;
137         uint8_t pan;
138         uint8_t capo;
139         uint8_t initial_volume;
140         uint8_t midi_instrument;
141         uint8_t half_up;
142         uint8_t simulate;
143 };
144
145 struct ptb_dynamic {
146         struct ptb_dynamic *prev, *next;
147
148         uint16_t section;
149         uint8_t staff;
150         uint8_t position;
151         uint16_t volume;
152 };
153
154 struct ptb_guitarin {
155         struct ptb_guitarin *prev, *next;
156
157         uint8_t offset; 
158         uint8_t section;
159         uint8_t staff;
160
161         /* OR'd numbers of guitars 
162          * (0x01 = guitar1, 0x02 = guitar2, 0x04 = guitar3, etc) */
163         uint8_t rhythm_slash;
164         uint8_t staff_in;
165 };
166
167 struct ptb_rect { /* FIXME */ };
168
169 struct ptb_color { uint8_t r,g,b; };
170
171 struct ptb_font {
172 #define WEIGHT_DONT_CARE                 0
173 #define WEIGHT_THIN              100
174 #define WEIGHT_EXTRALIGHT        200
175 #define WEIGHT_ULTRALIGHT        200
176 #define WEIGHT_LIGHT             300
177 #define WEIGHT_NORMAL            400
178 #define WEIGHT_REGULAR           400
179 #define WEIGHT_MEDIUM            500
180 #define WEIGHT_SEMIBOLD          600
181 #define WEIGHT_DEMIBOLD          600
182 #define WEIGHT_BOLD              700
183 #define WEIGHT_EXTRABOLD         800
184 #define WEIGHT_ULTRABOLD         800
185 #define WEIGHT_BLACK             900
186 #define WEIGHT_HEAVY             900
187         uint32_t pointsize;
188         uint32_t weight;
189         uint8_t underlined;
190         uint8_t strikeout;
191         uint8_t italic;
192         struct ptb_color color;
193         char *family;
194 };
195
196 struct ptb_floatingtext {
197         struct ptb_floatingtext *prev, *next;
198
199         char *text;
200         struct ptb_rect rect;
201 #define ALIGN_LEFT              0x01
202 #define ALIGN_CENTER    0x02
203 #define ALIGN_RIGHT             0x04
204 #define ALIGN_MASK              0x07
205 #define ALIGN_BORDER    0x08 /* text surrounded by border */
206         uint8_t alignment;
207         struct ptb_font font;
208 };
209
210 struct ptb_tempomarker {
211         struct ptb_tempomarker *prev, *next;
212
213         char *description;
214         uint16_t type; 
215         uint8_t bpm;
216         uint8_t section;
217         uint8_t offset;
218 };
219
220 struct ptb_chordname {
221         ptb_chord name[2];
222         uint8_t formula;
223         uint16_t formula_mods;
224         uint8_t type;
225 };
226
227 struct ptb_chorddiagram {
228         struct ptb_chorddiagram *prev, *next;
229
230         struct ptb_chordname name;
231
232         uint8_t frets;
233         uint8_t nr_strings;
234         ptb_tone *tones;
235 };
236
237 struct ptb_chordtext {
238         struct ptb_chordtext *prev, *next;
239
240         ptb_chord name[2];
241 #define CHORDTEXT_PROPERTY_NOCHORD                      0x10
242 #define CHORDTEXT_PROPERTY_PARENTHESES          0x20
243 #define CHORDTEXT_PROPERTY_FORMULA_M            0x01
244 #define CHORDTEXT_PROPERTY_FORMULA_PLUS         0x02
245 #define CHORDTEXT_PROPERTY_FORMULA_MIN          0x02
246 #define CHORDTEXT_PROPERTY_FORMULA_5            0x04
247 #define CHORDTEXT_PROPERTY_FORMULA_6            0x05
248 #define CHORDTEXT_PROPERTY_FORMULA_M6           0x06
249 #define CHORDTEXT_PROPERTY_FORMULA_7            0x07
250 #define CHORDTEXT_PROPERTY_FORMULA_MAJ7         0x08
251 #define CHORDTEXT_PROPERTY_FORMULA_M7           0x09
252 #define CHORDTEXT_PROPERTY_FORMULA_PLUS7        0x0A
253 #define CHORDTEXT_PROPERTY_FORMULA_MIN7         0x0B
254 #define CHORDTEXT_PROPERTY_FORMULA_MMAJ7        0x0C
255 #define CHORDTEXT_PROPERTY_FORMULA_M7B5         0x0D
256         uint8_t properties;
257         uint8_t offset;
258 #define CHORDTEXT_EXT_7_9                                       0x01
259 #define CHORDTEXT_PLUS_5                                        0x02
260 #define CHORDTEXT_EXT_7_13                                      0x04
261 #define CHORDTEXT_ADD_2                                         0x08
262 #define CHORDTEXT_ADD_9                                         0x40
263 #define CHORDTEXT_ADD_11                                        0x80
264         uint8_t additions;
265         uint8_t alterations;
266 #define CHORDTEXT_VII_OPEN                                      0x01
267 #define CHORDTEXT_VII_VI                                        0x06
268 #define CHORDTEXT_VII                                           0x08
269 #define CHORDTEXT_VII_TYPE_2                            0x20
270 #define CHORDTEXT_VII_TYPE_3                            0x40
271         uint8_t VII;
272 };
273
274 struct ptb_position {
275         struct ptb_position *prev, *next;
276
277         uint8_t offset;
278 #define POSITION_PICKSTROKE_DOWN                                0x01
279 #define POSITION_STACCATO                                               0x02
280 #define POSITION_ACCENT                                                 0x04
281 #define POSITION_HEAVY_ACCENT                                   0x08
282 #define POSITION_TREMOLO_PICKING                                0x10
283 #define POSITION_PALM_MUTE                                              0x20
284         uint8_t palm_mute;
285         uint8_t length;
286 #define POSITION_DOTS_1                                                 0x01
287 #define POSITION_DOTS_2                                                 0x02
288 #define POSITION_DOTS_REST                                              0x04
289 #define POSITION_DOTS_VIBRATO                                   0x08
290 #define POSITION_DOTS_WIDE_VIBRATO                              0x10
291 #define POSITION_DOTS_ARPEGGIO_UP                               0x20
292 #define POSITION_DOTS_ARPEGGIO_DOWN                             0x40
293         uint8_t dots;
294
295         /* Irregular grouping: 
296            Play x notes in the time of y
297            Stored as (x - 1) * 8 + (y - 1)
298         */
299 #define POSITION_PROPERTY_IRREGULAR_GROUPING    0x007F
300 #define POSITION_PROPERTY_IN_SINGLE_BEAM                0x0080
301 #define POSITION_PROPERTY_IN_DOUBLE_BEAM                0x0100
302 #define POSITION_PROPERTY_IN_TRIPLE_BEAM                0x0200
303 #define POSITION_PROPERTY_FIRST_IN_BEAM                 0x0400
304 #define POSITION_PROPERTY_PARTIAL_BEAM                  0x0800
305 #define POSITION_PROPERTY_LAST_IN_BEAM                  0x1000
306 #define POSITION_PROPERTY_MIDDLE_IN_BEAM                0x2000
307         uint16_t properties;
308         uint8_t let_ring;
309         uint8_t fermenta;
310 #define POSITION_FERMENTA_ACCIACCATURA                  0x01
311 #define POSITION_FERMENTA_TRIPLET_FEEL_FIRST    0x02
312 #define POSITION_FERMENTA_TRIPLET_FEEL_SECOND   0x04
313 #define POSITION_FERMENTA_LET_RING                              0x08
314 #define POSITION_FERMENTA_FERMENTA                              0x10
315 #define POSITION_FERMENTA_TRIPLET_1                             0x20
316 #define POSITION_FERMENTA_TRIPLET_2                             0x40
317 #define POSITION_FERMENTA_TRIPLET_3                             0x80
318         
319         uint8_t nr_additional_data;
320         struct ptb_position_additional
321         {
322                 uint8_t start_volume;
323                 uint8_t end_volume;
324                 uint8_t duration; /* Number of following positions */
325                 uint8_t properties;
326 #define POSITION_ADDITIONAL_VOLUMESWELL                 0x61
327 #define POSITION_ADDITIONAL_TREMOLOBAR                  0x63
328         } *additional;
329
330         struct ptb_linedata *linedatas;
331 };
332
333
334
335 #define STAFF_TYPE_BASS_KEY                     0x10
336 #define STAFF_CLEF_MASK                         0xf0
337 #define STAFF_TAB_STAFF_TYPE_MASK       0x0f
338
339 struct ptb_staff {
340         struct ptb_staff *prev, *next;
341
342         /* Number of strings OR-ed with some settings */
343         uint8_t properties;
344         uint8_t highest_note_space;
345         uint8_t lowest_note_space;
346         uint8_t symbol_space;
347         uint8_t tab_staff_space;
348
349         /* first array is for high melody
350          * second is for low melody
351          */
352         struct ptb_position *positions[2];
353 };
354
355 struct ptb_bend
356 {
357         uint8_t bend_pitch:4;
358         uint8_t release_pitch:4;
359         uint8_t bend1;
360         uint8_t bend2;
361         uint8_t bend3;
362 };
363
364 struct ptb_linedata {
365         struct ptb_linedata *prev, *next;
366
367         union {
368                 struct {
369                         unsigned int fret:5;
370                         unsigned int string:3;
371                 } detailed;
372                 uint8_t tone; 
373         }; 
374 #define LINEDATA_PROPERTY_TIE                                   0x01
375 #define LINEDATA_PROPERTY_MUTED                                 0x02
376 #define LINEDATA_PROPERTY_CONTINUES                             0x04
377 #define LINEDATA_PROPERTY_HAMMERON_FROM                 0x08
378 #define LINEDATA_PROPERTY_PULLOFF_FROM                  0x10
379 #define LINEDATA_PROPERTY_DEST_NOWHERE                  0x20
380 #define LINEDATA_PROPERTY_NATURAL_HARMONIC              0x40
381 #define LINEDATA_PROPERTY_GHOST_NOTE                    0x80
382         uint8_t properties;
383         uint8_t transcribe;
384 #define LINEDATA_TRANSCRIBE_8VA                                 0x01
385 #define LINEDATA_TRANSCRIBE_15MA                                0x02
386 #define LINEDATA_TRANSCRIBE_8VB                                 0x04
387 #define LINEDATA_TRANSCRIBE_15MB                                0x08
388         uint8_t conn_to_next;
389         struct ptb_bend *bends;
390 };
391
392 #define METER_TYPE_BEAM_2       0x0080  
393 #define METER_TYPE_BEAM_4       0x0100
394 #define METER_TYPE_BEAM_3       0x0180
395 #define METER_TYPE_BEAM_6       0x0200
396 #define METER_TYPE_BEAM_5       0x0280
397 #define METER_TYPE_COMMON       0x4000
398 #define METER_TYPE_CUT          0x8000
399 #define METER_TYPE_SHOW         0x1000
400
401 #define END_MARK_TYPE_NORMAL     0x00
402 #define END_MARK_TYPE_DOUBLELINE 0x20
403 #define END_MARK_TYPE_REPEAT     0x80
404
405 struct ptb_section {
406         struct ptb_section *prev, *next;
407
408         char letter;
409         struct ptb_staff *staffs;
410         struct ptb_chordtext *chordtexts;
411         struct ptb_rhythmslash *rhythmslashes;
412         struct ptb_direction *directions;
413         struct ptb_musicbar *musicbars;
414         
415         /* Number of times to repeat OR-ed with end mark type */
416         uint8_t end_mark;
417         uint16_t meter_type;
418         union {
419                 uint8_t beat_info;
420                 struct {
421                         unsigned int beat:3;
422                         unsigned int beat_value:5;
423                 } detailed;
424         };
425         uint8_t metronome_pulses_per_measure;
426         uint16_t properties;
427         uint8_t key_extra;
428         uint8_t position_width;
429         char *description;
430 };
431
432 struct ptb_sectionsymbol {
433         struct ptb_sectionsymbol *prev, *next;
434
435         uint16_t section;
436         uint8_t position;
437         uint32_t data;
438 };
439
440 struct ptb_musicbar {
441         struct ptb_musicbar *prev, *next;
442
443         uint8_t offset;
444
445 #define MUSICBAR_PROPERTY_SINGLE_BAR    0x01
446 #define MUSICBAR_PROPERTY_DOUBLE_BAR    0x20
447 #define MUSICBAR_PROPERTY_FREE_BAR      0x40
448 #define MUSICBAR_PROPERTY_REPEAT_BEGIN  0x60
449 #define MUSICBAR_PROPERTY_REPEAT_END    0x80
450 #define MUSICBAR_PROPERTY_END_BAR       0xA0
451         /* Number of times to repeat OR-ed only with REPEAT_END property */
452         uint8_t properties;
453         char letter;
454         char *description;
455 };
456
457 struct ptb_rhythmslash {
458         struct ptb_rhythmslash *prev, *next;
459
460 #define RHYTHMSLASH_PROPERTY_IN_SINGLE_BEAM     0x01
461 #define RHYTHMSLASH_PROPERTY_IN_DOUBLE_BEAM     0x02
462 #define RHYTHMSLASH_PROPERTY_FIRST_IN_BEAM      0x04
463 #define RHYTHMSLASH_PROPERTY_PARTIAL_BEAM       0x08
464 #define RHYTHMSLASH_PROPERTY_LAST_IN_BEAM       0x10
465 #define RHYTHMSLASH_PROPERTY_TRIPLET_FIRST      0x20
466 #define RHYTHMSLASH_PROPERTY_TRIPLET_SECOND     0x40
467 #define RHYTHMSLASH_PROPERTY_TRIPLET_THIRD      0x80
468         uint8_t properties;
469         uint8_t offset;
470         uint8_t dotted;
471         uint8_t length;
472         uint8_t extra;
473 #define RHYTHMSLASH_EXTRA_ARPEGGIO_UP           0x02
474 #define RHYTHMSLASH_EXTRA_ACCENT                        0x20
475 #define RHYTHMSLASH_EXTRA_HEAVY_ACCENT          0x40
476         uint8_t singlenote; /* 
477                 5 MSB contains fret
478                 3 LSB contains string
479                 */
480 };
481
482 struct ptb_direction {
483         struct ptb_direction *prev, *next;
484         uint8_t nr_items;
485 };
486
487 struct ptbf {
488         int fd;
489         int mode;
490         char *filename;
491         char *data;
492         struct ptb_hdr hdr;
493         struct ptb_instrument {
494                 struct ptb_guitar *guitars;
495                 struct ptb_section *sections;
496                 struct ptb_guitarin *guitarins;
497                 struct ptb_chorddiagram *chorddiagrams;
498                 struct ptb_tempomarker *tempomarkers;
499                 struct ptb_dynamic *dynamics;
500                 struct ptb_floatingtext *floatingtexts;
501                 struct ptb_sectionsymbol *sectionsymbols;
502         } instrument[2];
503         off_t curpos;
504         struct ptb_font default_font;
505         struct ptb_font chord_name_font;
506         struct ptb_font tablature_font;
507         uint32_t staff_line_space; /* amount of space between lines on tab staff */
508         uint32_t fade_in; /* amount of fade-in at start of song */
509         uint32_t fade_out; /* amount of fade-out at end of song */
510 };
511
512 extern struct ptbf *ptb_read_mem(const char *data, size_t length);
513 extern struct ptbf *ptb_read_file(const char *ptb);
514 extern int ptb_write_file(const char *ptb, struct ptbf *);
515 extern void ptb_free(struct ptbf *);
516
517 extern void ptb_set_debug(int level);
518 extern void ptb_set_asserts_fatal(int yes);
519
520 extern uint8_t ptb_get_octave(struct ptb_guitar *guitar, uint8_t string, uint8_t fret);
521 extern uint8_t ptb_get_step(struct ptb_guitar *guitar, uint8_t string, uint8_t fret);
522 extern const char *ptb_get_tone(ptb_tone);
523 extern const char *ptb_get_tone_full(ptb_tone);
524
525 extern void ptb_get_position_difference(struct ptb_section *, int start, int end, int *bars, int *length);
526
527 /* Reading tuning data files (tunings.dat) */
528
529 struct ptb_tuning_dict {
530         uint16_t nr_tunings;
531
532         struct ptb_tuning {
533                 char *name;
534                 uint8_t capo;
535                 uint8_t nr_strings;
536                 uint8_t *strings;
537         } *tunings;
538 };
539
540 extern struct ptb_tuning_dict *ptb_read_tuning_dict(const char *);
541 extern int ptb_write_tuning_dict(const char *, struct ptb_tuning_dict *);
542 extern void ptb_free_tuning_dict(struct ptb_tuning_dict *);
543 extern const char *ptb_tuning_get_note(char);
544
545 #ifdef __cplusplus
546 }
547 #endif
548
549 #endif /* __PTB_H__ */