Song.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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 <memory>
  27. #include <utility>
  28. #include <QtCore/QSharedMemory>
  29. #include <QtCore/QVector>
  30. #include <QHash>
  31. #include <QString>
  32. #include "TrackContainer.h"
  33. #include "AudioEngine.h"
  34. #include "Controller.h"
  35. #include "Keymap.h"
  36. #include "lmms_constants.h"
  37. #include "MeterModel.h"
  38. #include "Scale.h"
  39. #include "VstSyncController.h"
  40. class AutomationTrack;
  41. class MidiClip;
  42. class TimeLineWidget;
  43. const bpm_t MinTempo = 10;
  44. const bpm_t DefaultTempo = 140;
  45. const bpm_t MaxTempo = 999;
  46. const tick_t MaxSongLength = 9999 * DefaultTicksPerBar;
  47. class LMMS_EXPORT Song : public TrackContainer
  48. {
  49. Q_OBJECT
  50. mapPropertyFromModel( int,getTempo,setTempo,m_tempoModel );
  51. mapPropertyFromModel( int,masterPitch,setMasterPitch,m_masterPitchModel );
  52. mapPropertyFromModel( int,masterVolume,setMasterVolume, m_masterVolumeModel );
  53. public:
  54. enum PlayModes
  55. {
  56. Mode_None,
  57. Mode_PlaySong,
  58. Mode_PlayPattern,
  59. Mode_PlayMidiClip,
  60. Mode_PlayAutomationClip,
  61. Mode_Count
  62. } ;
  63. struct SaveOptions {
  64. /**
  65. * Should we discard MIDI ControllerConnections from project files?
  66. */
  67. BoolModel discardMIDIConnections{false};
  68. /**
  69. * Should we save the project as a project bundle? (with resources)
  70. */
  71. BoolModel saveAsProjectBundle{false};
  72. void setDefaultOptions() {
  73. discardMIDIConnections.setValue(false);
  74. saveAsProjectBundle.setValue(false);
  75. }
  76. };
  77. void clearErrors();
  78. void collectError( const QString error );
  79. bool hasErrors();
  80. QString errorSummary();
  81. class PlayPos : public TimePos
  82. {
  83. public:
  84. PlayPos( const int abs = 0 ) :
  85. TimePos( abs ),
  86. m_timeLine( nullptr ),
  87. m_currentFrame( 0.0f )
  88. {
  89. }
  90. inline void setCurrentFrame( const float f )
  91. {
  92. m_currentFrame = f;
  93. }
  94. inline float currentFrame() const
  95. {
  96. return m_currentFrame;
  97. }
  98. inline void setJumped( const bool jumped )
  99. {
  100. m_jumped = jumped;
  101. }
  102. inline bool jumped() const
  103. {
  104. return m_jumped;
  105. }
  106. TimeLineWidget * m_timeLine;
  107. private:
  108. float m_currentFrame;
  109. bool m_jumped;
  110. } ;
  111. void processNextBuffer();
  112. inline int getLoadingTrackCount() const
  113. {
  114. return m_nLoadingTrack;
  115. }
  116. inline int getMilliseconds() const
  117. {
  118. return m_elapsedMilliSeconds[m_playMode];
  119. }
  120. inline int getMilliseconds(PlayModes playMode) const
  121. {
  122. return m_elapsedMilliSeconds[playMode];
  123. }
  124. inline void setToTime(TimePos const & pos)
  125. {
  126. m_elapsedMilliSeconds[m_playMode] = pos.getTimeInMilliseconds(getTempo());
  127. m_playPos[m_playMode].setTicks(pos.getTicks());
  128. }
  129. inline void setToTime(TimePos const & pos, PlayModes playMode)
  130. {
  131. m_elapsedMilliSeconds[playMode] = pos.getTimeInMilliseconds(getTempo());
  132. m_playPos[playMode].setTicks(pos.getTicks());
  133. }
  134. inline void setToTimeByTicks(tick_t ticks)
  135. {
  136. m_elapsedMilliSeconds[m_playMode] = TimePos::ticksToMilliseconds(ticks, getTempo());
  137. m_playPos[m_playMode].setTicks(ticks);
  138. }
  139. inline void setToTimeByTicks(tick_t ticks, PlayModes playMode)
  140. {
  141. m_elapsedMilliSeconds[playMode] = TimePos::ticksToMilliseconds(ticks, getTempo());
  142. m_playPos[playMode].setTicks(ticks);
  143. }
  144. inline int getBars() const
  145. {
  146. return currentBar();
  147. }
  148. inline int ticksPerBar() const
  149. {
  150. return TimePos::ticksPerBar(m_timeSigModel);
  151. }
  152. // Returns the beat position inside the bar, 0-based
  153. inline int getBeat() const
  154. {
  155. return getPlayPos().getBeatWithinBar(m_timeSigModel);
  156. }
  157. // the remainder after bar and beat are removed
  158. inline int getBeatTicks() const
  159. {
  160. return getPlayPos().getTickWithinBeat(m_timeSigModel);
  161. }
  162. inline int getTicks() const
  163. {
  164. return currentTick();
  165. }
  166. inline f_cnt_t getFrames() const
  167. {
  168. return currentFrame();
  169. }
  170. inline bool isPaused() const
  171. {
  172. return m_paused;
  173. }
  174. inline bool isPlaying() const
  175. {
  176. return m_playing == true && m_exporting == false;
  177. }
  178. inline bool isStopped() const
  179. {
  180. return m_playing == false && m_paused == false;
  181. }
  182. inline bool isExporting() const
  183. {
  184. return m_exporting;
  185. }
  186. inline void setExportLoop( bool exportLoop )
  187. {
  188. m_exportLoop = exportLoop;
  189. }
  190. inline bool isRecording() const
  191. {
  192. return m_recording;
  193. }
  194. inline void setLoopRenderCount(int count)
  195. {
  196. if (count < 1)
  197. m_loopRenderCount = 1;
  198. else
  199. m_loopRenderCount = count;
  200. m_loopRenderRemaining = m_loopRenderCount;
  201. }
  202. inline int getLoopRenderCount() const
  203. {
  204. return m_loopRenderCount;
  205. }
  206. bool isExportDone() const;
  207. int getExportProgress() const;
  208. inline void setRenderBetweenMarkers( bool renderBetweenMarkers )
  209. {
  210. m_renderBetweenMarkers = renderBetweenMarkers;
  211. }
  212. inline PlayModes playMode() const
  213. {
  214. return m_playMode;
  215. }
  216. inline PlayPos & getPlayPos( PlayModes pm )
  217. {
  218. return m_playPos[pm];
  219. }
  220. inline const PlayPos & getPlayPos( PlayModes pm ) const
  221. {
  222. return m_playPos[pm];
  223. }
  224. inline PlayPos & getPlayPos()
  225. {
  226. return getPlayPos(m_playMode);
  227. }
  228. inline const PlayPos & getPlayPos() const
  229. {
  230. return getPlayPos(m_playMode);
  231. }
  232. void updateLength();
  233. bar_t length() const
  234. {
  235. return m_length;
  236. }
  237. bpm_t getTempo();
  238. AutomationClip * tempoAutomationClip() override;
  239. AutomationTrack * globalAutomationTrack()
  240. {
  241. return m_globalAutomationTrack;
  242. }
  243. //TODO: Add Q_DECL_OVERRIDE when Qt4 is dropped
  244. AutomatedValueMap automatedValuesAt(TimePos time, int clipNum = -1) const override;
  245. // file management
  246. void createNewProject();
  247. void createNewProjectFromTemplate( const QString & templ );
  248. void loadProject( const QString & filename );
  249. bool guiSaveProject();
  250. bool guiSaveProjectAs(const QString & filename);
  251. bool saveProjectFile(const QString & filename, bool withResources = false);
  252. const QString & projectFileName() const
  253. {
  254. return m_fileName;
  255. }
  256. bool isLoadingProject() const
  257. {
  258. return m_loadingProject;
  259. }
  260. void loadingCancelled()
  261. {
  262. m_isCancelled = true;
  263. Engine::audioEngine()->clearNewPlayHandles();
  264. }
  265. bool isCancelled()
  266. {
  267. return m_isCancelled;
  268. }
  269. bool isModified() const
  270. {
  271. return m_modified;
  272. }
  273. QString nodeName() const override
  274. {
  275. return "song";
  276. }
  277. virtual bool fixedClips() const
  278. {
  279. return false;
  280. }
  281. void addController( Controller * c );
  282. void removeController( Controller * c );
  283. const ControllerVector & controllers() const
  284. {
  285. return m_controllers;
  286. }
  287. MeterModel & getTimeSigModel()
  288. {
  289. return m_timeSigModel;
  290. }
  291. void exportProjectMidi(QString const & exportFileName) const;
  292. inline void setLoadOnLaunch(bool value) { m_loadOnLaunch = value; }
  293. SaveOptions &getSaveOptions() {
  294. return m_saveOptions;
  295. }
  296. bool isSavingProject() const;
  297. std::shared_ptr<const Scale> getScale(unsigned int index) const;
  298. std::shared_ptr<const Keymap> getKeymap(unsigned int index) const;
  299. void setScale(unsigned int index, std::shared_ptr<Scale> newScale);
  300. void setKeymap(unsigned int index, std::shared_ptr<Keymap> newMap);
  301. public slots:
  302. void playSong();
  303. void record();
  304. void playAndRecord();
  305. void playPattern();
  306. void playMidiClip( const MidiClip * midiClipToPlay, bool loop = true );
  307. void togglePause();
  308. void stop();
  309. void startExport();
  310. void stopExport();
  311. void setModified();
  312. void clearProject();
  313. void addPatternTrack();
  314. private slots:
  315. void insertBar();
  316. void removeBar();
  317. void addSampleTrack();
  318. void addAutomationTrack();
  319. void setTempo();
  320. void setTimeSignature();
  321. void masterVolumeChanged();
  322. void savePos();
  323. void updateFramesPerTick();
  324. private:
  325. Song();
  326. Song( const Song & );
  327. virtual ~Song();
  328. inline bar_t currentBar() const
  329. {
  330. return m_playPos[m_playMode].getBar();
  331. }
  332. inline tick_t currentTick() const
  333. {
  334. return m_playPos[m_playMode].getTicks();
  335. }
  336. inline f_cnt_t currentFrame() const
  337. {
  338. return m_playPos[m_playMode].getTicks() * Engine::framesPerTick() +
  339. m_playPos[m_playMode].currentFrame();
  340. }
  341. void setPlayPos( tick_t ticks, PlayModes playMode );
  342. void saveControllerStates( QDomDocument & doc, QDomElement & element );
  343. void restoreControllerStates( const QDomElement & element );
  344. void removeAllControllers();
  345. void saveScaleStates(QDomDocument &doc, QDomElement &element);
  346. void restoreScaleStates(const QDomElement &element);
  347. void saveKeymapStates(QDomDocument &doc, QDomElement &element);
  348. void restoreKeymapStates(const QDomElement &element);
  349. void processAutomations(const TrackList& tracks, TimePos timeStart, fpp_t frames);
  350. void setModified(bool value);
  351. void setProjectFileName(QString const & projectFileName);
  352. AutomationTrack * m_globalAutomationTrack;
  353. IntModel m_tempoModel;
  354. MeterModel m_timeSigModel;
  355. int m_oldTicksPerBar;
  356. IntModel m_masterVolumeModel;
  357. IntModel m_masterPitchModel;
  358. ControllerVector m_controllers;
  359. int m_nLoadingTrack;
  360. QString m_fileName;
  361. QString m_oldFileName;
  362. bool m_modified;
  363. bool m_loadOnLaunch;
  364. volatile bool m_recording;
  365. volatile bool m_exporting;
  366. volatile bool m_exportLoop;
  367. volatile bool m_renderBetweenMarkers;
  368. volatile bool m_playing;
  369. volatile bool m_paused;
  370. bool m_savingProject;
  371. bool m_loadingProject;
  372. bool m_isCancelled;
  373. SaveOptions m_saveOptions;
  374. QHash<QString, int> m_errors;
  375. PlayModes m_playMode;
  376. PlayPos m_playPos[Mode_Count];
  377. bar_t m_length;
  378. const MidiClip* m_midiClipToPlay;
  379. bool m_loopMidiClip;
  380. double m_elapsedMilliSeconds[Mode_Count];
  381. tick_t m_elapsedTicks;
  382. bar_t m_elapsedBars;
  383. VstSyncController m_vstSyncController;
  384. int m_loopRenderCount;
  385. int m_loopRenderRemaining;
  386. TimePos m_exportSongBegin;
  387. TimePos m_exportLoopBegin;
  388. TimePos m_exportLoopEnd;
  389. TimePos m_exportSongEnd;
  390. TimePos m_exportEffectiveLength;
  391. std::shared_ptr<Scale> m_scales[MaxScaleCount];
  392. std::shared_ptr<Keymap> m_keymaps[MaxKeymapCount];
  393. AutomatedValueMap m_oldAutomatedValues;
  394. friend class LmmsCore;
  395. friend class SongEditor;
  396. friend class mainWindow;
  397. friend class ControllerRackView;
  398. signals:
  399. void projectLoaded();
  400. void playbackStateChanged();
  401. void playbackPositionChanged();
  402. void lengthChanged( int bars );
  403. void tempoChanged( bpm_t newBPM );
  404. void timeSignatureChanged( int oldTicksPerBar, int ticksPerBar );
  405. void controllerAdded( Controller * );
  406. void controllerRemoved( Controller * );
  407. void updateSampleTracks();
  408. void stopped();
  409. void modified();
  410. void projectFileNameChanged();
  411. void scaleListChanged(int index);
  412. void keymapListChanged(int index);
  413. } ;
  414. #endif