tempomap.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. This file is part of QTau
  3. Copyright (C) 2013-2018 Tobias "Tomoko" Platen <tplaten@posteo.de>
  4. Copyright (C) 2013 digited <https://github.com/digited>
  5. Copyright (C) 2010-2013 HAL@ShurabaP <https://github.com/haruneko>
  6. QTau is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. SPDX-License-Identifier: GPL-3.0+
  17. */
  18. #include "tempomap.h"
  19. #include <QJsonObject>
  20. #include <Utils.h>
  21. bool indexCompare(const tempoindex &i1, const tempoindex &i2)
  22. {
  23. return i1.pos < i2.pos;
  24. }
  25. void TempoMap::updateIndexList()
  26. {
  27. qSort(_index.begin(), _index.end(), indexCompare);
  28. //calculate bar offsets for gui and sound
  29. }
  30. TempoMap::TempoMap()
  31. {
  32. }
  33. void TempoMap::addTempo(int pos, float tempo)
  34. {
  35. beginInsertRows(QModelIndex(),1,1);
  36. _tempo[pos] = tempo;
  37. tempoindex idx;
  38. idx.pos = pos;
  39. idx.type = 0;
  40. _index.append(idx);
  41. updateIndexList();
  42. endInsertRows();
  43. }
  44. void TempoMap::addTimeSignature(int pos, int numerator, int denominator)
  45. {
  46. beginInsertRows(QModelIndex(),1,1);
  47. fraction time;
  48. time.denominator = denominator;
  49. time.numerator = numerator;
  50. tempoindex idx;
  51. idx.pos = pos;
  52. idx.type = 1;
  53. _index.append(idx);
  54. _time[pos] = time;
  55. updateIndexList();
  56. endInsertRows();
  57. }
  58. void TempoMap::removeEventAt(int index)
  59. {
  60. if(index<_index.size() && index>=0)
  61. {
  62. beginRemoveRows(QModelIndex(),index,index);
  63. if(_index[index].type==1)
  64. {
  65. _time.remove(_index[index].pos);
  66. }
  67. else if(_index[index].type==0)
  68. {
  69. _tempo.remove(_index[index].pos);
  70. }
  71. _index.removeAt(index);
  72. endRemoveRows();
  73. }
  74. }
  75. fraction TempoMap::getTimeSignatureForBar(int pos)
  76. {
  77. fraction time = _time[pos];
  78. while(time.denominator==0 && time.numerator==0)
  79. {
  80. pos--;
  81. time = _time[pos];
  82. }
  83. return time;
  84. }
  85. QModelIndex TempoMap::index(int row, int column, const QModelIndex &parent) const
  86. {
  87. Q_UNUSED( parent );
  88. return createIndex(row,column);
  89. }
  90. QModelIndex TempoMap::parent(const QModelIndex &child) const
  91. {
  92. Q_UNUSED( child );
  93. return QModelIndex();
  94. }
  95. int TempoMap::rowCount(const QModelIndex &parent) const
  96. {
  97. Q_UNUSED( parent );
  98. return _index.count();
  99. }
  100. int TempoMap::columnCount(const QModelIndex &parent) const
  101. {
  102. Q_UNUSED( parent );
  103. return 3;
  104. }
  105. QVariant TempoMap::data(const QModelIndex &index, int role) const
  106. {
  107. if(role!=Qt::DisplayRole) return QVariant();
  108. int row = index.row();
  109. int col = index.column();
  110. int pos = _index[row].pos;
  111. int type = _index[row].type;
  112. if(col==0) return QVariant(pos+1).toString();
  113. if(col==1)
  114. {
  115. if(type==0) return "Tempo";
  116. if(type==1) return "Time Signature";
  117. }
  118. if(col==2)
  119. {
  120. if(type==0) return _tempo[pos];
  121. if(type==1) return QVariant(_time[pos].numerator).toString()+"/"+QVariant(_time[pos].denominator).toString();
  122. }
  123. return QVariant();
  124. }
  125. void TempoMap::beginEditing()
  126. {
  127. _origIndex = _index;
  128. _origTempo = _tempo;
  129. _origTime = _time;
  130. }
  131. void TempoMap::toJson(QJsonArray& array)
  132. {
  133. for(int i=0;i<_index.length();i++)
  134. {
  135. int pos = _index[i].pos;
  136. int type = _index[i].type;
  137. if(type==1)
  138. {
  139. QJsonObject val;
  140. val["pos"] = pos;
  141. val["type"] = "timeSig";
  142. val["denominator"] = _time[pos].denominator;
  143. val["numerator"] = _time[pos].numerator;
  144. array.append(val);
  145. }
  146. else if(type==0)
  147. {
  148. QJsonObject val;
  149. val["pos"] = pos;
  150. val["type"] = "tempo";
  151. val["tempo"] = _tempo[pos];
  152. array.append(val);
  153. }
  154. }
  155. }
  156. void TempoMap::fromJson(QJsonArray &array)
  157. {
  158. _index.clear();
  159. _time.clear();
  160. _tempo.clear();
  161. for(int i=0;i<array.size();i++)
  162. {
  163. QJsonObject val = array[i].toObject();
  164. if(val["type"].toString()=="timeSig")
  165. {
  166. int pos = val["pos"].toInt();
  167. int denominator = val["denominator"].toInt();
  168. int numerator = val["numerator"].toInt();
  169. this->addTimeSignature(pos,numerator,denominator);
  170. }
  171. else if(val["type"].toString()=="tempo")
  172. {
  173. int pos = val["pos"].toInt();
  174. int tempo = val["tempo"].toInt();
  175. this->addTempo(pos,tempo);
  176. }
  177. }
  178. }
  179. void TempoMap::undo()
  180. {
  181. _index = _origIndex;
  182. _tempo = _origTempo;
  183. _time = _origTime;
  184. }
  185. bool TempoMap::getBar(int pulses,float& time,int &bar,int end)
  186. {
  187. time=0;
  188. for(int i=0;i<128;i++)
  189. {
  190. fraction ts = getTimeSignatureForBar(i);
  191. float bpm = getBPMforBar(i);
  192. int barLength = ts.numerator*480*4/ts.denominator;
  193. if(pulses>=barLength) {
  194. pulses-=barLength;
  195. time += ts.numerator*60/bpm;
  196. }
  197. else {
  198. pulses+=end;
  199. time += pulses*60/bpm/480;
  200. bar = i;
  201. return true;
  202. }
  203. }
  204. return false;
  205. }
  206. float TempoMap::getBPMforBar(int pos)
  207. {
  208. float bpm = _tempo[pos];
  209. while(bpm==0)
  210. {
  211. pos--;
  212. bpm = _tempo[pos];
  213. }
  214. return bpm;
  215. }
  216. QString TempoMap::getLabel(int bar, tmlabel mode)
  217. {
  218. if(mode==TM_BPM)
  219. {
  220. if(_tempo.keys().contains(bar) && _tempo[bar]>0)
  221. return STR(_tempo[bar])+" BPM";
  222. }if(mode==TM_SIG)
  223. {
  224. if(_time.keys().contains(bar) && _time[bar].denominator>0)
  225. return STR(_time[bar].numerator)+"/"+ STR(_time[bar].denominator);
  226. }
  227. return "";
  228. }
  229. bool TempoMap::setLabel(tmlabel mode, int bar, QString label)
  230. {
  231. if(mode==TM_BPM)
  232. {
  233. if(label.length()==0)
  234. {
  235. auto it=_tempo.find(bar);
  236. _tempo.erase (it);
  237. return true;
  238. }
  239. else
  240. {
  241. float tempo = QVariant(label).toFloat();
  242. if(tempo>0)
  243. {
  244. _tempo[bar]=tempo;
  245. return true;
  246. }
  247. }
  248. }
  249. if(mode==TM_SIG)
  250. {
  251. if(label.length()==0)
  252. {
  253. auto it=_time.find(bar);
  254. _time.erase (it);
  255. return true;
  256. }
  257. else
  258. {
  259. QStringList tmp = label.split("/");
  260. if(tmp.length()==2)
  261. {
  262. fraction time;
  263. time.numerator = QVariant(tmp[0]).toInt();
  264. time.denominator = QVariant(tmp[1]).toInt();
  265. if(time.denominator>0 && time.numerator>0)
  266. {
  267. _time[bar]=time;
  268. return true;
  269. }
  270. }
  271. }
  272. }
  273. return false;
  274. }