diff options
-rw-r--r-- | muse2/muse/keyevent.cpp | 66 | ||||
-rw-r--r-- | muse2/muse/keyevent.h | 78 | ||||
-rw-r--r-- | muse2/muse/master/lmaster.cpp | 89 | ||||
-rw-r--r-- | muse2/muse/master/lmaster.h | 12 |
4 files changed, 144 insertions, 101 deletions
diff --git a/muse2/muse/keyevent.cpp b/muse2/muse/keyevent.cpp index de872c9c..865aef01 100644 --- a/muse2/muse/keyevent.cpp +++ b/muse2/muse/keyevent.cpp @@ -24,9 +24,8 @@ KeyList keymap; KeyList::KeyList() { - _key = 0; - insert(std::pair<const unsigned, KeyEvent*> (MAX_TICK+1, new KeyEvent(_key, 0))); -// _keySN = 1; + _key = KEY_C; + insert(std::pair<const unsigned, KeyEvent> (MAX_TICK+1, KeyEvent(_key, 0))); useList = true; } @@ -34,20 +33,20 @@ KeyList::KeyList() // add //--------------------------------------------------------- -void KeyList::add(unsigned tick, int key) +void KeyList::add(unsigned tick, key_enum key) { if (tick > MAX_TICK) tick = MAX_TICK; iKeyEvent e = upper_bound(tick); - if (tick == e->second->tick) - e->second->key = key; + if (tick == e->second.tick) + e->second.key = key; else { - KeyEvent* ne = e->second; - KeyEvent* ev = new KeyEvent(ne->key, ne->tick); - ne->key = key; - ne->tick = tick; - insert(std::pair<const unsigned, KeyEvent*> (tick, ev)); + KeyEvent& ne = e->second; + KeyEvent ev = KeyEvent(ne.key, ne.tick); + ne.key = key; + ne.tick = tick; + insert(std::pair<const unsigned, KeyEvent> (tick, ev)); } } @@ -60,7 +59,7 @@ void KeyList::dump() const printf("\nKeyList:\n"); for (ciKeyEvent i = begin(); i != end(); ++i) { printf("%6d %06d key %6d\n", - i->first, i->second->tick, i->second->key); + i->first, i->second.tick, i->second.key); } } @@ -71,26 +70,23 @@ void KeyList::dump() const void KeyList::clear() { - for (iKeyEvent i = begin(); i != end(); ++i) - delete i->second; KEYLIST::clear(); - insert(std::pair<const unsigned, KeyEvent*> (MAX_TICK+1, new KeyEvent(0, 0))); -// ++_keySN; + insert(std::pair<const unsigned, KeyEvent> (MAX_TICK+1, KeyEvent(_key, 0))); } //--------------------------------------------------------- -// key +// keyAtTick //--------------------------------------------------------- -int KeyList::key(unsigned tick) const +key_enum KeyList::keyAtTick(unsigned tick) const { if (useList) { ciKeyEvent i = upper_bound(tick); if (i == end()) { printf("no key at tick %d,0x%x\n", tick, tick); - return 1000; + return _key; } - return i->second->key; + return i->second.key; } else return _key; @@ -102,14 +98,12 @@ int KeyList::key(unsigned tick) const void KeyList::del(unsigned tick) { -// printf("KeyList::del(%d)\n", tick); iKeyEvent e = find(tick); if (e == end()) { printf("KeyList::del(%d): not found\n", tick); return; } del(e); -// ++_keySN; } void KeyList::del(iKeyEvent e) @@ -120,21 +114,19 @@ void KeyList::del(iKeyEvent e) printf("KeyList::del() HALLO\n"); return; } - ne->second->key = e->second->key; - ne->second->tick = e->second->tick; + ne->second.key = e->second.key; + ne->second.tick = e->second.tick; erase(e); -// ++_keySN; } //--------------------------------------------------------- // change //--------------------------------------------------------- -void KeyList::change(unsigned tick, int newkey) +void KeyList::change(unsigned tick, key_enum newkey) { iKeyEvent e = find(tick); - e->second->key = newkey; -// ++_keySN; + e->second.key = newkey; } //--------------------------------------------------------- @@ -156,10 +148,9 @@ void KeyList::change(unsigned tick, int newkey) // addKey //--------------------------------------------------------- -void KeyList::addKey(unsigned t, int key) +void KeyList::addKey(unsigned t, key_enum key) { add(t, key); -// ++_keySN; } //--------------------------------------------------------- @@ -169,7 +160,6 @@ void KeyList::addKey(unsigned t, int key) void KeyList::delKey(unsigned tick) { del(tick); -// ++_keySN; } //--------------------------------------------------------- @@ -190,7 +180,6 @@ bool KeyList::setMasterFlag(unsigned /*tick*/, bool val) { if (useList != val) { useList = val; -// ++_keySN; return true; } return false; @@ -206,7 +195,7 @@ void KeyList::write(int level, Xml& xml) const { xml.put(level++, "<keylist fix=\"%d\">", _key); for (ciKeyEvent i = begin(); i != end(); ++i) - i->second->write(level, xml, i->first); + i->second.write(level, xml, i->first); xml.tag(level, "/keylist"); } @@ -225,23 +214,22 @@ void KeyList::read(Xml& xml) return; case Xml::TagStart: if (tag == "key") { - KeyEvent* t = new KeyEvent(); - unsigned tick = t->read(xml); + KeyEvent t; + unsigned tick = t.read(xml); iKeyEvent pos = find(tick); if (pos != end()) erase(pos); - insert(std::pair<const int, KeyEvent*> (tick, t)); + insert(std::pair<const int, KeyEvent> (tick, t)); } else xml.unknown("keyList"); break; case Xml::Attribut: if (tag == "fix") - _key = xml.s2().toInt(); + _key = key_enum(xml.s2().toInt()); break; case Xml::TagEnd: if (tag == "keylist") { - //++_keySN; return; } default: @@ -280,7 +268,7 @@ int KeyEvent::read(Xml& xml) if (tag == "tick") tick = xml.parseInt(); else if (tag == "val") - key = xml.parseInt(); + key = key_enum(xml.parseInt()); else xml.unknown("KeyEvent"); break; diff --git a/muse2/muse/keyevent.h b/muse2/muse/keyevent.h index cf475a52..4a7fc8f9 100644 --- a/muse2/muse/keyevent.h +++ b/muse2/muse/keyevent.h @@ -16,64 +16,78 @@ class Xml; +//don't change this enum! changing the numeric values will affect +//all files using key_enum, and even worse: +//PREVIOUSLY SAVED FILES WILL BE CORRUPT because the keys are +//stored as integers. when the integer -> key mapping changes +//(by inserting or removing elements, for example), this will +//break stuff! (flo) +enum key_enum +{ + KEY_SHARP_BEGIN, + KEY_C, // C or am, uses # for "black keys" + KEY_G, + KEY_D, + KEY_A, + KEY_E, + KEY_B, // or H in german. + KEY_FIS, //replaces F with E# + KEY_SHARP_END, + KEY_B_BEGIN, + KEY_C_B, // the same as C, but uses b for "black keys" + KEY_F, + KEY_BES, // or B in german + KEY_ES, + KEY_AS, + KEY_DES, + KEY_GES, //sounds like FIS, but uses b instead of # + KEY_B_END +}; + + + + //--------------------------------------------------------- -// Tempo Event +// Key Event //--------------------------------------------------------- struct KeyEvent { - int key; - unsigned tick; // new tempo at tick - //unsigned frame; // precomputed time for tick in sec + key_enum key; + unsigned tick; int read(Xml&); void write(int, Xml&, int) const; KeyEvent() { } - KeyEvent(unsigned k, unsigned tk) { + KeyEvent(key_enum k, unsigned tk) { key = k; tick = tk; - //frame = 0; } }; //--------------------------------------------------------- -// TempoList +// KeyList //--------------------------------------------------------- -typedef std::map<unsigned, KeyEvent*, std::less<unsigned> > KEYLIST; +typedef std::map<unsigned, KeyEvent, std::less<unsigned> > KEYLIST; typedef KEYLIST::iterator iKeyEvent; typedef KEYLIST::const_iterator ciKeyEvent; typedef KEYLIST::reverse_iterator riKeyEvent; typedef KEYLIST::const_reverse_iterator criKeyEvent; + + class KeyList : public KEYLIST { -// int _keySN; // serial no to track key changes bool useList; - int _key; // key if not using key list + key_enum _key; // key if not using key list - void add(unsigned tick, int tempo); - void change(unsigned tick, int newKey); + void add(unsigned tick, key_enum tempo); + void change(unsigned tick, key_enum newKey); void del(iKeyEvent); void del(unsigned tick); public: - enum keyList { - keyC, - keyCis, - keyD, - keyDis, - keyE, - keyF, - keyFis, - keyG, - keyGis, - keyA, - keyB, - keyBes, - }; - - KeyList(); void clear(); @@ -81,13 +95,11 @@ class KeyList : public KEYLIST { void write(int, Xml&) const; void dump() const; - int key(unsigned tick) const; + key_enum keyAtTick(unsigned tick) const; - //int keySN() const { return _keySN; } -// void setKey(unsigned tick, int newKey); - void addKey(unsigned t, int newKey); + void addKey(unsigned t, key_enum newKey); void delKey(unsigned tick); -// void changeKey(unsigned tick, int newKey); +// void changeKey(unsigned tick, key_enum newKey); bool setMasterFlag(unsigned tick, bool val); }; diff --git a/muse2/muse/master/lmaster.cpp b/muse2/muse/master/lmaster.cpp index 8b2d84a8..412360e3 100644 --- a/muse2/muse/master/lmaster.cpp +++ b/muse2/muse/master/lmaster.cpp @@ -39,11 +39,53 @@ #define LMASTER_MSGBOX_STRING "MusE: List Editor" +//don't remove or insert new elements in keyStrs. +//only renaming (keeping the semantic sense) is allowed! (flo( QStringList keyStrs = QStringList() - << "C" << "C#" << "D" << "D#"<< "E" << "F" - << "F#"<< "G"<< "G#"<< "A"<< "B"<< "B#"; - - + << "C (sharps)" << "G" << "D" << "A"<< "E" << "B" << "F#" + << "C (flats)"<< "F"<< "Bb" << "Eb"<< "Ab"<< "Db"<< "Gb"; + +//don't change this function (except when renaming stuff) +key_enum stringToKey(QString input) //flo +{ + int index = keyStrs.indexOf(input); + key_enum map[]={KEY_C, KEY_G, KEY_D, KEY_A, KEY_E, KEY_B, KEY_FIS, KEY_C_B, KEY_F, KEY_BES, KEY_ES, KEY_AS, KEY_DES, KEY_GES}; + return map[index]; +} + +//don't change this function (except when renaming stuff) +QString keyToString(key_enum key) //flo +{ + int index; + switch(key) + { + case KEY_C: index= 0; break; + case KEY_G: index= 1; break; + case KEY_D: index= 2; break; + case KEY_A: index= 3; break; + case KEY_E: index= 4; break; + case KEY_B: index= 5; break; + case KEY_FIS: index= 6; break; + case KEY_C_B: index= 7; break; + case KEY_F: index= 8; break; + case KEY_BES: index= 9; break; + case KEY_ES: index=10; break; + case KEY_AS: index=11; break; + case KEY_DES: index=12; break; + case KEY_GES: index=13; break; + + case KEY_SHARP_BEGIN: + case KEY_SHARP_END: + case KEY_B_BEGIN: + case KEY_B_END: + printf("ILLEGAL FUNCTION CALL: keyToString called with key_sharp_begin etc.\n"); + break; + + default: + printf("ILLEGAL FUNCTION CALL: keyToString called with illegal key value (not in enum)\n"); + } + return keyStrs[index]; +} //--------------------------------------------------------- // closeEvent @@ -201,7 +243,7 @@ void LMaster::insertTempo(const TEvent* ev) new LMasterTempoItem(view, ev); } -void LMaster::insertKey(const KeyEvent* ev) +void LMaster::insertKey(const KeyEvent& ev) { new LMasterKeyEventItem(view, ev); } @@ -250,32 +292,32 @@ void LMaster::updateList() ++it; } - else if (ik != k->rend() && (is == s->rend() && (ik->second->tick >= it->second->tick) - || (it == t->rend() && ik->second->tick >= is->second->tick ))) {// ik biggest + else if (ik != k->rend() && (is == s->rend() && (ik->second.tick >= it->second->tick) + || (it == t->rend() && ik->second.tick >= is->second->tick ))) {// ik biggest insertKey(ik->second); ++ik; } else if (is != s->rend() && (ik == k->rend() && (is->second->tick >= it->second->tick) - || (it == t->rend() && is->second->tick >= ik->second->tick ))) {// is biggest + || (it == t->rend() && is->second->tick >= ik->second.tick ))) {// is biggest insertSig(is->second); ++is; } else if (it != t->rend() && (ik == k->rend() && (it->second->tick >= is->second->tick) - || (is == s->rend() && it->second->tick >= ik->second->tick ))) {// it biggest + || (is == s->rend() && it->second->tick >= ik->second.tick ))) {// it biggest insertTempo(it->second); ++it; } - else if (ik != k->rend() && ik->second->tick >= is->second->tick && ik->second->tick >= it->second->tick) {// ik biggest + else if (ik != k->rend() && ik->second.tick >= is->second->tick && ik->second.tick >= it->second->tick) {// ik biggest insertKey(ik->second); ++ik; } - else if (is != s->rend() && is->second->tick >= it->second->tick && is->second->tick >= ik->second->tick) { // is biggest + else if (is != s->rend() && is->second->tick >= it->second->tick && is->second->tick >= ik->second.tick) { // is biggest insertSig(is->second); ++is; } - else if (it != t->rend() && it->second->tick >= is->second->tick && it->second->tick >= ik->second->tick) { // it biggest + else if (it != t->rend() && it->second->tick >= is->second->tick && it->second->tick >= ik->second.tick) { // it biggest insertTempo(it->second); ++it; } @@ -467,8 +509,10 @@ void LMaster::itemDoubleClicked(QTreeWidgetItem* i) } else if (editedItem->getType() == LMASTER_KEYEVENT) { if (!key_editor) + { key_editor = new QComboBox(view->viewport()); - key_editor->addItems(keyStrs); + key_editor->addItems(keyStrs); + } //key_editor->setText(editedItem->text(LMASTER_VAL_COL)); key_editor->setCurrentIndex(keyStrs.indexOf(editedItem->text(LMASTER_VAL_COL))); key_editor->setGeometry(itemRect); @@ -602,7 +646,7 @@ void LMaster::returnPressed() } else if (editedItem->getType() == LMASTER_KEYEVENT) { LMasterKeyEventItem* k = (LMasterKeyEventItem*) editedItem; - int key = k->key(); + key_enum key = k->key(); // song->startUndo(); // audio->msgDeleteTempo(oldtick, tempo, false); // audio->msgAddTempo(newtick, tempo, false); @@ -662,9 +706,9 @@ void LMaster::returnPressed() key_editor->hide(); repaint(); LMasterKeyEventItem* e = (LMasterKeyEventItem*) editedItem; - const KeyEvent* t = e->getEvent(); - unsigned tick = t->tick; - int key = keyStrs.indexOf(input); + const KeyEvent& t = e->getEvent(); + unsigned tick = t.tick; + key_enum key = stringToKey(input); if (!editingNewItem) { // song->startUndo(); @@ -722,11 +766,11 @@ QString LMasterLViewItem::text(int column) const // LMasterKeyEventItem //! Initializes a LMasterKeyEventItem with a KeyEvent //--------------------------------------------------------- -LMasterKeyEventItem::LMasterKeyEventItem(QTreeWidget* parent, const KeyEvent* ev) +LMasterKeyEventItem::LMasterKeyEventItem(QTreeWidget* parent, const KeyEvent& ev) : LMasterLViewItem(parent) { keyEvent = ev; - unsigned t = ev->tick; + unsigned t = ev.tick; int bar, beat; unsigned tick; AL::sigmap.tickValues(t, &bar, &beat, &tick); @@ -738,8 +782,8 @@ LMasterKeyEventItem::LMasterKeyEventItem(QTreeWidget* parent, const KeyEvent* ev int msec = int((time - (min*60 + sec)) * 1000.0); c2.sprintf("%03d:%02d:%03d", min, sec, msec); c3 = "Key"; - //int dt = ev->key; - c4 = keyStrs[ev->key]; + //int dt = ev.key; + c4 = keyToString(ev.key); setText(0, c1); setText(1, c2); setText(2, c3); @@ -872,8 +916,7 @@ void LMaster::insertKey() //int newTick = AL::sigmap.bar2tick(m, b, t); int newTick = song->cpos(); - KeyEvent* ev = new KeyEvent(lastKey->key(), newTick); - new LMasterKeyEventItem(view, ev); + new LMasterKeyEventItem(view, KeyEvent(lastKey->key(), newTick)); QTreeWidgetItem* newKeyItem = view->topLevelItem(0); editingNewItem = true; // State diff --git a/muse2/muse/master/lmaster.h b/muse2/muse/master/lmaster.h index 925b5837..23c86c82 100644 --- a/muse2/muse/master/lmaster.h +++ b/muse2/muse/master/lmaster.h @@ -81,14 +81,14 @@ class LMasterTempoItem : public LMasterLViewItem { class LMasterKeyEventItem : public LMasterLViewItem { private: - const KeyEvent* keyEvent; + KeyEvent keyEvent; public: - LMasterKeyEventItem(QTreeWidget* parent, const KeyEvent* t); + LMasterKeyEventItem(QTreeWidget* parent, const KeyEvent& t); virtual LMASTER_LVTYPE getType() { return LMASTER_KEYEVENT; } - const KeyEvent* getEvent() { return keyEvent; } - virtual unsigned tick() { return keyEvent->tick; } - int key() { return keyEvent->key; } + const KeyEvent& getEvent() { return keyEvent; } + virtual unsigned tick() { return keyEvent.tick; } + key_enum key() { return keyEvent.key; } }; //--------------------------------------------------------- // LMasterTempoItem @@ -125,7 +125,7 @@ class LMaster : public MidiEditor { void updateList(); void insertTempo(const TEvent*); void insertSig(const SigEvent*); - void insertKey(const KeyEvent*); + void insertKey(const KeyEvent&); LMasterLViewItem* getItemAtPos(unsigned tick, LMASTER_LVTYPE t); void initShortcuts(); QLineEdit* editor; |