12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073 |
- /* ----------------------------------------------------------------- */
- /* The HMM-Based Singing Voice Synthesis System "Sinsy" */
- /* developed by Sinsy Working Group */
- /* http://sinsy.sourceforge.net/ */
- /* ----------------------------------------------------------------- */
- /* */
- /* Copyright (c) 2009-2015 Nagoya Institute of Technology */
- /* Department of Computer Science */
- /* */
- /* All rights reserved. */
- /* */
- /* Redistribution and use in source and binary forms, with or */
- /* without modification, are permitted provided that the following */
- /* conditions are met: */
- /* */
- /* - Redistributions of source code must retain the above copyright */
- /* notice, this list of conditions and the following disclaimer. */
- /* - Redistributions in binary form must reproduce the above */
- /* copyright notice, this list of conditions and the following */
- /* disclaimer in the documentation and/or other materials provided */
- /* with the distribution. */
- /* - Neither the name of the Sinsy working group nor the names of */
- /* its contributors may be used to endorse or promote products */
- /* derived from this software without specific prior written */
- /* permission. */
- /* */
- /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND */
- /* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, */
- /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
- /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
- /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS */
- /* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, */
- /* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
- /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */
- /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
- /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, */
- /* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY */
- /* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
- /* POSSIBILITY OF SUCH DAMAGE. */
- /* ----------------------------------------------------------------- */
- #include <fstream>
- #include "sinsy.h"
- #include "util_log.h"
- #include "Converter.h"
- #include "TempScore.h"
- #include "XmlReader.h"
- #include "XmlWriter.h"
- #include "InputFile.h"
- #include "OutputFile.h"
- #include "WritableStrStream.h"
- #ifdef HAVE_HTS
- #include "LabelStream.h"
- #include "LabelStrings.h"
- #include "LabelMaker.h"
- #include "HtsEngine.h"
- #include "SynthConditionImpl.h"
- #endif
- #include "ScorePosition.h"
- #include "ScoreDoctor.h"
- #include "util_score.h"
- namespace sinsy
- {
- namespace
- {
- const std::string DEFAULT_LANGUAGES = "j";
- class ScoreConverter : public IScoreWritable
- {
- public:
- //! constructor
- explicit ScoreConverter(IScore& s);
- //! destructor
- virtual ~ScoreConverter();
- //! set encoding
- virtual void setEncoding(const std::string& encoding);
- //! change tempo
- virtual void changeTempo(double tempo);
- //! change beat
- virtual void changeBeat(const Beat& beat);
- //! change dynamics
- virtual void changeDynamics(const Dynamics& dynamics);
- //! change key
- virtual void changeKey(const Key& key);
- //! change divisions (duration of a quarter note)
- virtual void changeDivisions(size_t d);
- //! start crescendo
- virtual void startCrescendo();
- //! start diminuendo
- virtual void startDiminuendo();
- //! stop crescendo
- virtual void stopCrescendo();
- //! stop diminuendo
- virtual void stopDiminuendo();
- //! add note
- virtual void addNote(const Note& note);
- private:
- //! copy constructor (donot use)
- ScoreConverter(const ScoreConverter&);
- //! assignment operator (donot use)
- ScoreConverter& operator=(const ScoreConverter&);
- //! score
- IScore& score;
- //! divisions
- size_t divisions;
- };
- /*!
- constructor
- */
- ScoreConverter::ScoreConverter(IScore& s) : score(s), divisions(BASE_DIVISIONS)
- {
- }
- /*!
- destructor
- */
- ScoreConverter::~ScoreConverter()
- {
- }
- /*!
- set encoding
- */
- void ScoreConverter::setEncoding(const std::string& encoding)
- {
- if (!score.setEncoding(encoding)) {
- ERR_MSG("Cannot set encoding : " << encoding);
- throw std::invalid_argument("ScoreConverter::setEncoding()");
- }
- }
- /*!
- change tempo
- */
- void ScoreConverter::changeTempo(double tempo)
- {
- if (!score.addTempoMark(tempo)) {
- ERR_MSG("Cannot change tempo : " << tempo);
- throw std::invalid_argument("ScoreConverter::changeTempo()");
- }
- }
- /*!
- change beat
- */
- void ScoreConverter::changeBeat(const Beat& beat)
- {
- if (!score.addBeatMark(beat.getBeats(), beat.getBeatType())) {
- ERR_MSG("Cannot change beat : " << beat);
- throw std::invalid_argument("ScoreConverter::changeBeat()");
- }
- }
- /*!
- change dynamics
- */
- void ScoreConverter::changeDynamics(const Dynamics& dynamics)
- {
- SuddenDynamicsType d(SUDDENDYNAMICSTYPE_N);
- if (dynamics == Dynamics::PPPP) {
- d = SUDDENDYNAMICSTYPE_PPPP;
- } else if (dynamics == Dynamics::PPP) {
- d = SUDDENDYNAMICSTYPE_PPP;
- } else if (dynamics == Dynamics::PP) {
- d = SUDDENDYNAMICSTYPE_PP;
- } else if (dynamics == Dynamics::P) {
- d = SUDDENDYNAMICSTYPE_P;
- } else if (dynamics == Dynamics::MP) {
- d = SUDDENDYNAMICSTYPE_MP;
- } else if (dynamics == Dynamics::N) {
- d = SUDDENDYNAMICSTYPE_N;
- } else if (dynamics == Dynamics::MF) {
- d = SUDDENDYNAMICSTYPE_MF;
- } else if (dynamics == Dynamics::F) {
- d = SUDDENDYNAMICSTYPE_F;
- } else if (dynamics == Dynamics::FF) {
- d = SUDDENDYNAMICSTYPE_FF;
- } else if (dynamics == Dynamics::FFF) {
- d = SUDDENDYNAMICSTYPE_FFF;
- } else if (dynamics == Dynamics::FFFF) {
- d = SUDDENDYNAMICSTYPE_FFFF;
- } else { // fail safe
- ERR_MSG("Error in API " << FUNC_NAME(dynamics) << " : unknown type");
- throw std::invalid_argument("ScoreConverter::changeDynamics() unknown type");
- }
- if (!score.addSuddenDynamicsMark(d)) {
- ERR_MSG("Error in API " << FUNC_NAME(dynamics) << " : Cannot add sudden dynamics mark");
- throw std::invalid_argument("ScoreConverter::changeDynamics()");
- }
- }
- /*!
- change key
- */
- void ScoreConverter::changeKey(const Key& key)
- {
- const Mode& mode(key.getMode());
- ModeType m(MODETYPE_MAJOR);
- if (mode == Mode::MAJOR) {
- m = MODETYPE_MAJOR;
- } else if (mode == Mode::MINOR) {
- m = MODETYPE_MINOR;
- } else { // fail safe
- ERR_MSG("Cannot change key : " << key);
- throw std::invalid_argument("ScoreConverter::changeKey() unknown mode type");
- }
- if (!score.addKeyMark(m, key.getFifths())) {
- ERR_MSG("Cannot change key : " << key);
- throw std::invalid_argument("ScoreConverter::changeKey()");
- }
- }
- /*!
- change divisions (duration of a quarter note)
- */
- void ScoreConverter::changeDivisions(size_t d)
- {
- if (d <= 0) {
- ERR_MSG("Cannot change divisions : " << d);
- std::invalid_argument("ScoreConverter::changeDivisions()");
- }
- divisions = d;
- }
- /*!
- start crescendo
- */
- void ScoreConverter::startCrescendo()
- {
- if (!score.addGradualDynamicsMark(GRADUALDYNAMICSTYPE_CRESCENDO_BEGIN)) {
- ERR_MSG("Cannot start crescendo");
- throw std::invalid_argument("ScoreConverter::startCrescendo()");
- }
- }
- /*!
- start diminuendo
- */
- void ScoreConverter::startDiminuendo()
- {
- if (!score.addGradualDynamicsMark(GRADUALDYNAMICSTYPE_DIMINUENDO_BEGIN)) {
- ERR_MSG("Cannot start diminuendo");
- throw std::invalid_argument("ScoreConverter::startDiminuendo()");
- }
- }
- /*!
- stop crescendo
- */
- void ScoreConverter::stopCrescendo()
- {
- if (!score.addGradualDynamicsMark(GRADUALDYNAMICSTYPE_CRESCENDO_END)) {
- ERR_MSG("Cannot stop crescendo");
- throw std::invalid_argument("ScoreConverter::stopCrescendo()");
- }
- }
- /*!
- stop diminuendo
- */
- void ScoreConverter::stopDiminuendo()
- {
- if (!score.addGradualDynamicsMark(GRADUALDYNAMICSTYPE_DIMINUENDO_END)) {
- ERR_MSG("Cannot stop diminuendo");
- throw std::invalid_argument("ScoreConverter::stopDiminuendo()");
- }
- }
- /*!
- add note
- */
- void ScoreConverter::addNote(const Note& note)
- {
- if (note.isRest()) { // rest
- if (!score.addRest(note.getDuration() * BASE_DIVISIONS / divisions)) {
- ERR_MSG("Cannot add rest : " << note);
- throw std::invalid_argument("ScoreConverter::addNote()");
- }
- } else { // pitch
- Pitch pitch(note.getPitch());
- TieType tieType(TIETYPE_NONE);
- if (note.isTieStart()) {
- tieType = TIETYPE_BEGIN;
- } else if (note.isTieStop()) {
- tieType = TIETYPE_END;
- }
- SlurType slurType(SLURTYPE_NONE);
- if (note.isSlurStart()) {
- slurType = SLURTYPE_BEGIN;
- } else if (note.isSlurStop()) {
- slurType = SLURTYPE_END;
- }
- SyllabicType syllabicType(SYLLABICTYPE_SINGLE);
- Syllabic syllabic(note.getSyllabic());
- if (Syllabic::BEGIN == syllabic) {
- syllabicType = SYLLABICTYPE_BEGIN;
- } else if (Syllabic::MIDDLE == syllabic) {
- syllabicType = SYLLABICTYPE_MIDDLE;
- } else if (Syllabic::END == syllabic) {
- syllabicType = SYLLABICTYPE_END;
- }
- if (!score.addNote(note.getDuration() * BASE_DIVISIONS / divisions, note.getLyric(), pitch.getOctave() * 12 + pitch.getStep(), note.hasAccent(), note.hasStaccato(), tieType, slurType, syllabicType, note.hasBreathMark())) {
- ERR_MSG("Cannot add note : " << note);
- throw std::invalid_argument("ScoreConverter::addNote()");
- }
- }
- }
- };
- /*!
- constructor
- */
- SynthCondition::SynthCondition() : impl(NULL)
- {
- #ifdef HAVE_HTS
- this->impl = new SynthConditionImpl();
- #endif
- }
- /*!
- destructor
- */
- SynthCondition::~SynthCondition()
- {
- #ifdef HAVE_HTS
- delete this->impl;
- #endif
- }
- /*!
- set play flag
- */
- void SynthCondition::setPlayFlag()
- {
- #ifdef HAVE_HTS
- this->impl->setPlayFlag();
- #endif
- }
- /*!
- unset play flag
- */
- void SynthCondition::unsetPlayFlag()
- {
- #ifdef HAVE_HTS
- this->impl->unsetPlayFlag();
- #endif
- }
- /*!
- set save file path
- */
- void SynthCondition::setSaveFilePath(const std::string& filePath)
- {
- #ifdef HAVE_HTS
- this->impl->setSaveFilePath(filePath);
- #endif
- }
- /*!
- unset save file path
- */
- void SynthCondition::unsetSaveFilePath()
- {
- #ifdef HAVE_HTS
- this->impl->unsetSaveFilePath();
- #endif
- }
- /*!
- set waveform buffer
- */
- void SynthCondition::setWaveformBuffer(std::vector<double>& waveform)
- {
- #ifdef HAVE_HTS
- this->impl->setWaveformBuffer(waveform);
- #endif
- }
- /*!
- unset waveform buffer
- */
- void SynthCondition::unsetWaveformBuffer()
- {
- #ifdef HAVE_HTS
- this->impl->unsetWaveformBuffer();
- #endif
- }
- class SinsyImpl : public IScoreWriter
- {
- public:
- //! constructor
- SinsyImpl() {}
- //! destructor
- virtual ~SinsyImpl() {}
- //! set languages
- bool setLanguages(const std::string& languages, const std::string& dirPath) {
- return converter.setLanguages(languages, dirPath);
- }
- //! load voice files
- bool loadVoices(const std::vector<std::string>& voices) {
- #ifdef HAVE_HTS
- return engine.load(voices);
- #endif
- }
- //! set encoding
- void setEncoding(const std::string& encoding) {
- score.setEncoding(encoding);
- }
- //! add key mark
- void changeKey(const Key& key) {
- score.changeKey(key);
- }
- //! change beat : default beat mark is 4/4
- void changeBeat(const Beat& beat) {
- score.changeBeat(beat);
- }
- //! change tempo : default tempo is 100bps
- void changeTempo(double tempo) {
- score.changeTempo(tempo);
- }
- //! change dynamics (sudden changes)
- void changeDynamics(const Dynamics& dynamics) {
- score.changeDynamics(dynamics);
- }
- //! start crescendo
- void startCrescendo() {
- score.startCrescendo();
- }
- //! stop crescendo
- void stopCrescendo() {
- score.stopCrescendo();
- }
- //! start diminuendo
- void startDiminuendo() {
- score.startDiminuendo();
- }
- //! stop diminuendo
- void stopDiminuendo() {
- score.stopDiminuendo();
- }
- //! add note to end of score
- void addNote(const Note& note) {
- score.addNote(note);
- }
- //! write score to given IScoreWritable object
- void write(IScoreWritable& maker) const {
- maker << score;
- }
- #ifdef HAVE_HTS
- //! set alpha for synthesis
- bool setAlpha(double alpha) {
- return engine.setAlpha(alpha);
- }
- //! set volume for synthesis
- bool setVolume(double volume) {
- return engine.setVolume(volume);
- }
- //! set interpolation weight for synthesis
- bool setInterpolationWeight(size_t index, double weight) {
- return engine.setInterpolationWeight(index, weight);
- }
- //! synthesize
- bool synthesize(SynthConditionImpl& condition) {
- LabelMaker labelMaker(converter);
- labelMaker << score;
- labelMaker.fix();
- LabelStrings label;
- labelMaker.outputLabel(label, false, 1, 2);
- return engine.synthesize(label, condition);
- }
- //! stop synthesizing
- void stop() {
- engine.stop();
- }
- //! reset stop flag
- void resetStopFlag() {
- engine.resetStopFlag();
- }
- #endif
- //! clear score
- void clearScore() {
- score.clear();
- }
- //! load score from MusicXML
- bool loadScoreFromMusicXML(InputFile& xml) {
- XmlReader xmlReader;
- if (!xmlReader.readXml(xml)) {
- ERR_MSG("Cannot parse Xml file");
- return false;
- }
- score << xmlReader;
- return true;
- }
- //! save score to MusicXML
- bool saveScoreToMusicXML(const std::string& xml, XmlWriter::Clef clef) {
- XmlWriter xmlWriter;
- xmlWriter.setClef(clef);
- xmlWriter << score;
- OutputFile outputFile(xml);
- if (!outputFile.isValid()) {
- ERR_MSG("Cannot open Xml file");
- return false;
- }
- WritableStrStream stream(outputFile);
- if (!xmlWriter.writeXml(stream)) {
- ERR_MSG("Cannot write Xml file");
- return false;
- }
- return true;
- }
- private:
- //! copy constructor (donot use)
- SinsyImpl(const SinsyImpl&);
- //! assignment operator (donot use)
- SinsyImpl& operator=(const SinsyImpl&);
- //! score
- ScoreDoctor score;
- //! converter
- Converter converter;
-
- #ifdef HAVE_HTS
- //! hts_engine API
- HtsEngine engine;
- #endif
- };
- /*!
- constructor
- */
- Sinsy::Sinsy() : impl(NULL)
- {
- try {
- impl = new SinsyImpl();
- } catch (const std::bad_alloc& ex) {
- ERR_MSG("Exception in API " << FUNC_NAME("") << " : " << ex.what());
- delete(impl); // fail safe
- }
- }
- /*!
- destructor
- */
- Sinsy::~Sinsy()
- {
- delete(impl);
- }
- /*!
- set languages
- */
- bool Sinsy::setLanguages(const std::string& languages, const std::string& dirPath)
- {
- try {
- if (!impl->setLanguages(languages, dirPath)) {
- return false;
- }
- } catch (const std::exception& ex) {
- ERR_MSG("Exception in API " << FUNC_NAME(languages << ", " << dirPath) << " : " << ex.what());
- return false;
- }
- return true;
- }
- /*!
- load voices
- */
- bool Sinsy::loadVoices(const std::vector<std::string>& voices)
- {
- try {
- if (!impl->loadVoices(voices)) {
- return false;
- }
- } catch (const std::exception& ex) {
- ERR_MSG("Exception in API " << FUNC_NAME("") << " : " << ex.what());
- return false;
- }
- return true;
- }
- /*!
- set encoding
- */
- bool Sinsy::setEncoding(const std::string& encoding)
- {
- try {
- impl->setEncoding(encoding);
- } catch (const std::exception& ex) {
- ERR_MSG("Exception in API " << FUNC_NAME(encoding) << " : " << ex.what());
- return false;
- }
- return true;
- }
- /*!
- add beat mark
- */
- bool Sinsy::addBeatMark(size_t beats, size_t beatType)
- {
- if (0 == beats) {
- ERR_MSG("Wrong beats (== 0) in API " << FUNC_NAME(beats << ", " << beatType));
- return false;
- }
- if (0 == beatType) {
- ERR_MSG("Wrong beat type (== 0) in API " << FUNC_NAME(beats << ", " << beatType));
- return false;
- }
- try {
- impl->changeBeat(Beat(beats, beatType));
- } catch (const std::exception& ex) {
- ERR_MSG("Exception in API " << FUNC_NAME(beats << ", " << beatType) << " : " << ex.what());
- return false;
- }
- return true;
- }
- /*!
- add tempo mark
- */
- bool Sinsy::addTempoMark(double tempo)
- {
- if (tempo <= 0.0) {
- return false;
- }
- try {
- impl->changeTempo(tempo);
- } catch (const std::exception& ex) {
- ERR_MSG("Exception in API " << FUNC_NAME(tempo) << " : " << ex.what());
- return false;
- }
- return true;
- }
- /*!
- add sudden dynamics mark
- */
- bool Sinsy::addSuddenDynamicsMark(SuddenDynamicsType suddenDynamicsType)
- {
- try {
- switch (suddenDynamicsType) {
- case SUDDENDYNAMICSTYPE_PPPP :
- impl->changeDynamics(Dynamics::PPPP);
- break;
- case SUDDENDYNAMICSTYPE_PPP :
- impl->changeDynamics(Dynamics::PPP);
- break;
- case SUDDENDYNAMICSTYPE_PP :
- impl->changeDynamics(Dynamics::PP);
- break;
- case SUDDENDYNAMICSTYPE_P :
- impl->changeDynamics(Dynamics::P);
- break;
- case SUDDENDYNAMICSTYPE_MP :
- impl->changeDynamics(Dynamics::MP);
- break;
- case SUDDENDYNAMICSTYPE_N :
- impl->changeDynamics(Dynamics::N);
- break;
- case SUDDENDYNAMICSTYPE_MF :
- impl->changeDynamics(Dynamics::MF);
- break;
- case SUDDENDYNAMICSTYPE_F :
- impl->changeDynamics(Dynamics::F);
- break;
- case SUDDENDYNAMICSTYPE_FF :
- impl->changeDynamics(Dynamics::FF);
- break;
- case SUDDENDYNAMICSTYPE_FFF :
- impl->changeDynamics(Dynamics::FFF);
- break;
- case SUDDENDYNAMICSTYPE_FFFF :
- impl->changeDynamics(Dynamics::FFFF);
- break;
- default :
- ERR_MSG("Unknown sudden dynamics type in API " << FUNC_NAME(suddenDynamicsType));
- return false;
- }
- } catch (const std::exception& ex) {
- ERR_MSG("Exception in API " << FUNC_NAME(suddenDynamicsType) << " : " << ex.what());
- return false;
- }
- return true;
- }
- /*!
- add gradual dynamics mark
- */
- bool Sinsy::addGradualDynamicsMark(GradualDynamicsType gradualDynamicsType)
- {
- try {
- switch (gradualDynamicsType) {
- case GRADUALDYNAMICSTYPE_CRESCENDO_BEGIN :
- impl->startCrescendo();
- break;
- case GRADUALDYNAMICSTYPE_CRESCENDO_END :
- impl->stopCrescendo();
- break;
- case GRADUALDYNAMICSTYPE_DIMINUENDO_BEGIN :
- impl->startDiminuendo();
- break;
- case GRADUALDYNAMICSTYPE_DIMINUENDO_END :
- impl->stopDiminuendo();
- break;
- default :
- ERR_MSG("Unknown gradual dynamics type in API " << FUNC_NAME(gradualDynamicsType));
- return false;
- }
- } catch (const std::exception& ex) {
- ERR_MSG("Exception in API " << FUNC_NAME(gradualDynamicsType) << " : " << ex.what());
- return false;
- }
- return true;
- }
- /*!
- add key mark
- */
- bool Sinsy::addKeyMark(ModeType modeType, int fifths)
- {
- try {
- Mode mode;
- switch (modeType) {
- case MODETYPE_MAJOR :
- mode = Mode::MAJOR;
- break;
- case MODETYPE_MINOR :
- mode = Mode::MINOR;
- break;
- default :
- ERR_MSG("Unknown mode type in API " << FUNC_NAME(modeType << ", " << fifths));
- return false;
- }
- impl->changeKey(Key(mode, fifths));
- } catch (const std::exception& ex) {
- ERR_MSG("Exception in API " << FUNC_NAME(modeType << ", " << fifths) << " : " << ex.what());
- return false;
- }
- return true;
- }
- /*!
- add note
- */
- bool Sinsy::addNote(size_t duration, const std::string& lyric, size_t pitch, bool accent, bool staccato, TieType tieType, SlurType slurType, SyllabicType syllabicType, bool breath)
- {
- if (duration <= 0) {
- ERR_MSG("Error in API " << FUNC_NAME("") << " : duration(" << duration << ") <= 0");
- return false;
- }
- try {
- Note note;
- note.setRest(false);
- note.setDuration(duration);
- note.setLyric(lyric);
- note.setPitch(Pitch(pitch % 12, pitch / 12));
- note.setAccent(accent);
- note.setStaccato(staccato);
- note.setBreathMark(breath);
- switch (tieType) {
- case TIETYPE_NONE :
- break;
- case TIETYPE_BEGIN :
- note.setTieStart(true);
- break;
- case TIETYPE_END :
- note.setTieStop(true);
- break;
- default :
- ERR_MSG("Error in API " << FUNC_NAME("") << " : unknown tie type : " << tieType);
- return false;
- }
- switch (slurType) {
- case SLURTYPE_NONE :
- break;
- case SLURTYPE_BEGIN :
- note.setSlurStart(true);
- break;
- case SLURTYPE_END :
- note.setSlurStop(true);
- break;
- default :
- ERR_MSG("Error in API " << FUNC_NAME("") << " : unknown slur type : " << slurType);
- return false;
- }
- switch (syllabicType) {
- case SYLLABICTYPE_SINGLE :
- note.setSyllabic(Syllabic::SINGLE);
- break;
- case SYLLABICTYPE_BEGIN :
- note.setSyllabic(Syllabic::BEGIN);
- break;
- case SYLLABICTYPE_MIDDLE :
- note.setSyllabic(Syllabic::MIDDLE);
- break;
- case SYLLABICTYPE_END :
- note.setSyllabic(Syllabic::END);
- break;
- default :
- ERR_MSG("Error in API " << FUNC_NAME("") << " : unknown syllabic type : " << syllabicType);
- return false;
- }
- impl->addNote(note);
- } catch (const std::exception& ex) {
- ERR_MSG("Exception in API " << FUNC_NAME("") << " : " << ex.what());
- return false;
- }
- return true;
- }
- /*!
- add rest
- */
- bool Sinsy::addRest(size_t duration)
- {
- if (duration <= 0) {
- ERR_MSG("Error in API " << FUNC_NAME(duration) << " : duration <= 0");
- return false;
- }
- try {
- Note note;
- note.setRest(true);
- note.setDuration(duration);
- impl->addNote(note);
- } catch (const std::exception& ex) {
- ERR_MSG("Exception in API " << FUNC_NAME(duration) << " : " << ex.what());
- return false;
- }
- return true;
- }
- /*!
- push score data to s
- */
- bool Sinsy::toScore(IScore& s) const
- {
- try {
- ScoreConverter cs(s);
- cs << *impl;
- } catch (const std::exception& ex) {
- ERR_MSG("Exception in API " << FUNC_NAME("") << " : " << ex.what());
- return false;
- }
- return true;
- }
- /*!
- set alpha
- */
- bool Sinsy::setAlpha(double alpha)
- {
- #ifdef HAVE_HTS
- return impl->setAlpha(alpha);
- #else
- return false;
- #endif
- }
- /*!
- set volume
- */
- bool Sinsy::setVolume(double volume)
- {
- #ifdef HAVE_HTS
- return impl->setVolume(volume);
- #else
- return false;
- #endif
- }
- /*!
- set interpolation weight
- */
- bool Sinsy::setInterpolationWeight(size_t index, double weight)
- {
- #ifdef HAVE_HTS
- return impl->setInterpolationWeight(index, weight);
- #else
- return false;
- #endif
- }
- /*!
- synthesize
- */
- bool Sinsy::synthesize(SynthCondition& condition)
- {
- #ifdef HAVE_HTS
- try {
- if (!impl->synthesize(*condition.impl)) {
- return false;
- }
- } catch (const std::exception& ex) {
- ERR_MSG("Exception in API " << FUNC_NAME("") << " : " << ex.what());
- return false;
- }
- return true;
- #else
- return false;
- #endif
- }
- /*!
- stop
- */
- bool Sinsy::stop()
- {
- #ifdef HAVE_HTS
- try {
- impl->stop();
- } catch (const std::exception& ex) {
- ERR_MSG("Exception in API " << FUNC_NAME("") << " : " << ex.what());
- return false;
- }
- return true;
- #else
- return false;
- #endif
- }
- /*!
- reset stop flag
- */
- bool Sinsy::resetStopFlag()
- {
- #ifdef HAVE_HTS
- try {
- impl->resetStopFlag();
- } catch (const std::exception& ex) {
- ERR_MSG("Exception in API " << FUNC_NAME("") << " : " << ex.what());
- return false;
- }
- return true;
- #else
- return false;
- #endif
- }
- /*!
- clear score
- */
- bool Sinsy::clearScore()
- {
- try {
- impl->clearScore();
- } catch (const std::exception& ex) {
- ERR_MSG("Exception in API " << FUNC_NAME("") << " : " << ex.what());
- return false;
- }
- return true;
- }
- /*!
- load score from MusicXML
- */
- bool Sinsy::loadScoreFromMusicXML(const std::string& xml)
- {
- try {
- InputFile xmlFile(xml);
- if(!xmlFile.isValid()) {
- ERR_MSG("Cannot open Xml file");
- return false;
- }
- if (!impl->loadScoreFromMusicXML(xmlFile)) {
- return false;
- }
- } catch (const std::exception& ex) {
- ERR_MSG("Exception in API " << FUNC_NAME("") << " : " << ex.what());
- return false;
- }
- return true;
- }
- /*!
- save score to MusicXML
- */
- bool Sinsy::saveScoreToMusicXML(const std::string& xml, ClefType clefType)
- {
- try {
- XmlWriter::Clef clef(XmlWriter::CLEF_DEFAULT);
- switch (clefType) {
- case CLEFTYPE_DEFAULT :
- clef = XmlWriter::CLEF_DEFAULT;
- break;
- case CLEFTYPE_G :
- clef = XmlWriter::CLEF_G;
- break;
- case CLEFTYPE_F :
- clef = XmlWriter::CLEF_F;
- break;
- case CLEFTYPE_C :
- clef = XmlWriter::CLEF_C;
- break;
- default :
- ERR_MSG("Error in API " << FUNC_NAME("") << " : unknown clef type : " << clefType);
- break;
- }
- if (!impl->saveScoreToMusicXML(xml, clef)) {
- return false;
- }
- } catch (const std::exception& ex) {
- ERR_MSG("Exception in API " << FUNC_NAME("") << " : " << ex.what());
- return false;
- }
- return true;
- }
- }; // namespace sinsy
|