1 /*
2 GUIDO Library
3 Copyright (C) 2006 Grame
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 Grame Research Laboratory, 9 rue du Garet, 69001 Lyon - France
20 research@grame.fr
21
22 */
23
24 #include <iostream>
25 #include "guidoMixValue.h"
26 #include "visitor.h"
27
28 using namespace std;
29 using namespace guido;
30
31 namespace guidolang
32 {
33
34 #define propagate(f,val) create(getArg1()->f(val), getArg2()->f(val))
35
36 //______________________________________________________________________________
37 // guidoMixValue
38 //______________________________________________________________________________
create(Sguidovalue v1,Sguidovalue v2)39 Sguidovalue guidoMixValue::create (Sguidovalue v1, Sguidovalue v2)
40 { valuePrint("guidoMixValue"); guidoMixValue * o = new guidoMixValue(v1, v2); assert(o!=0); return o; }
41
42 //______________________________________________________________________________
print(ostream & os)43 void guidoMixValue::print(ostream& os)
44 {
45 os << "guidoMixValue";
46 }
47
48 //______________________________________________________________________________
apply(Sguidovalue & arg)49 Sguidovalue guidoMixValue::apply (Sguidovalue& arg)
50 {
51 int voices = getArg1()->voices();
52 Sguidovalue top = arg->top(voices);
53 Sguidovalue bottom = arg->bottom(voices);
54 return create(getArg1()->apply(top), getArg2()->apply(bottom));
55 }
56
57 //______________________________________________________________________________
head(unsigned int length)58 Sguidovalue guidoMixValue::head (unsigned int length) { return propagate (head, length); }
head(const rational & length)59 Sguidovalue guidoMixValue::head (const rational& length) { return propagate (head, length); }
tail(unsigned int length)60 Sguidovalue guidoMixValue::tail (unsigned int length) { return propagate (tail, length); }
tail(const rational & length)61 Sguidovalue guidoMixValue::tail (const rational& length) { return propagate (tail, length); }
transpose(int interval)62 Sguidovalue guidoMixValue::transpose(int interval) { return propagate (transpose, interval); }
stretch(rational ratio)63 Sguidovalue guidoMixValue::stretch (rational ratio) { return propagate (stretch, ratio); }
stretch(float ratio)64 Sguidovalue guidoMixValue::stretch (float ratio) { return propagate (stretch, ratio); }
65
66 //______________________________________________________________________________
top(unsigned int vnum)67 Sguidovalue guidoMixValue::top (unsigned int vnum)
68 {
69 unsigned int vcount = getArg1()->voices();
70 return (vnum > vcount) ? create(getArg1(), getArg2()->top(vnum - vcount)) : getArg1()->top(vnum);
71 }
72
bottom(unsigned int vnum)73 Sguidovalue guidoMixValue::bottom (unsigned int vnum)
74 {
75 unsigned int vcount = getArg1()->voices();
76 return (vnum < vcount) ? create(getArg1()->bottom(vnum), getArg2()) : getArg2()->bottom(vnum - vcount);
77 }
78
79 //______________________________________________________________________________
length()80 unsigned int guidoMixValue::length ()
81 {
82 unsigned int l1 = getArg1()->length(); if (l1 == kInfinite) return kInfinite;
83 unsigned int l2 = getArg2()->length(); if (l2 == kInfinite) return kInfinite;
84 return (l1 > l2) ? l1 : l2;
85 }
86
duration()87 rational guidoMixValue::duration()
88 {
89 rational d1 = getArg1()->duration(); if (infinite(d1)) return d1;
90 rational d2 = getArg2()->duration(); if (infinite(d2)) return d2;
91 return (d1 > d2) ? d1 : d2;
92 }
93
voices()94 unsigned int guidoMixValue::voices ()
95 {
96 unsigned int v1 = getArg1()->voices(); if (v1 == kInfinite) return kInfinite;
97 unsigned int v2 = getArg2()->voices(); if (v2 == kInfinite) return kInfinite;
98 return v1 + v2;
99 }
100
pitch()101 unsigned int guidoMixValue::pitch ()
102 {
103 return getArg1()->pitch();
104 }
105
106 //______________________________________________________________________________
acceptIn(basevisitor & v)107 void guidoMixValue::acceptIn(basevisitor& v) {
108 if (visitor<SguidoMixValue>* p = dynamic_cast<visitor<SguidoMixValue>*>(&v)) {
109 SguidoMixValue ge = this;
110 p->visitStart (ge);
111 }
112 }
113
114 //______________________________________________________________________________
acceptOut(basevisitor & v)115 void guidoMixValue::acceptOut(basevisitor& v) {
116 if (visitor<SguidoMixValue>* p = dynamic_cast<visitor<SguidoMixValue>*>(&v)) {
117 SguidoMixValue ge = this;
118 p->visitEnd (ge);
119 }
120 }
121
122 } // namespace
123