View_smp.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * View_smp.h
  3. * ----------
  4. * Purpose: Sample tab, lower panel.
  5. * Notes : (currently none)
  6. * Authors: Olivier Lapicque
  7. * OpenMPT Devs
  8. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  9. */
  10. #pragma once
  11. #include "openmpt/all/BuildSettings.hpp"
  12. #include "Globals.h"
  13. #include "../soundlib/modsmp_ctrl.h"
  14. OPENMPT_NAMESPACE_BEGIN
  15. #define SMP_LEFTBAR_BUTTONS 8
  16. class OPLInstrDlg;
  17. class CViewSample: public CModScrollView
  18. {
  19. public:
  20. enum Flags
  21. {
  22. SMPSTATUS_MOUSEDRAG = 0x01,
  23. SMPSTATUS_KEYDOWN = 0x02,
  24. SMPSTATUS_NCLBTNDOWN = 0x04,
  25. SMPSTATUS_DRAWING = 0x08,
  26. };
  27. protected:
  28. enum class PasteMode
  29. {
  30. Replace,
  31. MixPaste,
  32. Insert
  33. };
  34. enum class HitTestItem
  35. {
  36. Nothing,
  37. SampleData,
  38. SelectionStart,
  39. SelectionEnd,
  40. LoopStart,
  41. LoopEnd,
  42. SustainStart,
  43. SustainEnd,
  44. CuePointFirst,
  45. CuePointLast = CuePointFirst + mpt::array_size<decltype(ModSample::cues)>::size - 1,
  46. };
  47. std::unique_ptr<OPLInstrDlg> m_oplEditor;
  48. CImageList m_bmpEnvBar;
  49. CRect m_rcClient;
  50. CDC m_offScreenDC, m_waveformDC;
  51. CBitmap m_offScreenBitmap, m_waveformBitmap;
  52. CFont m_timelineFont;
  53. SIZE m_sizeTotal;
  54. UINT m_nBtnMouseOver = 0xFFFF;
  55. int m_nZoom = 0; // < 0: Zoom into sample (2^x:1 ratio), 0: Auto zoom, > 0: Zoom out (1:2^x ratio)
  56. int m_timelineHeight = 0;
  57. int m_timelineUnit = 0;
  58. int m_timelineInterval = 0;
  59. decltype(ModSample::nC5Speed) m_cachedSampleRate = 8363;
  60. FlagSet<Flags> m_dwStatus;
  61. SmpLength m_dwBeginSel, m_dwEndSel, m_dwBeginDrag, m_dwEndDrag;
  62. SmpLength m_dwMenuParam;
  63. SmpLength m_nGridSegments = 0;
  64. SAMPLEINDEX m_nSample = 1;
  65. HitTestItem m_dragItem = HitTestItem::Nothing;
  66. CPoint m_startDragPoint;
  67. SmpLength m_startDragValue = MAX_SAMPLE_LENGTH;
  68. bool m_dragPreparedUndo = false, m_fineDrag = false, m_forceRedrawWaveform = true;
  69. // Sample drawing
  70. CPoint m_lastDrawPoint; // For drawing horizontal lines
  71. int m_drawChannel; // Which sample channel are we drawing on?
  72. // Note-off event buffer for MIDI sustain pedal
  73. std::array<std::vector<uint32>, 16> m_midiSustainBuffer;
  74. std::bitset<16> m_midiSustainActive;
  75. DWORD m_NcButtonState[SMP_LEFTBAR_BUTTONS];
  76. std::array<SmpLength, MAX_CHANNELS> m_dwNotifyPos;
  77. CModDoc::NoteToChannelMap m_noteChannel; // Note -> Preview channel assignment
  78. public:
  79. CViewSample();
  80. DECLARE_SERIAL(CViewSample)
  81. static std::pair<int, int> FindMinMax(const int8 *p, SmpLength numSamples, int numChannels);
  82. static std::pair<int, int> FindMinMax(const int16 *p, SmpLength numSamples, int numChannels);
  83. protected:
  84. MPT_NOINLINE void SetModified(SampleHint hint, bool updateAll, bool waveformModified);
  85. void UpdateScrollSize() { UpdateScrollSize(m_nZoom, true); }
  86. void UpdateScrollSize(int newZoom, bool forceRefresh, SmpLength centeredSample = SmpLength(-1));
  87. void UpdateOPLEditor();
  88. void SetCurrentSample(SAMPLEINDEX nSmp);
  89. bool IsOPLInstrument() const;
  90. void SetZoom(int nZoom, SmpLength centeredSample = SmpLength(-1));
  91. int32 SampleToScreen(SmpLength pos, bool ignoreScrollPos = false) const;
  92. SmpLength ScreenToSample(int32 x, bool ignoreSampleLength = false) const;
  93. int32 SecondsToScreen(double x) const;
  94. double ScreenToSeconds(int32 x, bool ignoreSampleLength = false) const;
  95. std::pair<HitTestItem, SmpLength> PointToItem(CPoint point, CRect *rect = nullptr) const;
  96. void PlayNote(ModCommand::NOTE note, const SmpLength nStartPos = 0, int volume = -1);
  97. void NoteOff(ModCommand::NOTE note);
  98. void InvalidateSample(bool invalidateWaveform = true);
  99. void InvalidateTimeline();
  100. void SetCurSel(SmpLength nBegin, SmpLength nEnd);
  101. void ScrollToPosition(int x);
  102. void DrawPositionMarks();
  103. void DrawSampleData1(HDC hdc, int ymed, int cx, int cy, SmpLength len, SampleFlags uFlags, const void *pSampleData);
  104. void DrawSampleData2(HDC hdc, int ymed, int cx, int cy, SmpLength len, SampleFlags uFlags, const void *pSampleData);
  105. void DrawNcButton(CDC *pDC, UINT nBtn);
  106. bool GetNcButtonRect(UINT button, CRect &rect) const;
  107. UINT GetNcButtonAtPoint(CPoint point, CRect *outRect = nullptr) const;
  108. void UpdateNcButtonState();
  109. void DoPaste(PasteMode pasteMode);
  110. // Sets sample data on sample draw.
  111. template<class T, class uT>
  112. void SetSampleData(ModSample &smp, const CPoint &point, const SmpLength old);
  113. // Sets initial draw point on sample draw.
  114. template<class T, class uT>
  115. void SetInitialDrawPoint(ModSample &smp, const CPoint &point);
  116. // Returns sample value corresponding given point in the sample view.
  117. template<class T, class uT>
  118. T GetSampleValueFromPoint(const ModSample &smp, const CPoint &point) const;
  119. int GetZoomLevel(SmpLength length) const;
  120. void DoZoom(int direction, const CPoint &zoomPoint = CPoint(-1, -1));
  121. bool CanZoomSelection() const;
  122. void ScrollToSample(SmpLength centeredSample, bool refresh = true);
  123. SmpLength ScrollPosToSamplePos() const {return ScrollPosToSamplePos(m_nZoom);}
  124. SmpLength ScrollPosToSamplePos(int nZoom) const;
  125. void OnMonoConvert(ctrlSmp::StereoToMonoMode convert);
  126. void TrimSample(bool trimToLoopEnd);
  127. int CalcScroll(int &currentPos, int amount, int style, int bar);
  128. SmpLength SnapToGrid(const SmpLength pos) const;
  129. public:
  130. //{{AFX_VIRTUAL(CViewSample)
  131. void OnDraw(CDC *) override;
  132. void OnInitialUpdate() override;
  133. void UpdateView(UpdateHint hint, CObject *pObj = nullptr) override;
  134. LRESULT OnModViewMsg(WPARAM, LPARAM) override;
  135. BOOL OnDragonDrop(BOOL, const DRAGONDROP *) override;
  136. LRESULT OnPlayerNotify(Notification *) override;
  137. BOOL PreTranslateMessage(MSG *pMsg) override;
  138. BOOL OnScrollBy(CSize sizeScroll, BOOL bDoScroll = TRUE) override;
  139. INT_PTR OnToolHitTest(CPoint point, TOOLINFO *pTI) const override;
  140. //}}AFX_VIRTUAL
  141. protected:
  142. //{{AFX_MSG(CViewSample)
  143. afx_msg BOOL OnEraseBkgnd(CDC *) { return TRUE; }
  144. afx_msg void OnSetFocus(CWnd *pOldWnd);
  145. afx_msg void OnSize(UINT nType, int cx, int cy);
  146. afx_msg void OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS* lpncsp);
  147. afx_msg LRESULT OnNcHitTest(CPoint point);
  148. afx_msg void OnNcPaint();
  149. afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
  150. afx_msg void OnNcMouseMove(UINT nHitTest, CPoint point);
  151. afx_msg void OnNcLButtonDown(UINT, CPoint);
  152. afx_msg void OnNcLButtonUp(UINT, CPoint);
  153. afx_msg void OnNcLButtonDblClk(UINT, CPoint);
  154. afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
  155. afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
  156. afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
  157. afx_msg void OnRButtonDown(UINT, CPoint);
  158. afx_msg void OnMouseMove(UINT, CPoint);
  159. afx_msg BOOL OnSetCursor(CWnd *pWnd, UINT nHitTest, UINT message);
  160. afx_msg void OnEditSelectAll();
  161. afx_msg void OnEditDelete();
  162. afx_msg void OnEditCut();
  163. afx_msg void OnEditCopy();
  164. afx_msg void OnEditPaste();
  165. afx_msg void OnEditMixPaste();
  166. afx_msg void OnEditInsertPaste();
  167. afx_msg void OnEditUndo();
  168. afx_msg void OnEditRedo();
  169. afx_msg void OnSetLoop();
  170. afx_msg void OnSetSustainLoop();
  171. afx_msg void On8BitConvert();
  172. afx_msg void On16BitConvert();
  173. afx_msg void OnMonoConvertMix() { OnMonoConvert(ctrlSmp::mixChannels); }
  174. afx_msg void OnMonoConvertLeft() { OnMonoConvert(ctrlSmp::onlyLeft); }
  175. afx_msg void OnMonoConvertRight() { OnMonoConvert(ctrlSmp::onlyRight); }
  176. afx_msg void OnMonoConvertSplit() { OnMonoConvert(ctrlSmp::splitSample); }
  177. afx_msg void OnSampleTrim() { TrimSample(false); }
  178. afx_msg void OnPrevInstrument();
  179. afx_msg void OnNextInstrument();
  180. afx_msg void OnZoomOnSel();
  181. afx_msg void OnDropFiles(HDROP hDropInfo);
  182. afx_msg void OnSetLoopStart();
  183. afx_msg void OnSetLoopEnd();
  184. afx_msg void OnConvertPingPongLoop();
  185. afx_msg void OnSetSustainStart();
  186. afx_msg void OnSetSustainEnd();
  187. afx_msg void OnConvertPingPongSustain();
  188. afx_msg void OnSetCuePoint(UINT nID);
  189. afx_msg void OnZoomUp();
  190. afx_msg void OnZoomDown();
  191. afx_msg void OnDrawingToggle();
  192. afx_msg void OnAddSilence();
  193. afx_msg void OnChangeGridSize();
  194. afx_msg void OnQuickFade();
  195. afx_msg LRESULT OnMidiMsg(WPARAM, LPARAM);
  196. afx_msg LRESULT OnCustomKeyMsg(WPARAM, LPARAM); //rewbs.customKeys
  197. afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);
  198. afx_msg void OnXButtonUp(UINT nFlags, UINT nButton, CPoint point);
  199. afx_msg void OnUpdateUndo(CCmdUI *pCmdUI);
  200. afx_msg void OnUpdateRedo(CCmdUI *pCmdUI);
  201. afx_msg void OnSampleSlice();
  202. afx_msg void OnSampleInsertCuePoint();
  203. afx_msg void OnSampleDeleteCuePoint();
  204. afx_msg void OnTimelineFormatSeconds() { SetTimelineFormat(TimelineFormat::Seconds); }
  205. afx_msg void OnTimelineFormatSamples() { SetTimelineFormat(TimelineFormat::Samples); }
  206. afx_msg void OnTimelineFormatSamplesPow2() { SetTimelineFormat(TimelineFormat::SamplesPow2); }
  207. void SetTimelineFormat(TimelineFormat fmt);
  208. //}}AFX_MSG
  209. DECLARE_MESSAGE_MAP()
  210. BOOL OnGestureZoom(CPoint ptCenter, long lDelta) override
  211. {
  212. DoZoom(lDelta / 10, ptCenter);
  213. return TRUE;
  214. }
  215. static bool IsCuePoint(HitTestItem item) { return item >= HitTestItem::CuePointFirst && item <= HitTestItem::CuePointLast; }
  216. static int CuePointFromItem(HitTestItem item) { return static_cast<int>(item) - static_cast<int>(HitTestItem::CuePointFirst); }
  217. };
  218. DECLARE_FLAGSET(CViewSample::Flags)
  219. OPENMPT_NAMESPACE_END