Song.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * Song.h - class song - the root of the model-tree
  3. *
  4. * Copyright (c) 2004-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 SONG_H
  25. #define SONG_H
  26. #include <utility>
  27. #include <QtCore/QSharedMemory>
  28. #include <QtCore/QVector>
  29. #include "TrackContainer.h"
  30. #include "Controller.h"
  31. #include "MeterModel.h"
  32. #include "Mixer.h"
  33. #include "VstSyncController.h"
  34. class AutomationTrack;
  35. class Pattern;
  36. class TimeLineWidget;
  37. const bpm_t MinTempo = 10;
  38. const bpm_t DefaultTempo = 140;
  39. const bpm_t MaxTempo = 999;
  40. const tick_t MaxSongLength = 9999 * DefaultTicksPerTact;
  41. class EXPORT Song : public TrackContainer
  42. {
  43. Q_OBJECT
  44. mapPropertyFromModel( int,getTempo,setTempo,m_tempoModel );
  45. mapPropertyFromModel( int,masterPitch,setMasterPitch,m_masterPitchModel );
  46. mapPropertyFromModel( int,masterVolume,setMasterVolume, m_masterVolumeModel );
  47. public:
  48. enum PlayModes
  49. {
  50. Mode_None,
  51. Mode_PlaySong,
  52. Mode_PlayBB,
  53. Mode_PlayPattern,
  54. Mode_PlayAutomationPattern,
  55. Mode_Count
  56. } ;
  57. void clearErrors();
  58. void collectError( const QString error );
  59. bool hasErrors();
  60. QString errorSummary();
  61. class PlayPos : public MidiTime
  62. {
  63. public:
  64. PlayPos( const int abs = 0 ) :
  65. MidiTime( abs ),
  66. m_timeLine( NULL ),
  67. m_currentFrame( 0.0f )
  68. {
  69. }
  70. inline void setCurrentFrame( const float f )
  71. {
  72. m_currentFrame = f;
  73. }
  74. inline float currentFrame() const
  75. {
  76. return m_currentFrame;
  77. }
  78. TimeLineWidget * m_timeLine;
  79. private:
  80. float m_currentFrame;
  81. } ;
  82. void processNextBuffer();
  83. inline int getLoadingTrackCount() const
  84. {
  85. return m_nLoadingTrack;
  86. }
  87. inline int getMilliseconds() const
  88. {
  89. return m_elapsedMilliSeconds;
  90. }
  91. inline void setMilliSeconds( float ellapsedMilliSeconds )
  92. {
  93. m_elapsedMilliSeconds = ellapsedMilliSeconds;
  94. }
  95. inline int getTacts() const
  96. {
  97. return currentTact();
  98. }
  99. inline int ticksPerTact() const
  100. {
  101. return MidiTime::ticksPerTact(m_timeSigModel);
  102. }
  103. // Returns the beat position inside the bar, 0-based
  104. inline int getBeat() const
  105. {
  106. return getPlayPos().getBeatWithinBar(m_timeSigModel);
  107. }
  108. // the remainder after bar and beat are removed
  109. inline int getBeatTicks() const
  110. {
  111. return getPlayPos().getTickWithinBeat(m_timeSigModel);
  112. }
  113. inline int getTicks() const
  114. {
  115. return currentTick();
  116. }
  117. inline f_cnt_t getFrames() const
  118. {
  119. return currentFrame();
  120. }
  121. inline bool isPaused() const
  122. {
  123. return m_paused;
  124. }
  125. inline bool isPlaying() const
  126. {
  127. return m_playing == true && m_exporting == false;
  128. }
  129. inline bool isStopped() const
  130. {
  131. return m_playing == false && m_paused == false;
  132. }
  133. inline bool isExporting() const
  134. {
  135. return m_exporting;
  136. }
  137. inline void setExportLoop( bool exportLoop )
  138. {
  139. m_exportLoop = exportLoop;
  140. }
  141. inline bool isRecording() const
  142. {
  143. return m_recording;
  144. }
  145. bool isExportDone() const;
  146. std::pair<MidiTime, MidiTime> getExportEndpoints() const;
  147. inline void setRenderBetweenMarkers( bool renderBetweenMarkers )
  148. {
  149. m_renderBetweenMarkers = renderBetweenMarkers;
  150. }
  151. inline PlayModes playMode() const
  152. {
  153. return m_playMode;
  154. }
  155. inline PlayPos & getPlayPos( PlayModes pm )
  156. {
  157. return m_playPos[pm];
  158. }
  159. inline const PlayPos & getPlayPos( PlayModes pm ) const
  160. {
  161. return m_playPos[pm];
  162. }
  163. inline const PlayPos & getPlayPos() const
  164. {
  165. return getPlayPos(m_playMode);
  166. }
  167. void updateLength();
  168. tact_t length() const
  169. {
  170. return m_length;
  171. }
  172. bpm_t getTempo();
  173. virtual AutomationPattern * tempoAutomationPattern();
  174. AutomationTrack * globalAutomationTrack()
  175. {
  176. return m_globalAutomationTrack;
  177. }
  178. //TODO: Add Q_DECL_OVERRIDE when Qt4 is dropped
  179. AutomatedValueMap automatedValuesAt(MidiTime time, int tcoNum = -1) const;
  180. // file management
  181. void createNewProject();
  182. void createNewProjectFromTemplate( const QString & templ );
  183. void loadProject( const QString & filename );
  184. bool guiSaveProject();
  185. bool guiSaveProjectAs( const QString & filename );
  186. bool saveProjectFile( const QString & filename );
  187. const QString & projectFileName() const
  188. {
  189. return m_fileName;
  190. }
  191. bool isLoadingProject() const
  192. {
  193. return m_loadingProject;
  194. }
  195. void loadingCancelled()
  196. {
  197. m_isCancelled = true;
  198. Engine::mixer()->clearNewPlayHandles();
  199. }
  200. bool isCancelled()
  201. {
  202. return m_isCancelled;
  203. }
  204. bool isModified() const
  205. {
  206. return m_modified;
  207. }
  208. virtual QString nodeName() const
  209. {
  210. return "song";
  211. }
  212. virtual bool fixedTCOs() const
  213. {
  214. return false;
  215. }
  216. void addController( Controller * c );
  217. void removeController( Controller * c );
  218. const ControllerVector & controllers() const
  219. {
  220. return m_controllers;
  221. }
  222. MeterModel & getTimeSigModel()
  223. {
  224. return m_timeSigModel;
  225. }
  226. public slots:
  227. void playSong();
  228. void record();
  229. void playAndRecord();
  230. void playBB();
  231. void playPattern( const Pattern * patternToPlay, bool loop = true );
  232. void togglePause();
  233. void stop();
  234. void importProject();
  235. void exportProject( bool multiExport = false );
  236. void exportProjectTracks();
  237. void exportProjectMidi();
  238. void startExport();
  239. void stopExport();
  240. void setModified();
  241. void clearProject();
  242. void addBBTrack();
  243. private slots:
  244. void insertBar();
  245. void removeBar();
  246. void addSampleTrack();
  247. void addAutomationTrack();
  248. void setTempo();
  249. void setTimeSignature();
  250. void masterVolumeChanged();
  251. void savePos();
  252. void updateFramesPerTick();
  253. private:
  254. Song();
  255. Song( const Song & );
  256. virtual ~Song();
  257. inline tact_t currentTact() const
  258. {
  259. return m_playPos[m_playMode].getTact();
  260. }
  261. inline tick_t currentTick() const
  262. {
  263. return m_playPos[m_playMode].getTicks();
  264. }
  265. inline f_cnt_t currentFrame() const
  266. {
  267. return m_playPos[m_playMode].getTicks() * Engine::framesPerTick() +
  268. m_playPos[m_playMode].currentFrame();
  269. }
  270. void setPlayPos( tick_t ticks, PlayModes playMode );
  271. void saveControllerStates( QDomDocument & doc, QDomElement & element );
  272. void restoreControllerStates( const QDomElement & element );
  273. void removeAllControllers();
  274. void processAutomations(const TrackList& tracks, MidiTime timeStart, fpp_t frames);
  275. AutomationTrack * m_globalAutomationTrack;
  276. IntModel m_tempoModel;
  277. MeterModel m_timeSigModel;
  278. int m_oldTicksPerTact;
  279. IntModel m_masterVolumeModel;
  280. IntModel m_masterPitchModel;
  281. ControllerVector m_controllers;
  282. int m_nLoadingTrack;
  283. QString m_fileName;
  284. QString m_oldFileName;
  285. bool m_modified;
  286. bool m_loadOnLaunch;
  287. volatile bool m_recording;
  288. volatile bool m_exporting;
  289. volatile bool m_exportLoop;
  290. volatile bool m_renderBetweenMarkers;
  291. volatile bool m_playing;
  292. volatile bool m_paused;
  293. bool m_loadingProject;
  294. bool m_isCancelled;
  295. QStringList m_errors;
  296. PlayModes m_playMode;
  297. PlayPos m_playPos[Mode_Count];
  298. tact_t m_length;
  299. const Pattern* m_patternToPlay;
  300. bool m_loopPattern;
  301. double m_elapsedMilliSeconds;
  302. tick_t m_elapsedTicks;
  303. tact_t m_elapsedTacts;
  304. VstSyncController m_vstSyncController;
  305. friend class LmmsCore;
  306. friend class SongEditor;
  307. friend class mainWindow;
  308. friend class ControllerRackView;
  309. signals:
  310. void projectLoaded();
  311. void playbackStateChanged();
  312. void playbackPositionChanged();
  313. void lengthChanged( int tacts );
  314. void tempoChanged( bpm_t newBPM );
  315. void timeSignatureChanged( int oldTicksPerTact, int ticksPerTact );
  316. void controllerAdded( Controller * );
  317. void controllerRemoved( Controller * );
  318. void updateSampleTracks();
  319. } ;
  320. #endif