MidiImport.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * MidiImport.h - support for importing MIDI-files
  3. *
  4. * Copyright (c) 2005-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program (see COPYING); if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301 USA.
  22. *
  23. */
  24. #ifndef _MIDI_IMPORT_H
  25. #define _MIDI_IMPORT_H
  26. #include <QString>
  27. #include <QPair>
  28. #include <QVector>
  29. #include "MidiEvent.h"
  30. #include "ImportFilter.h"
  31. class MidiImport : public ImportFilter
  32. {
  33. Q_OBJECT
  34. public:
  35. MidiImport( const QString & _file );
  36. virtual ~MidiImport();
  37. virtual PluginView * instantiateView( QWidget * )
  38. {
  39. return( NULL );
  40. }
  41. private:
  42. virtual bool tryImport( TrackContainer* tc );
  43. bool readSMF( TrackContainer* tc );
  44. bool readRIFF( TrackContainer* tc );
  45. bool readTrack( int _track_end, QString & _track_name );
  46. void error( void );
  47. inline int readInt( int _bytes )
  48. {
  49. int c, value = 0;
  50. do
  51. {
  52. c = readByte();
  53. if( c == -1 )
  54. {
  55. return( -1 );
  56. }
  57. value = ( value << 8 ) | c;
  58. } while( --_bytes );
  59. return( value );
  60. }
  61. inline int read32LE()
  62. {
  63. int value = readByte();
  64. value |= readByte() << 8;
  65. value |= readByte() << 16;
  66. value |= readByte() << 24;
  67. return value;
  68. }
  69. inline int readVar()
  70. {
  71. int c = readByte();
  72. int value = c & 0x7f;
  73. if( c & 0x80 )
  74. {
  75. c = readByte();
  76. value = ( value << 7 ) | ( c & 0x7f );
  77. if( c & 0x80 )
  78. {
  79. c = readByte();
  80. value = ( value << 7 ) | ( c & 0x7f );
  81. if( c & 0x80 )
  82. {
  83. c = readByte();
  84. value = ( value << 7 ) | c;
  85. if( c & 0x80 )
  86. {
  87. return -1;
  88. }
  89. }
  90. }
  91. }
  92. return( !file().atEnd() ? value : -1 );
  93. }
  94. inline int readID()
  95. {
  96. return read32LE();
  97. }
  98. inline void skip( int _bytes )
  99. {
  100. while( _bytes > 0 )
  101. {
  102. readByte();
  103. --_bytes;
  104. }
  105. }
  106. typedef QVector<QPair<int, MidiEvent> > EventVector;
  107. EventVector m_events;
  108. int m_timingDivision;
  109. } ;
  110. #endif