drumstick  1.1.3
qsmf.h
Go to the documentation of this file.
1 /*
2  Standard MIDI File component
3  Copyright (C) 2006-2019, Pedro Lopez-Cabanillas <plcl@users.sf.net>
4 
5  Based on midifile.c by Tim Thompson, M.Czeiszperger and Greg Lee
6 
7  This library is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef DRUMSTICK_QSMF_H
22 #define DRUMSTICK_QSMF_H
23 
24 #include "macros.h"
25 #include <QObject>
26 
27 class QDataStream;
28 
36 namespace drumstick {
37 
38 #define MThd 0x4d546864
39 #define MTrk 0x4d54726b
41 /* Standard MIDI Files meta event definitions */
42 #define meta_event 0xff
43 #define sequence_number 0x00
44 #define text_event 0x01
45 #define copyright_notice 0x02
46 #define sequence_name 0x03
47 #define instrument_name 0x04
48 #define lyric 0x05
49 #define marker 0x06
50 #define cue_point 0x07
51 #define forced_channel 0x20
52 #define forced_port 0x21
53 #define end_of_track 0x2f
54 #define set_tempo 0x51
55 #define smpte_offset 0x54
56 #define time_signature 0x58
57 #define key_signature 0x59
58 #define sequencer_specific 0x7f
60 /* MIDI status commands most significant bit is 1 */
61 #define note_off 0x80
62 #define note_on 0x90
63 #define poly_aftertouch 0xa0
64 #define control_change 0xb0
65 #define program_chng 0xc0
66 #define channel_aftertouch 0xd0
67 #define pitch_wheel 0xe0
68 #define system_exclusive 0xf0
69 #define end_of_sysex 0xf7
71 #define midi_command_mask 0xf0
72 #define midi_channel_mask 0x0f
74 #define major_mode 0
75 #define minor_mode 1
82 class DRUMSTICK_EXPORT QSmf : public QObject
83 {
84  Q_OBJECT
85 
86 public:
87  QSmf(QObject * parent = nullptr);
88  virtual ~QSmf();
89 
90  void readFromStream(QDataStream *stream);
91  void readFromFile(const QString& fileName);
92  void writeToStream(QDataStream *stream);
93  void writeToFile(const QString& fileName);
94 
95  void writeMetaEvent(long deltaTime, int type, const QByteArray& data);
96  void writeMetaEvent(long deltaTime, int type, const QString& data);
97  void writeMetaEvent(long deltaTime, int type, int data);
98  void writeMetaEvent(long deltaTime, int type);
99 
100  void writeMidiEvent(long deltaTime, int type, int chan, int b1);
101  void writeMidiEvent(long deltaTime, int type, int chan, int b1, int b2);
102  void writeMidiEvent(long deltaTime, int type, int chan, const QByteArray& data);
103  void writeMidiEvent(long deltaTime, int type, long len, char* data);
104 
105  void writeTempo(long deltaTime, long tempo);
106  void writeBpmTempo(long deltaTime, int tempo);
107  void writeTimeSignature(long deltaTime, int num, int den, int cc, int bb);
108  void writeKeySignature(long deltaTime, int tone, int mode);
109  void writeSequenceNumber(long deltaTime, int seqnum);
110 
111  long getCurrentTime();
112  long getCurrentTempo();
113  long getRealTime();
114  long getFilePos();
115  int getDivision();
116  void setDivision(int division);
117  int getTracks();
118  void setTracks(int tracks);
119  int getFileFormat();
120  void setFileFormat(int fileFormat);
121  QTextCodec* getTextCodec();
122  void setTextCodec(QTextCodec *codec);
123 
124 signals:
129  void signalSMFError(const QString& errorStr);
136  void signalSMFHeader(int format, int ntrks, int division);
143  void signalSMFNoteOn(int chan, int pitch, int vol);
150  void signalSMFNoteOff(int chan, int pitch, int vol);
157  void signalSMFKeyPress(int chan, int pitch, int press);
164  void signalSMFCtlChange(int chan, int ctl, int value);
170  void signalSMFPitchBend(int chan, int value);
176  void signalSMFProgram(int chan, int patch);
182  void signalSMFChanPress(int chan, int press);
187  void signalSMFSysex(const QByteArray& data);
192  void signalSMFSeqSpecific(const QByteArray& data);
199  void signalSMFMetaUnregistered(int typ, const QByteArray& data);
205  void signalSMFMetaMisc(int typ, const QByteArray& data);
210  void signalSMFSequenceNum(int seq);
215  void signalSMFforcedChannel(int channel);
220  void signalSMFforcedPort(int port);
226  void signalSMFText(int typ, const QString& data);
235  void signalSMFSmpte(int b0, int b1, int b2, int b3, int b4);
243  void signalSMFTimeSig(int b0, int b1, int b2, int b3);
249  void signalSMFKeySig(int b0, int b1);
254  void signalSMFTempo(int tempo);
258  void signalSMFendOfTrack();
262  void signalSMFTrackStart();
266  void signalSMFTrackEnd();
271  void signalSMFWriteTempoTrack();
276  void signalSMFWriteTrack(int track);
277 
278 private:
282  struct QSmfRecTempo
283  {
284  quint64 tempo;
285  quint64 time;
286  };
287 
288  class QSmfPrivate;
289  QSmfPrivate *d;
290 
291  void SMFRead();
292  void SMFWrite();
293  quint8 getByte();
294  void putByte(quint8 value);
295  void readHeader();
296  void readTrack();
297  quint16 to16bit(quint8 c1, quint8 c2);
298  quint32 to32bit(quint8 c1, quint8 c2, quint8 c3, quint8 c4);
299  quint16 read16bit();
300  quint32 read32bit();
301  void write16bit(quint16 data);
302  void write32bit(quint32 data);
303  void writeVarLen(quint64 value);
304  double ticksToSecs(quint64 ticks, quint16 division, quint64 tempo);
305  long readVarLen();
306  void readExpected(const QString& s);
307  void addTempo(quint64 tempo, quint64 time);
308  quint64 findTempo();
309  void SMFError(const QString& s);
310  void channelMessage(quint8 status, quint8 c1, quint8 c2);
311  void msgInit();
312  void msgAdd(quint8 b);
313  void metaEvent(quint8 b);
314  void sysEx();
315  void badByte(quint8 b, int p);
316  quint8 lowerByte(quint16 x);
317  quint8 upperByte(quint16 x);
318  bool endOfSmf();
319  void writeHeaderChunk(int format, int ntracks, int division);
320  void writeTrackChunk(int track);
321 };
322 
323 } /* namespace drumstick */
324 
327 #endif /* DRUMSTICK_QSMF_H */
QObject
macros.h
drumstick::QSmf
Standard MIDI Files input/output.
Definition: qsmf.h:83