21 #include <QDataStream>
49 class QSmf::QSmfPrivate {
59 m_OldCurrTempo(500000),
77 double m_DblOldRealtime;
80 quint64 m_OldCurrTempo;
81 quint64 m_OldRealTime;
82 quint64 m_OldCurrTime;
83 quint64 m_RevisedTime;
84 quint64 m_TempoChangeTime;
86 quint64 m_NumBytesWritten;
91 QDataStream *m_IOStream;
93 QList<QSmfRecTempo> m_TempoList;
110 d->m_TempoList.clear();
117 bool QSmf::endOfSmf()
119 return d->m_IOStream->atEnd();
126 quint8 QSmf::getByte()
141 void QSmf::putByte(quint8 value)
143 *d->m_IOStream << value;
144 d->m_NumBytesWritten++;
152 void QSmf::addTempo(quint64 tempo, quint64 time)
154 QSmfRecTempo tempoRec;
155 tempoRec.tempo = tempo;
156 tempoRec.time = time;
157 d->m_TempoList.append(tempoRec);
163 void QSmf::readHeader()
168 d->m_CurrTempo = 500000;
169 d->m_OldCurrTempo = 500000;
170 addTempo(d->m_CurrTempo, 0);
171 if (d->m_Interactive)
179 readExpected(
"MThd");
180 d->m_ToBeRead = read32bit();
181 d->m_fileFormat = read16bit();
182 d->m_Tracks = read16bit();
183 d->m_Division = read16bit();
188 while ((d->m_ToBeRead > 0) && !endOfSmf())
192 if (d->m_ToBeRead > 0)
194 SMFError(
"Unexpected end of input");
201 void QSmf::readTrack()
206 static const quint8 chantype[16] =
207 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 1, 1, 2, 0 };
216 quint64 delta_ticks, save_time, save_tempo;
218 sysexcontinue =
false;
220 if (d->m_Interactive)
222 d->m_ToBeRead = std::numeric_limits<unsigned long long>::max();
226 readExpected(
"MTrk");
227 d->m_ToBeRead = read32bit();
231 d->m_DblRealTime = 0;
232 d->m_DblOldRealtime = 0;
233 d->m_OldCurrTime = 0;
234 d->m_OldRealTime = 0;
235 d->m_CurrTempo = findTempo();
239 while (!endOfSmf() && (d->m_Interactive || d->m_ToBeRead > 0))
242 if (d->m_Interactive)
248 delta_ticks = unsigned(readVarLen());
249 d->m_RevisedTime = d->m_CurrTime;
250 d->m_CurrTime += delta_ticks;
251 while (d->m_RevisedTime < d->m_CurrTime)
253 save_time = d->m_RevisedTime;
254 save_tempo = d->m_CurrTempo;
255 d->m_CurrTempo = findTempo();
256 if (d->m_CurrTempo != d->m_OldCurrTempo)
258 d->m_OldCurrTempo = d->m_CurrTempo;
259 d->m_OldRealTime = d->m_RealTime;
260 if (d->m_RevisedTime != d->m_TempoChangeTime)
262 d->m_DblOldRealtime = d->m_DblRealTime;
263 d->m_OldCurrTime = save_time;
265 delta_secs = ticksToSecs(d->m_RevisedTime - d->m_OldCurrTime,
266 quint16(d->m_Division), save_tempo);
267 d->m_DblRealTime = d->m_DblOldRealtime + delta_secs * 1600.0;
268 d->m_RealTime = llround(d->m_DblRealTime);
269 if (d->m_RevisedTime == d->m_TempoChangeTime)
271 d->m_OldCurrTime = d->m_RevisedTime;
272 d->m_DblOldRealtime = d->m_DblRealTime;
277 delta_secs = ticksToSecs(d->m_RevisedTime - d->m_OldCurrTime,
278 quint16(d->m_Division), d->m_CurrTempo);
279 d->m_DblRealTime = d->m_DblOldRealtime + delta_secs * 1600.0;
280 d->m_RealTime = llround(d->m_DblRealTime);
288 SMFError(
"didn't find expected continuation of a SysEx");
296 SMFError(
"unexpected running status");
305 needed = chantype[status >> 4 & 0x0f];
318 channelMessage(status, c1, getByte());
322 channelMessage(status, c1, 0);
332 lookfor = quint64(readVarLen());
333 lookfor = d->m_ToBeRead - lookfor;
335 while ((d->m_ToBeRead > lookfor) && !endOfSmf())
342 lookfor = quint64(readVarLen());
343 lookfor = d->m_ToBeRead - lookfor;
346 while ((d->m_ToBeRead > lookfor) && !endOfSmf())
357 sysexcontinue =
true;
361 lookfor = readVarLen();
362 lookfor = d->m_ToBeRead - lookfor;
367 while ((d->m_ToBeRead > lookfor) && !endOfSmf())
377 sysexcontinue =
false;
382 badByte(c, d->m_IOStream->device()->pos() - 1);
385 if ((d->m_ToBeRead > lookfor) && endOfSmf())
387 SMFError(
"Unexpected end of input");
400 for ( i = d->m_Tracks; (i > 0) && !endOfSmf(); i--)
413 void QSmf::SMFWrite()
417 writeHeaderChunk(d->m_fileFormat, d->m_Tracks, d->m_Division);
419 if (d->m_fileFormat == 1)
423 for (i = 0; i < d->m_Tracks; ++i)
435 d->m_IOStream = stream;
445 QFile file(fileName);
446 file.open(QIODevice::ReadOnly);
447 QDataStream ds(&file);
458 d->m_IOStream = stream;
468 QFile file(fileName);
469 file.open(QIODevice::WriteOnly);
470 QDataStream ds(&file);
481 void QSmf::writeHeaderChunk(
int format,
int ntracks,
int division)
485 write16bit(quint16(format));
486 write16bit(quint16(ntracks));
487 write16bit(quint16(division));
494 void QSmf::writeTrackChunk(
int track)
504 offset = d->m_IOStream->device()->pos();
506 write32bit(trklength);
507 d->m_NumBytesWritten = 0;
511 place_marker = d->m_IOStream->device()->pos();
512 d->m_IOStream->device()->seek(offset);
513 trklength = d->m_NumBytesWritten;
515 write32bit(trklength);
516 d->m_IOStream->device()->seek(place_marker);
527 writeVarLen(deltaTime);
529 putByte(d->m_LastStatus);
531 writeVarLen(data.size());
532 foreach(
char byte, data)
544 writeVarLen(deltaTime);
548 if (d->m_codec ==
nullptr)
549 lcldata = data.toLatin1();
551 lcldata = d->m_codec->fromUnicode(data);
552 writeVarLen(lcldata.length());
553 foreach(
char byte, lcldata)
566 writeVarLen(deltaTime);
580 writeVarLen(deltaTime);
594 const QByteArray& data)
598 writeVarLen(deltaTime);
608 SMFError(
"error: MIDI channel greater than 16");
612 if (d->m_LastStatus != c)
624 j = (data[0] == type ? 1 : 0);
625 for (i = j; i < data.size(); ++i)
641 writeVarLen(deltaTime);
644 SMFError(
"error: Wrong method for a system exclusive event");
648 SMFError(
"error: MIDI channel greater than 16");
651 if (d->m_LastStatus != c)
670 writeVarLen(deltaTime);
673 SMFError(
"error: Wrong method for a system exclusive event");
677 SMFError(
"error: MIDI channel greater than 16");
680 if (d->m_LastStatus != c)
698 unsigned int i, j, size;
700 writeVarLen(quint64(deltaTime));
703 SMFError(
"error: type should be system exclusive");
708 size = unsigned(len);
713 j = (c == type ? 1 : 0);
714 for (i = j; i < unsigned(len); ++i)
716 putByte(quint8(data[i]));
727 writeVarLen(deltaTime);
729 putByte(d->m_LastStatus);
732 putByte((seqnum >> 8) & 0xff);
733 putByte(seqnum & 0xff);
743 writeVarLen(deltaTime);
747 putByte((tempo >> 16) & 0xff);
748 putByte((tempo >> 8) & 0xff);
749 putByte(tempo & 0xff);
759 long us_tempo = 60000000l / tempo;
773 writeVarLen(deltaTime);
791 writeVarLen(quint64(deltaTime));
795 putByte(quint8(tone));
796 putByte(mode & 0x01);
803 void QSmf::writeVarLen(quint64 value)
807 buffer = value & 0x7f;
808 while ((value >>= 7) > 0)
812 buffer += (value & 0x7f);
816 putByte(buffer & 0xff);
826 void QSmf::write32bit(quint32 data)
828 putByte((data >> 24) & 0xff);
829 putByte((data >> 16) & 0xff);
830 putByte((data >> 8) & 0xff);
831 putByte(data & 0xff);
834 void QSmf::write16bit(quint16 data)
836 putByte((data >> 8) & 0xff);
837 putByte(data & 0xff);
840 quint16 QSmf::to16bit(quint8 c1, quint8 c2)
843 value = quint16(c1 << 8);
848 quint32 QSmf::to32bit(quint8 c1, quint8 c2, quint8 c3, quint8 c4)
851 value = unsigned(c1 << 24);
852 value += unsigned(c2 << 16);
853 value += unsigned(c3 << 8);
858 quint16 QSmf::read16bit()
863 return to16bit(c1, c2);
866 quint32 QSmf::read32bit()
868 quint8 c1, c2, c3, c4;
873 return to32bit(c1, c2, c3, c4);
876 long QSmf::readVarLen()
889 value = (value << 7) + (c & 0x7f);
890 }
while ((c & 0x80) != 0);
895 void QSmf::readExpected(
const QString& s)
899 for (j = 0; j < s.length(); ++j)
902 if (QChar(b) != s[j])
904 SMFError(QString(
"Invalid (%1) SMF format at %2").arg(b, 0, 16).arg(d->m_IOStream->device()->pos()));
910 quint64 QSmf::findTempo()
912 quint64 result, old_tempo, new_tempo;
913 QSmfRecTempo rec = d->m_TempoList.last();
914 old_tempo = d->m_CurrTempo;
915 new_tempo = d->m_CurrTempo;
916 QList<QSmfRecTempo>::Iterator it;
917 for( it = d->m_TempoList.begin(); it != d->m_TempoList.end(); ++it )
920 if (rec.time <= d->m_CurrTime)
922 old_tempo = rec.tempo;
924 new_tempo = rec.tempo;
925 if (rec.time > d->m_RevisedTime)
930 if ((rec.time <= d->m_RevisedTime) || (rec.time > d->m_CurrTime))
932 d->m_RevisedTime = d->m_CurrTime;
937 d->m_RevisedTime = rec.time;
938 d->m_TempoChangeTime = d->m_RevisedTime;
947 double QSmf::ticksToSecs(quint64 ticks, quint16 division, quint64 tempo)
951 double smpte_resolution;
955 result = double(ticks * tempo)/(division * 1000000.0);
959 smpte_format = upperByte(division);
960 smpte_resolution = lowerByte(division);
961 result = double(ticks)/(smpte_format * smpte_resolution
967 void QSmf::SMFError(
const QString& s)
972 void QSmf::channelMessage(quint8 status, quint8 c1, quint8 c2)
979 SMFError(QString(
"ChannelMessage with bad c1 = %1").arg(c1));
984 SMFError(QString(
"ChannelMessage with bad c2 = %1").arg(c2));
1008 k = c1 + (c2 << 7) - 8192;
1012 SMFError(QString(
"Invalid MIDI status %1. Unhandled event").arg(status));
1017 void QSmf::metaEvent(quint8 b)
1020 QByteArray m(d->m_MsgBuff);
1035 if (d->m_codec ==
nullptr)
1038 s = d->m_codec->toUnicode(m);
1052 d->m_CurrTempo = to32bit(0, m[0], m[1], m[2]);
1054 rec = d->m_TempoList.last();
1055 if (rec.tempo == d->m_CurrTempo)
1059 if (rec.time > d->m_CurrTime)
1063 addTempo(d->m_CurrTempo, d->m_CurrTime);
1086 QByteArray varr(d->m_MsgBuff);
1090 void QSmf::badByte(quint8 b,
int p)
1092 SMFError(QString(
"Unexpected byte (%1) at %2").arg(b, 2, 16).arg(p));
1095 quint8 QSmf::lowerByte(quint16 x)
1100 quint8 QSmf::upperByte(quint16 x)
1102 return ((x >> 8) & 0xff);
1105 void QSmf::msgInit()
1107 d->m_MsgBuff.truncate(0);
1110 void QSmf::msgAdd(quint8 b)
1112 int s = d->m_MsgBuff.size();
1113 d->m_MsgBuff.resize(s + 1);
1114 d->m_MsgBuff[s] = b;
1125 return d->m_CurrTime;
1134 return d->m_CurrTempo;
1143 return d->m_RealTime;
1152 return d->m_Division;
1161 d->m_Division = division;
1179 d->m_Tracks = tracks;
1188 return d->m_fileFormat;
1197 d->m_fileFormat = fileFormat;
1206 return long(d->m_IOStream->device()->pos());
The QObject class is the base class of all Qt objects.
int getTracks()
Gets the number of tracks.
void signalSMFKeyPress(int chan, int pitch, int press)
Emitted after reading a Polyphonic Aftertouch message.
void setTextCodec(QTextCodec *codec)
Sets the text codec for text meta-events.
long getRealTime()
Gets the real time in seconds.
long getCurrentTempo()
Gets the current tempo.
void signalSMFforcedChannel(int channel)
Emitted after reading a Forced channel message.
void setDivision(int division)
Sets the resolution.
long getFilePos()
Gets the position in the SMF stream.
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 signalSMFText(int typ, const QString &data)
Emitted after reading a SMF text message.
void signalSMFKeySig(int b0, int b1)
Emitted after reading a SMF Key Signature smessage.
long getCurrentTime()
Gets the current time in ticks.
void signalSMFChanPress(int chan, int press)
Emitted after reading a Channel Aftertouch message.
void setTracks(int tracks)
Sets the number of tracks.
void writeTimeSignature(long deltaTime, int num, int den, int cc, int bb)
Writes a Time Signature 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.
void signalSMFTrackStart()
Emitted after reading a track prefix.
void signalSMFError(const QString &errorStr)
Emitted for a SMF read or write error.
int getDivision()
Gets the resolution.
QSmf(QObject *parent=nullptr)
Constructor.
void writeToFile(const QString &fileName)
Writes a SMF stream to a disk file.
void signalSMFSeqSpecific(const QByteArray &data)
Emitted after reading a Sequencer specific message.
void signalSMFWriteTempoTrack()
Emitted to request the user to write the tempo track.
void signalSMFendOfTrack()
Emitted after reading a End-Of-Track message.
void writeSequenceNumber(long deltaTime, int seqnum)
Writes a MIDI Sequence number.
void signalSMFHeader(int format, int ntrks, int division)
Emitted after reading a SMF header.
void readFromStream(QDataStream *stream)
Reads a SMF stream.
void signalSMFProgram(int chan, int patch)
Emitted after reading a Program change message.
void signalSMFNoteOff(int chan, int pitch, int vol)
Emitted after reading a Note Off message.
void writeMetaEvent(long deltaTime, int type, const QByteArray &data)
Writes a variable length Meta Event.
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.
QTextCodec * getTextCodec()
Gets the text codec used for text meta-events I/O.
virtual ~QSmf()
Destructor.
void writeBpmTempo(long deltaTime, int tempo)
Writes a Tempo change message.
void readFromFile(const QString &fileName)
Reads a SMF stream from a disk file.
void signalSMFSysex(const QByteArray &data)
Emitted after reading a System Exclusive message.
void writeToStream(QDataStream *stream)
Writes a SMF stream.
void writeTempo(long deltaTime, long tempo)
Writes a Tempo change message.
void signalSMFCtlChange(int chan, int ctl, int value)
Emitted after reading a Control Change message.
void writeKeySignature(long deltaTime, int tone, int mode)
Writes a key Signature message.
void setFileFormat(int fileFormat)
Sets the SMF file format.
void signalSMFPitchBend(int chan, int value)
Emitted after reading a Bender message.
int getFileFormat()
Gets the SMF file format.
void writeMidiEvent(long deltaTime, int type, int chan, int b1)
Writes a MIDI message with a single parameter.
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.
const quint8 system_exclusive
MIDI event System Exclusive begin.
const quint8 sequencer_specific
SMF Sequencer specific.
const quint32 MTrk
SMF Track prefix.
const quint8 forced_channel
SMF Forced MIDI channel.
const quint8 midi_channel_mask
Mask to extract the channel from the status byte.
const quint8 note_on
MIDI event Note On.
const quint8 control_change
MIDI event Control change.
const quint8 note_off
MIDI event Note Off.
const quint8 smpte_offset
SMF SMPTE offset.
const quint8 sequence_number
SMF Sequence number.
const quint8 sequence_name
SMF Sequence name.
const quint8 pitch_wheel
MIDI event Bender.
const quint8 meta_event
SMF Meta Event prefix.
const quint8 time_signature
SMF Time signature.
const quint8 end_of_track
SMF End of track.
const quint32 MThd
SMF Header prefix.
const quint8 poly_aftertouch
MIDI event Polyphonic pressure.
const quint8 key_signature
SMF Key signature.
const quint8 text_event
SMF Text event.
const quint8 channel_aftertouch
MIDI event Channel after-touch.
const quint8 instrument_name
SMF Instrument name.
const quint8 marker
SMF Marker.
const quint8 forced_port
SMF Forced MIDI port.
const quint8 copyright_notice
SMF Copyright notice.
const quint8 program_chng
MIDI event Program change.
const quint8 midi_command_mask
Mask to extract the command from the status byte.
const quint8 end_of_sysex
MIDI event System Exclusive end.
const quint8 lyric
SMF Lyric.
const quint8 set_tempo
SMF Tempo change.
Standard MIDI Files Input/Output.