svcore  1.9
NoteModel.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006 Chris Cannam.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #ifndef _NOTE_MODEL_H_
17 #define _NOTE_MODEL_H_
18 
19 #include "IntervalModel.h"
20 #include "NoteData.h"
21 #include "base/RealTime.h"
23 #include "base/Pitch.h"
24 
38 struct Note
39 {
40 public:
41  Note(long _frame) : frame(_frame), value(0.0f), duration(0), level(1.f) { }
42  Note(long _frame, float _value, int _duration, float _level, QString _label) :
43  frame(_frame), value(_value), duration(_duration), level(_level), label(_label) { }
44 
45  int getDimensions() const { return 3; }
46 
47  long frame;
48  float value;
49  int duration;
50  float level;
51  QString label;
52 
53  QString getLabel() const { return label; }
54 
55  void toXml(QTextStream &stream,
56  QString indent = "",
57  QString extraAttributes = "") const
58  {
59  stream <<
60  QString("%1<point frame=\"%2\" value=\"%3\" duration=\"%4\" level=\"%5\" label=\"%6\" %7/>\n")
61  .arg(indent).arg(frame).arg(value).arg(duration).arg(level)
62  .arg(XmlExportable::encodeEntities(label)).arg(extraAttributes);
63  }
64 
65  QString toDelimitedDataString(QString delimiter, int sampleRate) const
66  {
67  QStringList list;
68  list << RealTime::frame2RealTime(frame, sampleRate).toString().c_str();
69  list << QString("%1").arg(value);
70  list << RealTime::frame2RealTime(duration, sampleRate).toString().c_str();
71  list << QString("%1").arg(level);
72  if (label != "") list << label;
73  return list.join(delimiter);
74  }
75 
76  struct Comparator {
77  bool operator()(const Note &p1,
78  const Note &p2) const {
79  if (p1.frame != p2.frame) return p1.frame < p2.frame;
80  if (p1.value != p2.value) return p1.value < p2.value;
81  if (p1.duration != p2.duration) return p1.duration < p2.duration;
82  if (p1.level != p2.level) return p1.level < p2.level;
83  return p1.label < p2.label;
84  }
85  };
86 
87  struct OrderComparator {
88  bool operator()(const Note &p1,
89  const Note &p2) const {
90  return p1.frame < p2.frame;
91  }
92  };
93 };
94 
95 
96 class NoteModel : public IntervalModel<Note>, public NoteExportable
97 {
98  Q_OBJECT
99 
100 public:
101  NoteModel(int sampleRate, int resolution,
102  bool notifyOnAdd = true) :
103  IntervalModel<Note>(sampleRate, resolution, notifyOnAdd),
105  {
107  }
108 
109  NoteModel(int sampleRate, int resolution,
110  float valueMinimum, float valueMaximum,
111  bool notifyOnAdd = true) :
112  IntervalModel<Note>(sampleRate, resolution,
113  valueMinimum, valueMaximum,
114  notifyOnAdd),
116  {
118  }
119 
120  virtual ~NoteModel()
121  {
123  }
124 
125  float getValueQuantization() const { return m_valueQuantization; }
127 
128  QString getTypeName() const { return tr("Note"); }
129 
130  virtual bool canPlay() const { return true; }
131 
132  virtual QString getDefaultPlayClipId() const
133  {
134  return "elecpiano";
135  }
136 
137  virtual void toXml(QTextStream &out,
138  QString indent = "",
139  QString extraAttributes = "") const
140  {
141  std::cerr << "NoteModel::toXml: extraAttributes = \""
142  << extraAttributes.toStdString() << std::endl;
143 
145  (out,
146  indent,
147  QString("%1 subtype=\"note\" valueQuantization=\"%2\"")
148  .arg(extraAttributes).arg(m_valueQuantization));
149  }
150 
155  virtual int getColumnCount() const
156  {
157  return 6;
158  }
159 
160  virtual QString getHeading(int column) const
161  {
162  switch (column) {
163  case 0: return tr("Time");
164  case 1: return tr("Frame");
165  case 2: return tr("Pitch");
166  case 3: return tr("Duration");
167  case 4: return tr("Level");
168  case 5: return tr("Label");
169  default: return tr("Unknown");
170  }
171  }
172 
173  virtual QVariant getData(int row, int column, int role) const
174  {
175  if (column < 4) {
176  return IntervalModel<Note>::getData(row, column, role);
177  }
178 
180  if (i == m_points.end()) return QVariant();
181 
182  switch (column) {
183  case 4: return i->level;
184  case 5: return i->label;
185  default: return QVariant();
186  }
187  }
188 
189  virtual Command *getSetDataCommand(int row, int column, const QVariant &value, int role)
190  {
191  if (column < 4) {
193  (row, column, value, role);
194  }
195 
196  if (role != Qt::EditRole) return 0;
198  if (i == m_points.end()) return 0;
199  EditCommand *command = new EditCommand(this, tr("Edit Data"));
200 
201  Point point(*i);
202  command->deletePoint(point);
203 
204  switch (column) {
205  case 4: point.level = value.toDouble(); break;
206  case 5: point.label = value.toString(); break;
207  }
208 
209  command->addPoint(point);
210  return command->finish();
211  }
212 
213  virtual SortType getSortType(int column) const
214  {
215  if (column == 5) return SortAlphabetical;
216  return SortNumeric;
217  }
218 
223  NoteList getNotes() const {
225  }
226 
227  NoteList getNotesWithin(int startFrame, int endFrame) const {
228 
229  PointList points = getPoints(startFrame, endFrame);
230  NoteList notes;
231 
232  for (PointList::iterator pli =
233  points.begin(); pli != points.end(); ++pli) {
234 
235  int duration = pli->duration;
236  if (duration == 0 || duration == 1) {
237  duration = getSampleRate() / 20;
238  }
239 
240  int pitch = lrintf(pli->value);
241 
242  int velocity = 100;
243  if (pli->level > 0.f && pli->level <= 1.f) {
244  velocity = lrintf(pli->level * 127);
245  }
246 
247  NoteData note(pli->frame, duration, pitch, velocity);
248 
249  if (getScaleUnits() == "Hz") {
250  note.frequency = pli->value;
252  note.isMidiPitchQuantized = false;
253  }
254 
255  notes.push_back(note);
256  }
257 
258  return notes;
259  }
260 
261 protected:
263 };
264 
265 #endif
std::multiset< Note, typename Note ::OrderComparator > PointList
Definition: SparseModel.h:69
static PlayParameterRepository * getInstance()
int duration
Definition: NoteModel.h:49
float frequency
Definition: NoteData.h:31
NoteModel(int sampleRate, int resolution, bool notifyOnAdd=true)
Definition: NoteModel.h:101
static RealTime frame2RealTime(long frame, unsigned int sampleRate)
Convert a sample frame at the given sample rate into a RealTime.
Definition: RealTime.cpp:450
float level
Definition: NoteModel.h:50
PointList::const_iterator PointListConstIterator
Definition: SparseModel.h:71
bool isMidiPitchQuantized
Definition: NoteData.h:32
virtual bool canPlay() const
Definition: NoteModel.h:130
virtual int getSampleRate() const
Definition: SparseModel.h:53
float getValueQuantization() const
Definition: NoteModel.h:125
NoteModel – a concrete IntervalModel for notes.
Definition: NoteModel.h:38
void removePlayable(const Playable *playable)
bool operator()(const Note &p1, const Note &p2) const
Definition: NoteModel.h:77
virtual QVariant getData(int row, int column, int role) const
TabularModel methods.
Definition: NoteModel.h:173
float value
Definition: NoteModel.h:48
bool operator()(const Note &p1, const Note &p2) const
Definition: NoteModel.h:88
virtual int getColumnCount() const
TabularModel methods.
Definition: NoteModel.h:155
virtual QVariant getData(int row, int column, int role) const
TabularModel methods.
Definition: IntervalModel.h:68
int getDimensions() const
Definition: NoteModel.h:45
static int getPitchForFrequency(float frequency, float *centsOffsetReturn=0, float concertA=0.0)
Return the nearest MIDI pitch to the given frequency.
Definition: Pitch.cpp:35
virtual QString getHeading(int column) const
Definition: NoteModel.h:160
static QString encodeEntities(QString)
virtual QString getScaleUnits() const
Note(long _frame)
Definition: NoteModel.h:41
virtual int getStartFrame() const
Definition: SparseModel.h:484
long frame
Definition: NoteModel.h:47
void setValueQuantization(float q)
Definition: NoteModel.h:126
NoteList getNotes() const
NoteExportable methods.
Definition: NoteModel.h:223
void toXml(QTextStream &stream, QString indent="", QString extraAttributes="") const
Definition: NoteModel.h:55
static QString notes[]
Definition: Pitch.cpp:87
virtual const SparseModel< Note >::PointList & getPoints() const
Definition: IntervalModel.h:60
Model containing sparse data (points with some properties) of which the properties include a duration...
Definition: IntervalModel.h:29
NoteList getNotesWithin(int startFrame, int endFrame) const
Definition: NoteModel.h:227
Note(long _frame, float _value, int _duration, float _level, QString _label)
Definition: NoteModel.h:42
QString toDelimitedDataString(QString delimiter, int sampleRate) const
Definition: NoteModel.h:65
virtual int getEndFrame() const
Definition: SparseModel.h:496
std::vector< NoteData > NoteList
Definition: NoteData.h:44
NoteModel(int sampleRate, int resolution, float valueMinimum, float valueMaximum, bool notifyOnAdd=true)
Definition: NoteModel.h:109
QString getTypeName() const
Return the type of the model.
Definition: NoteModel.h:128
virtual ~NoteModel()
Definition: NoteModel.h:120
float m_valueQuantization
Definition: NoteModel.h:262
virtual Command * getSetDataCommand(int row, int column, const QVariant &value, int role)
Definition: IntervalModel.h:89
virtual Command * getSetDataCommand(int row, int column, const QVariant &value, int role)
Definition: NoteModel.h:189
virtual QString getDefaultPlayClipId() const
Definition: NoteModel.h:132
virtual void toXml(QTextStream &out, QString indent="", QString extraAttributes="") const
Stream this exportable object out to XML on a text stream.
Definition: NoteModel.h:137
virtual void toXml(QTextStream &stream, QString indent="", QString extraAttributes="") const
Stream this exportable object out to XML on a text stream.
virtual SortType getSortType(int column) const
Definition: NoteModel.h:213
QString getLabel() const
Definition: NoteModel.h:53
std::string toString(bool align=false) const
Return a human-readable debug-type string to full precision (probably not a format to show to a user ...
Definition: RealTime.cpp:191
void addPlayable(const Playable *playable)
PointListIterator getPointListIteratorForRow(int row)
Definition: SparseModel.h:405
int midiPitch
Definition: NoteData.h:30
QString label
Definition: NoteModel.h:51