📄 lilypondexporter.cpp
字号:
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: *//* Rosegarden A MIDI and audio sequencer and musical notation editor. This program is Copyright 2000-2007 Guillaume Laurent <glaurent@telegraph-road.org>, Chris Cannam <cannam@all-day-breakfast.com>, Richard Bown <richard.bown@ferventsoftware.com> The moral rights of Guillaume Laurent, Chris Cannam, and Richard Bown to claim authorship of this work have been asserted. This file is Copyright 2002 Hans Kieserman <hkieserman@mail.com> with heavy lifting from csoundio as it was on 13/5/2002. Numerous additions and bug fixes by Michael McIntyre <dmmcintyr@users.sourceforge.net> Some restructuring by Chris Cannam. Brain surgery to support LilyPond 2.x export by Heikki Junes. Other copyrights also apply to some parts of this work. Please see the AUTHORS file and individual file headers for details. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. See the file COPYING included with this distribution for more information.*/#include "LilypondExporter.h"#include <klocale.h>#include "misc/Debug.h"#include "misc/Strings.h"#include "document/ConfigGroups.h"#include "base/BaseProperties.h"#include "base/Composition.h"#include "base/Configuration.h"#include "base/Event.h"#include "base/Exception.h"#include "base/Instrument.h"#include "base/NotationTypes.h"#include "base/PropertyName.h"#include "base/Segment.h"#include "base/SegmentNotationHelper.h"#include "base/Sets.h"#include "base/Staff.h"#include "base/Studio.h"#include "base/Track.h"#include "base/NotationQuantizer.h"#include "document/RosegardenGUIDoc.h"#include "gui/application/RosegardenApplication.h"#include "gui/application/RosegardenGUIView.h"#include "gui/editors/notation/NotationProperties.h"#include "gui/editors/notation/NotationView.h"#include "gui/editors/guitar/Chord.h"#include "gui/general/ProgressReporter.h"#include "gui/widgets/CurrentProgressDialog.h"#include <kconfig.h>#include <kmessagebox.h>#include <qfileinfo.h>#include <qobject.h>#include <qregexp.h>#include <qstring.h>#include <qtextcodec.h>#include <kapplication.h>#include <sstream>namespace Rosegarden{using namespace BaseProperties;const PropertyName LilypondExporter::SKIP_PROPERTY = "LilypondExportSkipThisEvent";LilypondExporter::LilypondExporter(RosegardenGUIApp *parent, RosegardenGUIDoc *doc, std::string fileName) : ProgressReporter((QObject *)parent, "lilypondExporter"), m_doc(doc), m_fileName(fileName){ m_composition = &m_doc->getComposition(); m_studio = &m_doc->getStudio(); m_view = ((RosegardenGUIApp *)parent)->getView(); m_notationView = NULL; readConfigVariables();}LilypondExporter::LilypondExporter(NotationView *parent, RosegardenGUIDoc *doc, std::string fileName) : ProgressReporter((QObject *)parent, "lilypondExporter"), m_doc(doc), m_fileName(fileName){ m_composition = &m_doc->getComposition(); m_studio = &m_doc->getStudio(); m_view = NULL; m_notationView = ((NotationView *)parent); readConfigVariables();}voidLilypondExporter::readConfigVariables(void){ // grab config info KConfig *cfg = kapp->config(); cfg->setGroup(NotationViewConfigGroup); m_paperSize = cfg->readUnsignedNumEntry("lilypapersize", PAPER_A4); m_paperLandscape = cfg->readBoolEntry("lilypaperlandscape", false); m_fontSize = cfg->readUnsignedNumEntry("lilyfontsize", FONT_20); m_raggedBottom = cfg->readBoolEntry("lilyraggedbottom", false); m_exportSelection = cfg->readUnsignedNumEntry("lilyexportselection", EXPORT_NONMUTED_TRACKS); m_exportLyrics = cfg->readBoolEntry("lilyexportlyrics", true); m_exportMidi = cfg->readBoolEntry("lilyexportmidi", false); m_exportTempoMarks = cfg->readUnsignedNumEntry("lilyexporttempomarks", EXPORT_NONE_TEMPO_MARKS); m_exportPointAndClick = cfg->readBoolEntry("lilyexportpointandclick", false); m_exportBeams = cfg->readBoolEntry("lilyexportbeamings", false); m_exportStaffGroup = cfg->readBoolEntry("lilyexportstaffgroup", false); m_exportStaffMerge = cfg->readBoolEntry("lilyexportstaffmerge", false); m_lyricsHAlignment = cfg->readBoolEntry("lilylyricshalignment", LEFT_ALIGN); m_languageLevel = cfg->readUnsignedNumEntry("lilylanguage", LILYPOND_VERSION_2_6);}LilypondExporter::~LilypondExporter(){ // nothing}voidLilypondExporter::handleStartingEvents(eventstartlist &eventsToStart, std::ofstream &str){ eventstartlist::iterator m = eventsToStart.begin(); while (m != eventsToStart.end()) { try { Indication i(**m); if (i.getIndicationType() == Indication::Slur) { if ((*m)->get <Bool>(NotationProperties::SLUR_ABOVE)) str << "^( "; else str << "_( "; } else if (i.getIndicationType() == Indication::PhrasingSlur) { str << "\\( "; } else if (i.getIndicationType() == Indication::Crescendo) { str << "\\< "; } else if (i.getIndicationType() == Indication::Decrescendo) { str << "\\> "; } } catch (Event::BadType) { // Not an indication } catch (Event::NoData e) { std::cerr << "Bad indication: " << e.getMessage() << std::endl; } eventstartlist::iterator n(m); ++n; eventsToStart.erase(m); m = n; }}voidLilypondExporter::handleEndingEvents(eventendlist &eventsInProgress, const Segment::iterator &j, std::ofstream &str){ eventendlist::iterator k = eventsInProgress.begin(); while (k != eventsInProgress.end()) { eventendlist::iterator l(k); ++l; // Handle and remove all the relevant events in progress // This assumes all deferred events are indications try { Indication i(**k); timeT indicationEnd = (*k)->getNotationAbsoluteTime() + i.getIndicationDuration(); timeT eventEnd = (*j)->getNotationAbsoluteTime() + (*j)->getNotationDuration(); if (indicationEnd < eventEnd || ((i.getIndicationType() == Indication::Slur || i.getIndicationType() == Indication::PhrasingSlur) && indicationEnd == eventEnd)) { if (i.getIndicationType() == Indication::Slur) { str << ") "; } else if (i.getIndicationType() == Indication::PhrasingSlur) { str << "\\) "; } else if (i.getIndicationType() == Indication::Crescendo || i.getIndicationType() == Indication::Decrescendo) { str << "\\! "; } eventsInProgress.erase(k); } } catch (Event::BadType) { // not an indication } catch (Event::NoData e) { std::cerr << "Bad indication: " << e.getMessage() << std::endl; } k = l; }}std::stringLilypondExporter::convertPitchToLilyNote(int pitch, Accidental accidental, const Rosegarden::Key &key){ Pitch p(pitch, accidental); std::string lilyNote = ""; lilyNote += (char)tolower(p.getNoteName(key)); // std::cout << "lilyNote: " << lilyNote << std::endl; Accidental acc = p.getAccidental(key); if (acc == Accidentals::DoubleFlat) lilyNote += "eses"; else if (acc == Accidentals::Flat) lilyNote += "es"; else if (acc == Accidentals::Sharp) lilyNote += "is"; else if (acc == Accidentals::DoubleSharp) lilyNote += "isis"; return lilyNote;}std::stringLilypondExporter::composeLilyMark(std::string eventMark, bool stemUp){ std::string inStr = "", outStr = ""; std::string prefix = (stemUp) ? "_" : "^"; // shoot text mark straight through unless it's sf or rf if (Marks::isTextMark(eventMark)) { inStr = protectIllegalChars(Marks::getTextFromMark(eventMark)); if (inStr == "sf") { inStr = "\\sf"; } else if (inStr == "rf") { inStr = "\\rfz"; } else { inStr = "\\markup { \\italic " + inStr + " } "; } outStr = prefix + inStr; } else if (Marks::isFingeringMark(eventMark)) { // fingering marks: use markup syntax only for non-trivial fingerings inStr = protectIllegalChars(Marks::getFingeringFromMark(eventMark)); if (inStr != "0" && inStr != "1" && inStr != "2" && inStr != "3" && inStr != "4" && inStr != "5" && inStr != "+" ) { inStr = "\\markup { \\finger \"" + inStr + "\" } "; } outStr = prefix + inStr; } else { outStr = "-"; // use full \accent format for everything, even though some shortcuts // exist, for the sake of consistency if (eventMark == Marks::Accent) { outStr += "\\accent"; } else if (eventMark == Marks::Tenuto) { outStr += "\\tenuto"; } else if (eventMark == Marks::Staccato) { outStr += "\\staccato"; } else if (eventMark == Marks::Staccatissimo) { outStr += "\\staccatissimo"; } else if (eventMark == Marks::Marcato) { outStr += "\\marcato"; } else if (eventMark == Marks::Trill) { outStr += "\\trill"; } else if (eventMark == Marks::LongTrill) { // span trill up to the next note: // tweak the beginning of the next note using an invisible rest having zero length outStr += "\\startTrillSpan s4*0 \\stopTrillSpan"; } else if (eventMark == Marks::Turn) { outStr += "\\turn"; } else if (eventMark == Marks::Pause) { outStr += "\\fermata"; } else if (eventMark == Marks::UpBow) { outStr += "\\upbow"; } else if (eventMark == Marks::DownBow) { outStr += "\\downbow"; } else { outStr = ""; std::cerr << "LilypondExporter::composeLilyMark() - unhandled mark: " << eventMark << std::endl; } } return outStr;}std::stringLilypondExporter::indent(const int &column){ std::string outStr = ""; for (int c = 1; c <= column; c++) { outStr += " "; } return outStr;}std::stringLilypondExporter::protectIllegalChars(std::string inStr){ QString tmpStr = strtoqstr(inStr); tmpStr.replace(QRegExp("&"), "\\&");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -