csoundsoundcontroller.cpp 8.16 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/****************************************************************************
**
** Copyright (C) 2016 by Sandro S. Andrade <sandroandrade@kde.org>
**
** 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) version 3 or any later version
** accepted by the membership of KDE e.V. (or its successor approved
** by the membership of KDE e.V.), which shall act as a proxy
** defined in Section 14 of version 3 of the license.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/

#include "csoundsoundcontroller.h"
#include "csengine.h"

#include <QtMath>
#include <QLoggingCategory>
#include <QtQml>

Q_DECLARE_LOGGING_CATEGORY(MINUETANDROID)

CsoundSoundController::CsoundSoundController(QObject *parent):
    Minuet::ISoundController(parent),
    m_csoundEngine(new CsEngine)
{
    qmlRegisterType<CsoundSoundController>("org.kde.minuet", 1, 0, "CsoundSoundController");
    openExerciseFile();
//    setQuestionLabel("new question");
}

void CsoundSoundController::openExerciseFile()
{
    QStringList templateList;
    templateList.append(QStringLiteral("assets:/share/template.csd"));
Sandro Andrade's avatar
Sandro Andrade committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//    templateList.append(QStringLiteral("assets:/share/template_rhythm.csd"));

    qDebug() << "DIRPATH: " << QCoreApplication::applicationDirPath();
    qDebug() << "ASSETSPATH: " << QDir("assets:/").canonicalPath();
    qDebug() << "QStandardPaths::ApplicationsLocation: " << QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
    qDebug() << "QStandardPaths::GenericDataLocation: " << QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
    qDebug() << "QStandardPaths::AppDataLocation: " << QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);
    qDebug() << "QStandardPaths::AppLocalDataLocation: " << QStandardPaths::standardLocations(QStandardPaths::AppLocalDataLocation);
    qDebug() << "QStandardPaths::AppConfigLocation: " << QStandardPaths::standardLocations(QStandardPaths::AppConfigLocation);
    QDir appDir(QCoreApplication::applicationDirPath());
    qDebug() << "Contains libfluidOpcodes.so? " << appDir.entryList(QDir::Files).contains("libfluidOpcodes.so");
    qDebug() << "DIRPATH: " << appDir << " Contents: " << appDir.entryList(QDir::AllEntries);
    appDir.cdUp();
    qDebug() << "DIRPATH: " << appDir << " Contents: " << appDir.entryList(QDir::AllEntries);
    appDir.cdUp();
    qDebug() << "DIRPATH: " << appDir << " Contents: " << appDir.entryList(QDir::AllEntries);
    QDir reserved("/data/data/org.kde.minuet/qt-reserved-files/");
    qDebug() << "RESERVED: " << reserved << " Contents: " << reserved.entryList(QDir::AllEntries);

    QDirIterator it("/", QDirIterator::Subdirectories);
Gustavo Carneiro's avatar
Gustavo Carneiro committed
65
    while (it.hasNext()) {
Sandro Andrade's avatar
Sandro Andrade committed
66
        qDebug() << "FILE: " << it.next();
Gustavo Carneiro's avatar
Gustavo Carneiro committed
67
    }
68
69
    foreach (const QString &templateString, templateList) {
        QFile sfile(templateString);
Gustavo Carneiro's avatar
Gustavo Carneiro committed
70
        if (!sfile.open(QIODevice::ReadOnly | QIODevice::Text)) {
71
            return;
Gustavo Carneiro's avatar
Gustavo Carneiro committed
72
        }
73
74
75
76
77
78
79
80

        QTextStream in(&sfile);
        QString lineData;
        QString tempBeginLine;
        QString tempEndLine;

        while (!in.atEnd()) {
            lineData = in.readLine();
Sandro Andrade's avatar
Sandro Andrade committed
81
            lineData.replace("APPDIRPATH", QCoreApplication::applicationDirPath());
Yuri Chornoivan's avatar
Yuri Chornoivan committed
82
            tempBeginLine = tempBeginLine + lineData + '\n';
83
84
85
86
87
88
89
90
            if (lineData.contains("<CsScore>")) {
                m_begLine.append(tempBeginLine);
                break;
            }
        }

        while (!in.atEnd()) {
            lineData = in.readLine();
Yuri Chornoivan's avatar
Yuri Chornoivan committed
91
            tempEndLine += lineData + '\n';
92
93
94
        }
        m_endLine.append(tempEndLine);
    }
Sandro Andrade's avatar
Sandro Andrade committed
95
96
    qDebug() << "m_begLine: " << m_begLine;
    qDebug() << "m_endLine: " << m_endLine;
97
98
99
100
101
102
103
104
105
106
}

void CsoundSoundController::appendEvent(QList<unsigned int> midiNotes, QList<float> barStartInfo, QString playMode)
{
    //TODO : use grantlee processing or any other text template library
    int templateNumber = playMode == "rhythm" ? 1:0;
    QString content;
    QString fifthParam = QStringLiteral("100");
    QFile m_csdFileOpen(QStringLiteral("./template.csd"));

Sandro Andrade's avatar
Sandro Andrade committed
107
    qDebug() << "CREATING " << QDir::currentPath() + "/template.csd";
Gustavo Carneiro's avatar
Gustavo Carneiro committed
108
    if (!m_csdFileOpen.isOpen()) {
109
110
111
112
113
114
115
116
117
118
        m_csdFileOpen.open(QIODevice::ReadWrite | QIODevice::Text);
    }
    m_csdFileOpen.resize(0);

    if (playMode == "rhythm") {
        QString wave = QStringLiteral("f 1 0 16384 10 1\n\n");
        content += wave;
        fifthParam = QStringLiteral("");
    }

Gustavo Carneiro's avatar
Gustavo Carneiro committed
119
    for (int i = 0; i < midiNotes.count(); i++) {
120
        QString initScore = QString("i 1 %1 %2 %3 %4\n").arg(QString::number(barStartInfo.at(i))).arg(QString::number(1)).arg(QString::number(midiNotes.at(i))).arg(fifthParam);
Sandro Andrade's avatar
Sandro Andrade committed
121
//        content += initScore;
122
123
124
    }

    if (playMode != "rhythm") {
Gustavo Carneiro's avatar
Gustavo Carneiro committed
125
        QString instrInit = "i 99 0 " + QString::number(barStartInfo.at(barStartInfo.count() -1) +1) + "\ne\n";//instrument will be active till the end of the notes +1 second
Sandro Andrade's avatar
Sandro Andrade committed
126
//        content += instrInit;
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
    }

    QString templateContent = m_begLine[templateNumber] + content + m_endLine[templateNumber];
    m_csdFileOpen.seek(0);
    QByteArray contentByte = templateContent.toUtf8();
    m_csdFileOpen.write(contentByte);
}

CsoundSoundController::~CsoundSoundController()
{
    delete m_csoundEngine;
}

void CsoundSoundController::prepareFromExerciseOptions(QJsonArray selectedExerciseOptions)
{
    float barStart = 0;
    QList<unsigned int> midiNotes;
    QList<float> barStartInfo;

    if (m_playMode == "rhythm") {
Gustavo Carneiro's avatar
Gustavo Carneiro committed
147
        for (int k = 0; k < 4; ++k) {
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
            midiNotes.append(80);
            barStartInfo.append(barStart++);
        }
    }

    int exerciseOptionsSize = selectedExerciseOptions.size();
    for (int i = 0; i < exerciseOptionsSize; ++i) {
        QString sequence = selectedExerciseOptions[i].toObject()[QStringLiteral("sequence")].toString();

        unsigned int chosenRootNote = selectedExerciseOptions[i].toObject()[QStringLiteral("rootNote")].toString().toInt();
        if (m_playMode != "rhythm") {
            midiNotes.append(chosenRootNote);
            barStartInfo.append(barStart);

            unsigned int j = 1;
            foreach(const QString &additionalNote, sequence.split(' ')) {
Gustavo Carneiro's avatar
Gustavo Carneiro committed
164
                midiNotes.append(chosenRootNote + additionalNote.toInt());
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
                barStartInfo.append((m_playMode == "scale") ? barStart+j:barStart);
                ++j;
            }
            barStart++;
        }
        else {
            midiNotes.append(80);
            barStartInfo.append(barStart);
            foreach(QString additionalNote, sequence.split(' ')) { // krazy:exclude=foreach
                midiNotes.append(37);
                barStartInfo.append(barStart);
                float dotted = 1;
                if (additionalNote.endsWith('.')) {
                    dotted = 1.5;
                    additionalNote.chop(1);
                }
                barStart += dotted*1*(4.0/additionalNote.toInt());
            }
        }
    }

    if (m_playMode == "rhythm") {
        midiNotes.append(80);
        barStartInfo.append(barStart);
    }
    appendEvent(midiNotes, barStartInfo, m_playMode);
}

void CsoundSoundController::prepareFromMidiFile(const QString &fileName)
{
    Q_UNUSED(fileName)
}

void CsoundSoundController::play()
{
    m_csoundEngine->start();
    setState(PlayingState);
}

void CsoundSoundController::pause()
{

}

void CsoundSoundController::stop()
{
    m_csoundEngine->stop();
    setState(StoppedState);
}

void CsoundSoundController::reset()
{
}

void CsoundSoundController::setPitch (qint8 pitch)
{
    Q_UNUSED(pitch)
}


void CsoundSoundController::setVolume (quint8 volume)
{
    Q_UNUSED(volume)
}


void CsoundSoundController::setTempo (quint8 tempo)
{
    Q_UNUSED(tempo)
}

//#include "moc_csoundsoundcontroller.cpp"