drumstick 2.9.0
C++ MIDI libraries using Qt objects, idioms, and style.
qsmf.h
Go to the documentation of this file.
1/*
2 Standard MIDI File component
3 Copyright (C) 2006-2023, 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 3 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#include <QScopedPointer>
27
28class QTextCodec;
29class QDataStream;
30
36#if defined(DRUMSTICK_STATIC)
37#define DRUMSTICK_FILE_EXPORT
38#else
39#if defined(drumstick_file_EXPORTS)
40#define DRUMSTICK_FILE_EXPORT Q_DECL_EXPORT
41#else
42#define DRUMSTICK_FILE_EXPORT Q_DECL_IMPORT
43#endif
44#endif
45
46namespace drumstick {
51namespace File {
52
58const quint32 MThd = 0x4d546864;
59const quint32 MTrk = 0x4d54726b;
61/* Standard MIDI Files meta event definitions */
62const quint8 meta_event = 0xff;
63const quint8 sequence_number = 0x00;
64const quint8 text_event = 0x01;
65const quint8 copyright_notice = 0x02;
66const quint8 sequence_name = 0x03;
67const quint8 instrument_name = 0x04;
68const quint8 lyric = 0x05;
69const quint8 marker = 0x06;
70const quint8 cue_point = 0x07;
71const quint8 forced_channel = 0x20;
72const quint8 forced_port = 0x21;
73const quint8 end_of_track = 0x2f;
74const quint8 set_tempo = 0x51;
75const quint8 smpte_offset = 0x54;
76const quint8 time_signature = 0x58;
77const quint8 key_signature = 0x59;
78const quint8 sequencer_specific = 0x7f;
80/* MIDI status commands most significant bit is 1 */
81const quint8 note_off = 0x80;
82const quint8 note_on = 0x90;
83const quint8 poly_aftertouch = 0xa0;
84const quint8 control_change = 0xb0;
85const quint8 program_chng = 0xc0;
86const quint8 channel_aftertouch = 0xd0;
87const quint8 pitch_wheel = 0xe0;
88const quint8 system_exclusive = 0xf0;
89const quint8 end_of_sysex = 0xf7;
91const quint8 midi_command_mask = 0xf0;
92const quint8 midi_channel_mask = 0x0f;
94const quint8 major_mode = 0;
95const quint8 minor_mode = 1;
102class DRUMSTICK_FILE_EXPORT QSmf : public QObject
103{
104 Q_OBJECT
105
106public:
107 explicit QSmf(QObject * parent = nullptr);
108 virtual ~QSmf();
109
110 void readFromStream(QDataStream *stream);
111 void readFromFile(const QString& fileName);
112 void writeToStream(QDataStream *stream);
113 void writeToFile(const QString& fileName);
114
115 void writeMetaEvent(long deltaTime, int type, const QByteArray& data);
116 void writeMetaEvent(long deltaTime, int type, const QString& data);
117 void writeMetaEvent(long deltaTime, int type, int data);
118 void writeMetaEvent(long deltaTime, int type);
119
120 void writeMidiEvent(long deltaTime, int type, int chan, int b1);
121 void writeMidiEvent(long deltaTime, int type, int chan, int b1, int b2);
122 void writeMidiEvent(long deltaTime, int type, int chan, const QByteArray& data);
123 void writeMidiEvent(long deltaTime, int type, long len, char* data);
124
125 void writeTempo(long deltaTime, long tempo);
126 void writeBpmTempo(long deltaTime, int tempo);
127 void writeTimeSignature(long deltaTime, int num, int den, int cc, int bb);
128 void writeKeySignature(long deltaTime, int tone, int mode);
129 void writeSequenceNumber(long deltaTime, int seqnum);
130
131 long getCurrentTime();
132 long getCurrentTempo();
133 long getRealTime();
134 long getFilePos();
135 int getDivision();
136 void setDivision(int division);
137 int getTracks();
138 void setTracks(int tracks);
139 int getFileFormat();
140 void setFileFormat(int fileFormat);
141 Q_DECL_DEPRECATED QTextCodec* getTextCodec();
142 Q_DECL_DEPRECATED void setTextCodec(QTextCodec *codec);
143
144Q_SIGNALS:
149 void signalSMFError(const QString& errorStr);
156 void signalSMFHeader(int format, int ntrks, int division);
163 void signalSMFNoteOn(int chan, int pitch, int vol);
170 void signalSMFNoteOff(int chan, int pitch, int vol);
177 void signalSMFKeyPress(int chan, int pitch, int press);
184 void signalSMFCtlChange(int chan, int ctl, int value);
190 void signalSMFPitchBend(int chan, int value);
196 void signalSMFProgram(int chan, int patch);
202 void signalSMFChanPress(int chan, int press);
207 void signalSMFSysex(const QByteArray& data);
212 void signalSMFSeqSpecific(const QByteArray& data);
219 void signalSMFMetaUnregistered(int typ, const QByteArray& data);
225 void signalSMFMetaMisc(int typ, const QByteArray& data);
230 void signalSMFSequenceNum(int seq);
235 void signalSMFforcedChannel(int channel);
240 void signalSMFforcedPort(int port);
248 Q_DECL_DEPRECATED void signalSMFText(int typ, const QString& data);
254 void signalSMFText2(int typ, const QByteArray& data);
263 void signalSMFSmpte(int b0, int b1, int b2, int b3, int b4);
271 void signalSMFTimeSig(int b0, int b1, int b2, int b3);
277 void signalSMFKeySig(int b0, int b1);
282 void signalSMFTempo(int tempo);
310 void signalSMFWriteTrack(int track);
311
312private:
316 struct QSmfRecTempo
317 {
318 quint64 tempo;
319 quint64 time;
320 };
321
322 class QSmfPrivate;
323 QScopedPointer<QSmfPrivate> d;
324
325 void SMFRead();
326 void SMFWrite();
327 quint8 getByte();
328 void putByte(quint8 value);
329 void readHeader();
330 void readTrack();
331 quint16 to16bit(quint8 c1, quint8 c2);
332 quint32 to32bit(quint8 c1, quint8 c2, quint8 c3, quint8 c4);
333 quint16 read16bit();
334 quint32 read32bit();
335 void write16bit(quint16 data);
336 void write32bit(quint32 data);
337 void writeVarLen(quint64 value);
338 double ticksToSecs(quint64 ticks, quint16 division, quint64 tempo);
339 long readVarLen();
340 void readExpected(const QString& s);
341 void addTempo(quint64 tempo, quint64 time);
342 quint64 findTempo();
343 void SMFError(const QString& s);
344 void channelMessage(quint8 status, quint8 c1, quint8 c2);
345 void msgInit();
346 void msgAdd(quint8 b);
347 void metaEvent(quint8 b);
348 void sysEx();
349 void badByte(quint8 b, int p);
350 quint8 lowerByte(quint16 x);
351 quint8 upperByte(quint16 x);
352 bool endOfSmf();
353 void writeHeaderChunk(int format, int ntracks, int division);
354 void writeTrackChunk(int track);
355};
356
357QString DRUMSTICK_FILE_EXPORT drumstickLibraryVersion();
358
361}} /* namespace drumstick::File */
362
363#endif /* DRUMSTICK_QSMF_H */
The QObject class is the base class of all Qt objects.
Standard MIDI Files input/output.
Definition: qsmf.h:103
void signalSMFKeyPress(int chan, int pitch, int press)
Emitted after reading a Polyphonic Aftertouch message.
void signalSMFforcedChannel(int channel)
Emitted after reading a Forced channel message.
void signalSMFforcedPort(int port)
Emitted after reading a Forced port message.
void signalSMFTimeSig(int b0, int b1, int b2, int b3)
Emitted after reading a SMF Time signature message.
void signalSMFKeySig(int b0, int b1)
Emitted after reading a SMF Key Signature smessage.
void signalSMFChanPress(int chan, int press)
Emitted after reading a Channel Aftertouch message.
void signalSMFTrackEnd()
Emitted after a track has finished.
void signalSMFNoteOn(int chan, int pitch, int vol)
Emitted after reading a Note On message.
void signalSMFTempo(int tempo)
Emitted after reading a Tempo Change message.
void signalSMFWriteTrack(int track)
Emitted to request the user to write a track.
Q_DECL_DEPRECATED void signalSMFText(int typ, const QString &data)
Emitted after reading a SMF text message.
void signalSMFTrackStart()
Emitted after reading a track prefix.
void signalSMFError(const QString &errorStr)
Emitted for a SMF read or write error.
void signalSMFSeqSpecific(const QByteArray &data)
Emitted after reading a Sequencer specific message.
void signalSMFWriteTempoTrack()
Emitted to request the user to prepare the tempo track.
void signalSMFendOfTrack()
Emitted after reading a End-Of-Track message.
void signalSMFHeader(int format, int ntrks, int division)
Emitted after reading a SMF header.
void signalSMFProgram(int chan, int patch)
Emitted after reading a Program change message.
void signalSMFText2(int typ, const QByteArray &data)
Emitted after reading a SMF text message.
void signalSMFNoteOff(int chan, int pitch, int vol)
Emitted after reading a Note Off message.
void signalSMFSequenceNum(int seq)
Emitted after reading a Sequence number message.
void signalSMFMetaMisc(int typ, const QByteArray &data)
Emitted after reading any SMF Meta message.
void signalSMFSysex(const QByteArray &data)
Emitted after reading a System Exclusive message.
void signalSMFCtlChange(int chan, int ctl, int value)
Emitted after reading a Control Change message.
void signalSMFPitchBend(int chan, int value)
Emitted after reading a Bender message.
void signalSMFMetaUnregistered(int typ, const QByteArray &data)
Emitted after reading an unregistered SMF Meta message.
void signalSMFSmpte(int b0, int b1, int b2, int b3, int b4)
Emitted after reading a SMPT offset message.
const quint8 cue_point
SMF Cue point.
Definition: qsmf.h:70
const quint8 system_exclusive
MIDI event System Exclusive begin.
Definition: qsmf.h:88
const quint8 sequencer_specific
SMF Sequencer specific.
Definition: qsmf.h:78
const quint32 MTrk
SMF Track prefix.
Definition: qsmf.h:59
const quint8 forced_channel
SMF Forced MIDI channel.
Definition: qsmf.h:71
const quint8 midi_channel_mask
Mask to extract the channel from the status byte.
Definition: qsmf.h:92
QString drumstickLibraryVersion()
drumstickLibraryVersion provides the Drumstick version as an edited QString
Definition: qsmf.cpp:1248
const quint8 note_on
MIDI event Note On.
Definition: qsmf.h:82
const quint8 control_change
MIDI event Control change.
Definition: qsmf.h:84
const quint8 major_mode
Major mode scale.
Definition: qsmf.h:94
const quint8 note_off
MIDI event Note Off.
Definition: qsmf.h:81
const quint8 smpte_offset
SMF SMPTE offset.
Definition: qsmf.h:75
const quint8 sequence_number
SMF Sequence number.
Definition: qsmf.h:63
const quint8 minor_mode
Minor mode scale.
Definition: qsmf.h:95
const quint8 sequence_name
SMF Sequence name.
Definition: qsmf.h:66
const quint8 pitch_wheel
MIDI event Bender.
Definition: qsmf.h:87
const quint8 meta_event
SMF Meta Event prefix.
Definition: qsmf.h:62
const quint8 time_signature
SMF Time signature.
Definition: qsmf.h:76
const quint8 end_of_track
SMF End of track.
Definition: qsmf.h:73
const quint32 MThd
SMF Header prefix.
Definition: qsmf.h:58
const quint8 poly_aftertouch
MIDI event Polyphonic pressure.
Definition: qsmf.h:83
const quint8 key_signature
SMF Key signature.
Definition: qsmf.h:77
const quint8 text_event
SMF Text event.
Definition: qsmf.h:64
const quint8 channel_aftertouch
MIDI event Channel after-touch.
Definition: qsmf.h:86
const quint8 instrument_name
SMF Instrument name.
Definition: qsmf.h:67
const quint8 marker
SMF Marker.
Definition: qsmf.h:69
const quint8 forced_port
SMF Forced MIDI port.
Definition: qsmf.h:72
const quint8 copyright_notice
SMF Copyright notice.
Definition: qsmf.h:65
const quint8 program_chng
MIDI event Program change.
Definition: qsmf.h:85
const quint8 midi_command_mask
Mask to extract the command from the status byte.
Definition: qsmf.h:91
const quint8 end_of_sysex
MIDI event System Exclusive end.
Definition: qsmf.h:89
const quint8 lyric
SMF Lyric.
Definition: qsmf.h:68
const quint8 set_tempo
SMF Tempo change.
Definition: qsmf.h:74
Drumstick common.
Definition: alsaclient.cpp:68