~ubuntu-branches/ubuntu/precise/csound/precise

« back to all changes in this revision

Viewing changes to frontends/CsoundAC/MusicModel.cpp

  • Committer: Package Import Robot
  • Author(s): Felipe Sateler
  • Date: 2012-04-19 09:26:46 UTC
  • mfrom: (3.2.19 sid)
  • Revision ID: package-import@ubuntu.com-20120419092646-96xbj1n6atuqosk2
Tags: 1:5.17.6~dfsg-1
* New upstream release
 - Do not build the wiimote opcodes (we need wiiuse).
* Add new API function to symbols file
* Disable lua opcodes, they were broken. Requires OpenMP to be enabled.
* Backport fixes from upstream:
  - Link dssi4cs with dl. Backport
  - Fix building of CsoundAC

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#include "Exception.hpp"
22
22
#include "Composition.hpp"
23
23
#include "System.hpp"
 
24
#include <cstdio>
 
25
#include <cstdlib>
24
26
 
25
27
namespace csound
26
28
{
38
40
  {
39
41
  }
40
42
 
41
 
  void MusicModel::generate()
 
43
  int MusicModel::generate()
42
44
  {
 
45
    int errorStatus = 0;
43
46
    cppSound->removeScore();
44
47
    if (children.size()) {
45
48
      score.clear();
46
49
    }
47
50
    traverse(getLocalCoordinates(), score);
48
51
    System::message("Generated %d events.\n", score.size());
 
52
    return errorStatus;
49
53
  }
50
54
 
51
 
  void MusicModel::render()
 
55
  int MusicModel::render()
52
56
  {
53
 
    generate();
54
 
    createCsoundScore(csoundScoreHeader);
55
 
    perform();
 
57
    int errorStatus = generate();
 
58
    if (errorStatus) {
 
59
      return errorStatus;
 
60
    }
 
61
    errorStatus = perform();
 
62
    return errorStatus;
56
63
  }
57
64
 
58
65
  void MusicModel::createCsoundScore(std::string addToScore, double extendSeconds)
66
73
    char buffer[0x100];
67
74
    std::sprintf(buffer, "\ns %9.3f", extendSeconds);
68
75
    cppSound->addScoreLine(buffer);
69
 
    std::sprintf(buffer, "\ne %9.3f", extendSeconds);
70
 
    cppSound->addScoreLine(buffer);
 
76
    //std::sprintf(buffer, "\ne %9.3f", extendSeconds);
 
77
    //cppSound->addScoreLine(buffer);
71
78
    cppSound->exportForPerformance();
72
79
  }
73
80
 
74
 
  void MusicModel::perform()
 
81
  int MusicModel::perform()
75
82
  {
76
 
    cppSound->perform();
 
83
    int errorStatus = 0;
 
84
    cppSound->setCommand(getCsoundCommand());
 
85
    createCsoundScore(csoundScoreHeader);
 
86
    errorStatus = cppSound->perform();
 
87
    if (errorStatus == 1) {
 
88
      errorStatus = 0;
 
89
    }
 
90
    // The Csound command is managed from MusicModel,
 
91
    // not from CppSound. So we clear out what we set.
 
92
    cppSound->setCommand("");
 
93
    return errorStatus;
77
94
  }
78
95
 
79
 
 
80
 
 
81
96
  void MusicModel::clear()
82
97
  {
83
98
    Node::clear();
84
 
    MusicModel::clear();
 
99
    Composition::clear();
85
100
    cppSound->removeScore();
86
101
  }
87
102
 
128
143
  }
129
144
 
130
145
  void MusicModel::arrange(int oldInstrumentNumber,
131
 
                            int newInstrumentNumber,
132
 
                            double gain)
 
146
                           int newInstrumentNumber,
 
147
                           double gain)
133
148
  {
134
149
    score.arrange(oldInstrumentNumber, newInstrumentNumber, gain);
135
150
  }
136
151
 
137
152
  void MusicModel::arrange(int oldInstrumentNumber,
138
 
                            int newInstrumentNumber,
139
 
                            double gain,
140
 
                            double pan)
 
153
                           int newInstrumentNumber,
 
154
                           double gain,
 
155
                           double pan)
141
156
  {
142
157
    score.arrange(oldInstrumentNumber, newInstrumentNumber, gain, pan);
143
158
  }
152
167
  {
153
168
    int csoundInstrumentNumber = cppSound->getInstrumentNumber(csoundInstrumentName);
154
169
    arrange(silenceInstrumentNumber, csoundInstrumentNumber, gain);
155
 
   }
 
170
  }
156
171
 
157
172
  void MusicModel::arrange(int silenceInstrumentNumber, std::string csoundInstrumentName, double gain, double pan)
