SpectMorph
smsampleview.hh
1 // Licensed GNU LGPL v3 or later: http://www.gnu.org/licenses/lgpl.html
2 
3 #ifndef SPECTMORPH_SAMPLE_VIEW_HH
4 #define SPECTMORPH_SAMPLE_VIEW_HH
5 
6 #include "smaudio.hh"
7 #include "smwavdata.hh"
8 #include "smblockutils.hh"
9 
10 #include <QWidget>
11 
12 namespace SpectMorph {
13 
14 class SampleView : public QWidget
15 {
16  Q_OBJECT
17 
18 public:
19  enum EditMarkerType {
20  MARKER_NONE,
21  MARKER_LOOP_START,
22  MARKER_LOOP_END,
23  MARKER_CLIP_START,
24  MARKER_CLIP_END
25  };
26  class Markers {
27  public:
28  virtual size_t count() = 0;
29  virtual EditMarkerType type (size_t marker) = 0;
30  virtual float position (size_t marker) = 0;
31  virtual bool valid (size_t marker) = 0;
32  virtual void set_position (size_t marker, float new_position) = 0;
33  virtual void clear (size_t marker) = 0;
34  };
35 
36 private:
37  std::vector<float> signal;
38  Audio *audio;
39  Markers *markers;
40  double attack_start;
41  double attack_end;
42  double hzoom;
43  double vzoom;
44  EditMarkerType m_edit_marker_type;
45  bool button_1_pressed;
46 
47  void update_size();
48  void mousePressEvent (QMouseEvent *event);
49  void move_marker (int x);
50  void mouseMoveEvent (QMouseEvent *event);
51  void mouseReleaseEvent (QMouseEvent *event);
52 
53 public:
54  SampleView();
55  void load (const WavData *wav_data, SpectMorph::Audio *audio, Markers *markers = 0);
56  void set_zoom (double hzoom, double vzoom);
57  void paintEvent (QPaintEvent *event);
58 
59  void set_edit_marker_type (EditMarkerType marker_type);
60  EditMarkerType edit_marker_type();
61 
62  template<class Painter> static void
63  draw_signal (std::vector<float>& signal, Painter& painter, const QRect& rect, int height, double vz, double hz)
64  {
65  int last_i0 = -1;
66  int last_x = 0;
67  double last_value = 0;
68 
69  for (int x = rect.x(); x < rect.x() + rect.width(); x++)
70  {
71  int i0 = x / hz;
72  int i1 = (x + 1) / hz + 1;
73 
74  if (last_i0 != i0)
75  {
76  if (i0 < int (signal.size()) && i0 >= 0 && i1 < int (signal.size() + 1) && i1 > 0)
77  {
78  painter.drawLine (last_x, (height / 2) + last_value * vz, x, (height / 2) + signal[i0] * vz);
79 
80  float min_value, max_value;
81  Block::range (i1 - i0, &signal[i0], min_value, max_value);
82 
83  painter.drawLine (x, (height / 2) + min_value * vz, x, (height / 2) + max_value * vz);
84 
85  last_x = x;
86  last_value = signal[i1 - 1];
87  }
88  last_i0 = i0;
89  }
90  }
91  }
92 signals:
93  void audio_edit();
94  void mouse_time_changed (int pos);
95 };
96 
97 }
98 
99 #endif
Audio sample containing many blocks.
Definition: smaudio.hh:81
Definition: smsampleview.hh:26
Definition: smalignedarray.cc:7
Definition: smwavdata.hh:13
Definition: smsampleview.hh:14