Previous: Extending, Up: Marsyas Expression Syntax


9.6.4.13 Marsyas Expression Examples
     #include <stdio.h>
     #include "MarSystemManager.h"
     #include "EvExpr.h"
     
     using namespace std;
     using namespace Marsyas;
     
     void sched1()
     {
       MarSystemManager mng;
     
       MarSystem* fanin = mng.create("Fanin", "fanin");
       fanin->addMarSystem(mng.create("SineSource", "src1"));
       fanin->addMarSystem(mng.create("SineSource", "src2"));
       fanin->updctrl("SineSource/src1/mrs_real/frequency",3000.0);
       fanin->updctrl("SineSource/src2/mrs_real/frequency",1000.0);
     
       MarSystem* series = mng.create("Series", "series");
       series->addMarSystem(fanin);
     
       series->addMarSystem(mng.create("AudioSink", "dest"));
       series->updctrl("AudioSink/dest/mrs_bool/initAudio", true);
     
     
     #if 0
       // using aliases makes this a little more readable, see the next bit
       EvExpr* e = new EvExpr(series,
         Ex("Fanin/fanin/SineSource/src1/mrs_real/frequency << 120. + 3000. * R.rand(), \
             Fanin/fanin/SineSource/src2/mrs_real/frequency << 120. + 800. * R.rand(),"
            "'src1='+Fanin/fanin/SineSource/src1/mrs_real/frequency+ \
             ' src2='+Fanin/fanin/SineSource/src2/mrs_real/frequency+'\n'>>Stream.op"),
         Rp("true"));
     
     #else
       EvExpr* e = new EvExpr(series,
             // First line to Ex will be the init expression, run once, when event is posted
         Ex("Fanin/fanin/SineSource/src1/mrs_real/frequency >> @freq1, \
             Fanin/fanin/SineSource/src2/mrs_real/frequency >> @freq2 ",
            // Second line to Ex is the expression, repeated each time the event is posted
            "freq1 << 120. + 3000. * R.rand(),\
             freq2 << 120. + 800. * R.rand(),\
             'src1=' + freq1 + ' src2=' + freq2 + '\n' >> Stream.op"),
         Rp("true"));
     
     #endif
     
       e->set_repeat(Repeat("0.25s"));
     
       series->updctrl(TmTime("TmSampleCount/Virtual","0s"), e);
     
       for (int i=1;i<100;i++) series->tick();
     
       delete series;
     }

Figure 9.15: How to generate random sine waves

     void sched2()
     {
       MarSystemManager mng;
     
       MarSystem* fanin = mng.create("Fanin", "fanin");
       fanin->addMarSystem(mng.create("SineSource", "src1"));
       fanin->addMarSystem(mng.create("SineSource", "src2"));
       fanin->updctrl("SineSource/src1/mrs_real/frequency",400.0);  // A
     
       fanin->updctrl("SineSource/src2/mrs_real/frequency",554.37); // C#
       fanin->updctrl("SineSource/src3/mrs_real/frequency",659.26); // E
     
       MarSystem* series = mng.create("Series", "series");
       series->addMarSystem(fanin);
     
       series->addMarSystem(mng.create("AudioSink", "dest"));
       series->updctrl("AudioSink/dest/mrs_bool/initAudio", true);
     
       EvExpr* e =
         new EvExpr(series,
           Ex("tmr<<Timer.cur","tmr.prefix + ' ' + tmr.time + '\n' >> Stream.op"),
           Rp("true"));
     
       e->set_repeat(Repeat("0.25s"));
     
       series->updctrl(TmTime("TmSampleCount/Virtual","0s"), e);
     
       for (int i=1;i<100;i++) series->tick();
     
       delete series;
     }

Figure 9.16: Printing the time of the current timer