158
173
  {
172
187
 
173
188
  std::string MusicModel::getCsoundCommand() const
174
189
  {
175
 
    return cppSound->getCommand();
 
190
    std::string command_ = cppSound->getCommand();
 
191
    if (command_.size() == 0)
 
192
      {
 
193
        const char *temp_path = std::getenv("TEMP");
 
194
        if (temp_path == 0)
 
195
          {
 
196
            temp_path = "";
 
197
          }
 
198
        std::string orcname = std::tmpnam(0);
 
199
        std::string sconame = std::tmpnam(0);
 
200
        char buffer[0x200];
 
201
        std::sprintf(buffer,
 
202
                     "csound --midi-key=4 --midi-velocity=5 -m167 -RWdfo %s %s%s.orc %s%s.sco",
 
203
                     getOutputSoundfileName().c_str(), temp_path, orcname.c_str(), temp_path, sconame.c_str());
 
204
        command_ = buffer;
 
205
      }
 
206
    return command_;
176
207
  }
177
208
 
178
209
  long MusicModel::getThis()
185
216
    return (Node *)this;
186
217
  }
187
218
 
 
219
  int MusicModel::processArgs(const std::vector<std::string> &args)
 
220
  {
 
221
    System::inform("BEGAN MusicModel::processArgv()...\n");
 
222
    std::map<std::string, std::string> argsmap;
 
223
    std::string key;
 
224
    for (size_t i = 0, n = args.size(); i < n; ++i)
 
225
      {
 
226
        const std::string token = args[i];
 
227
        std::string value = "";
 
228
        if (token.find("--") == 0) 
 
229
          {
 
230
            key = token;
 
231
          }
 
232
        else
 
233
          {
 
234
            value = token;
 
235
          }
 
236
        argsmap[key] = value;
 
237
        System::inform("argument[%2d]: %s =  %s\n", i, key.c_str(), value.c_str());
 
238
      }
 
239
    char command[0x200];
 
240
    int errorStatus = 0;
 
241
    bool postPossible = false;
 
242
    std::string playSoundfileName = getOutputSoundfileName();
 
243
    if ((argsmap.find("--midi") != argsmap.end()) && !errorStatus)
 
244
      {
 
245
        errorStatus = generate();
 
246
        if (errorStatus) {
 
247
          return errorStatus;
 
248
        }
 
249
        getScore().save(getMidiFilename().c_str());
 
250
      }
 
251
    if ((argsmap.find("--csound") != argsmap.end()) && !errorStatus)
 
252
      {
 
253
        postPossible = true;
 
254
        errorStatus = render();
 
255
      }
 
256
    if ((argsmap.find("--pianoteq") != argsmap.end()) && !errorStatus)
 
257
      {
 
258
        std::sprintf(command, "Pianoteq --midi %s\n", getMidiFilename().c_str());
 
259
        System::inform("Executing command: %s", command);
 
260
        errorStatus = std::system(command);
 
261
      }
 
262
    if ((argsmap.find("--pianoteq-wav") != argsmap.end()) && !errorStatus)
 
263
      {
 
264
        postPossible = true;
 
265
        std::sprintf(command, "Pianoteq --headless --midi %s --rate 48000 --wav %s\n", getMidiFilename().c_str(), getOutputSoundfileName().c_str());
 
266
        System::inform("Executing command: %s", command);
 
267
        errorStatus = std::system(command);
 
268
      }
 
269
    if ((argsmap.find("--playmidi") != argsmap.end()) && !errorStatus)
 
270
      {
 
271
        std::sprintf(command, "%s %s\n", argsmap["--playmidi"].c_str(), getMidiFilename().c_str());
 
272
        System::inform("Executing command: %s", command);
 
273
        errorStatus = std::system(command);
 
274
      }
 
275
    if ((argsmap.find("--post") != argsmap.end()) && !errorStatus && postPossible)
 
276
      {
 
277
        errorStatus = translateMaster();
 
278
        playSoundfileName = getNormalizedSoundfileName();
 
279
      }
 
280
    if ((argsmap.find("--playwav") != argsmap.end()) && !errorStatus)
 
281
      {
 
282
        std::sprintf(command, "%s %s\n", argsmap["--playwav"].c_str(), playSoundfileName.c_str());
 
283
        System::inform("Executing command: %s", command);
 
284
        errorStatus = std::system(command);
 
285
      }
 
286
    System::inform("ENDED MusicModel::processArgv().\n");
 
287
    return errorStatus;
 
288
  }
 
289
 
 
290
  void MusicModel::stop()
 
291
  {
 
292
    std::cout << "MusicModel::stop()..." << std::endl;
 
293
    cppSound->stop();
 
294
  }
188
295
}
189