allegro.cpp 111 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520
  1. // Allegro: music representation system, with
  2. // extensible in-memory sequence structure
  3. // upward compatible with MIDI
  4. // implementations in C++ and Serpent
  5. // external, text-based representation
  6. // compatible with Aura
  7. //
  8. /* CHANGE LOG:
  9. 04 apr 03 -- fixed bug in add_track that caused infinite loop
  10. */
  11. #include "assert.h"
  12. #include "stdlib.h"
  13. #include "stdio.h"
  14. #include "string.h"
  15. #include "memory.h"
  16. #include <iostream>
  17. #include <fstream>
  18. using namespace std;
  19. #include "allegro.h"
  20. #include "algrd_internal.h"
  21. #include "algsmfrd_internal.h"
  22. // #include "trace.h" -- only needed for debugging
  23. #include "math.h"
  24. #define STREQL(x, y) (strcmp(x, y) == 0)
  25. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  26. #define ROUND(x) ((int) ((x) + 0.5))
  27. // 4311 is type cast ponter to long warning
  28. // 4996 is warning against strcpy
  29. // 4267 is size_t to long warning
  30. //#pragma warning(disable: 4311 4996 4267)
  31. Alg_atoms symbol_table;
  32. Serial_read_buffer Alg_track::ser_read_buf; // declare the static variables
  33. Serial_write_buffer Alg_track::ser_write_buf;
  34. bool within(double d1, double d2, double epsilon)
  35. {
  36. d1 -= d2;
  37. return d1 < epsilon && d1 > -epsilon;
  38. }
  39. char *heapify(const char *s)
  40. {
  41. char *h = new char[strlen(s) + 1];
  42. strcpy(h, s);
  43. return h;
  44. }
  45. void Alg_atoms::expand()
  46. {
  47. maxlen = (maxlen + 5); // extra growth for small sizes
  48. maxlen += (maxlen >> 2); // add 25%
  49. Alg_attribute *new_atoms = new Alg_attribute[maxlen];
  50. // now do copy
  51. memcpy(new_atoms, atoms, len * sizeof(Alg_attribute));
  52. if (atoms) delete[] atoms;
  53. atoms = new_atoms;
  54. }
  55. // insert_new -- insert an attribute name and type
  56. //
  57. // attributes are stored as a string consisting of the type
  58. // (a char) followed by the attribute name. This makes it
  59. // easy to retrieve the type or the name or both.
  60. //
  61. Alg_attribute Alg_atoms::insert_new(const char *name, char attr_type)
  62. {
  63. if (len == maxlen) expand();
  64. char *h = new char[strlen(name) + 2];
  65. strcpy(h + 1, name);
  66. *h = attr_type;
  67. atoms[len++] = h;
  68. return h;
  69. }
  70. Alg_attribute Alg_atoms::insert_attribute(Alg_attribute attr)
  71. {
  72. // should use hash algorithm
  73. for (int i = 0; i < len; i++) {
  74. if (STREQL(attr, atoms[i])) {
  75. return atoms[i];
  76. }
  77. }
  78. return insert_new(attr + 1, attr[0]);
  79. }
  80. Alg_attribute Alg_atoms::insert_string(const char *name)
  81. {
  82. char attr_type = name[strlen(name) - 1];
  83. for (int i = 0; i < len; i++) {
  84. if (attr_type == atoms[i][0] &&
  85. STREQL(name, atoms[i] + 1)) {
  86. return atoms[i];
  87. }
  88. }
  89. return insert_new(name, attr_type);
  90. }
  91. void Alg_parameter::copy(Alg_parameter_ptr parm)
  92. {
  93. *this = *parm; // copy all fields
  94. // if the value is a string, copy the string
  95. if (attr_type() == 's') {
  96. s = heapify(s);
  97. }
  98. }
  99. void Alg_parameter::show()
  100. {
  101. switch (attr[0]) {
  102. case 'r':
  103. printf("%s:%g", attr_name(), r);
  104. break;
  105. case 's':
  106. printf("%s:%s", attr_name(), s);
  107. break;
  108. case 'i':
  109. printf("%s:%ld", attr_name(), i);
  110. break;
  111. case 'l':
  112. printf("%s:%s", attr_name(), (l ? "t" : "f"));
  113. break;
  114. case 'a':
  115. printf("%s:%s", attr_name(), a);
  116. break;
  117. }
  118. }
  119. Alg_parameter::~Alg_parameter()
  120. {
  121. if (attr_type() == 's' && s) {
  122. delete[] s;
  123. }
  124. }
  125. void Alg_parameters::insert_real(Alg_parameters **list, const char *name,
  126. double r)
  127. {
  128. Alg_parameters_ptr a = new Alg_parameters(*list);
  129. *list = a;
  130. a->parm.set_attr(symbol_table.insert_string(name));
  131. a->parm.r = r;
  132. assert(a->parm.attr_type() == 'r');
  133. }
  134. void Alg_parameters::insert_string(Alg_parameters **list, const char *name,
  135. const char *s)
  136. {
  137. Alg_parameters_ptr a = new Alg_parameters(*list);
  138. *list = a;
  139. a->parm.set_attr(symbol_table.insert_string(name));
  140. // string is deleted when parameter is deleted
  141. a->parm.s = heapify(s);
  142. assert(a->parm.attr_type() == 's');
  143. }
  144. void Alg_parameters::insert_integer(Alg_parameters **list, const char *name,
  145. long i)
  146. {
  147. Alg_parameters_ptr a = new Alg_parameters(*list);
  148. *list = a;
  149. a->parm.set_attr(symbol_table.insert_string(name));
  150. a->parm.i = i;
  151. assert(a->parm.attr_type() == 'i');
  152. }
  153. void Alg_parameters::insert_logical(Alg_parameters **list, const char *name,
  154. bool l)
  155. {
  156. Alg_parameters_ptr a = new Alg_parameters(*list);
  157. *list = a;
  158. a->parm.set_attr(symbol_table.insert_string(name));
  159. a->parm.l = l;
  160. assert(a->parm.attr_type() == 'l');
  161. }
  162. void Alg_parameters::insert_atom(Alg_parameters **list, const char *name,
  163. const char *s)
  164. {
  165. Alg_parameters_ptr a = new Alg_parameters(*list);
  166. *list = a;
  167. a->parm.set_attr(symbol_table.insert_string(name));
  168. a->parm.a = symbol_table.insert_string(s);
  169. assert(a->parm.attr_type() == 'a');
  170. }
  171. Alg_parameters *Alg_parameters::remove_key(Alg_parameters **list,
  172. const char *name)
  173. {
  174. while (*list) {
  175. if (STREQL((*list)->parm.attr_name(), name)) {
  176. Alg_parameters_ptr p = *list;
  177. *list = p->next;
  178. p->next = nullptr;
  179. return p; // caller should free this pointer
  180. }
  181. list = &((*list)->next);
  182. }
  183. return nullptr;
  184. }
  185. Alg_parameter_ptr Alg_parameters::find(Alg_attribute attr)
  186. {
  187. assert(attr);
  188. Alg_parameters_ptr temp = this;
  189. while (temp) {
  190. if (temp->parm.attr == attr) {
  191. return &(temp->parm);
  192. }
  193. }
  194. return nullptr;
  195. }
  196. int Alg_event::get_type_code()
  197. {
  198. if (!is_note()) {
  199. const char* attr = get_attribute();
  200. if (STREQL(attr, "gater")) // volume change
  201. return ALG_GATE;
  202. if (STREQL(attr, "bendr")) // pitch bend
  203. return ALG_BEND;
  204. if (strncmp(attr, "control", 7) == 0) // control change
  205. // note that midi control changes have attributes of the form
  206. // "control<n>" where n is the decimal number (as a character string)
  207. // of the midi controller, e.g. control2 is the breath controller.
  208. // We don't check for decimal numbers in the range 0-127, so any
  209. // attribute that begins with "control" is an ALG_CONTROL:
  210. return ALG_CONTROL;
  211. if (STREQL(attr, "programi")) // program change
  212. return ALG_PROGRAM;
  213. if (STREQL(attr, "pressurer")) // pressure change
  214. return ALG_PRESSURE;
  215. if (STREQL(attr, "keysigi")) // key signature
  216. return ALG_KEYSIG;
  217. if (STREQL(attr, "timesig_numi")) // time signature numerator
  218. return ALG_TIMESIG_NUM;
  219. if (STREQL(attr, "timesig_deni")) // time signature denominator
  220. return ALG_TIMESIG_DEN;
  221. return ALG_OTHER;
  222. }
  223. return ALG_NOTE; // it is a note
  224. }
  225. void Alg_event::set_parameter(Alg_parameter_ptr new_parameter)
  226. {
  227. Alg_parameter_ptr parm;
  228. if (is_note()) {
  229. Alg_note_ptr note = (Alg_note_ptr) this;
  230. parm = note->parameters->find(new_parameter->attr);
  231. if (!parm) {
  232. note->parameters = new Alg_parameters(note->parameters);
  233. parm = &(note->parameters->parm);
  234. }
  235. } else { // update
  236. Alg_update_ptr update = (Alg_update_ptr) this;
  237. parm = &(update->parameter);
  238. }
  239. parm->copy(new_parameter); // copy entire parameter
  240. }
  241. void Alg_event::set_string_value(const char *a, const char *value)
  242. {
  243. assert(a); // must be non-null
  244. Alg_attribute attr = symbol_table.insert_string(a);
  245. assert(attr[0] == 's');
  246. Alg_parameter parm;
  247. parm.set_attr(attr);
  248. parm.s = value;
  249. set_parameter(&parm);
  250. parm.s = nullptr; // do this to prevent string from being freed
  251. }
  252. void Alg_event::set_real_value(const char *a, double value)
  253. {
  254. assert(a); // must be non-null
  255. // attr is like a, but it has the type code prefixed for
  256. // fast lookup, and it is a unique string in symbol_table
  257. // e.g. a="attackr" -> attr="rattackr"
  258. Alg_attribute attr = symbol_table.insert_string(a);
  259. assert(attr[0] == 'r');
  260. Alg_parameter parm;
  261. parm.set_attr(attr);
  262. parm.r = value;
  263. set_parameter(&parm);
  264. // since type is 'r' we don't have to NULL the string
  265. }
  266. void Alg_event::set_logical_value(const char *a, bool value)
  267. {
  268. assert(a); // must be non-null
  269. Alg_attribute attr = symbol_table.insert_string(a);
  270. assert(attr[0] == 'l');
  271. Alg_parameter parm;
  272. parm.set_attr(attr);
  273. parm.l = value;
  274. set_parameter(&parm);
  275. // since type is 'l' we don't have to NULL the string
  276. }
  277. void Alg_event::set_integer_value(const char *a, long value)
  278. {
  279. assert(a); // must be non-null
  280. Alg_attribute attr = symbol_table.insert_string(a);
  281. assert(attr[0] == 'i');
  282. Alg_parameter parm;
  283. parm.set_attr(attr);
  284. parm.i = value;
  285. set_parameter(&parm);
  286. // since tpye is 'i' we don't have to NULL the string
  287. }
  288. void Alg_event::set_atom_value(const char *a, const char *value)
  289. {
  290. assert(a); // must be non-null
  291. Alg_attribute attr = symbol_table.insert_string(a);
  292. assert(attr[0] == 'a');
  293. Alg_parameter parm;
  294. parm.set_attr(attr);
  295. parm.a = value;
  296. set_parameter(&parm);
  297. /* since type is 'a' we don't have to null the string */
  298. }
  299. float Alg_event::get_pitch()
  300. {
  301. assert(is_note());
  302. Alg_note* note = (Alg_note *) this;
  303. return note->pitch;
  304. }
  305. float Alg_event::get_loud()
  306. {
  307. assert(is_note());
  308. Alg_note* note = (Alg_note *) this;
  309. return note->loud;
  310. }
  311. double Alg_event::get_start_time()
  312. {
  313. assert(is_note());
  314. Alg_note* note = (Alg_note *) this;
  315. return note->time;
  316. }
  317. double Alg_event::get_end_time()
  318. {
  319. assert(is_note());
  320. Alg_note* note = (Alg_note *) this;
  321. return note->time + note->dur;
  322. }
  323. double Alg_event::get_duration()
  324. {
  325. assert(is_note());
  326. Alg_note* note = (Alg_note *) this;
  327. return note->dur;
  328. }
  329. void Alg_event::set_pitch(float p)
  330. {
  331. assert(is_note());
  332. Alg_note* note = (Alg_note *) this;
  333. note->pitch = p;
  334. }
  335. void Alg_event::set_loud(float l)
  336. {
  337. assert(is_note());
  338. Alg_note *note = (Alg_note *) this;
  339. note->loud = l;
  340. }
  341. void Alg_event::set_duration(double d)
  342. {
  343. assert(is_note());
  344. Alg_note* note = (Alg_note *) this;
  345. note->dur = d;
  346. }
  347. bool Alg_event::has_attribute(const char *a)
  348. {
  349. assert(is_note());
  350. assert(a); // must be non-null
  351. Alg_note* note = (Alg_note *) this;
  352. Alg_attribute attr = symbol_table.insert_string(a);
  353. Alg_parameter_ptr parm = note->parameters->find(attr);
  354. return parm != nullptr;
  355. }
  356. char Alg_event::get_attribute_type(const char *a)
  357. {
  358. assert(is_note());
  359. assert(a);
  360. return a[strlen(a) - 1];
  361. }
  362. const char *Alg_event::get_string_value(const char *a, const char *value)
  363. {
  364. assert(is_note());
  365. assert(a); // must be non-null
  366. Alg_note* note = (Alg_note *) this;
  367. Alg_attribute attr = symbol_table.insert_string(a);
  368. assert(a[0] == 's'); // must be of type string
  369. Alg_parameter_ptr parm = note->parameters->find(attr);
  370. if (parm) return parm->s;
  371. return value;
  372. }
  373. double Alg_event::get_real_value(const char *a, double value)
  374. {
  375. assert(is_note());
  376. assert(a);
  377. Alg_note* note = (Alg_note *) this;
  378. Alg_attribute attr = symbol_table.insert_string(a);
  379. assert(a[0] == 'r'); // must be of type real
  380. Alg_parameter_ptr parm = note->parameters->find(attr);
  381. if (parm) return parm->r;
  382. return value;
  383. }
  384. bool Alg_event::get_logical_value(const char *a, bool value)
  385. {
  386. assert(is_note());
  387. assert(a);
  388. Alg_note* note = (Alg_note *) this;
  389. Alg_attribute attr = symbol_table.insert_string(a);
  390. assert(a[0] == 'l'); // must be of type logical
  391. Alg_parameter_ptr parm = note->parameters->find(attr);
  392. if (parm) return parm->l;
  393. return value;
  394. }
  395. long Alg_event::get_integer_value(const char *a, long value)
  396. {
  397. assert(is_note());
  398. assert(a);
  399. Alg_note* note = (Alg_note *) this;
  400. Alg_attribute attr = symbol_table.insert_string(a);
  401. assert(a[0] == 'i'); // must be of type integer
  402. Alg_parameter_ptr parm = note->parameters->find(attr);
  403. if (parm) return parm->i;
  404. return value;
  405. }
  406. const char *Alg_event::get_atom_value(const char *a, const char *value)
  407. {
  408. assert(is_note());
  409. assert(a);
  410. Alg_note* note = (Alg_note *) this;
  411. Alg_attribute attr = symbol_table.insert_string(a);
  412. assert(a[0] == 'a'); // must be of type atom
  413. Alg_parameter_ptr parm = note->parameters->find(attr);
  414. if (parm) return parm->a;
  415. // if default is a string, convert to an atom (unique
  416. // string in symbol table) and return it
  417. return (value == nullptr ? nullptr :
  418. symbol_table.insert_string(value));
  419. }
  420. void Alg_event::delete_attribute(const char *a)
  421. {
  422. assert(is_note());
  423. Alg_note* note = (Alg_note *) this;
  424. Alg_parameters::remove_key(&(note->parameters), a);
  425. }
  426. const char *Alg_event::get_attribute()
  427. // Note: this returns a string, not an Alg_attribute
  428. {
  429. assert(is_update());
  430. Alg_update* update = (Alg_update *) this;
  431. return update->parameter.attr_name();
  432. }
  433. char Alg_event::get_update_type()
  434. {
  435. assert(is_update());
  436. Alg_update* update = (Alg_update *) this;
  437. return update->parameter.attr_type();
  438. }
  439. const char *Alg_event::get_string_value()
  440. {
  441. assert(is_update());
  442. Alg_update* update = (Alg_update *) this;
  443. assert(get_update_type() == 's');
  444. return update->parameter.s;
  445. }
  446. double Alg_event::get_real_value()
  447. {
  448. assert(is_update());
  449. Alg_update* update = (Alg_update *) this;
  450. assert(get_update_type() == 'r');
  451. return update->parameter.r;
  452. }
  453. bool Alg_event::get_logical_value()
  454. {
  455. assert(is_update());
  456. Alg_update* update = (Alg_update *) this;
  457. assert(get_update_type() == 'l');
  458. return update->parameter.l;
  459. }
  460. long Alg_event::get_integer_value()
  461. {
  462. assert(is_update());
  463. Alg_update* update = (Alg_update *) this;
  464. assert(get_update_type() == 'i');
  465. return update->parameter.i;
  466. }
  467. const char *Alg_event::get_atom_value()
  468. {
  469. assert(is_update());
  470. Alg_update* update = (Alg_update *) this;
  471. assert(get_update_type() == 'a');
  472. return update->parameter.a;
  473. }
  474. bool Alg_event::overlap(double t, double len, bool all)
  475. {
  476. // event starts within region
  477. if (time >= t && time <= t + len - ALG_EPS)
  478. return true;
  479. if (all && is_note()) {
  480. double dur = ((Alg_note_ptr) this)->dur;
  481. // note overlaps with region
  482. if (time < t && time + dur - ALG_EPS > t)
  483. return true;
  484. }
  485. // does not overlap
  486. return false;
  487. }
  488. Alg_note::Alg_note(Alg_note_ptr note)
  489. {
  490. *this = *note; // copy all fields
  491. // parameters is now a shared pointer. We need to copy the
  492. // parameters
  493. Alg_parameters_ptr next_param_ptr = parameters;
  494. while (next_param_ptr) {
  495. Alg_parameters_ptr new_params = new Alg_parameters(next_param_ptr->next);
  496. new_params->parm.copy(&(next_param_ptr->parm)); // copy the attribute and value
  497. next_param_ptr = new_params->next;
  498. }
  499. }
  500. Alg_note::~Alg_note()
  501. {
  502. while (parameters) {
  503. Alg_parameters_ptr to_delete = parameters;
  504. parameters = parameters->next;
  505. delete to_delete;
  506. }
  507. }
  508. void Alg_note::show()
  509. {
  510. printf("Alg_note: time %g, chan %ld, dur %g, key %ld, "
  511. "pitch %g, loud %g, attributes ",
  512. time, chan, dur, key, pitch, loud);
  513. Alg_parameters_ptr parms = parameters;
  514. while (parms) {
  515. parms->parm.show();
  516. printf(" ");
  517. parms = parms->next;
  518. }
  519. printf("\n");
  520. }
  521. Alg_update::Alg_update(Alg_update_ptr update)
  522. {
  523. *this = *update; // copy all fields
  524. // parameter requires careful copy to possibly duplicate string value:
  525. this->parameter.copy(&(update->parameter));
  526. }
  527. void Alg_update::show()
  528. {
  529. printf("Alg_update: ");
  530. parameter.show();
  531. printf("\n");
  532. }
  533. void Alg_events::expand()
  534. {
  535. maxlen = (maxlen + 5); // extra growth for small sizes
  536. maxlen += (maxlen >> 2); // add 25%
  537. Alg_event_ptr *new_events = new Alg_event_ptr[maxlen];
  538. // now do copy
  539. memcpy(new_events, events, len * sizeof(Alg_event_ptr));
  540. if (events) delete[] events;
  541. events = new_events;
  542. }
  543. void Alg_events::insert(Alg_event_ptr event)
  544. {
  545. if (maxlen <= len) {
  546. expand();
  547. }
  548. // Note: if the new event is the last one, the assignment
  549. // events[i] = event; (below) will never execute, so just
  550. // in case, we do the assignment here. events[len] will
  551. // be replaced during the memmove() operation below if
  552. // this is not the last event.
  553. events[len] = event;
  554. len++;
  555. // find insertion point: (this could be a binary search)
  556. for (int i = 0; i < len; i++) {
  557. if (events[i]->time > event->time) {
  558. // insert event at i
  559. memmove(&events[i + 1], &events[i],
  560. sizeof(Alg_event_ptr) * (len - i - 1));
  561. events[i] = event;
  562. return;
  563. }
  564. }
  565. }
  566. Alg_event_ptr Alg_events::uninsert(long index)
  567. {
  568. assert(0 <= index && index < len);
  569. Alg_event_ptr event = events[index];
  570. //printf("memmove: %x from %x (%d)\n", events + index, events + index + 1,
  571. // sizeof(Alg_event_ptr) * (len - index - 1));
  572. memmove(events + index, events + index + 1,
  573. sizeof(Alg_event_ptr) * (len - index - 1));
  574. len--;
  575. return event;
  576. }
  577. void Alg_events::append(Alg_event_ptr event)
  578. {
  579. if (maxlen <= len) {
  580. expand();
  581. }
  582. events[len++] = event;
  583. // keep track of last note_off time
  584. if (event->is_note()) {
  585. Alg_note_ptr note = (Alg_note_ptr) event;
  586. double note_off = note->time + note->dur;
  587. if (note_off > last_note_off)
  588. last_note_off = note_off;
  589. }
  590. }
  591. Alg_events::~Alg_events()
  592. {
  593. assert(!in_use);
  594. // individual events are not deleted, only the array
  595. if (events) {
  596. delete[] events;
  597. }
  598. }
  599. Alg_event_list::Alg_event_list(Alg_track *owner)
  600. {
  601. events_owner = owner;
  602. sequence_number = owner->sequence_number;
  603. beat_dur = 0.0; real_dur = 0.0; type = 'e';
  604. }
  605. Alg_event_ptr const &Alg_event_list::operator [](int i)
  606. {
  607. assert(i >= 0 && i < len);
  608. return events[i];
  609. }
  610. Alg_event_list::~Alg_event_list()
  611. {
  612. // note that the events contained in the list are not destroyed
  613. }
  614. void Alg_event_list::set_start_time(Alg_event *event, double t)
  615. {
  616. // For Alg_event_list, find the owner and do the update there
  617. // For Alg_track, change the time and move the event to the right place
  618. // For Alg_seq, find the track and do the update there
  619. long index = 0, i;
  620. Alg_track_ptr track_ptr = nullptr;
  621. if (type == 'e') { // this is an Alg_event_list
  622. // make sure the owner has not changed its event set
  623. assert(events_owner &&
  624. sequence_number == events_owner->sequence_number);
  625. // do the update on the owner
  626. events_owner->set_start_time(event, t);
  627. return;
  628. } else if (type == 't') { // this is an Alg_track
  629. // find the event in the track
  630. track_ptr = (Alg_track_ptr) this;
  631. // this should be a binary search since events are in time order
  632. // probably there should be member function to do the search
  633. for (index = 0; index < length(); index++) {
  634. if ((*track_ptr)[index] == event) goto found_event;
  635. }
  636. } else { // type == 's', an Alg_seq
  637. Alg_seq_ptr seq = (Alg_seq_ptr) this;
  638. for (i = 0; i < seq->tracks(); i++) {
  639. track_ptr = seq->track(i);
  640. // if you implemented binary search, you could call it
  641. // instead of this loop too.
  642. for (index = 0; index < track_ptr->length(); index++) {
  643. if ((*track_ptr)[index] == event) goto found_event;
  644. }
  645. }
  646. }
  647. assert(false); // event not found seq or track!
  648. found_event:
  649. // at this point, track[index] == event
  650. // we could be clever and figure out exactly what notes to move
  651. // but it is simpler to just remove the event and reinsert it:
  652. track_ptr->uninsert(index);
  653. event->time = t;
  654. track_ptr->insert(event);
  655. }
  656. void Alg_beats::expand()
  657. {
  658. maxlen = (maxlen + 5); // extra growth for small sizes
  659. maxlen += (maxlen >> 2); // add 25%
  660. Alg_beat_ptr new_beats = new Alg_beat[maxlen];
  661. // now do copy
  662. memcpy(new_beats, beats, len * sizeof(Alg_beat));
  663. if (beats) delete[] beats;
  664. beats = new_beats;
  665. }
  666. void Alg_beats::insert(long i, Alg_beat_ptr beat)
  667. {
  668. assert(i >= 0 && i <= len);
  669. if (maxlen <= len) {
  670. expand();
  671. }
  672. memmove(&beats[i + 1], &beats[i], sizeof(Alg_beat) * (len - i));
  673. memcpy(&beats[i], beat, sizeof(Alg_beat));
  674. len++;
  675. }
  676. Alg_time_map::Alg_time_map(Alg_time_map *map)
  677. {
  678. refcount = 0;
  679. assert(map->beats[0].beat == 0 && map->beats[0].time == 0);
  680. assert(map->beats.len > 0);
  681. // new_beats[0] = map->beats[0];
  682. // this is commented because
  683. // both new_beats[0] and map->beats[0] should be (0, 0)
  684. for (int i = 1; i < map->beats.len; i++) {
  685. beats.insert(i, &map->beats[i]);
  686. }
  687. last_tempo = map->last_tempo;
  688. last_tempo_flag = map->last_tempo_flag;
  689. }
  690. void Alg_time_map::show()
  691. {
  692. printf("Alg_time_map: ");
  693. for (int i = 0; i < beats.len; i++) {
  694. Alg_beat &b = beats[i];
  695. printf("(%g, %g) ", b.time, b.beat);
  696. }
  697. printf("last tempo: %g\n", last_tempo);
  698. }
  699. long Alg_time_map::locate_time(double time)
  700. {
  701. int i = 0;
  702. while ((i < beats.len) && (time > beats[i].time)) {
  703. i++;
  704. }
  705. return i;
  706. }
  707. long Alg_time_map::locate_beat(double beat)
  708. {
  709. int i = 0;
  710. while ((i < beats.len) && (beat > beats[i].beat)) {
  711. i++;
  712. }
  713. return i;
  714. }
  715. double Alg_time_map::beat_to_time(double beat)
  716. {
  717. Alg_beat_ptr mbi;
  718. Alg_beat_ptr mbi1;
  719. if (beat <= 0) {
  720. return beat;
  721. }
  722. int i = locate_beat(beat);
  723. // case 1: beat is between two time/beat pairs
  724. if (0 < i && i < beats.len) {
  725. mbi = &beats[i - 1];
  726. mbi1 = &beats[i];
  727. // case 2: beat is beyond last time/beat pair
  728. } else if (i == beats.len) {
  729. if (last_tempo_flag) {
  730. return beats[i - 1].time +
  731. (beat - beats[i - 1].beat) / last_tempo;
  732. } else if (i == 1) {
  733. return beat * 60.0 / ALG_DEFAULT_BPM;
  734. // so we use that as default allegro tempo too
  735. } else {
  736. mbi = &beats[i - 2];
  737. mbi1 = &beats[i - 1];
  738. }
  739. // case 3: beat is at time 0
  740. } else /* if (i == 0) */ {
  741. return beats[0].time;
  742. }
  743. // whether we extrapolate or interpolate, the math is the same
  744. double time_dif = mbi1->time - mbi->time;
  745. double beat_dif = mbi1->beat - mbi->beat;
  746. return mbi->time + (beat - mbi->beat) * time_dif / beat_dif;
  747. }
  748. double Alg_time_map::time_to_beat(double time)
  749. {
  750. Alg_beat_ptr mbi;
  751. Alg_beat_ptr mbi1;
  752. if (time <= 0.0) return time;
  753. int i = locate_time(time);
  754. if (i == beats.len) {
  755. if (last_tempo_flag) {
  756. return beats[i - 1].beat +
  757. (time - beats[i - 1].time) * last_tempo;
  758. } else if (i == 1) {
  759. return time * (ALG_DEFAULT_BPM / 60.0);
  760. } else {
  761. mbi = &beats[i - 2];
  762. mbi1 = &beats[i - 1];
  763. }
  764. } else {
  765. mbi = &beats[i - 1];
  766. mbi1 = & beats[i];
  767. }
  768. double time_dif = mbi1->time - mbi->time;
  769. double beat_dif = mbi1->beat - mbi->beat;
  770. return mbi->beat + (time - mbi->time) * beat_dif / time_dif;
  771. }
  772. void Alg_time_map::insert_beat(double time, double beat)
  773. {
  774. int i = locate_time(time); // i is insertion point
  775. if (i < beats.len && within(beats[i].time, time, 0.000001)) {
  776. // replace beat if time is already in the map
  777. beats[i].beat = beat;
  778. } else {
  779. Alg_beat point;
  780. point.beat = beat;
  781. point.time = time;
  782. beats.insert(i, &point);
  783. }
  784. // beats[i] contains new beat
  785. // make sure we didn't generate a zero tempo.
  786. // if so, space beats by one microbeat as necessary
  787. long j = i;
  788. if (j == 0) j = 1; // do not adjust beats[0]
  789. while (j < beats.len &&
  790. beats[j - 1].beat + 0.000001 >= beats[j].beat) {
  791. beats[j].beat = beats[j - 1].beat + 0.000001;
  792. j++;
  793. }
  794. }
  795. bool Alg_time_map::insert_tempo(double tempo, double beat)
  796. {
  797. tempo = tempo / 60.0; // convert to beats per second
  798. // change the tempo at the given beat until the next beat event
  799. if (beat < 0) return false;
  800. double time = beat_to_time(beat);
  801. long i = locate_time(time);
  802. if (i >= beats.len || !within(beats[i].time, time, 0.000001)) {
  803. insert_beat(time, beat);
  804. }
  805. // now i is index of beat where tempo will change
  806. if (i == beats.len - 1) {
  807. last_tempo = tempo;
  808. // printf("last_tempo to %g\n", last_tempo);
  809. last_tempo_flag = true;
  810. } else { // adjust all future beats
  811. // compute the difference in beats
  812. double diff = beats[i + 1].beat - beats[i].beat;
  813. // convert beat difference to seconds at new tempo
  814. diff = diff / tempo;
  815. // figure out old time difference:
  816. double old_diff = beats[i + 1].time - time;
  817. // compute difference too
  818. diff = diff - old_diff;
  819. // apply new_diff to score and beats
  820. i++;
  821. while (i < beats.len) {
  822. beats[i].time = beats[i].time + diff;
  823. i++;
  824. }
  825. }
  826. return true;
  827. }
  828. double Alg_time_map::get_tempo(double beat)
  829. {
  830. Alg_beat_ptr mbi;
  831. Alg_beat_ptr mbi1;
  832. // if beat < 0, there is probably an error; return something nice anyway
  833. if (beat < 0) return ALG_DEFAULT_BPM / 60.0;
  834. long i = locate_beat(beat);
  835. // this code is similar to beat_to_time() so far, but we want to get
  836. // beyond beat if possible because we want the tempo FOLLOWING beat
  837. // (Consider the case beat == 0.0)
  838. if (i < beats.len && beat >= beats[i].beat) i++;
  839. // case 1: beat is between two time/beat pairs
  840. if (i < beats.len) {
  841. mbi = &beats[i - 1];
  842. mbi1 = &beats[i];
  843. // case 2: beat is beyond last time/beat pair
  844. } else /* if (i == beats.len) */ {
  845. if (last_tempo_flag) {
  846. return last_tempo;
  847. } else if (i == 1) {
  848. return ALG_DEFAULT_BPM / 60.0;
  849. } else {
  850. mbi = &beats[i - 2];
  851. mbi1 = &beats[i - 1];
  852. }
  853. }
  854. double time_dif = mbi1->time - mbi->time;
  855. double beat_dif = mbi1->beat - mbi->beat;
  856. return beat_dif / time_dif;
  857. }
  858. bool Alg_time_map::set_tempo(double tempo, double start_beat, double end_beat)
  859. {
  860. if (start_beat >= end_beat) return false;
  861. // algorithm: insert a beat event if necessary at start_beat
  862. // and at end_beat
  863. // delete intervening map elements
  864. // change the tempo
  865. insert_beat(beat_to_time(start_beat), start_beat);
  866. insert_beat(beat_to_time(end_beat), end_beat);
  867. long start_x = locate_beat(start_beat) + 1;
  868. long stop_x = locate_beat(end_beat);
  869. while (stop_x < beats.len) {
  870. beats[start_x] = beats[stop_x];
  871. start_x++;
  872. stop_x++;
  873. }
  874. beats.len = start_x; // truncate the map to new length
  875. return insert_tempo(tempo, start_beat);
  876. }
  877. bool Alg_time_map::stretch_region(double b0, double b1, double dur)
  878. {
  879. // find current duration
  880. double t0 = beat_to_time(b0);
  881. double t1 = beat_to_time(b1);
  882. double old_dur = t1 - t0;
  883. if (old_dur <= 0 || dur <= 0) return false;
  884. double scale = dur / old_dur; // larger scale => slower
  885. // insert a beat if necessary at b0 and b1
  886. insert_beat(t0, b0);
  887. insert_beat(t1, b1);
  888. long start_x = locate_beat(b0);
  889. long stop_x = locate_beat(b1);
  890. double orig_time = beats[start_x].time;
  891. double prev_time = orig_time;
  892. for (int i = start_x + 1; i < beats.len; i++) {
  893. double delta = beats[i].time - orig_time;
  894. if (i <= stop_x) { // change tempo to next Alg_beat
  895. delta *= scale;
  896. }
  897. orig_time = beats[i].time;
  898. prev_time += delta;
  899. beats[i].time = prev_time;
  900. }
  901. return true;
  902. }
  903. void Alg_time_map::trim(double start, double end, bool units_are_seconds)
  904. {
  905. // extract the time map from start to end and shift to time zero
  906. // start and end are time in seconds if units_are_seconds is true
  907. int i = 0; // index into beats
  908. int start_index; // index of first breakpoint after start
  909. int count = 1;
  910. double initial_beat = start;
  911. double final_beat = end;
  912. if (units_are_seconds) {
  913. initial_beat = time_to_beat(start);
  914. final_beat = time_to_beat(end);
  915. } else {
  916. start = beat_to_time(initial_beat);
  917. end = beat_to_time(final_beat);
  918. }
  919. while (i < length() && beats[i].time < start) i++;
  920. // now i is index into beats of the first breakpoint after start
  921. // beats[0] is (0,0) and remains that way
  922. // copy beats[start_index] to beats[1], etc.
  923. // skip any beats at or near (start,initial_beat), using count
  924. // to keep track of how many entries there are
  925. start_index = i;
  926. while (i < length() && beats[i].time < end) {
  927. if (beats[i].time - start > ALG_EPS &&
  928. beats[i].beat - initial_beat > ALG_EPS) {
  929. beats[i].time = beats[i].time - start;
  930. beats[i].beat = beats[i].beat - initial_beat;
  931. beats[i - start_index + 1] = beats[i];
  932. count = count + 1;
  933. } else {
  934. start_index = start_index + 1;
  935. }
  936. i = i + 1;
  937. }
  938. // set last tempo data
  939. // we last examined beats[i-1] and copied it to
  940. // beats[i - start_index]. Next tempo should come
  941. // from beats[i] and store in beats[i - start_index + 1]
  942. // case 1: there is at least one breakpoint beyond end
  943. // => interpolate to put a breakpoint at end
  944. // case 2: no more breakpoints => set last tempo data
  945. if (i < length()) {
  946. // we know beats[i].time >= end, so case 1 applies
  947. beats[i - start_index + 1].time = end - start;
  948. beats[i - start_index + 1].beat = final_beat - initial_beat;
  949. count = count + 1;
  950. }
  951. // else we'll just use stored last tempo (if any)
  952. beats.len = count;
  953. }
  954. void Alg_time_map::cut(double start, double len, bool units_are_seconds)
  955. {
  956. // remove portion of time map from start to start + len,
  957. // shifting the tail left by len. start and len are in whatever
  958. // units the score is in. If you cut the time_map as well as cut
  959. // the tracks of the sequence, then sequences will preserve the
  960. // association between tempo changes and events
  961. double end = start + len;
  962. double initial_beat = start;
  963. double final_beat = end;
  964. int i = 0;
  965. if (units_are_seconds) {
  966. initial_beat = time_to_beat(start);
  967. final_beat = time_to_beat(end);
  968. } else {
  969. start = beat_to_time(initial_beat);
  970. end = beat_to_time(final_beat);
  971. len = end - start;
  972. }
  973. double beat_len = final_beat - initial_beat;
  974. while (i < length() && beats[i].time < start - ALG_EPS) {
  975. i = i + 1;
  976. }
  977. // if no beats exist at or after start, just return; nothing to cut
  978. if (i == length()) return;
  979. // now i is index into beats of the first breakpoint on or
  980. // after start, insert (start, initial_beat) in map
  981. if (i < length() && within(beats[i].time, start, ALG_EPS)) {
  982. // perterb time map slightly (within alg_eps) to place
  983. // break point exactly at the start time
  984. beats[i].time = start;
  985. beats[i].beat = initial_beat;
  986. } else {
  987. Alg_beat point(start, initial_beat);
  988. beats.insert(i, &point);
  989. }
  990. // now, we're correct up to beats[i] and beats[i] happens at start.
  991. // find first beat after end so we can start shifting from there
  992. i = i + 1;
  993. int start_index = i;
  994. while (i < length() && beats[i].time < end + ALG_EPS) i++;
  995. // now beats[i] is the next point to be included in beats
  996. // but from i onward, we must shift by (-len, -beat_len)
  997. while (i < length()) {
  998. Alg_beat &b = beats[i];
  999. b.time = b.time - len;
  1000. b.beat = b.beat - beat_len;
  1001. beats[start_index] = b;
  1002. i = i + 1;
  1003. start_index = start_index + 1;
  1004. }
  1005. beats.len = start_index;
  1006. }
  1007. void Alg_time_map::paste(double beat, Alg_track *tr)
  1008. {
  1009. // insert a given time map at a given time and dur (in beats)
  1010. Alg_time_map_ptr from_map = tr->get_time_map();
  1011. // printf("time map paste\nfrom map\n");
  1012. // from_map->show();
  1013. // printf("to map\n");
  1014. // show();
  1015. Alg_beats &from = from_map->beats;
  1016. double time = beat_to_time(beat);
  1017. // Locate the point at which dur occurs
  1018. double dur = tr->get_beat_dur();
  1019. double tr_end_time = from_map->beat_to_time(dur);
  1020. // add offset to make room for insert
  1021. int i = locate_beat(beat);
  1022. while (i < length()) {
  1023. beats[i].beat += dur;
  1024. beats[i].time += tr_end_time;
  1025. i++;
  1026. }
  1027. // printf("after opening up\n");
  1028. // show();
  1029. // insert point at beginning and end of paste
  1030. insert_beat(time, beat);
  1031. // printf("after beginning point insert\n");
  1032. // show();
  1033. // insert_beat(time + tr_end_time, beat + dur);
  1034. // printf("after ending point insert\n");
  1035. // show();
  1036. int j = from_map->locate_beat(dur);
  1037. for (i = 0; i < j; i++) {
  1038. insert_beat(from[i].time + time, // shift by time
  1039. from[i].beat + beat); // and beat
  1040. }
  1041. // printf("after inserts\n");
  1042. show();
  1043. }
  1044. void Alg_time_map::insert_time(double start, double len)
  1045. {
  1046. // find time,beat pair that determines tempo at start
  1047. // compute beat offset = (delta beat / delta time) * len
  1048. // add len,beat offset to each following Alg_beat
  1049. // show();
  1050. int i = locate_time(start); // start <= beats[i].time
  1051. if (beats[i].time == start) i++; // start < beats[i].time
  1052. // case 1: between beats
  1053. if (i > 0 && i < length()) {
  1054. double beat_offset = len * (beats[i].beat - beats[i-1].beat) /
  1055. (beats[i].time - beats[i-1].time);
  1056. while (i < length()) {
  1057. beats[i].beat += beat_offset;
  1058. beats[i].time += len;
  1059. i++;
  1060. }
  1061. } // otherwise, last tempo is in effect; nothing to do
  1062. // printf("time_map AFTER INSERT\n");
  1063. // show();
  1064. }
  1065. void Alg_time_map::insert_beats(double start, double len)
  1066. {
  1067. int i = locate_beat(start); // start <= beats[i].beat
  1068. if (beats[i].beat == start) i++;
  1069. if (i > 0 && i < length()) {
  1070. double time_offset = len * (beats[i].time - beats[i-1].time) /
  1071. (beats[i].beat - beats[i-1].beat);
  1072. while (i < length()) {
  1073. beats[i].time += time_offset;
  1074. beats[i].beat += len;
  1075. i++;
  1076. }
  1077. } // otherwise, last tempo is in effect; nothing to do
  1078. // printf("time_map AFTER INSERT\n");
  1079. // show();
  1080. }
  1081. Alg_track::Alg_track(Alg_time_map *map, bool seconds)
  1082. {
  1083. type = 't';
  1084. time_map = nullptr;
  1085. units_are_seconds = seconds;
  1086. set_time_map(map);
  1087. }
  1088. Alg_event_ptr Alg_track::copy_event(Alg_event_ptr event)
  1089. {
  1090. Alg_event *new_event;
  1091. if (event->is_note()) {
  1092. new_event = new Alg_note((Alg_note_ptr) event);
  1093. } else { // update
  1094. new_event = new Alg_update((Alg_update_ptr) event);
  1095. }
  1096. return new_event;
  1097. }
  1098. Alg_track::Alg_track(Alg_track &track)
  1099. {
  1100. type = 't';
  1101. time_map = nullptr;
  1102. for (int i = 0; i < track.length(); i++) {
  1103. append(copy_event(track.events[i]));
  1104. }
  1105. set_time_map(track.time_map);
  1106. units_are_seconds = track.units_are_seconds;
  1107. }
  1108. Alg_track::Alg_track(Alg_event_list_ref event_list, Alg_time_map_ptr map,
  1109. bool units_are_seconds)
  1110. {
  1111. type = 't';
  1112. time_map = nullptr;
  1113. for (int i = 0; i < event_list.length(); i++) {
  1114. append(copy_event(event_list[i]));
  1115. }
  1116. set_time_map(map);
  1117. this->units_are_seconds = units_are_seconds;
  1118. }
  1119. void Alg_track::serialize(void **buffer, long *bytes)
  1120. {
  1121. // first determine whether this is a seq or a track.
  1122. // if it is a seq, then we will write the time map and a set of tracks
  1123. // if it is a track, we just write the track data and not the time map
  1124. //
  1125. // The code will align doubles on ALIGN boundaries, and longs and
  1126. // floats are aligned to multiples of 4 bytes.
  1127. //
  1128. // The format for a seq is:
  1129. // 'ALGS' -- indicates that this is a sequence
  1130. // long length of all seq data in bytes starting with 'ALGS'
  1131. // long channel_offset_per_track
  1132. // long units_are_seconds
  1133. // time_map:
  1134. // double last_tempo
  1135. // long last_tempo_flag
  1136. // long len -- number of tempo changes
  1137. // for each tempo change (Alg_beat):
  1138. // double time
  1139. // double beat
  1140. // time_sigs:
  1141. // long len -- number of time_sigs
  1142. // long pad
  1143. // for each time signature:
  1144. // double beat
  1145. // double num
  1146. // double den
  1147. // tracks:
  1148. // long len -- number of tracks
  1149. // long pad
  1150. // for each track:
  1151. // 'ALGT' -- indicates this is a track
  1152. // long length of all track data in bytes starting with 'ALGT'
  1153. // long units_are_seconds
  1154. // double beat_dur
  1155. // double real_dur
  1156. // long len -- number of events
  1157. // for each event:
  1158. // long selected
  1159. // long type
  1160. // long key
  1161. // long channel
  1162. // double time
  1163. // if this is a note:
  1164. // double pitch
  1165. // double dur
  1166. // double loud
  1167. // long len -- number of parameters
  1168. // for each parameter:
  1169. // char attribute[] with zero pad to ALIGN
  1170. // one of the following, depending on type:
  1171. // double r
  1172. // char s[] terminated by zero
  1173. // long i
  1174. // long l
  1175. // char a[] terminated by zero
  1176. // zero pad to ALIGN
  1177. // else if this is an update
  1178. // (same representation as parameter above)
  1179. // zero pad to ALIGN
  1180. //
  1181. // The format for a track is given within the Seq format above
  1182. assert(get_type() == 't');
  1183. ser_write_buf.init_for_write();
  1184. serialize_track();
  1185. *buffer = ser_write_buf.to_heap(bytes);
  1186. }
  1187. void Alg_seq::serialize(void **buffer, long *bytes)
  1188. {
  1189. assert(get_type() == 's');
  1190. ser_write_buf.init_for_write();
  1191. serialize_seq();
  1192. *buffer = ser_write_buf.to_heap(bytes);
  1193. }
  1194. void Serial_write_buffer::check_buffer(long needed)
  1195. {
  1196. if (len < (ptr - buffer) + needed) { // do we need more space?
  1197. long new_len = len * 2; // exponential growth is important
  1198. // initially, length is zero, so bump new_len to a starting value
  1199. if (new_len == 0) new_len = 1024;
  1200. // make sure new_len is as big as needed
  1201. if (needed > new_len) new_len = needed;
  1202. char *new_buffer = new char[new_len]; // allocate space
  1203. ptr = new_buffer + (ptr - buffer); // relocate ptr to new buffer
  1204. if (len > 0) { // we had a buffer already
  1205. memcpy(new_buffer, buffer, len); // copy from old buffer
  1206. delete buffer; // free old buffer
  1207. }
  1208. buffer = new_buffer; // update buffer information
  1209. len = new_len;
  1210. }
  1211. }
  1212. void Alg_seq::serialize_seq()
  1213. {
  1214. int i; // loop counters
  1215. // we can easily compute how much buffer space we need until we
  1216. // get to tracks, so expand at least that much
  1217. long needed = 64 + 16 * time_map->beats.len + 24 * time_sig.length();
  1218. ser_write_buf.check_buffer(needed);
  1219. ser_write_buf.set_char('A');
  1220. ser_write_buf.set_char('L');
  1221. ser_write_buf.set_char('G');
  1222. ser_write_buf.set_char('S');
  1223. long length_offset = ser_write_buf.get_posn();
  1224. ser_write_buf.set_int32(0); // leave room to come back and write length
  1225. ser_write_buf.set_int32(channel_offset_per_track);
  1226. ser_write_buf.set_int32(units_are_seconds);
  1227. ser_write_buf.set_double(beat_dur);
  1228. ser_write_buf.set_double(real_dur);
  1229. ser_write_buf.set_double(time_map->last_tempo);
  1230. ser_write_buf.set_int32(time_map->last_tempo_flag);
  1231. ser_write_buf.set_int32(time_map->beats.len);
  1232. for (i = 0; i < time_map->beats.len; i++) {
  1233. ser_write_buf.set_double(time_map->beats[i].time);
  1234. ser_write_buf.set_double(time_map->beats[i].beat);
  1235. }
  1236. ser_write_buf.set_int32(time_sig.length());
  1237. ser_write_buf.pad();
  1238. for (i = 0; i < time_sig.length(); i++) {
  1239. ser_write_buf.set_double(time_sig[i].beat);
  1240. ser_write_buf.set_double(time_sig[i].num);
  1241. ser_write_buf.set_double(time_sig[i].den);
  1242. }
  1243. ser_write_buf.set_int32(tracks());
  1244. ser_write_buf.pad();
  1245. for (i = 0; i < tracks(); i++) {
  1246. track(i)->serialize_track();
  1247. }
  1248. // do not include ALGS, include padding at end
  1249. ser_write_buf.store_long(length_offset, ser_write_buf.get_posn() - length_offset);
  1250. }
  1251. void Alg_track::serialize_track()
  1252. {
  1253. // to simplify the code, copy from parameter addresses to locals
  1254. int j;
  1255. ser_write_buf.check_buffer(32);
  1256. ser_write_buf.set_char('A');
  1257. ser_write_buf.set_char('L');
  1258. ser_write_buf.set_char('G');
  1259. ser_write_buf.set_char('T');
  1260. long length_offset = ser_write_buf.get_posn(); // save location for track length
  1261. ser_write_buf.set_int32(0); // room to write track length
  1262. ser_write_buf.set_int32(units_are_seconds);
  1263. ser_write_buf.set_double(beat_dur);
  1264. ser_write_buf.set_double(real_dur);
  1265. ser_write_buf.set_int32(len);
  1266. for (j = 0; j < len; j++) {
  1267. ser_write_buf.check_buffer(24);
  1268. Alg_event *event = (*this)[j];
  1269. ser_write_buf.set_int32(event->get_selected());
  1270. ser_write_buf.set_int32(event->get_type());
  1271. ser_write_buf.set_int32(event->get_identifier());
  1272. ser_write_buf.set_int32(event->chan);
  1273. ser_write_buf.set_double(event->time);
  1274. if (event->is_note()) {
  1275. ser_write_buf.check_buffer(20);
  1276. Alg_note *note = (Alg_note *) event;
  1277. ser_write_buf.set_float(note->pitch);
  1278. ser_write_buf.set_float(note->loud);
  1279. ser_write_buf.set_double(note->dur);
  1280. long parm_num_offset = ser_write_buf.get_posn();
  1281. long parm_num = 0;
  1282. ser_write_buf.set_int32(0); // placeholder for no. parameters
  1283. Alg_parameters_ptr parms = note->parameters;
  1284. while (parms) {
  1285. serialize_parameter(&(parms->parm));
  1286. parms = parms->next;
  1287. parm_num++;
  1288. }
  1289. ser_write_buf.store_long(parm_num_offset, parm_num);
  1290. } else {
  1291. assert(event->is_update());
  1292. Alg_update *update = (Alg_update *) event;
  1293. serialize_parameter(&(update->parameter));
  1294. }
  1295. ser_write_buf.check_buffer(7); // maximum padding possible
  1296. ser_write_buf.pad();
  1297. }
  1298. // write length, not including ALGT, including padding at end
  1299. ser_write_buf.store_long(length_offset, ser_write_buf.get_posn() - length_offset);
  1300. }
  1301. void Alg_track::serialize_parameter(Alg_parameter *parm)
  1302. {
  1303. // add eight to account for name + zero end-of-string and the
  1304. // possibility of adding 7 padding bytes
  1305. long len = strlen(parm->attr_name()) + 8;
  1306. ser_write_buf.check_buffer(len);
  1307. ser_write_buf.set_string(parm->attr_name());
  1308. ser_write_buf.pad();
  1309. switch (parm->attr_type()) {
  1310. case 'r':
  1311. ser_write_buf.check_buffer(8);
  1312. ser_write_buf.set_double(parm->r);
  1313. break;
  1314. case 's':
  1315. ser_write_buf.check_buffer(strlen(parm->s) + 1);
  1316. ser_write_buf.set_string(parm->s);
  1317. break;
  1318. case 'i':
  1319. ser_write_buf.check_buffer(4);
  1320. ser_write_buf.set_int32(parm->i);
  1321. break;
  1322. case 'l':
  1323. ser_write_buf.check_buffer(4);
  1324. ser_write_buf.set_int32(parm->l);
  1325. break;
  1326. case 'a':
  1327. ser_write_buf.check_buffer(strlen(parm->a) + 1);
  1328. ser_write_buf.set_string(parm->a);
  1329. break;
  1330. }
  1331. }
  1332. Alg_track *Alg_track::unserialize(void *buffer, long len)
  1333. {
  1334. assert(len > 8);
  1335. ser_read_buf.init_for_read(buffer, len);
  1336. bool alg = ser_read_buf.get_char() == 'A' &&
  1337. ser_read_buf.get_char() == 'L' &&
  1338. ser_read_buf.get_char() == 'G';
  1339. assert(alg); (void)alg; // unused variable
  1340. char c = ser_read_buf.get_char();
  1341. if (c == 'S') {
  1342. Alg_seq *seq = new Alg_seq;
  1343. ser_read_buf.unget_chars(4); // undo get_char() of A,L,G,S
  1344. seq->unserialize_seq();
  1345. return seq;
  1346. } else {
  1347. assert(c == 'T');
  1348. Alg_track *track = new Alg_track;
  1349. ser_read_buf.unget_chars(4); // undo get_char() of A,L,G,T
  1350. track->unserialize_track();
  1351. return track;
  1352. }
  1353. }
  1354. //#pragma warning(disable: 4800) // long to bool performance warning
  1355. /* Note: this Alg_seq must have a default initialized Alg_time_map.
  1356. * It will be filled in with data from the ser_read_buf buffer.
  1357. */
  1358. void Alg_seq::unserialize_seq()
  1359. {
  1360. ser_read_buf.check_input_buffer(48);
  1361. bool algs = (ser_read_buf.get_char() == 'A') &&
  1362. (ser_read_buf.get_char() == 'L') &&
  1363. (ser_read_buf.get_char() == 'G') &&
  1364. (ser_read_buf.get_char() == 'S');
  1365. assert(algs); (void)algs; // unused variable
  1366. long len = ser_read_buf.get_int32();
  1367. assert(ser_read_buf.get_len() >= len); (void)len; // unused variable
  1368. channel_offset_per_track = ser_read_buf.get_int32();
  1369. units_are_seconds = ser_read_buf.get_int32() != 0;
  1370. beat_dur = ser_read_buf.get_double();
  1371. real_dur = ser_read_buf.get_double();
  1372. // no need to allocate an Alg_time_map since it's done during initialization
  1373. time_map->last_tempo = ser_read_buf.get_double();
  1374. time_map->last_tempo_flag = ser_read_buf.get_int32() != 0;
  1375. long beats = ser_read_buf.get_int32();
  1376. ser_read_buf.check_input_buffer(beats * 16 + 4);
  1377. int i;
  1378. for (i = 0; i < beats; i++) {
  1379. double time = ser_read_buf.get_double();
  1380. double beat = ser_read_buf.get_double();
  1381. time_map->insert_beat(time, beat);
  1382. // printf("time_map: %g, %g\n", time, beat);
  1383. }
  1384. long time_sig_len = ser_read_buf.get_int32();
  1385. ser_read_buf.get_pad();
  1386. ser_read_buf.check_input_buffer(time_sig_len * 24 + 8);
  1387. for (i = 0; i < time_sig_len; i++) {
  1388. double beat = ser_read_buf.get_double();
  1389. double num = ser_read_buf.get_double();
  1390. double den = ser_read_buf.get_double();
  1391. time_sig.insert(beat, num, den);
  1392. }
  1393. long tracks_num = ser_read_buf.get_int32();
  1394. ser_read_buf.get_pad();
  1395. add_track(tracks_num - 1); // create tracks_num tracks
  1396. for (i = 0; i < tracks_num; i++) {
  1397. track(i)->unserialize_track();
  1398. }
  1399. // assume seq started at beginning of buffer. len measures
  1400. // bytes after 'ALGS' header, so add 4 bytes and compare to
  1401. // current buffer position -- they should agree
  1402. assert(ser_read_buf.get_posn() == len + 4);
  1403. }
  1404. void Alg_track::unserialize_track()
  1405. {
  1406. ser_read_buf.check_input_buffer(32);
  1407. bool algt = (ser_read_buf.get_char() == 'A') &&
  1408. (ser_read_buf.get_char() == 'L') &&
  1409. (ser_read_buf.get_char() == 'G') &&
  1410. (ser_read_buf.get_char() == 'T');
  1411. assert(algt); (void)algt; // unused variable
  1412. long offset = ser_read_buf.get_posn(); // stored length does not include 'ALGT'
  1413. long bytes = ser_read_buf.get_int32();
  1414. assert(bytes <= ser_read_buf.get_len() - offset); (void)offset; (void)bytes; // unused variable
  1415. units_are_seconds = (bool) ser_read_buf.get_int32();
  1416. beat_dur = ser_read_buf.get_double();
  1417. real_dur = ser_read_buf.get_double();
  1418. int event_count = ser_read_buf.get_int32();
  1419. for (int i = 0; i < event_count; i++) {
  1420. ser_read_buf.check_input_buffer(24);
  1421. long selected = ser_read_buf.get_int32();
  1422. char type = (char) ser_read_buf.get_int32();
  1423. long key = ser_read_buf.get_int32();
  1424. long channel = ser_read_buf.get_int32();
  1425. double time = ser_read_buf.get_double();
  1426. if (type == 'n') {
  1427. ser_read_buf.check_input_buffer(20);
  1428. float pitch = ser_read_buf.get_float();
  1429. float loud = ser_read_buf.get_float();
  1430. double dur = ser_read_buf.get_double();
  1431. Alg_note *note =
  1432. create_note(time, channel, key, pitch, loud, dur);
  1433. note->set_selected(selected != 0);
  1434. long param_num = ser_read_buf.get_int32();
  1435. int j;
  1436. // this builds a list of parameters in the correct order
  1437. // (although order shouldn't matter)
  1438. Alg_parameters_ptr *list = &note->parameters;
  1439. for (j = 0; j < param_num; j++) {
  1440. *list = new Alg_parameters(nullptr);
  1441. unserialize_parameter(&((*list)->parm));
  1442. list = &((*list)->next);
  1443. }
  1444. append(note);
  1445. } else {
  1446. assert(type == 'u');
  1447. Alg_update *update = create_update(time, channel, key);
  1448. update->set_selected(selected != 0);
  1449. unserialize_parameter(&(update->parameter));
  1450. append(update);
  1451. }
  1452. ser_read_buf.get_pad();
  1453. }
  1454. assert(offset + bytes == ser_read_buf.get_posn());
  1455. }
  1456. void Alg_track::unserialize_parameter(Alg_parameter_ptr parm_ptr)
  1457. {
  1458. Alg_attribute attr = ser_read_buf.get_string();
  1459. parm_ptr->attr = symbol_table.insert_string(attr);
  1460. switch (parm_ptr->attr_type()) {
  1461. case 'r':
  1462. ser_read_buf.check_input_buffer(8);
  1463. parm_ptr->r = ser_read_buf.get_double();
  1464. break;
  1465. case 's':
  1466. parm_ptr->s = heapify(ser_read_buf.get_string());
  1467. break;
  1468. case 'i':
  1469. ser_read_buf.check_input_buffer(4);
  1470. parm_ptr->i = ser_read_buf.get_int32();
  1471. break;
  1472. case 'l':
  1473. ser_read_buf.check_input_buffer(4);
  1474. parm_ptr->l = ser_read_buf.get_int32() != 0;
  1475. break;
  1476. case 'a':
  1477. parm_ptr->a = symbol_table.insert_attribute(ser_read_buf.get_string());
  1478. break;
  1479. }
  1480. }
  1481. //#pragma warning(default: 4800)
  1482. void Alg_track::set_time_map(Alg_time_map *map)
  1483. {
  1484. if (time_map) time_map->dereference();
  1485. if (map == nullptr) {
  1486. time_map = new Alg_time_map(); // new default map
  1487. time_map->reference();
  1488. } else {
  1489. time_map = map;
  1490. time_map->reference();
  1491. }
  1492. }
  1493. void Alg_track::convert_to_beats()
  1494. // modify all times and durations in notes to beats
  1495. {
  1496. if (units_are_seconds) {
  1497. units_are_seconds = false;
  1498. long i;
  1499. for (i = 0; i < length(); i++) {
  1500. Alg_event_ptr e = events[i];
  1501. double beat = time_map->time_to_beat(e->time);
  1502. if (e->is_note()) {
  1503. Alg_note_ptr n = (Alg_note_ptr) e;
  1504. n->dur = time_map->time_to_beat(n->time + n->dur) - beat;
  1505. }
  1506. e->time = beat;
  1507. }
  1508. }
  1509. }
  1510. void Alg_track::convert_to_seconds()
  1511. // modify all times and durations in notes to seconds
  1512. {
  1513. if (!units_are_seconds) {
  1514. last_note_off = time_map->beat_to_time(last_note_off);
  1515. units_are_seconds = true;
  1516. long i;
  1517. for (i = 0; i < length(); i++) {
  1518. Alg_event_ptr e = events[i];
  1519. double time = time_map->beat_to_time(e->time);
  1520. if (e->is_note()) {
  1521. Alg_note_ptr n = (Alg_note_ptr) e;
  1522. n->dur = time_map->beat_to_time(n->time + n->dur) - time;
  1523. }
  1524. e->time = time;
  1525. }
  1526. }
  1527. }
  1528. void Alg_track::set_dur(double duration)
  1529. {
  1530. // set beat_dur and real_dur
  1531. if (units_are_seconds) {
  1532. set_real_dur(duration);
  1533. set_beat_dur(time_map->time_to_beat(duration));
  1534. } else {
  1535. set_beat_dur(duration);
  1536. set_real_dur(time_map->beat_to_time(duration));
  1537. }
  1538. }
  1539. Alg_note *Alg_track::create_note(double time, int channel, int identifier,
  1540. float pitch, float loudness, double duration)
  1541. {
  1542. Alg_note *note = new Alg_note();
  1543. note->time = time;
  1544. note->chan = channel;
  1545. note->set_identifier(identifier);
  1546. note->pitch = pitch;
  1547. note->loud = loudness;
  1548. note->dur = duration;
  1549. return note;
  1550. }
  1551. Alg_update *Alg_track::create_update(double time, int channel, int identifier)
  1552. {
  1553. Alg_update *update = new Alg_update();
  1554. update->time = time;
  1555. update->chan = channel;
  1556. update->set_identifier(identifier);
  1557. return update;
  1558. }
  1559. Alg_track_ptr Alg_track::cut(double t, double len, bool all)
  1560. {
  1561. // since we are translating notes in time, do not copy or use old timemap
  1562. Alg_track_ptr track = new Alg_track();
  1563. track->units_are_seconds = units_are_seconds;
  1564. if (units_are_seconds) {
  1565. track->set_real_dur(len);
  1566. track->set_beat_dur(time_map->time_to_beat(t + len) -
  1567. time_map->time_to_beat(t));
  1568. } else {
  1569. track->set_beat_dur(len);
  1570. track->set_real_dur(time_map->beat_to_time(t + len) -
  1571. time_map->beat_to_time(t));
  1572. }
  1573. int i;
  1574. int new_len = 0;
  1575. int change = 0;
  1576. for (i = 0; i < length(); i++) {
  1577. Alg_event_ptr event = events[i];
  1578. if (event->overlap(t, len, all)) {
  1579. event->time -= t;
  1580. track->append(event);
  1581. change = 1;
  1582. } else { // if we're not cutting this event, move it to
  1583. // eliminate the gaps in events left by cut events
  1584. events[new_len] = event;
  1585. // adjust times of events after t + len
  1586. if (event->time > t + len - ALG_EPS) {
  1587. event->time -= len;
  1588. change = 1;
  1589. }
  1590. new_len++;
  1591. }
  1592. }
  1593. // Alg_event_lists based on this track become invalid
  1594. sequence_number += change;
  1595. this->len = new_len; // adjust length since we removed events
  1596. return track;
  1597. }
  1598. Alg_track_ptr Alg_track::copy(double t, double len, bool all)
  1599. {
  1600. // since we are translating notes in time, do not copy or use old timemap
  1601. Alg_track_ptr track = new Alg_track();
  1602. track->units_are_seconds = units_are_seconds;
  1603. if (units_are_seconds) {
  1604. track->set_real_dur(len);
  1605. track->set_beat_dur(time_map->time_to_beat(t + len) -
  1606. time_map->time_to_beat(t));
  1607. } else {
  1608. track->set_beat_dur(len);
  1609. track->set_real_dur(time_map->beat_to_time(t + len) -
  1610. time_map->beat_to_time(t));
  1611. }
  1612. int i;
  1613. for (i = 0; i < length(); i++) {
  1614. Alg_event_ptr event = events[i];
  1615. if (event->overlap(t, len, all)) {
  1616. Alg_event_ptr new_event = copy_event(event);
  1617. new_event->time -= t;
  1618. track->append(new_event);
  1619. }
  1620. }
  1621. return track;
  1622. }
  1623. void Alg_track::paste(double t, Alg_event_list *seq)
  1624. {
  1625. assert(get_type() == 't');
  1626. // seq can be an Alg_event_list, an Alg_track, or an Alg_seq
  1627. // if it is an Alg_event_list, units_are_seconds must match
  1628. bool prev_units_are_seconds;
  1629. if (seq->get_type() == 'e') {
  1630. assert(seq->get_owner()->get_units_are_seconds() == units_are_seconds);
  1631. prev_units_are_seconds = seq->get_owner()->get_units_are_seconds();
  1632. } else { // make it match
  1633. Alg_track_ptr tr = (Alg_track_ptr) seq;
  1634. prev_units_are_seconds = tr->get_units_are_seconds();
  1635. if (units_are_seconds) tr->convert_to_seconds();
  1636. else tr->convert_to_beats();
  1637. }
  1638. double dur = (units_are_seconds ? seq->get_real_dur() :
  1639. seq->get_beat_dur());
  1640. // Note: in the worst case, seq may contain notes
  1641. // that start almost anytime up to it's duration,
  1642. // so the simplest algorithm is simply a sequence
  1643. // of inserts. If this turns out to be too slow,
  1644. // we can do a merge sort in the case that seq
  1645. // is an Alg_track (if it's an Alg_event_list, we
  1646. // are not guaranteed that the events are in time
  1647. // order, but currently, only a true seq is allowed)
  1648. int i;
  1649. for (i = 0; i < length(); i++) {
  1650. if (events[i]->time > t - ALG_EPS) {
  1651. events[i]->time += dur;
  1652. }
  1653. }
  1654. for (i = 0; i < seq->length(); i++) {
  1655. Alg_event *new_event = copy_event((*seq)[i]);
  1656. new_event->time += t;
  1657. insert(new_event);
  1658. }
  1659. // restore track units to what they were before
  1660. if (seq->get_type() != 'e') {
  1661. Alg_track_ptr tr = (Alg_track_ptr) seq;
  1662. if (prev_units_are_seconds) tr->convert_to_seconds();
  1663. else tr->convert_to_beats();
  1664. }
  1665. }
  1666. void Alg_track::merge(double t, Alg_event_list_ptr seq)
  1667. {
  1668. Alg_event_list_ref s = *seq;
  1669. for (int i = 0; i < s.length(); i++) {
  1670. Alg_event *new_event;
  1671. if (s[i]->is_note()) {
  1672. new_event = new Alg_note((Alg_note_ptr) s[i]);
  1673. } else {
  1674. new_event = new Alg_update((Alg_update_ptr) s[i]);
  1675. }
  1676. new_event->time += t;
  1677. insert(new_event);
  1678. }
  1679. }
  1680. void Alg_track::clear(double t, double len, bool all)
  1681. {
  1682. int i;
  1683. int move_to = 0;
  1684. for (i = 0; i < length(); i++) {
  1685. Alg_event_ptr event = events[i];
  1686. if (event->overlap(t, len, all)) {
  1687. delete events[i];
  1688. } else { // if we're not clearing this event, move it to
  1689. // eliminate the gaps in events left by cleared events
  1690. events[move_to] = event;
  1691. // adjust times of events after t + len. This test is based
  1692. // on the one in Alg_event::overlap() for consistency.
  1693. if (event->time > t + len - ALG_EPS && event->time > t)
  1694. event->time -= len;
  1695. move_to++;
  1696. }
  1697. }
  1698. if (move_to != this->len) { // we cleared at least one note
  1699. sequence_number++; // Alg_event_lists based on this track become invalid
  1700. }
  1701. this->len = move_to; // adjust length since we removed events
  1702. }
  1703. void Alg_track::silence(double t, double len, bool all)
  1704. {
  1705. int i;
  1706. int move_to = 0;
  1707. for (i = 0; i < length(); i++) {
  1708. Alg_event_ptr event = events[i];
  1709. if (event->overlap(t, len, all)) {
  1710. delete events[i];
  1711. } else { // if we're not clearing this event, move it to
  1712. // eliminate the gaps in events left by cleared events
  1713. events[move_to] = event;
  1714. move_to++;
  1715. }
  1716. }
  1717. if (move_to != this->len) { // we cleared at least one note
  1718. sequence_number++; // Alg_event_lists based on this track become invalid
  1719. }
  1720. this->len = move_to; // adjust length since we removed events
  1721. }
  1722. void Alg_track::insert_silence(double t, double len)
  1723. {
  1724. int i;
  1725. for (i = 0; i < length(); i++) {
  1726. Alg_event_ptr event = events[i];
  1727. if (event->time > t - ALG_EPS) event->time += len;
  1728. }
  1729. }
  1730. Alg_event_list *Alg_track::find(double t, double len, bool all,
  1731. long channel_mask, long event_type_mask)
  1732. {
  1733. int i;
  1734. Alg_event_list *list = new Alg_event_list(this);
  1735. if (units_are_seconds) { // t and len are seconds
  1736. list->set_real_dur(len);
  1737. list->set_beat_dur(get_time_map()->time_to_beat(t + len) -
  1738. get_time_map()->time_to_beat(t));
  1739. } else { // t and len are beats
  1740. list->set_real_dur(get_time_map()->beat_to_time(t + len) -
  1741. get_time_map()->beat_to_time(t));
  1742. list->set_beat_dur(len);
  1743. }
  1744. for (i = 0; i < length(); i++) {
  1745. Alg_event_ptr event = events[i];
  1746. if (event->overlap(t, len, all)) {
  1747. if ((channel_mask == 0 ||
  1748. (event->chan < 32 &&
  1749. (channel_mask & (1 << event->chan)))) &&
  1750. ((event_type_mask == 0 ||
  1751. (event_type_mask & (1 << event->get_type_code()))))) {
  1752. list->append(event);
  1753. }
  1754. }
  1755. }
  1756. return list;
  1757. }
  1758. void Alg_time_sigs::expand()
  1759. {
  1760. assert(maxlen >= len);
  1761. maxlen = (maxlen + 5); // extra growth for small sizes
  1762. maxlen += (maxlen >> 2); // add 25%
  1763. Alg_time_sig_ptr new_time_sigs = new Alg_time_sig[maxlen];
  1764. // now do copy
  1765. memcpy(new_time_sigs, time_sigs, len * sizeof(Alg_time_sig));
  1766. if (time_sigs)
  1767. delete[] time_sigs;
  1768. time_sigs = new_time_sigs;
  1769. }
  1770. void Alg_time_sigs::insert(double beat, double num, double den, bool force)
  1771. {
  1772. // find insertion point:
  1773. for (int i = 0; i < len; i++) {
  1774. if (within(time_sigs[i].beat, beat, ALG_EPS)) {
  1775. // overwrite location i with new info
  1776. time_sigs[i].beat = beat;
  1777. time_sigs[i].num = num;
  1778. time_sigs[i].den = den;
  1779. return;
  1780. } else if (time_sigs[i].beat > beat) {
  1781. if ((i > 0 && // check if redundant with prev. time sig
  1782. time_sigs[i - 1].num == num &&
  1783. time_sigs[i - 1].den == den &&
  1784. within(fmod(beat - time_sigs[i - 1].beat,
  1785. 4 * time_sigs[i-1].num / time_sigs[i-1].den),
  1786. 0, ALG_EPS)) ||
  1787. // check if redundant with implied initial 4/4 time sig:
  1788. (i == 0 && num == 4 && den == 4 &&
  1789. within(fmod(beat, 4), 0, ALG_EPS))) {
  1790. if (!force) return; // redundant inserts can be ignored here
  1791. }
  1792. // make room for new event
  1793. if (maxlen <= len) expand();
  1794. // insert new event at i
  1795. memmove(&time_sigs[i + 1], &time_sigs[i],
  1796. sizeof(Alg_time_sig) * (len - i));
  1797. time_sigs[i].beat = beat;
  1798. time_sigs[i].num = num;
  1799. time_sigs[i].den = den;
  1800. len++;
  1801. return;
  1802. }
  1803. }
  1804. // if we fall out of loop, then this goes at end
  1805. if (maxlen <= len) expand();
  1806. time_sigs[len].beat = beat;
  1807. time_sigs[len].num = num;
  1808. time_sigs[len].den = den;
  1809. len++;
  1810. }
  1811. void Alg_time_sigs::show()
  1812. {
  1813. printf("Alg_time_sig: ");
  1814. for (int i = 0; i < len; i++) {
  1815. printf("(%g: %g/%g) ", time_sigs[i].beat, time_sigs[i].num, time_sigs[i].den);
  1816. }
  1817. printf("\n");
  1818. }
  1819. int Alg_time_sigs::find_beat(double beat)
  1820. {
  1821. // index where you would insert a new time signature at beat
  1822. int i = 0;
  1823. while (i < len && time_sigs[i].beat < beat - ALG_EPS) i++;
  1824. return i;
  1825. }
  1826. double Alg_time_sigs::get_bar_len(double beat)
  1827. {
  1828. int i = find_beat(beat);
  1829. double num = 4.0;
  1830. double den = 4.0;
  1831. if (i != 0) {
  1832. num = time_sigs[i - 1].num;
  1833. den = time_sigs[i - 1].den;
  1834. }
  1835. return 4 * num / den;
  1836. }
  1837. void Alg_time_sigs::cut(double start, double end, double dur)
  1838. {
  1839. // remove time_sig's from start to end -- these must be
  1840. // in beats (not seconds).
  1841. // The duration of the whole sequence is dur (beats).
  1842. // If the first bar line after end comes before a time signature
  1843. // and does not fall on a bar line, insert a time signature at
  1844. // the time of the bar line to retain relative bar line positions
  1845. int i = find_beat(end);
  1846. // i is where you would insert a new time sig at beat,
  1847. // Case 1: beat coincides with a time sig at i. Time signature
  1848. // at beat means that there is a barline at beat, so when beat
  1849. // is shifted to start, the relative barline positions are preserved
  1850. if (len > 0 &&
  1851. within(end, time_sigs[i].beat, ALG_EPS)) {
  1852. // beat coincides with time signature change, so end is on a barline
  1853. /* do nothing */ ;
  1854. // Case 2: there is no time signature before end
  1855. } else if (i == 0 && (len == 0 ||
  1856. time_sigs[0].beat > end)) {
  1857. // If the next time signature does not fall on a barline,
  1858. // then end must not be on a barline, so there is a partial
  1859. // measure from end to the next barline. We need
  1860. // a time signature there to preserve relative barline
  1861. // locations. It may be that the next bar after start is
  1862. // due to another time signature, in which case we do not
  1863. // need to insert anything.
  1864. double measures = end / 4.0;
  1865. double imeasures = ROUND(measures);
  1866. if (!within(measures, imeasures, ALG_EPS)) {
  1867. // start is not on a barline, maybe add one here:
  1868. double bar_loc = (int(measures) + 1) * 4.0;
  1869. if (bar_loc < dur - ALG_EPS &&
  1870. (len == 0 || time_sigs[0].beat > bar_loc + ALG_EPS)) {
  1871. insert(bar_loc, 4, 4, true); // forced insert
  1872. }
  1873. }
  1874. // This case should never be true because if i == 0, either there
  1875. // are no time signatures before beat (Case 2),
  1876. // or there is one time signature at beat (Case 1)
  1877. } else if (i == 0) {
  1878. /* do nothing (might be good to assert(false)) */ ;
  1879. // Case 3: i-1 must be the effective time sig position
  1880. } else {
  1881. // get the time signature in effect at end
  1882. Alg_time_sig &tsp = time_sigs[i - 1];
  1883. double beats_per_measure = (tsp.num * 4) / tsp.den;
  1884. double measures = (end - tsp.beat) / beats_per_measure;
  1885. int imeasures = ROUND(measures);
  1886. if (!within(measures, imeasures, ALG_EPS)) {
  1887. // end is not on a measure, so we need to insert a time sig
  1888. // to force a bar line at the first measure location after
  1889. // beat, if any
  1890. double bar_loc = tsp.beat + beats_per_measure * (int(measures) + 1);
  1891. // insert new time signature at bar_loc
  1892. // It will have the same time signature, but the position will
  1893. // force a barline to match the barline before the shift
  1894. // However, we should not insert a barline if there is a
  1895. // time signature earlier than the barline time
  1896. if (i < len /* time_sigs[i] is the last one */ &&
  1897. time_sigs[i].beat < bar_loc - ALG_EPS) {
  1898. /* do not insert because there's already a time signature */;
  1899. } else if (bar_loc < dur - ALG_EPS) {
  1900. insert(bar_loc, tsp.num, tsp.den, true); // forced insert
  1901. }
  1902. }
  1903. // else beat coincides with a barline, so no need for an extra
  1904. // time signature to force barline alignment
  1905. }
  1906. // Figure out if time signature at start matches
  1907. // the time signature at end. If not, we need to insert a
  1908. // time signature at end to force the correct time signature
  1909. // there.
  1910. // Find time signature at start:
  1911. double start_num = 4.0; // default if no time signature specified
  1912. double start_den = 4.0;
  1913. i = find_beat(start);
  1914. // A time signature at start would go at index i, so the effective
  1915. // time signature prior to start is at i - 1. If i == 0, the default
  1916. // time signature is in effect prior to start.
  1917. if (i != 0) {
  1918. start_num = time_sigs[i - 1].num;
  1919. start_den = time_sigs[i - 1].den;
  1920. }
  1921. // Find the time signature at end:
  1922. double end_num = 4.0; // default if no time signature specified
  1923. double end_den = 4.0;
  1924. int j = find_beat(end);
  1925. if (j != 0) {
  1926. end_num = time_sigs[j - 1].num;
  1927. end_den = time_sigs[j - 1].den;
  1928. }
  1929. // compare: If meter changes and there is no time signature at end,
  1930. // insert a time signature at end
  1931. if (end < dur - ALG_EPS &&
  1932. (start_num != end_num || start_den != end_den) &&
  1933. (j >= len || !within(time_sigs[j].beat, end, ALG_EPS))) {
  1934. insert(end, end_num, end_den, true);
  1935. }
  1936. // Remove time signatures from start to end (not including one AT
  1937. // end, if there is one there. Be careful with ALG_EPS on that one.)
  1938. // since we may have inserted a time signature, find position again:
  1939. int i0 = find_beat(start);
  1940. int i1 = i0;
  1941. // scan to end of cut region
  1942. while (i1 < len && time_sigs[i1].beat < end - ALG_EPS) {
  1943. i1++;
  1944. }
  1945. // scan from end to len(time_sig)
  1946. while (i1 < len) {
  1947. Alg_time_sig &ts = time_sigs[i1];
  1948. ts.beat -= (end - start);
  1949. time_sigs[i0] = ts;
  1950. i0++;
  1951. i1++;
  1952. }
  1953. len = i1;
  1954. }
  1955. void Alg_time_sigs::trim(double start, double end)
  1956. {
  1957. // remove time_sig's not in [start, end), but retain
  1958. // barline positions relative to the notes. This means that
  1959. // if the meter (time signature) changes between start and
  1960. // end that we need to insert a time signature at start.
  1961. // Also, if trim() would cause barlines to move, we need to
  1962. // insert a time signature on a barline (timesignatures
  1963. // imply the beginning of a bar even if the previous bar
  1964. // does not have enough beats. Note that bars do not need
  1965. // to have an integer number of beats).
  1966. //
  1967. // units must be in beats (not seconds)
  1968. //
  1969. // Uses Alg_time_sigs::cut() to avoid writing a special case
  1970. double dur = end + 1000;
  1971. if (len > 0) {
  1972. dur = time_sigs[len - 1].beat + 1000;
  1973. }
  1974. cut(end, dur, dur);
  1975. cut(0, start, dur);
  1976. #ifdef IGNORE_THIS_OLD_CODE
  1977. // first, skip time signatures up to start
  1978. int i = find_beat(start);
  1979. // i is where you would insert a new time sig at beat,
  1980. // Case 1: beat coincides with a time sig at i. Time signature
  1981. // at beat means that there is a barline at beat, so when beat
  1982. // is shifted to 0, the relative barline positions are preserved
  1983. if (len > 0 &&
  1984. within(start, time_sigs[i].beat, ALG_EPS)) {
  1985. // beat coincides with time signature change, so offset must
  1986. // be a multiple of beats
  1987. /* do nothing */ ;
  1988. // Case 2: there is no time signature before start
  1989. } else if (i == 0 && (len == 0 ||
  1990. time_sigs[0].beat > start)) {
  1991. // If the next time signature does not fall on a barline,
  1992. // then start must not be on a barline, so there is a partial
  1993. // measure from start to the next barline. We need
  1994. // a time signature there to preserve relative barline
  1995. // locations. It may be that the next bar after start is
  1996. // due to another time signature, in which case we do not
  1997. // need to insert anything.
  1998. double measures = start / 4.0;
  1999. double imeasures = ROUND(measures);
  2000. if (!within(measures, imeasures, ALG_EPS)) {
  2001. // start is not on a barline, maybe add one here:
  2002. double bar_loc = (int(measures) + 1) * 4.0;
  2003. if (len == 0 || time_sigs[1].beat > bar_loc + ALG_EPS) {
  2004. insert(bar_loc, 4, 4, true);
  2005. }
  2006. }
  2007. // This case should never be true because if i == 0, either there
  2008. // are no time signatures before beat (Case 2),
  2009. // or there is one time signature at beat (Case 1)
  2010. } else if (i == 0) {
  2011. /* do nothing (might be good to assert(false)) */ ;
  2012. // Case 3: i-1 must be the effective time sig position
  2013. } else {
  2014. i -= 1; // index the time signature in effect at start
  2015. Alg_time_sig &tsp = time_sigs[i];
  2016. double beats_per_measure = (tsp.num * 4) / tsp.den;
  2017. double measures = (start - tsp.beat) / beats_per_measure;
  2018. int imeasures = ROUND(measures);
  2019. if (!within(measures, imeasures, ALG_EPS)) {
  2020. // beat is not on a measure, so we need to insert a time sig
  2021. // to force a bar line at the first measure location after
  2022. // beat, if any
  2023. double bar_loc = tsp.beat + beats_per_measure * (int(measures) + 1);
  2024. // insert new time signature at bar_loc
  2025. // It will have the same time signature, but the position will
  2026. // force a barline to match the barline before the shift
  2027. insert(bar_loc, tsp.num, tsp.den, true);
  2028. }
  2029. // else beat coincides with a barline, so no need for an extra
  2030. // time signature to force barline alignment
  2031. }
  2032. // since we may have inserted a time signature, find position again:
  2033. int i_in = find_beat(start);
  2034. int i_out = 0;
  2035. // put time_sig at start if necessary
  2036. // if 0 < i_in < len, then the time sig at i_in is either
  2037. // at start or after start.
  2038. // If after start, then insert time sig at i_in-1 at 0.
  2039. // Otherwise, we'll pick up time sig at i_in below.
  2040. // If 0 == i_in < len, then the time sig at i_in is either
  2041. // at start or after start.
  2042. // If after start, then time sig at 0 is 4/4, but that's the
  2043. // default, so do nothing.
  2044. // Otherwise, we'll pick up time sig at i_in below.
  2045. // If 0 < i_in == len, then insert time_sig at i_in-1 at start
  2046. // If 0 == i_in == len, then 4/4 default applies and we're done.
  2047. //
  2048. // So the conditions for inserting time_sig[in_i-1] at 0 are:
  2049. // (0 < i_in < len and time_sig[i] > start+ALG_EPS) OR
  2050. // (0 < i_in == len)
  2051. // We can rewrite this to
  2052. // (0 < i_in) && ((i_in < len && time_sig[i_in].beat > start + ALG_EPS) ||
  2053. // (i_in == len)))
  2054. //
  2055. if (0 < i_in && ((i_in < len && time_sigs[i_in].beat > start + ALG_EPS) ||
  2056. (i_in == len))) {
  2057. time_sigs[0] = time_sigs[i_in - 1];
  2058. time_sigs[0].beat = 0.0;
  2059. i_out = 1;
  2060. }
  2061. // copy from i_in to i_out as we scan time_sig array to end of cut region
  2062. while (i_in < len && time_sigs[i_in].beat < end - ALG_EPS) {
  2063. Alg_time_sig &ts = time_sigs[i_in];
  2064. ts.beat = ts.beat - start;
  2065. time_sigs[i_out] = ts;
  2066. i_in++;
  2067. i_out++;
  2068. }
  2069. len = i_out;
  2070. #endif
  2071. }
  2072. void Alg_time_sigs::paste(double start, Alg_seq *seq)
  2073. {
  2074. // printf("time_sig::insert before paste\n");
  2075. // show();
  2076. Alg_time_sigs &from = seq->time_sig;
  2077. // printf("time_sig::insert from\n");
  2078. // from.show();
  2079. // insert time signatures from seq into this time_sigs at start
  2080. if (len == 0 && from.len == 0) {
  2081. return; // default applies
  2082. }
  2083. int i = find_beat(start);
  2084. // remember the time signature at the splice point
  2085. double num_after_splice = 4;
  2086. double den_after_splice = 4; // default
  2087. double num_before_splice = 4;
  2088. double den_before_splice = 4; // default
  2089. // this is computed for use in aligning beats after the inserted
  2090. // time signatures and duration. It is the position of time signature
  2091. // in effect immediately after start (the time signature will be
  2092. // before start or at start)
  2093. double beat_after_splice = 0.0;
  2094. // three cases:
  2095. // 1) time sig at splice is at i-1
  2096. // for this, we must have len>0 & i>0
  2097. // two sub-cases:
  2098. // A) i < len && time_sig[i].beat > start
  2099. // B) i == len
  2100. // 2) time_sig at splice is at i
  2101. // for this, i < len && time_sig[i].beat ~= start
  2102. // 3) time_sig at splice is default 4/4
  2103. if (len > 0 && i > 0 &&
  2104. ((i < len && time_sigs[i].beat > start + ALG_EPS) ||
  2105. (i == len))) {
  2106. // no time_signature at i
  2107. num_after_splice = time_sigs[i-1].num;
  2108. den_after_splice = time_sigs[i-1].den;
  2109. beat_after_splice = time_sigs[i - 1].beat;
  2110. num_before_splice = num_after_splice;
  2111. den_before_splice = den_after_splice;
  2112. } else if (i < len && time_sigs[i].beat <= start + ALG_EPS) {
  2113. // time_signature at i is at "start" beats
  2114. num_after_splice = time_sigs[i].num;
  2115. den_after_splice = time_sigs[i].den;
  2116. beat_after_splice = start;
  2117. if (i > 0) { // time signature before start is at i - 1
  2118. num_before_splice = time_sigs[i-1].num;
  2119. den_before_splice = time_sigs[i-1].den;
  2120. }
  2121. }
  2122. // i is where insert will go, time_sig[i].beat >= start
  2123. // begin by adding duration to time_sig's at i and above
  2124. // move time signatures forward by duration of seq
  2125. double dur = seq->get_beat_dur();
  2126. while (i < len) {
  2127. time_sigs[i].beat += dur;
  2128. i++;
  2129. }
  2130. //printf("time_sig::insert after making space\n");
  2131. //show();
  2132. // If time signature of "from" is not the effective time signature
  2133. // at start, insert a time_signature at start. This may create
  2134. // an extra measure if seq does not begin on a measure boundary
  2135. double num_of_insert = 4.0;
  2136. double den_of_insert = 4.0;
  2137. double beat_of_insert = 0.0;
  2138. /* int first_from_index = 0; // where to start copying from TODO: LMMS commented out unused variable */
  2139. if (from.length() > 0 && from[0].beat < ALG_EPS) {
  2140. // there is an initial time signature in "from"
  2141. num_of_insert = from[0].num;
  2142. den_of_insert = from[0].den;
  2143. // since we are handling the first time signature in from,
  2144. // we can start copying at index == 1:
  2145. /* first_from_index = 1; TODO: LMMS commented out unused variable */
  2146. }
  2147. // compare time signatures to see if we need a change at start:
  2148. if (num_before_splice != num_of_insert ||
  2149. den_before_splice != den_of_insert) {
  2150. // note that this will overwrite an existing time signature if
  2151. // it is within ALG_EPS of start -- this is correct because the
  2152. // existing time signature will already be recorded as
  2153. // num_after_splice and den_after_splice
  2154. insert(start, num_of_insert, den_of_insert);
  2155. }
  2156. //printf("time_sig::insert after 4/4 at start\n");
  2157. //show();
  2158. // insert time signatures from seq offset by start
  2159. for (i = 0; i < from.length() && from[i].beat < dur - ALG_EPS; i++) {
  2160. num_of_insert = from[i].num; // keep latest time signature info
  2161. den_of_insert = from[i].den;
  2162. beat_of_insert = from[i].beat;
  2163. insert(start + beat_of_insert, num_of_insert, den_of_insert);
  2164. }
  2165. //printf("time_sig::insert after pasting in sigs\n");
  2166. //show();
  2167. // now insert time signature at end of splice if necessary
  2168. // if the time signature changes, we need to insert a time signature
  2169. // immediately:
  2170. if (num_of_insert != num_after_splice &&
  2171. den_of_insert != den_after_splice) {
  2172. insert(start + dur, num_after_splice, den_after_splice);
  2173. num_of_insert = num_after_splice;
  2174. den_of_insert = den_after_splice;
  2175. beat_of_insert = start + dur;
  2176. }
  2177. // if the insert had a partial number of measures, we might need an
  2178. // additional time signature to realign the barlines after the insert
  2179. // To decide, we compare the beat of the first barline on or after
  2180. // start before the splice to the beat of the first barline on or
  2181. // after start + dur after the splice. In a sense, this is the "same"
  2182. // barline, so it should be shifted exactly by dur.
  2183. // First, compute the beat of the first barline on or after start:
  2184. double beats_per_measure = (num_after_splice * 4) / den_after_splice;
  2185. double measures = (start - beat_after_splice) / beats_per_measure;
  2186. // Measures might be slightly negative due to rounding. Use max()
  2187. // to eliminate any negative rounding error:
  2188. int imeasures = int(MAX(measures, 0.0));
  2189. double old_bar_loc = beat_after_splice + (imeasures * beats_per_measure);
  2190. if (old_bar_loc < start) old_bar_loc += beats_per_measure;
  2191. // now old_bar_loc is the original first bar position after start
  2192. // Do similar calculation for position after end after the insertion:
  2193. // beats_per_measure already calculated because signatures match
  2194. measures = (start + dur - beat_of_insert) / beats_per_measure;
  2195. imeasures = int(MAX(measures, 0.0));
  2196. double new_bar_loc = beat_of_insert + (imeasures * beats_per_measure);
  2197. if (new_bar_loc < start + dur) new_bar_loc += beats_per_measure;
  2198. // old_bar_loc should be shifted by dur:
  2199. old_bar_loc += dur;
  2200. // now the two bar locations should be equal, but due to rounding,
  2201. // they could be off by one measure
  2202. double diff = (new_bar_loc - old_bar_loc) + beats_per_measure;
  2203. double diff_in_measures = diff / beats_per_measure;
  2204. // if diff_in_measures is not (approximately) integer, we need to
  2205. // force a barline (time signature) after start + dur to maintain
  2206. // the relationship between barliness and notes
  2207. if (!within(diff_in_measures, ROUND(diff_in_measures), ALG_EPS)) {
  2208. // recall that old_bar_loc is shifted by dur
  2209. insert(old_bar_loc, num_after_splice, den_after_splice);
  2210. }
  2211. //printf("time_sig::insert after sig at end of splice\n");
  2212. //show();
  2213. }
  2214. void Alg_time_sigs::insert_beats(double start, double dur)
  2215. {
  2216. int i = find_beat(start);
  2217. // time_sigs[i] is after beat and needs to shift
  2218. // Compute the time of the first bar at or after beat so that
  2219. // a bar can be placed at bar_loc + dur
  2220. double tsnum = 4.0;
  2221. double tsden = 4.0;
  2222. double tsbeat = 0.0; // defaults
  2223. // three cases:
  2224. // 1) time sig at splice is at i-1
  2225. // for this, we must have len>0 & i>0
  2226. // two sub-cases:
  2227. // A) i < len && time_sig[i].beat > start
  2228. // B) i == len
  2229. // 2) time_sig at splice is at i
  2230. // for this, i < len && time_sig[i].beat ~= start
  2231. // 3) time_sig at splice is default 4/4
  2232. if (len > 0 && i > 0 &&
  2233. ((i < len && time_sigs[i].beat > start + ALG_EPS) ||
  2234. (i == len))) {
  2235. // no time_signature at i
  2236. tsnum = time_sigs[i-1].num;
  2237. tsden = time_sigs[i-1].den;
  2238. tsbeat = time_sigs[i-1].beat;
  2239. } else if (i < len && time_sigs[i].beat <= start + ALG_EPS) {
  2240. // time_signature at i is at "start" beats
  2241. tsnum = time_sigs[i].num;
  2242. tsden = time_sigs[i].den;
  2243. tsbeat = start;
  2244. i++; // we want i to be index of next time signature after start
  2245. }
  2246. // invariant: i is index of next time signature after start
  2247. // increase beat times from i to len - 1 by dur
  2248. for (int j = i; j < len; j++) {
  2249. time_sigs[j].beat += dur;
  2250. }
  2251. // insert a time signature to maintain bar positions if necessary
  2252. double beats_per_measure = (tsnum * 4) / tsden;
  2253. double measures = dur / beats_per_measure; // shift distance
  2254. int imeasures = ROUND(measures);
  2255. if (!within(measures, imeasures, ALG_EPS)) {
  2256. // shift is not a whole number of measures, so we may need to insert
  2257. // time signature after silence
  2258. // compute measures from time signature to next bar after time
  2259. measures = (start - tsbeat) / beats_per_measure;
  2260. // round up and add to tsbeat to get time of next bar
  2261. double bar_loc = tsbeat + beats_per_measure * (int(measures) + 1);
  2262. // translate bar_loc by len:
  2263. bar_loc += dur; // this is where we want a bar to be, but maybe
  2264. // there is a time signature change before bar, in which case we
  2265. // should not insert a new time signature
  2266. // The next time signature after start is at i if i < len
  2267. if (i < len && time_sigs[i].beat < bar_loc) {
  2268. /* do not insert */;
  2269. } else {
  2270. insert(bar_loc, tsnum, tsden);
  2271. }
  2272. }
  2273. }
  2274. double Alg_time_sigs::nearest_beat(double beat)
  2275. {
  2276. int i = find_beat(beat);
  2277. // i is where we would insert time signature at beat
  2278. // case 1: there is no time signature
  2279. if (i == 0 && len == 0) {
  2280. return ROUND(beat);
  2281. // case 2: beat falls approximately on time signature
  2282. } else if (i < len && within(time_sigs[i].beat, beat, ALG_EPS)) {
  2283. return time_sigs[i].beat;
  2284. // case 3: beat is after no time signature and before one
  2285. } else if (i == 0) {
  2286. double trial_beat = ROUND(beat);
  2287. // it is possible that we rounded up past a time signature
  2288. if (trial_beat > time_sigs[0].beat - ALG_EPS) {
  2289. return time_sigs[0].beat;
  2290. }
  2291. return trial_beat;
  2292. }
  2293. // case 4: beat is after some time signature
  2294. double trial_beat = time_sigs[i - 1].beat +
  2295. ROUND(beat - time_sigs[i - 1].beat);
  2296. // rounding may advance trial_beat past next time signature:
  2297. if (i < len && trial_beat > time_sigs[i].beat - ALG_EPS) {
  2298. return time_sigs[i].beat;
  2299. }
  2300. return trial_beat;
  2301. }
  2302. Alg_tracks::~Alg_tracks()
  2303. {
  2304. reset();
  2305. }
  2306. void Alg_tracks::expand_to(int new_max)
  2307. {
  2308. maxlen = new_max;
  2309. Alg_track_ptr *new_tracks = new Alg_track_ptr[maxlen];
  2310. // now do copy
  2311. memcpy(new_tracks, tracks, len * sizeof(Alg_track_ptr));
  2312. if (tracks) {
  2313. delete[] tracks;
  2314. }
  2315. tracks = new_tracks;
  2316. }
  2317. void Alg_tracks::expand()
  2318. {
  2319. maxlen = (maxlen + 5); // extra growth for small sizes
  2320. maxlen += (maxlen >> 2); // add 25%
  2321. expand_to(maxlen);
  2322. }
  2323. void Alg_tracks::append(Alg_track_ptr track)
  2324. {
  2325. if (maxlen <= len) {
  2326. expand();
  2327. }
  2328. tracks[len] = track;
  2329. len++;
  2330. }
  2331. void Alg_tracks::add_track(int track_num, Alg_time_map_ptr time_map,
  2332. bool seconds)
  2333. // Create a new track at index track_num.
  2334. // If track already exists, this call does nothing.
  2335. // If highest previous track is not at track_num-1, then
  2336. // create tracks at len, len+1, ..., track_num.
  2337. {
  2338. assert(track_num >= 0);
  2339. if (track_num == maxlen) {
  2340. // use eponential growth to insert tracks sequentially
  2341. expand();
  2342. } else if (track_num > maxlen) {
  2343. // grow to exact size for random inserts
  2344. expand_to(track_num + 1);
  2345. }
  2346. if (track_num < len) return; // don't add if already there
  2347. while (len <= track_num) {
  2348. tracks[len] = new Alg_track(time_map, seconds);
  2349. //printf("allocated track at %d (%x, this %x) = %x\n", len,
  2350. // &(tracks[len]), this, tracks[len]);
  2351. len++;
  2352. }
  2353. }
  2354. void Alg_tracks::reset()
  2355. {
  2356. // all track events are incorporated into the seq,
  2357. // so all we need to delete are the arrays of pointers
  2358. for (int i = 0; i < len; i++) {
  2359. // printf("deleting track at %d (%x, this %x) = %x\n", i, &(tracks[i]),
  2360. // this, tracks[i]);
  2361. delete tracks[i];
  2362. }
  2363. if (tracks) delete [] tracks;
  2364. tracks = nullptr;
  2365. len = 0;
  2366. maxlen = 0;
  2367. }
  2368. void Alg_tracks::set_in_use(bool flag)
  2369. {
  2370. for (int i = 0; i < len; i++) {
  2371. tracks[i]->in_use = flag;
  2372. }
  2373. }
  2374. void Alg_iterator::expand_to(int new_max)
  2375. {
  2376. maxlen = new_max;
  2377. Alg_pending_event_ptr new_pending_events = new Alg_pending_event[maxlen];
  2378. // now do copy
  2379. memcpy(new_pending_events, pending_events,
  2380. len * sizeof(Alg_pending_event));
  2381. if (pending_events) {
  2382. delete[] pending_events;
  2383. }
  2384. pending_events = new_pending_events;
  2385. }
  2386. void Alg_iterator::expand()
  2387. {
  2388. maxlen = (maxlen + 5); // extra growth for small sizes
  2389. maxlen += (maxlen >> 2); // add 25%
  2390. expand_to(maxlen);
  2391. }
  2392. Alg_iterator::~Alg_iterator()
  2393. {
  2394. if (pending_events) {
  2395. delete[] pending_events;
  2396. }
  2397. }
  2398. /* in the heap, the children of N are (N+1)*2 and (N+1)*2-1, so
  2399. * the parent of N is (N+1)/2-1. This would be easier if arrays
  2400. * were 1-based instead of 0-based
  2401. */
  2402. #define HEAP_PARENT(loc) ((((loc) + 1) / 2) - 1)
  2403. #define FIRST_CHILD(loc) (((loc) * 2) + 1)
  2404. void Alg_iterator::show()
  2405. {
  2406. for (int i = 0; i < len; i++) {
  2407. Alg_pending_event_ptr p = &(pending_events[i]);
  2408. printf(" %d: %p[%ld]@%g on %d\n", i, p->events, p->index,
  2409. p->offset, p->note_on);
  2410. }
  2411. }
  2412. bool Alg_iterator::earlier(int i, int j)
  2413. // see if event i is earlier than event j
  2414. {
  2415. // note-offs are scheduled ALG_EPS early so that if a note-off is
  2416. // followed immediately with the same timestamp by a note-on (common
  2417. // in MIDI files), the note-off will be scheduled first
  2418. double t_i = pending_events[i].time;
  2419. double t_j = pending_events[j].time;
  2420. if (t_i < t_j) return true;
  2421. // not sure if this case really exists or this is the best rule, but
  2422. // we want to give precedence to note-off events
  2423. else if (t_i == t_j && pending_events[j].note_on) return true;
  2424. return false;
  2425. }
  2426. void Alg_iterator::insert(Alg_events_ptr events, long index,
  2427. bool note_on, void *cookie, double offset)
  2428. {
  2429. if (len == maxlen) expand();
  2430. pending_events[len].events = events;
  2431. pending_events[len].index = index;
  2432. pending_events[len].note_on = note_on;
  2433. pending_events[len].cookie = cookie;
  2434. pending_events[len].offset = offset;
  2435. Alg_event_ptr event = (*events)[index];
  2436. pending_events[len].time = (note_on ? event->time :
  2437. event->get_end_time() - ALG_EPS) + offset;
  2438. /* BEGIN DEBUG *
  2439. printf("insert %p=%p[%d] @ %g\n", event, events, index,
  2440. pending_events[len].time);
  2441. printf(" is_note %d note_on %d time %g dur %g end_time %g offset %g\n",
  2442. event->is_note(), note_on, event->time, event->get_duration(),
  2443. event->get_end_time(), offset);
  2444. }
  2445. * END DEBUG */
  2446. int loc = len;
  2447. int loc_parent = HEAP_PARENT(loc);
  2448. len++;
  2449. // sift up:
  2450. while (loc > 0 &&
  2451. earlier(loc, loc_parent)) {
  2452. // swap loc with loc_parent
  2453. Alg_pending_event temp = pending_events[loc];
  2454. pending_events[loc] = pending_events[loc_parent];
  2455. pending_events[loc_parent] = temp;
  2456. loc = loc_parent;
  2457. loc_parent = HEAP_PARENT(loc);
  2458. }
  2459. }
  2460. bool Alg_iterator::remove_next(Alg_events_ptr &events, long &index,
  2461. bool &note_on, void *&cookie,
  2462. double &offset, double &time)
  2463. {
  2464. if (len == 0) return false; // empty!
  2465. events = pending_events[0].events;
  2466. index = pending_events[0].index;
  2467. note_on = pending_events[0].note_on;
  2468. offset = pending_events[0].offset;
  2469. cookie = pending_events[0].cookie;
  2470. offset = pending_events[0].offset;
  2471. time = pending_events[0].time;
  2472. len--;
  2473. pending_events[0] = pending_events[len];
  2474. // sift down
  2475. long loc = 0;
  2476. long loc_child = FIRST_CHILD(loc);
  2477. while (loc_child < len) {
  2478. if (loc_child + 1 < len) {
  2479. if (earlier(loc_child + 1, loc_child)) {
  2480. loc_child++;
  2481. }
  2482. }
  2483. if (earlier(loc_child, loc)) {
  2484. Alg_pending_event temp = pending_events[loc];
  2485. pending_events[loc] = pending_events[loc_child];
  2486. pending_events[loc_child] = temp;
  2487. loc = loc_child;
  2488. loc_child = FIRST_CHILD(loc);
  2489. } else {
  2490. loc_child = len;
  2491. }
  2492. }
  2493. // printf("After remove:"); show();
  2494. return true;
  2495. }
  2496. Alg_seq::Alg_seq(const char *filename, bool smf, double *offset_ptr)
  2497. {
  2498. basic_initialization();
  2499. ifstream inf(filename, smf ? ios::binary | ios::in : ios::in);
  2500. if (inf.fail()) {
  2501. error = alg_error_open;
  2502. return;
  2503. }
  2504. if (smf) {
  2505. error = alg_smf_read(inf, this);
  2506. if (offset_ptr) *offset_ptr = 0.0;
  2507. } else {
  2508. error = alg_read(inf, this, offset_ptr);
  2509. }
  2510. inf.close();
  2511. }
  2512. Alg_seq::Alg_seq(istream &file, bool smf, double *offset_ptr)
  2513. {
  2514. basic_initialization();
  2515. if (smf) {
  2516. error = alg_smf_read(file, this);
  2517. if (offset_ptr) *offset_ptr = 0.0;
  2518. } else {
  2519. error = alg_read(file, this, offset_ptr);
  2520. }
  2521. }
  2522. void Alg_seq::seq_from_track(Alg_track_ref tr)
  2523. {
  2524. type = 's';
  2525. // copy everything
  2526. set_beat_dur(tr.get_beat_dur());
  2527. set_real_dur(tr.get_real_dur());
  2528. // copy time_map
  2529. set_time_map(new Alg_time_map(tr.get_time_map()));
  2530. units_are_seconds = tr.get_units_are_seconds();
  2531. if (tr.get_type() == 's') {
  2532. Alg_seq_ref s = *(tr.to_alg_seq());
  2533. channel_offset_per_track = s.channel_offset_per_track;
  2534. add_track(s.tracks() - 1);
  2535. // copy each track
  2536. for (int i = 0; i < tracks(); i++) {
  2537. Alg_track_ref from_track = *(s.track(i));
  2538. Alg_track_ref to_track = *(track(i));
  2539. to_track.set_beat_dur(from_track.get_beat_dur());
  2540. to_track.set_real_dur(from_track.get_real_dur());
  2541. if (from_track.get_units_are_seconds())
  2542. to_track.convert_to_seconds();
  2543. for (int j = 0; j < from_track.length(); j++) {
  2544. Alg_event_ptr event = copy_event(from_track[j]);
  2545. to_track.append(event);
  2546. }
  2547. }
  2548. } else if (tr.get_type() == 't') {
  2549. add_track(0);
  2550. channel_offset_per_track = 0;
  2551. Alg_track_ptr to_track = track(0);
  2552. to_track->set_beat_dur(tr.get_beat_dur());
  2553. to_track->set_real_dur(tr.get_real_dur());
  2554. for (int j = 0; j < tr.length(); j++) {
  2555. Alg_event_ptr event = copy_event(tr[j]);
  2556. to_track->append(event);
  2557. }
  2558. } else {
  2559. assert(false); // expected track or sequence
  2560. }
  2561. }
  2562. int Alg_seq::tracks()
  2563. {
  2564. return track_list.length();
  2565. }
  2566. Alg_track_ptr Alg_seq::track(int i)
  2567. {
  2568. assert(0 <= i && i < track_list.length());
  2569. return &(track_list[i]);
  2570. }
  2571. //#pragma warning(disable: 4715) // ok not to return a value here
  2572. Alg_event_ptr const &Alg_seq::operator[](int i)
  2573. {
  2574. int ntracks = track_list.length();
  2575. int tr = 0;
  2576. while (tr < ntracks) {
  2577. Alg_track *a_track = track(tr);
  2578. if (a_track && i < a_track->length()) {
  2579. return (*a_track)[i];
  2580. } else if (a_track) {
  2581. i -= a_track->length();
  2582. }
  2583. tr++;
  2584. }
  2585. throw std::out_of_range{"Alg_seq::operator[] - Index out of range."};
  2586. }
  2587. //#pragma warning(default: 4715)
  2588. void Alg_seq::convert_to_beats()
  2589. {
  2590. if (!units_are_seconds) return;
  2591. for (int i = 0; i < tracks(); i++) {
  2592. track(i)->convert_to_beats();
  2593. }
  2594. // note that the Alg_seq inherits units_are_seconds from an
  2595. // empty track. Each track also has a (redundant) field called
  2596. // units are seconds. These should always be consistent.
  2597. units_are_seconds = false;
  2598. }
  2599. void Alg_seq::convert_to_seconds()
  2600. {
  2601. if (units_are_seconds) return;
  2602. //printf("convert_to_seconds, tracks %d\n", tracks());
  2603. //printf("last_tempo of seq: %g on map %x\n",
  2604. // get_time_map()->last_tempo, get_time_map());
  2605. for (int i = 0; i < tracks(); i++) {
  2606. //printf("last_tempo of track %d: %g on %x\n", i,
  2607. // track(i)->get_time_map()->last_tempo,
  2608. // track(i)->get_time_map());
  2609. track(i)->convert_to_seconds();
  2610. }
  2611. // update our copy of last_note_off (which may or may not be valid)
  2612. last_note_off = time_map->beat_to_time(last_note_off);
  2613. // note that the Alg_seq inherits units_are_seconds from an
  2614. // empty track. Each track also has a (redundant) field called
  2615. // units are seconds. These should always be consistent.
  2616. units_are_seconds = true;
  2617. }
  2618. Alg_track_ptr Alg_seq::cut_from_track(int track_num, double start,
  2619. double dur, bool all)
  2620. {
  2621. assert(track_num >= 0 && track_num < tracks());
  2622. Alg_track_ptr tr = track(track_num);
  2623. return tr->cut(start, dur, all);
  2624. }
  2625. void Alg_seq::copy_time_sigs_to(Alg_seq *dest)
  2626. {
  2627. // copy time signatures
  2628. for (int i = 0; i < time_sig.length(); i++) {
  2629. dest->time_sig.insert(time_sig[i].beat, time_sig[i].num,
  2630. time_sig[i].den);
  2631. }
  2632. }
  2633. void Alg_seq::set_time_map(Alg_time_map *map)
  2634. {
  2635. Alg_track::set_time_map(map);
  2636. for (int i = 0; i < tracks(); i++) {
  2637. track(i)->set_time_map(map);
  2638. }
  2639. }
  2640. Alg_seq_ptr Alg_seq::cut(double start, double len, bool all)
  2641. // return sequence from start to start+len and modify this
  2642. // sequence by removing that time-span
  2643. {
  2644. double dur = get_dur();
  2645. // fix parameters to fall within existing sequence
  2646. if (start > dur) return nullptr; // nothing to cut
  2647. if (start < 0) start = 0; // can't start before sequence starts
  2648. if (start + len > dur) // can't cut after end:
  2649. len = dur - start;
  2650. Alg_seq_ptr result = new Alg_seq();
  2651. Alg_time_map_ptr map = new Alg_time_map(get_time_map());
  2652. result->set_time_map(map);
  2653. copy_time_sigs_to(result);
  2654. result->units_are_seconds = units_are_seconds;
  2655. result->track_list.reset();
  2656. for (int i = 0; i < tracks(); i++) {
  2657. Alg_track_ptr cut_track = cut_from_track(i, start, len, all);
  2658. result->track_list.append(cut_track);
  2659. // initially, result->last_note_off is zero. We want to know the
  2660. // maximum over all cut_tracks, so compute that here:
  2661. result->last_note_off = MAX(result->last_note_off,
  2662. cut_track->last_note_off);
  2663. // since we're moving to a new sequence, change the track's time_map
  2664. result->track_list[i].set_time_map(map);
  2665. }
  2666. // put units in beats to match time_sig's. Note that we need
  2667. // two different end times. For result, we want the time of the
  2668. // last note off, but for cutting out the time signatures in this,
  2669. // we use len.
  2670. double ts_start = start;
  2671. double ts_end = start + len;
  2672. double ts_dur = dur;
  2673. double ts_last_note_off = start + result->last_note_off;
  2674. if (units_are_seconds) {
  2675. ts_start = time_map->time_to_beat(ts_start);
  2676. ts_end = time_map->time_to_beat(ts_end);
  2677. ts_last_note_off = time_map->time_to_beat(ts_last_note_off);
  2678. ts_dur = time_map->time_to_beat(ts_dur);
  2679. }
  2680. // result is shifted from start to 0 and has length len, but
  2681. // time_sig and time_map are copies from this. Adjust time_sig,
  2682. // time_map, and duration fields in result. The time_sig and
  2683. // time_map data is retained out to last_note_off so that we have
  2684. // information for the entire duration of all the notes, even though
  2685. // this might extend beyond the duration of the track. (Warning:
  2686. // no info is retained for notes with negative times.)
  2687. result->time_sig.trim(ts_start, ts_last_note_off);
  2688. result->time_map->trim(start, start + result->last_note_off,
  2689. result->units_are_seconds);
  2690. // even though there might be notes sticking out beyond len, the
  2691. // track duration is len, not last_note_off. (Warning: if all is
  2692. // true, there may also be notes at negative offsets. These times
  2693. // cannot be mapped between beat and time representations, so there
  2694. // may be subtle bugs or unexpected behaviors in that case.)
  2695. result->set_dur(len);
  2696. // we sliced out a portion of each track, so now we need to
  2697. // slice out the corresponding sections of time_sig and time_map
  2698. // as well as to adjust the duration.
  2699. time_sig.cut(ts_start, ts_end, ts_dur);
  2700. time_map->cut(start, len, units_are_seconds);
  2701. set_dur(dur - len);
  2702. return result;
  2703. }
  2704. void Alg_seq::insert_silence_in_track(int track_num, double t, double len)
  2705. {
  2706. Alg_track_ptr tr = track(track_num);
  2707. tr->insert_silence(t, len);
  2708. }
  2709. void Alg_seq::insert_silence(double t, double len)
  2710. {
  2711. for (int i = 0; i < tracks(); i++) {
  2712. insert_silence_in_track(i, t, len);
  2713. }
  2714. double t_beats = t;
  2715. double len_beats = len;
  2716. // insert into time_sig array; use time_sig_paste,
  2717. // which requires us to build a simple time_sig array
  2718. if (units_are_seconds) {
  2719. time_map->insert_time(t, len);
  2720. t_beats = time_map->time_to_beat(t);
  2721. len_beats = time_map->time_to_beat(t + len) - t_beats;
  2722. } else {
  2723. time_map->insert_beats(t_beats, len_beats);
  2724. }
  2725. time_sig.insert_beats(t_beats, len_beats);
  2726. // Final duration is defined to be t + len + whatever was
  2727. // in the sequence after t (if any). This translates to
  2728. // t + len + max(dur - t, 0)
  2729. set_dur(t + len + MAX(get_dur() - t, 0.0));
  2730. }
  2731. Alg_track_ptr Alg_seq::copy_track(int track_num, double t, double len, bool all)
  2732. {
  2733. return track_list[track_num].copy(t, len, all);
  2734. }
  2735. Alg_seq *Alg_seq::copy(double start, double len, bool all)
  2736. {
  2737. // fix parameters to fall within existing sequence
  2738. if (start > get_dur()) return nullptr; // nothing to copy
  2739. if (start < 0) start = 0; // can't copy before sequence starts
  2740. if (start + len > get_dur()) // can't copy after end:
  2741. len = get_dur() - start;
  2742. // return (new) sequence from start to start + len
  2743. Alg_seq_ptr result = new Alg_seq();
  2744. Alg_time_map_ptr map = new Alg_time_map(get_time_map());
  2745. result->set_time_map(map);
  2746. copy_time_sigs_to(result);
  2747. result->units_are_seconds = units_are_seconds;
  2748. result->track_list.reset();
  2749. for (int i = 0; i < tracks(); i++) {
  2750. Alg_track_ptr copy = copy_track(i, start, len, all);
  2751. result->track_list.append(copy);
  2752. result->last_note_off = MAX(result->last_note_off,
  2753. copy->last_note_off);
  2754. // since we're copying to a new seq, change the track's time_map
  2755. result->track_list[i].set_time_map(map);
  2756. }
  2757. // put units in beats to match time_sig's. Note that we need
  2758. // two different end times. For result, we want the time of the
  2759. // last note off, but for cutting out the time signatures in this,
  2760. // we use len.
  2761. double ts_start = start;
  2762. double ts_end = start + len;
  2763. double ts_last_note_off = start + result->last_note_off;
  2764. if (units_are_seconds) {
  2765. ts_start = time_map->time_to_beat(ts_start);
  2766. ts_end = time_map->time_to_beat(ts_end);
  2767. ts_last_note_off = time_map->time_to_beat(ts_last_note_off);
  2768. }
  2769. result->time_sig.trim(ts_start, ts_last_note_off);
  2770. result->time_map->trim(start, start + result->last_note_off,
  2771. units_are_seconds);
  2772. result->set_dur(len);
  2773. return result;
  2774. }
  2775. void Alg_seq::paste(double start, Alg_seq *seq)
  2776. {
  2777. // Insert seq at time, opening up space for it.
  2778. // To manipulate time map, we need units as beats.
  2779. // Save original form so we can convert back if necessary.
  2780. bool units_should_be_seconds = units_are_seconds;
  2781. bool seq_units_should_be_seconds = seq->get_units_are_seconds();
  2782. if (units_are_seconds) {
  2783. start = time_map->time_to_beat(start);
  2784. convert_to_beats();
  2785. }
  2786. seq->convert_to_beats();
  2787. // do a paste on each track
  2788. int i;
  2789. for (i = 0; i < seq->tracks(); i++) {
  2790. if (i >= tracks()) {
  2791. add_track(i);
  2792. }
  2793. track(i)->paste(start, seq->track(i));
  2794. }
  2795. // make sure all tracks were opened up for an insert, even if
  2796. // there is nothing to insert
  2797. while (i < tracks()) {
  2798. track(i)->insert_silence(start, seq->get_dur());
  2799. i++;
  2800. }
  2801. // paste in tempo track
  2802. time_map->paste(start, seq);
  2803. // paste in time signatures
  2804. time_sig.paste(start, seq);
  2805. set_dur(get_beat_dur() + seq->get_dur());
  2806. assert(!seq->units_are_seconds && !units_are_seconds);
  2807. if (units_should_be_seconds) {
  2808. convert_to_seconds();
  2809. }
  2810. if (seq_units_should_be_seconds) {
  2811. seq->convert_to_seconds();
  2812. }
  2813. }
  2814. void Alg_seq::merge(double t, Alg_event_list_ptr seq)
  2815. {
  2816. // seq must be an Alg_seq:
  2817. assert(seq->get_type() == 's');
  2818. Alg_seq_ptr s = (Alg_seq_ptr) seq;
  2819. for (int i = 0; i < s->tracks(); i++) {
  2820. if (tracks() <= i) add_track(i);
  2821. track(i)->merge(t, s->track(i));
  2822. }
  2823. }
  2824. void Alg_seq::silence_track(int track_num, double start, double len, bool all)
  2825. {
  2826. // remove events in [time, time + len) and close gap
  2827. Alg_track_ptr tr = track(track_num);
  2828. tr->silence(start, len, all);
  2829. }
  2830. void Alg_seq::silence(double t, double len, bool all)
  2831. {
  2832. for (int i = 0; i < tracks(); i++) {
  2833. silence_track(i, t, len, all);
  2834. }
  2835. }
  2836. void Alg_seq::clear_track(int track_num, double start, double len, bool all)
  2837. {
  2838. // remove events in [time, time + len) and close gap
  2839. Alg_track_ptr tr = track(track_num);
  2840. tr->clear(start, len, all);
  2841. }
  2842. void Alg_seq::clear(double start, double len, bool all)
  2843. {
  2844. // Fix parameters to fall within existing sequence
  2845. double dur = get_dur();
  2846. if (start > dur) return; // nothing to cut
  2847. if (start < 0) start = 0; // can't start before sequence starts
  2848. if (start + len > dur) // can't cut after end:
  2849. len = dur - start;
  2850. for (int i = 0; i < tracks(); i++)
  2851. clear_track(i, start, len, all);
  2852. // Put units in beats to match time_sig's.
  2853. double ts_start = start;
  2854. double ts_end = start + len;
  2855. double ts_dur = dur;
  2856. if (units_are_seconds) {
  2857. ts_start = time_map->time_to_beat(ts_start);
  2858. ts_end = time_map->time_to_beat(ts_end);
  2859. ts_dur = time_map->time_to_beat(ts_dur);
  2860. }
  2861. // we sliced out a portion of each track, so now we need to
  2862. // slice out the corresponding sections of time_sig and time_map
  2863. // as well as to adjust the duration.
  2864. time_sig.cut(ts_start, ts_end, ts_dur);
  2865. time_map->cut(start, len, units_are_seconds);
  2866. set_dur(dur - len);
  2867. }
  2868. Alg_event_list_ptr Alg_seq::find_in_track(int track_num, double t, double len,
  2869. bool all, long channel_mask,
  2870. long event_type_mask)
  2871. {
  2872. return track(track_num)->find(t, len, all, channel_mask, event_type_mask);
  2873. }
  2874. Alg_seq::~Alg_seq()
  2875. {
  2876. int i, j;
  2877. // Tracks does not delete Alg_events elements
  2878. for (j = 0; j < track_list.length(); j++) {
  2879. Alg_track &notes = track_list[j];
  2880. // Alg_events does not delete notes
  2881. for (i = 0; i < notes.length(); i++) {
  2882. Alg_event_ptr event = notes[i];
  2883. delete event;
  2884. }
  2885. }
  2886. }
  2887. long Alg_seq::seek_time(double time, int track_num)
  2888. // find index of first score event after time
  2889. {
  2890. long i;
  2891. Alg_events &notes = track_list[track_num];
  2892. for (i = 0; i < notes.length(); i++) {
  2893. if (notes[i]->time > time) {
  2894. break;
  2895. }
  2896. }
  2897. return i;
  2898. }
  2899. bool Alg_seq::insert_beat(double time, double beat)
  2900. // insert a time,beat pair
  2901. // return true or false (false indicates an error, no update)
  2902. // it is an error to imply a negative tempo or to insert at
  2903. // a negative time
  2904. {
  2905. if (time < 0 || beat < 0) return false;
  2906. if (time == 0.0 && beat > 0)
  2907. time = 0.000001; // avoid infinite tempo, offset time by 1us
  2908. if (time == 0.0 && beat == 0.0)
  2909. return true; // (0,0) is already in the map!
  2910. convert_to_beats(); // beats are invariant when changing tempo
  2911. time_map->insert_beat(time, beat);
  2912. return true;
  2913. }
  2914. // input is time, return value is time
  2915. double Alg_seq::nearest_beat_time(double time, double *beat)
  2916. {
  2917. double b = time_map->time_to_beat(time);
  2918. b = time_sig.nearest_beat(b);
  2919. if (beat) *beat = b;
  2920. return time_map->beat_to_time(b);
  2921. }
  2922. bool Alg_seq::stretch_region(double b0, double b1, double dur)
  2923. {
  2924. bool units_should_be_seconds = units_are_seconds;
  2925. convert_to_beats();
  2926. bool result = time_map->stretch_region(b0, b1, dur);
  2927. if (units_should_be_seconds) convert_to_seconds();
  2928. return result;
  2929. }
  2930. bool Alg_seq::insert_tempo(double bpm, double beat)
  2931. {
  2932. double bps = bpm / 60.0; // convert to beats per second
  2933. // change the tempo at the given beat until the next beat event
  2934. if (beat < 0) return false;
  2935. convert_to_beats(); // beats are invariant when changing tempo
  2936. double time = time_map->beat_to_time(beat);
  2937. long i = time_map->locate_time(time);
  2938. if (i >= time_map->beats.len || !within(time_map->beats[i].time, time, 0.000001)) {
  2939. insert_beat(time, beat);
  2940. }
  2941. // now i is index of beat where tempo will change
  2942. if (i == time_map->beats.len - 1) {
  2943. time_map->last_tempo = bps;
  2944. time_map->last_tempo_flag = true;
  2945. } else { // adjust all future beats
  2946. // compute the difference in beats
  2947. double diff = time_map->beats[i + 1].beat - time_map->beats[i].beat;
  2948. // convert beat difference to seconds at new tempo
  2949. diff = diff / bps;
  2950. // figure out old time difference:
  2951. double old_diff = time_map->beats[i + 1].time - time;
  2952. // compute difference too
  2953. diff = diff - old_diff;
  2954. // apply new_diff to score and beats
  2955. while (i < time_map->beats.len) {
  2956. time_map->beats[i].time = time_map->beats[i].time + diff;
  2957. i++;
  2958. }
  2959. }
  2960. return true;
  2961. }
  2962. void Alg_seq::add_event(Alg_event_ptr event, int track_num)
  2963. // add_event puts an event in a given track (track_num).
  2964. // The track must exist. The time and duration of the
  2965. // event are interpreted according to whether the Alg_seq
  2966. // is currently in beats or seconds (see convert_to_beats())
  2967. {
  2968. track_list[track_num].insert(event);
  2969. /*
  2970. if (event->is_note()) {
  2971. Alg_note_ptr n = (Alg_note_ptr) event;
  2972. trace("note %d at %g for %g\n", n->get_identifier(), n->time, n->dur);
  2973. }
  2974. */
  2975. }
  2976. double Alg_seq::get_tempo(double beat)
  2977. {
  2978. return time_map->get_tempo(beat);
  2979. }
  2980. bool Alg_seq::set_tempo(double bpm, double start_beat, double end_beat)
  2981. // set tempo from start_beat to end_beat
  2982. {
  2983. // this is an optimization, the test is repeated in Alg_time_seq::set_tempo()
  2984. if (start_beat >= end_beat) return false;
  2985. bool units_should_be_seconds = units_are_seconds;
  2986. convert_to_beats();
  2987. double dur = get_dur();
  2988. bool result = time_map->set_tempo(bpm, start_beat, end_beat);
  2989. // preserve sequence duration in beats when tempo changes
  2990. set_dur(dur);
  2991. if (units_should_be_seconds) convert_to_seconds();
  2992. return result;
  2993. }
  2994. double Alg_seq::get_bar_len(double beat)
  2995. {
  2996. return time_sig.get_bar_len(beat);
  2997. }
  2998. void Alg_seq::set_time_sig(double beat, double num, double den)
  2999. {
  3000. time_sig.insert(beat, num, den);
  3001. }
  3002. void Alg_seq::beat_to_measure(double beat, long *measure, double *m_beat,
  3003. double *num, double *den)
  3004. {
  3005. // return [measure, beat, num, den]
  3006. double m = 0; // measure number
  3007. double bpm;
  3008. int tsx;
  3009. bpm = 4;
  3010. // assume 4/4 if no time signature
  3011. double prev_beat = 0;
  3012. double prev_num = 4;
  3013. double prev_den = 4;
  3014. if (beat < 0) beat = 0; // negative measures treated as zero
  3015. for (tsx = 0; tsx < time_sig.length(); tsx++) {
  3016. if (time_sig[tsx].beat <= beat) {
  3017. // round m up to an integer (but allow for a small
  3018. // numerical inaccuracy)
  3019. m = m + (long) (0.99 + (time_sig[tsx].beat - prev_beat) / bpm);
  3020. bpm = time_sig[tsx].num * 4 / time_sig[tsx].den;
  3021. prev_beat = time_sig[tsx].beat;
  3022. prev_num = time_sig[tsx].num;
  3023. prev_den = time_sig[tsx].den;
  3024. } else {
  3025. m = m + (beat - prev_beat) / bpm;
  3026. *measure = (long) m;
  3027. *m_beat = (m - *measure) * bpm;
  3028. *num = prev_num;
  3029. *den = prev_den;
  3030. return;
  3031. }
  3032. }
  3033. // if we didn't return yet, compute after last time signature
  3034. Alg_time_sig initial(0, 4, 4);
  3035. Alg_time_sig &prev = initial;
  3036. if (tsx > 0) { // use last time signature
  3037. prev = time_sig[time_sig.length() - 1];
  3038. }
  3039. bpm = prev.num * 4 / prev.den;
  3040. m = m + (beat - prev.beat) / bpm;
  3041. *measure = (long) m;
  3042. *m_beat = (m - *measure) * bpm;
  3043. *num = prev.num;
  3044. *den = prev.den;
  3045. }
  3046. /*
  3047. void Alg_seq::set_events(Alg_event_ptr *events, long len, long max)
  3048. {
  3049. convert_to_seconds(); // because notes are in seconds
  3050. notes.set_events(events, len, max);
  3051. }
  3052. */
  3053. void Alg_iterator::begin_seq(Alg_seq_ptr s, void *cookie, double offset)
  3054. {
  3055. // keep an array of indexes into tracks
  3056. // printf("new pending\n");
  3057. int i;
  3058. for (i = 0; i < s->track_list.length(); i++) {
  3059. if (s->track_list[i].length() > 0) {
  3060. insert(&(s->track_list[i]), 0, true, cookie, offset);
  3061. }
  3062. }
  3063. }
  3064. Alg_event_ptr Alg_iterator::next(bool *note_on, void **cookie_ptr,
  3065. double *offset_ptr, double end_time)
  3066. // return the next event in time from any track
  3067. {
  3068. bool on;
  3069. double when;
  3070. if (!remove_next(events_ptr, index, on, cookie, offset, when)) {
  3071. return nullptr;
  3072. }
  3073. if (note_on) *note_on = on;
  3074. Alg_event_ptr event = (*events_ptr)[index];
  3075. if (on) {
  3076. if (note_off_flag && event->is_note() &&
  3077. (end_time == 0 ||
  3078. (*events_ptr)[index]->get_end_time() + offset < end_time)) {
  3079. // this was a note-on, so insert pending note-off
  3080. insert(events_ptr, index, false, cookie, offset);
  3081. }
  3082. // for both note-ons and updates, insert next event (at index + 1)
  3083. // DO NOT INCREMENT index: it must be preserved for request_note_off()
  3084. if (index + 1 < events_ptr->length() &&
  3085. (end_time == 0 || // zero means ignore end time
  3086. // stop iterating when end time is reached
  3087. (*events_ptr)[index + 1]->time + offset < end_time)) {
  3088. insert(events_ptr, index + 1, true, cookie, offset);
  3089. }
  3090. }
  3091. if (cookie_ptr) *cookie_ptr = cookie;
  3092. if (offset_ptr) *offset_ptr = offset;
  3093. return event;
  3094. }
  3095. void Alg_iterator::request_note_off()
  3096. {
  3097. assert(index >= 0 && index < events_ptr->length());
  3098. insert(events_ptr, index, false, cookie, offset);
  3099. }
  3100. void Alg_iterator::end()
  3101. {
  3102. }
  3103. void Alg_seq::merge_tracks()
  3104. {
  3105. long sum = 0;
  3106. long i;
  3107. for (i = 0; i < track_list.length(); i++) {
  3108. sum = sum + track(i)->length();
  3109. }
  3110. // preallocate array for efficiency:
  3111. Alg_event_ptr *notes = new Alg_event_ptr[sum];
  3112. Alg_iterator iterator(this, false);
  3113. iterator.begin();
  3114. long notes_index = 0;
  3115. Alg_event_ptr event;
  3116. while ((event = iterator.next())) {
  3117. notes[notes_index++] = event;
  3118. }
  3119. track_list.reset(); // don't need them any more
  3120. add_track(0);
  3121. track(0)->set_events(notes, sum, sum);
  3122. iterator.end();
  3123. }
  3124. void Alg_seq::set_in_use(bool flag)
  3125. {
  3126. Alg_track::set_in_use(flag);
  3127. track_list.set_in_use(flag);
  3128. }
  3129. // sr_letter_to_type = {"i": 'Integer', "r": 'Real', "s": 'String',
  3130. // "l": 'Logical', "a": 'Symbol'}