Song.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 * DefaultTicksPerBar;
  41. class LMMS_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. struct SaveOptions {
  58. /**
  59. * Should we discard MIDI ControllerConnections from project files?
  60. */
  61. BoolModel discardMIDIConnections{false};
  62. void setDefaultOptions() {
  63. discardMIDIConnections.setValue(false);
  64. }
  65. };
  66. void clearErrors();
  67. void collectError( const QString error );
  68. bool hasErrors();
  69. QString errorSummary();
  70. class PlayPos : public MidiTime
  71. {
  72. public:
  73. PlayPos( const int abs = 0 ) :
  74. MidiTime( abs ),
  75. m_timeLine( NULL ),
  76. m_currentFrame( 0.0f )
  77. {
  78. }
  79. inline void setCurrentFrame( const float f )
  80. {
  81. m_currentFrame = f;
  82. }
  83. inline float currentFrame() const
  84. {
  85. return m_currentFrame;
  86. }
  87. inline void setJumped( const bool jumped )
  88. {
  89. m_jumped = jumped;
  90. }
  91. inline bool jumped() const
  92. {
  93. return m_jumped;
  94. }
  95. TimeLineWidget * m_timeLine;
  96. private:
  97. float m_currentFrame;
  98. bool m_jumped;
  99. } ;
  100. void processNextBuffer();
  101. inline int getLoadingTrackCount() const
  102. {
  103. return m_nLoadingTrack;
  104. }
  105. inline int getMilliseconds() const
  106. {
  107. return m_elapsedMilliSeconds[m_playMode];
  108. }
  109. inline int getMilliseconds(PlayModes playMode) const
  110. {
  111. return m_elapsedMilliSeconds[playMode];
  112. }
  113. inline void setToTime(MidiTime const & midiTime)
  114. {
  115. m_elapsedMilliSeconds[m_playMode] = midiTime.getTimeInMilliseconds(getTempo());
  116. m_playPos[m_playMode].setTicks(midiTime.getTicks());
  117. }
  118. inline void setToTime(MidiTime const & midiTime, PlayModes playMode)
  119. {
  120. m_elapsedMilliSeconds[playMode] = midiTime.getTimeInMilliseconds(getTempo());
  121. m_playPos[playMode].setTicks(midiTime.getTicks());
  122. }
  123. inline void setToTimeByTicks(tick_t ticks)
  124. {
  125. m_elapsedMilliSeconds[m_playMode] = MidiTime::ticksToMilliseconds(ticks, getTempo());
  126. m_playPos[m_playMode].setTicks(ticks);
  127. }
  128. inline void setToTimeByTicks(tick_t ticks, PlayModes playMode)
  129. {
  130. m_elapsedMilliSeconds[playMode] = MidiTime::ticksToMilliseconds(ticks, getTempo());
  131. m_playPos[playMode].setTicks(ticks);
  132. }
  133. inline int getBars() const
  134. {
  135. return currentBar();
  136. }
  137. inline int ticksPerBar() const
  138. {
  139. return MidiTime::ticksPerBar(m_timeSigModel);
  140. }
  141. // Returns the beat position inside the bar, 0-based
  142. inline int getBeat() const
  143. {
  144. return getPlayPos().getBeatWithinBar(m_timeSigModel);
  145. }
  146. // the remainder after bar and beat are removed
  147. inline int getBeatTicks() const
  148. {
  149. return getPlayPos().getTickWithinBeat(m_timeSigModel);
  150. }
  151. inline int getTicks() const
  152. {
  153. return currentTick();
  154. }
  155. inline f_cnt_t getFrames() const
  156. {
  157. return currentFrame();
  158. }
  159. inline bool isPaused() const
  160. {
  161. return m_paused;
  162. }
  163. inline bool isPlaying() const
  164. {
  165. return m_playing == true && m_exporting == false;
  166. }
  167. inline bool isStopped() const
  168. {
  169. return m_playing == false && m_paused == false;
  170. }
  171. inline bool isExporting() const
  172. {
  173. return m_exporting;
  174. }
  175. inline void setExportLoop( bool exportLoop )
  176. {
  177. m_exportLoop = exportLoop;
  178. }
  179. inline bool isRecording() const
  180. {
  181. return m_recording;
  182. }
  183. inline void setLoopRenderCount(int count)
  184. {
  185. if (count < 1)
  186. m_loopRenderCount = 1;
  187. else
  188. m_loopRenderCount = count;
  189. m_loopRenderRemaining = m_loopRenderCount;
  190. }
  191. inline int getLoopRenderCount() const
  192. {
  193. return m_loopRenderCount;
  194. }
  195. bool isExportDone() const;
  196. int getExportProgress() const;
  197. inline void setRenderBetweenMarkers( bool renderBetweenMarkers )
  198. {
  199. m_renderBetweenMarkers = renderBetweenMarkers;
  200. }
  201. inline PlayModes playMode() const
  202. {
  203. return m_playMode;
  204. }
  205. inline PlayPos & getPlayPos( PlayModes pm )
  206. {
  207. return m_playPos[pm];
  208. }
  209. inline const PlayPos & getPlayPos( PlayModes pm ) const
  210. {
  211. return m_playPos[pm];
  212. }
  213. inline const PlayPos & getPlayPos() const
  214. {
  215. return getPlayPos(m_playMode);
  216. }
  217. void updateLength();
  218. bar_t length() const
  219. {
  220. return m_length;
  221. }
  222. bpm_t getTempo();
  223. AutomationPattern * tempoAutomationPattern() override;
  224. AutomationTrack * globalAutomationTrack()
  225. {
  226. return m_globalAutomationTrack;
  227. }
  228. //TODO: Add Q_DECL_OVERRIDE when Qt4 is dropped
  229. AutomatedValueMap automatedValuesAt(MidiTime time, int tcoNum = -1) const override;
  230. // file management
  231. void createNewProject();
  232. void createNewProjectFromTemplate( const QString & templ );
  233. void loadProject( const QString & filename );
  234. bool guiSaveProject();
  235. bool guiSaveProjectAs( const QString & filename );
  236. bool saveProjectFile( const QString & filename );
  237. const QString & projectFileName() const
  238. {
  239. return m_fileName;
  240. }
  241. bool isLoadingProject() const
  242. {
  243. return m_loadingProject;
  244. }
  245. void loadingCancelled()
  246. {
  247. m_isCancelled = true;
  248. Engine::mixer()->clearNewPlayHandles();
  249. }
  250. bool isCancelled()
  251. {
  252. return m_isCancelled;
  253. }
  254. bool isModified() const
  255. {
  256. return m_modified;
  257. }
  258. QString nodeName() const override
  259. {
  260. return "song";
  261. }
  262. virtual bool fixedTCOs() const
  263. {
  264. return false;
  265. }
  266. void addController( Controller * c );
  267. void removeController( Controller * c );
  268. const ControllerVector & controllers() const
  269. {
  270. return m_controllers;
  271. }
  272. MeterModel & getTimeSigModel()
  273. {
  274. return m_timeSigModel;
  275. }
  276. void exportProjectMidi(QString const & exportFileName) const;
  277. inline void setLoadOnLauch(bool value) { m_loadOnLaunch = value; }
  278. SaveOptions &getSaveOptions() {
  279. return m_saveOptions;
  280. }
  281. bool isSavingProject() const;
  282. public slots:
  283. void playSong();
  284. void record();
  285. void playAndRecord();
  286. void playBB();
  287. void playPattern( const Pattern * patternToPlay, bool loop = true );
  288. void togglePause();
  289. void stop();
  290. void startExport();
  291. void stopExport();
  292. void setModified();
  293. void clearProject();
  294. void addBBTrack();
  295. private slots:
  296. void insertBar();
  297. void removeBar();
  298. void addSampleTrack();
  299. void addAutomationTrack();
  300. void setTempo();
  301. void setTimeSignature();
  302. void masterVolumeChanged();
  303. void savePos();
  304. void updateFramesPerTick();
  305. private:
  306. Song();
  307. Song( const Song & );
  308. virtual ~Song();
  309. inline bar_t currentBar() const
  310. {
  311. return m_playPos[m_playMode].getBar();
  312. }
  313. inline tick_t currentTick() const
  314. {
  315. return m_playPos[m_playMode].getTicks();
  316. }
  317. inline f_cnt_t currentFrame() const
  318. {
  319. return m_playPos[m_playMode].getTicks() * Engine::framesPerTick() +
  320. m_playPos[m_playMode].currentFrame();
  321. }
  322. void setPlayPos( tick_t ticks, PlayModes playMode );
  323. void saveControllerStates( QDomDocument & doc, QDomElement & element );
  324. void restoreControllerStates( const QDomElement & element );
  325. void removeAllControllers();
  326. void processAutomations(const TrackList& tracks, MidiTime timeStart, fpp_t frames);
  327. void setModified(bool value);
  328. void setProjectFileName(QString const & projectFileName);
  329. AutomationTrack * m_globalAutomationTrack;
  330. IntModel m_tempoModel;
  331. MeterModel m_timeSigModel;
  332. int m_oldTicksPerBar;
  333. IntModel m_masterVolumeModel;
  334. IntModel m_masterPitchModel;
  335. ControllerVector m_controllers;
  336. int m_nLoadingTrack;
  337. QString m_fileName;
  338. QString m_oldFileName;
  339. bool m_modified;
  340. bool m_loadOnLaunch;
  341. volatile bool m_recording;
  342. volatile bool m_exporting;
  343. volatile bool m_exportLoop;
  344. volatile bool m_renderBetweenMarkers;
  345. volatile bool m_playing;
  346. volatile bool m_paused;
  347. bool m_savingProject;
  348. bool m_loadingProject;
  349. bool m_isCancelled;
  350. SaveOptions m_saveOptions;
  351. QStringList m_errors;
  352. PlayModes m_playMode;
  353. PlayPos m_playPos[Mode_Count];
  354. bar_t m_length;
  355. const Pattern* m_patternToPlay;
  356. bool m_loopPattern;
  357. double m_elapsedMilliSeconds[Mode_Count];
  358. tick_t m_elapsedTicks;
  359. bar_t m_elapsedBars;
  360. VstSyncController m_vstSyncController;
  361. int m_loopRenderCount;
  362. int m_loopRenderRemaining;
  363. MidiTime m_exportSongBegin;
  364. MidiTime m_exportLoopBegin;
  365. MidiTime m_exportLoopEnd;
  366. MidiTime m_exportSongEnd;
  367. MidiTime m_exportEffectiveLength;
  368. friend class LmmsCore;
  369. friend class SongEditor;
  370. friend class mainWindow;
  371. friend class ControllerRackView;
  372. signals:
  373. void projectLoaded();
  374. void playbackStateChanged();
  375. void playbackPositionChanged();
  376. void lengthChanged( int bars );
  377. void tempoChanged( bpm_t newBPM );
  378. void timeSignatureChanged( int oldTicksPerBar, int ticksPerBar );
  379. void controllerAdded( Controller * );
  380. void controllerRemoved( Controller * );
  381. void updateSampleTracks();
  382. void stopped();
  383. void modified();
  384. void projectFileNameChanged();
  385. } ;
  386. #endif