|
From: Tapio V. <aa...@us...> - 2011-01-13 14:12:28
|
Module: editor
Branch: master
Commit: 5646d8e9b0164433ec9cc1e52ff3ae19aa9667f0
Author: Tapio Vierros <tap...@gm...>
Date: Thu Jan 13 16:11:41 2011 +0200
Framework for exporting song to various formats.
---
editor.ui | 6 ------
editorapp.cc | 36 +++++++++++++++++++++++++++++++++++-
editorapp.hh | 3 +++
song.hh | 18 +++++++++++++++++-
songwriter-ini.cc | 29 +++++++++++++++++++++++++++++
songwriter-txt.cc | 19 +++++++++++++++++++
songwriter-xml .cc | 20 ++++++++++++++++++++
songwriter.hh | 37 +++++++++++++++++++++++++++++++++++++
8 files changed, 160 insertions(+), 8 deletions(-)
diff --git a/editor.ui b/editor.ui
index f62336f..c43993b 100644
--- a/editor.ui
+++ b/editor.ui
@@ -464,17 +464,11 @@
</property>
</action>
<action name="actionUltraStarTXT">
- <property name="enabled">
- <bool>false</bool>
- </property>
<property name="text">
<string>&UltraStar TXT</string>
</property>
</action>
<action name="actionFoFMIDI">
- <property name="enabled">
- <bool>false</bool>
- </property>
<property name="text">
<string>&FoF MIDI</string>
</property>
diff --git a/editorapp.cc b/editorapp.cc
index 59504fa..cf2881e 100644
--- a/editorapp.cc
+++ b/editorapp.cc
@@ -3,6 +3,7 @@
#include "editorapp.hh"
#include "notelabel.hh"
#include "notegraphwidget.hh"
+#include "songwriter.hh"
EditorApp::EditorApp(QWidget *parent): QMainWindow(parent)
@@ -104,7 +105,7 @@ void EditorApp::on_actionNew_triggered()
void EditorApp::on_actionOpen_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
- "",
+ QDir::homePath(),
tr("All supported formats (*.TBD!!! *.xml *.mid *.ini *.txt)") + ";;" +
tr("Project files (*.TBD!!!)") + ";;" + // FIXME: Project file extension
tr("SingStar XML (*.xml)") + ";;" +
@@ -128,6 +129,39 @@ void EditorApp::on_actionOpen_triggered()
}
}
+void EditorApp::on_actionSingStarXML_triggered()
+{
+ QString path = QFileDialog::getExistingDirectory(this, tr("Export SingStar XML"), QDir::homePath());
+ if (!path.isNull()) {
+ try { SingStarXMLWriter(*song.data(), path); }
+ catch (const std::exception& e) {
+ QMessageBox::critical(this, tr("Error exporting song!"), e.what());
+ }
+ }
+}
+
+void EditorApp::on_actionUltraStarTXT_triggered()
+{
+ QString path = QFileDialog::getExistingDirectory(this, tr("Export UltraStar TXT"), QDir::homePath());
+ if (!path.isNull()) {
+ try { UltraStarTXTWriter(*song.data(), path); }
+ catch (const std::exception& e) {
+ QMessageBox::critical(this, tr("Error exporting song!"), e.what());
+ }
+ }
+}
+
+void EditorApp::on_actionFoFMIDI_triggered()
+{
+ QString path = QFileDialog::getExistingDirectory(this, tr("Export FoF MIDI"), QDir::homePath());
+ if (!path.isNull()) {
+ try { FoFMIDIWriter(*song.data(), path); }
+ catch (const std::exception& e) {
+ QMessageBox::critical(this, tr("Error exporting song!"), e.what());
+ }
+ }
+}
+
void EditorApp::on_actionExit_triggered()
{
// TODO: Check if a save prompt is in order
diff --git a/editorapp.hh b/editorapp.hh
index 4a66eed..9a3b53a 100644
--- a/editorapp.hh
+++ b/editorapp.hh
@@ -25,6 +25,9 @@ public slots:
// File menu
void on_actionNew_triggered();
void on_actionOpen_triggered();
+ void on_actionSingStarXML_triggered();
+ void on_actionUltraStarTXT_triggered();
+ void on_actionFoFMIDI_triggered();
void on_actionExit_triggered();
// Edit menu
diff --git a/song.hh b/song.hh
index 43903fb..dfacd36 100644
--- a/song.hh
+++ b/song.hh
@@ -77,7 +77,23 @@ class Song {
}
}
}
- };
+ }
+ VocalTrack getVocalTrack(std::string vocalTrack = TrackName::LEAD_VOCAL) const {
+ if(vocalTracks.find(vocalTrack) != vocalTracks.end()) {
+ return vocalTracks.find(vocalTrack)->second;
+ } else {
+ if(vocalTracks.find(TrackName::LEAD_VOCAL) != vocalTracks.end()) {
+ return vocalTracks.find(TrackName::LEAD_VOCAL)->second;
+ } else {
+ if(!vocalTracks.empty()) {
+ return vocalTracks.begin()->second;
+ } else {
+ return dummyVocal;
+ }
+ }
+ }
+ }
+
std::vector<std::string> getVocalTrackNames() {
std::vector<std::string> result;
for (VocalTracks::const_iterator it = vocalTracks.begin(); it != vocalTracks.end(); ++it) {
diff --git a/songwriter-ini.cc b/songwriter-ini.cc
new file mode 100644
index 0000000..b9b1cf1
--- /dev/null
+++ b/songwriter-ini.cc
@@ -0,0 +1,29 @@
+#include "songwriter.hh"
+#include <fstream>
+#include <iostream>
+
+void FoFMIDIWriter::writeMIDI() {
+ throw std::runtime_error("MIDI export is not implemented.");
+ std::ofstream f((path + "notes.mid").c_str(), std::ios::binary);
+ // FIXME: The following is just an example and doesn't actually output MID format
+ char buf[1024] = {};
+ Notes const& notes = s.getVocalTrack().notes;
+ std::cout << notes.size() << std::endl;
+ for (unsigned int i = 0; i < notes.size(); ++i) {
+ Note const& n = notes[i];
+ buf[0] = 0xFF;
+ buf[1] = n.note; // MIDI note value
+ // Others are n.begin, n.end, n.type etc. (see notes.hh)
+ f.write(buf, 1024);
+ }
+}
+
+void FoFMIDIWriter::writeINI() {
+ std::ofstream f((path + "song.ini").c_str(), std::ios::binary);
+ f << "[song]\n";
+ f << "name = " << s.title << std::endl;
+ f << "artist = " << s.artist << std::endl;
+ f << "genre = " << s.genre << std::endl;
+ f << "year = " << s.year << std::endl;
+}
+
diff --git a/songwriter-txt.cc b/songwriter-txt.cc
new file mode 100644
index 0000000..ffa6e7e
--- /dev/null
+++ b/songwriter-txt.cc
@@ -0,0 +1,19 @@
+#include "songwriter.hh"
+#include <fstream>
+#include <iostream>
+
+void UltraStarTXTWriter::writeTXT() {
+ throw std::runtime_error("TXT export is not implemented.");
+ std::ofstream f((path + "notes.txt").c_str(), std::ios::binary);
+ // FIXME: The following is just an example and doesn't actually output TXT format
+ char buf[1024] = {};
+ Notes const& notes = s.getVocalTrack().notes;
+ std::cout << notes.size() << std::endl;
+ for (unsigned int i = 0; i < notes.size(); ++i) {
+ Note const& n = notes[i];
+ buf[0] = 0xFF;
+ buf[1] = n.note; // MIDI note value
+ // Others are n.begin, n.end, n.type etc. (see notes.hh)
+ f.write(buf, 1024);
+ }
+}
diff --git a/songwriter-xml .cc b/songwriter-xml .cc
new file mode 100644
index 0000000..71d2581
--- /dev/null
+++ b/songwriter-xml .cc
@@ -0,0 +1,20 @@
+#include "songwriter.hh"
+#include <fstream>
+#include <iostream>
+
+void SingStarXMLWriter::writeXML() {
+ throw std::runtime_error("XML export is not implemented.");
+ // FIXME: Use QtXML
+ std::ofstream f((path + "notes.xml").c_str(), std::ios::binary);
+ // FIXME: The following is just an example and doesn't actually output XML format
+ char buf[1024] = {};
+ Notes const& notes = s.getVocalTrack().notes;
+ std::cout << notes.size() << std::endl;
+ for (unsigned int i = 0; i < notes.size(); ++i) {
+ Note const& n = notes[i];
+ buf[0] = 0xFF;
+ buf[1] = n.note; // MIDI note value
+ // Others are n.begin, n.end, n.type etc. (see notes.hh)
+ f.write(buf, 1024);
+ }
+}
diff --git a/songwriter.hh b/songwriter.hh
new file mode 100644
index 0000000..3d06e60
--- /dev/null
+++ b/songwriter.hh
@@ -0,0 +1,37 @@
+#include "song.hh"
+#include <QDir>
+#include <fstream>
+
+struct SongWriter
+{
+ SongWriter(const Song& s_, const QString& path_)
+ : s(s_), path(path_.toStdString()) { QDir dir; dir.mkpath(path_); }
+ const Song& s;
+ std::string path;
+};
+
+struct SingStarXMLWriter: public SongWriter
+{
+ SingStarXMLWriter(const Song& s_, const QString& path_)
+ : SongWriter(s_, path_) { writeXML(); }
+private:
+ void writeXML();
+};
+
+struct UltraStarTXTWriter: public SongWriter
+{
+ UltraStarTXTWriter(const Song& s_, const QString& path_)
+ : SongWriter(s_, path_) { writeTXT(); }
+private:
+ void writeTXT();
+};
+
+struct FoFMIDIWriter: public SongWriter
+{
+ FoFMIDIWriter(const Song& s_, const QString& path_)
+ : SongWriter(s_, path_) { writeINI(); writeMIDI(); }
+
+private:
+ void writeINI();
+ void writeMIDI();
+};
|