2 (c) 2005-2006 Jelmer Vernooij <jelmer@samba.org>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 void abc_write_header(FILE *out, struct ptbf *ret)
37 if(ret->hdr.classification == CLASSIFICATION_SONG) {
38 if(ret->hdr.class_info.song.title) fprintf(out, "T: %s\n", ret->hdr.class_info.song.title);
39 if(ret->hdr.class_info.song.music_by) fprintf(out, "C: %s\n", ret->hdr.class_info.song.music_by);
40 if(ret->hdr.class_info.song.words_by) fprintf(out, "%% Words By: %s\n", ret->hdr.class_info.song.words_by);
41 if(ret->hdr.class_info.song.copyright) fprintf(out, "%% Copyright: %s\n", ret->hdr.class_info.song.copyright);
42 if(ret->hdr.class_info.song.guitar_transcribed_by) fprintf(out, "Z: %s\n", ret->hdr.class_info.song.guitar_transcribed_by);
43 if(ret->hdr.class_info.song.release_type == RELEASE_TYPE_PR_AUDIO &&
44 ret->hdr.class_info.song.release_info.pr_audio.album_title) fprintf(out, "%% Album Title: %s\n", ret->hdr.class_info.song.release_info.pr_audio.album_title);
45 } else if(ret->hdr.classification == CLASSIFICATION_LESSON) {
46 if(ret->hdr.class_info.lesson.title) fprintf(out, "T:%s\n", ret->hdr.class_info.lesson.title);
47 if(ret->hdr.class_info.lesson.artist) fprintf(out, "C: %s\n", ret->hdr.class_info.lesson.artist);
48 if(ret->hdr.class_info.lesson.author) fprintf(out, "Z: %s\n", ret->hdr.class_info.lesson.author);
49 if(ret->hdr.class_info.lesson.copyright) fprintf(out, "%% Copyright: %s\n", ret->hdr.class_info.lesson.copyright);
54 static char *note_names[] = { "c", "c#", "d", "d#", "e", "f", "f#", "g", "g#", "a", "a#", "b" };
56 void abc_write_linedata(FILE *out, struct ptb_guitar *gtr, struct ptb_linedata *ld)
58 uint8_t octave = ptb_get_octave(gtr, ld->detailed.string, ld->detailed.fret);
59 uint8_t step = ptb_get_step(gtr, ld->detailed.string, ld->detailed.fret);
63 strcpy(n, note_names[step]);
65 if (octave < 4) n[0] = toupper(n[0]);
67 fprintf(out, "%s", n);
69 for (i = octave; i < 4; i++) fprintf(out, ",");
70 for (i = 5; i < octave; i++) fprintf(out, "'");
73 void abc_write_position(FILE *out, struct ptb_guitar *gtr, struct ptb_position *ps)
75 struct ptb_linedata *ld;
77 for (ld = ps->linedatas; ld; ld = ld->next) {
78 abc_write_linedata(out, gtr, ld);
82 void abc_write_staff(FILE *out, struct ptb_guitar *gtr, struct ptb_staff *staff)
84 struct ptb_position *ps;
86 for (ps = staff->positions[0]; ps; ps = ps->next) {
87 abc_write_position(out, gtr, ps);
93 void abc_write_section(FILE *out, struct ptb_guitar *gtr, struct ptb_section *sec)
97 for (st = sec->staffs; st; st = st->next) {
98 abc_write_staff(out, gtr, st);
102 void abc_write_chordtext(FILE *out, struct ptb_chordtext *name) {
105 if(name->properties & CHORDTEXT_PROPERTY_NOCHORD) {
106 fprintf(out, "N.C.");
109 if(name->properties & CHORDTEXT_PROPERTY_PARENTHESES) {
113 if(!(name->properties & CHORDTEXT_PROPERTY_NOCHORD) |
114 (name->properties & CHORDTEXT_PROPERTY_PARENTHESES)) {
115 if(name->name[0] == name->name[1]) {
116 fprintf(out, "%s", ptb_get_tone(name->name[0]));
118 fprintf(out, "%s/%s", ptb_get_tone(name->name[0]),
119 ptb_get_tone(name->name[1]));
123 if(name->properties & CHORDTEXT_PROPERTY_PARENTHESES) {
130 int abc_write_lyrics(FILE *out, struct ptbf *ret)
132 if(ret->hdr.classification != CLASSIFICATION_SONG || !ret->hdr.class_info.song.lyrics) return 0;
133 fprintf(out, "W: %s\n\n", ret->hdr.class_info.song.lyrics);
137 int main(int argc, const char **argv)
142 struct ptb_section *section;
149 struct poptOption options[] = {
151 {"debug", 'd', POPT_ARG_NONE, &debugging, 0, "Turn on debugging output" },
152 {"outputfile", 'o', POPT_ARG_STRING, &output, 0, "Write to specified file", "FILE" },
153 {"regular", 'r', POPT_ARG_NONE, &instrument, 0, "Write tabs for regular guitar" },
154 {"bass", 'b', POPT_ARG_NONE, &instrument, 1, "Write tabs for bass guitar"},
155 {"version", 'v', POPT_ARG_NONE, &version, 'v', "Show version information" },
159 pc = poptGetContext(argv[0], argc, argv, options, 0);
160 poptSetOtherOptionHelp(pc, "file.ptb");
161 while((c = poptGetNextOpt(pc)) >= 0) {
164 printf("ptb2abc Version "PACKAGE_VERSION"\n");
165 printf("(C) 2005-2006 Jelmer Vernooij <jelmer@samba.org>\n");
171 ptb_set_debug(debugging);
173 if(!poptPeekArg(pc)) {
174 poptPrintUsage(pc, stderr, 0);
177 input = poptGetArg(pc);
178 ret = ptb_read_file(input);
181 perror("Read error: ");
186 int baselength = strlen(input);
187 if (!strcmp(input + strlen(input) - 4, ".ptb")) {
190 output = malloc(baselength + 6);
191 strncpy(output, input, baselength);
192 strcpy(output + baselength, ".abc");
195 if(!strcmp(output, "-")) {
198 out = fopen(output, "w+");
205 fprintf(out, "%% Generated by ptb2abc (C) 2005-2006 Jelmer Vernooij <jelmer@samba.org>\n");
206 fprintf(out, "%% See http://jelmer.vernstok.nl/oss/ptabtools/ for more info\n\n");
207 fprintf(out, "X:1\n");
209 abc_write_header(out, ret);
210 abc_write_lyrics(out, ret);
212 fprintf(out, "M: C\n");
213 fprintf(out, "K: Cm\n");
214 fprintf(out, "L: 1/4\n");
216 section = ret->instrument[instrument].sections;
218 abc_write_section(out, ret->instrument[instrument].guitars, section);
219 fprintf(out, "\n\n");
220 section = section->next;
223 if(output)fclose(out);