File: smf2xml.cpp

package info (click to toggle)
musescore2 2.3.2%2Bdfsg4-16
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 170,464 kB
  • sloc: cpp: 262,612; xml: 176,707; sh: 3,377; ansic: 1,246; python: 356; makefile: 227; perl: 82; pascal: 78
file content (172 lines) | stat: -rw-r--r-- 6,346 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//=============================================================================
//  MuseScore
//  Linux Music Score Editor
//
//  Copyright (C) 2002-2007 Werner Schweer and others
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//=============================================================================

#include <stdio.h>
#include <unistd.h>
#include <QFile>

#include "xmlwriter.h"
#include "midifile.h"

static const char versionString[] = "0.1";

bool debugMode = false;

//---------------------------------------------------------
//   printVersion
//---------------------------------------------------------

static void printVersion()
      {
      printf("This is smf2xml version %s\n", versionString);
      }

//---------------------------------------------------------
//   usage
//---------------------------------------------------------

static void usage()
      {
      printVersion();
      printf("Usage: smf2xml [args] [infile] [outfile]\n");
      printf("   args:\n"
             "      -v      print version\n"
             "      -d      debug mode\n"
            );
      }

//---------------------------------------------------------
//   main
//---------------------------------------------------------

int main(int argc, char* argv[])
      {
      int c;
      while ((c = getopt(argc, argv, "vd")) != EOF) {
            switch (c) {
                  case 'v':
                        printVersion();
                        return 0;
                  case 'd':
                        debugMode = true;
                        break;
                  default:
                        usage();
                        return -1;
                  }
            }
      QIODevice* in = 0;
      QIODevice* out = 0;

      switch (argc - optind) {
            case 2:
                  out = new QFile(argv[1 + optind]);
                  if (!out->open(QIODevice::WriteOnly)) {
                        fprintf(stderr, "Cannot open output file <%s>: %s\n", argv[2], strerror(errno));
                        return -3;
                        }
            case 1:
                  in = new QFile(argv[0 + optind]);
                  if (!in->open(QIODevice::ReadOnly)) {
                        fprintf(stderr, "Cannot open input file <%s>: %s\n", argv[1], strerror(errno));
                        return -4;
                        }
                  break;
            case 0:
                  break;
            default:
                  usage();
                  return -2;
                  break;
            }
      if (in == 0) {
            in = new QFile;
            ((QFile*)in)->open(stdin, QIODevice::ReadOnly);
            }
      if (out == 0) {
            out = new QFile;
            ((QFile*)out)->open(stdout, QIODevice::WriteOnly);
            }

      MidiFile mf;
      mf.read(in);

      XmlWriter xml(out);
      xml.header();
      xml.stag("SMF");
      xml.tag("division", mf.division());
      xml.tag("format", mf.format());

      for (MidiTrack* t : mf.tracks()) {
            xml.stag("Track");
            for (auto i = t->events().begin(); i != t->events().end(); ++i) {
                  switch (i->second.type()) {
                        case MidiEventType::NOTEOFF:
                              xml.tagE(QString("NoteOff tick=\"%1\" c=\"%2\" a=\"%3\" b=\"%4\"")
                                 .arg(i->first)
                                 .arg(int(i->second.channel()), 0, 10)
                                 .arg(int(i->second.dataA()), 2, 16)
                                 .arg(int(i->second.dataB()), 2, 16)
                                 );
                              break;
                        case MidiEventType::NOTEON:
                              xml.tagE(QString("NoteOn  tick=\"%1\" c=\"%2\" a=\"%3\" b=\"%4\"")
                                 .arg(i->first)
                                 .arg(int(i->second.channel()), 0, 10)
                                 .arg(int(i->second.dataA()), 2, 16)
                                 .arg(int(i->second.dataB()), 2, 16)
                                 );
                              break;
                        case MidiEventType::CONTROLLER:
                              xml.tagE(QString("Ctrl    tick=\"%1\" c=\"%2\" a=\"%3\" b=\"%4\"")
                                 .arg(i->first)
                                 .arg(int(i->second.channel()), 0, 10)
                                 .arg(int(i->second.dataA()), 2, 16)
                                 .arg(int(i->second.dataB()), 2, 16)
                                 );
                              break;
                        case MidiEventType::PROGRAM:
                              xml.tagE(QString("Program tick=\"%1\" c=\"%2\" a=\"%3\"")
                                 .arg(i->first)
                                 .arg(int(i->second.channel()), 0, 10)
                                 .arg(int(i->second.dataA()), 2, 16)
                                 );
                              break;
                        default:
                              printf("unknown event\n");
                              xml.tagE(QString("Event   tick=\"%1\" t=\"%2\" c=\"%3\" a=\"%4\" b=\"%5\"")
                                 .arg(i->first)
                                 .arg(int(i->second.type()), 2, 16)
                                 .arg(int(i->second.channel()), 0, 10)
                                 .arg(int(i->second.dataA()), 2, 16)
                                 .arg(int(i->second.dataB()), 2, 16)
                                 );
                              break;
                        }
                  }
            xml.etag();
            }

      xml.etag();
      delete in;
      delete out;

      return 0;
      }