1 /*
2  * QGuidoAR.cpp
3  *
4  * Created by Christophe Daudin on 12/05/09.
5  * Copyright 2009 Grame. All rights reserved.
6  *
7  * GNU Lesser General Public License Usage
8  * Alternatively, this file may be used under the terms of the GNU Lesser
9  * General Public License version 2.1 as published by the Free Software
10  * Foundation and appearing in the file LICENSE.LGPL included in the
11  * packaging of this file.  Please review the following information to
12  * ensure the GNU Lesser General Public License version 2.1 requirements
13  * will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
14  *
15  *
16  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
17  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18  */
19 #include "QGuidoAR.h"
20 
21 
22 #include <sstream>
23 #include <assert.h>
24 
25 #ifdef WIN32
26 # pragma warning (disable : 4786)
27 # define basename(name)	(name)
28 # define _CRT_SECURE_NO_DEPRECATE
29 #else
30 # include <libgen.h>
31 #endif
32 
33 #include <stdlib.h>
34 #include <iostream>
35 #include <QtDebug>
36 
37 #include "libguidoar.h"
38 
39 typedef guido::garErr (*GuidoAROperation)( const char * , const char * , std::ostream& );
40 
41 /*
42 //--------------------------------------------------------------------------------
43 QString operate( const QString& str1 , const QString& str2 ,guido::operation * guidoOperation )
44 {
45 	guido::guidoparser r;
46 	guido::SARMusic g1 = r.parseString( str1.toUtf8().data() );
47 	if (!g1)
48 //		readErr(*argsPtr);
49 		return "";
50 
51 	guido::SARMusic g2 = r.parseString( str2.toUtf8().data() );
52 	if (!g2)
53 //		readErr(*argsPtr);
54 		return "";
55 
56 	g1 = (*guidoOperation)(g1, g2);
57 
58 	if (g1)
59 	{
60 		guido::Sguidoelement result = g1;
61 		std::ostringstream oss;
62 		oss << result;
63 		QString str_result( oss.str().c_str() );
64 		return str_result;
65 	}
66 	return "";
67 }
68 */
69 
70 //--------------------------------------------------------------------------------
operate(const QString & str1,const QString & str2,GuidoAROperation operation)71 QString operate( const QString& str1 , const QString& str2 , GuidoAROperation operation )
72 {
73 	std::ostringstream oss;
74 	guido::garErr err = operation( str1.toUtf8().constData() , str2.toUtf8().constData() , oss );
75 	if ( err == guido::kNoErr )
76 		return QString( oss.str().c_str() );
77 	else
78 		return "Error";
79 }
80 
81 
82 //--------------------------------------------------------------------------------
sequence(const QString & str1,const QString & str2)83 QString QGuidoAR::sequence( const QString& str1 , const QString& str2 )
84 {
85 	return operate( str1 , str2 , guido::guidoGSeq );
86 }
87 
88 //--------------------------------------------------------------------------------
parallelAlignToOperation(QGuidoAR::ParallelAlign parallelAlign)89 GuidoAROperation parallelAlignToOperation( QGuidoAR::ParallelAlign parallelAlign )
90 {
91 	switch (parallelAlign)
92 	{
93 		case QGuidoAR::Left:	return guido::guidoGPar;
94 		case QGuidoAR::Right:	return guido::guidoGRPar;
95 		default :				assert(0); return guido::guidoGPar;
96 	}
97 }
98 
99 //--------------------------------------------------------------------------------
parallel(const QString & str1,const QString & str2,ParallelAlign parallelAlign)100 QString QGuidoAR::parallel( const QString& str1 , const QString& str2 , ParallelAlign parallelAlign )
101 {
102 	return operate( str1 , str2 , parallelAlignToOperation(parallelAlign) );
103 }
104 
105 //--------------------------------------------------------------------------------
duration(const QString & str1,const QString & str2)106 QString QGuidoAR::duration( const QString& str1 , const QString& str2 )
107 {
108 	return operate( str1 , str2 , guido::guidoGSetDuration );
109 }
110 
111 //--------------------------------------------------------------------------------
interleave(const QString & str1,const QString & str2)112 QString QGuidoAR::interleave( const QString& str1 , const QString& str2 )
113 {
114 	return operate( str1 , str2 , guido::guidoGSeq );
115 }
116 
117 //--------------------------------------------------------------------------------
transpose(const QString & str1,const QString & str2)118 QString QGuidoAR::transpose( const QString& str1 , const QString& str2 )
119 {
120 	return operate( str1 , str2 , guido::guidoGTranpose );
121 }
122 
123 //--------------------------------------------------------------------------------
head(const QString & str1,const QString & str2)124 QString QGuidoAR::head( const QString& str1 , const QString& str2 )
125 {
126 	return operate( str1 , str2 , guido::guidoGHead );
127 }
128 
129 //--------------------------------------------------------------------------------
tail(const QString & str1,const QString & str2)130 QString QGuidoAR::tail( const QString& str1 , const QString& str2 )
131 {
132 	return operate( str1 , str2 , guido::guidoGTail );
133 }
134 
135 //--------------------------------------------------------------------------------
top(const QString & str1,const QString & str2)136 QString QGuidoAR::top( const QString& str1 , const QString& str2 )
137 {
138 	return operate( str1 , str2 , guido::guidoGTop );
139 }
140 
141 //--------------------------------------------------------------------------------
bottom(const QString & str1,const QString & str2)142 QString QGuidoAR::bottom( const QString& str1 , const QString& str2 )
143 {
144 	return operate( str1 , str2 , guido::guidoGBottom );
145 }
146 
147 //--------------------------------------------------------------------------------
rythm(const QString & str1,const QString & str2)148 QString QGuidoAR::rythm( const QString& str1 , const QString& str2 )
149 {
150 	std::ostringstream oss;
151 	guido::garErr err = guido::guidoApplyRythm( str1.toUtf8().constData() , str2.toUtf8().constData() , guido::kApplyForwardLoop , oss );
152 	if ( err == guido::kNoErr )
153 		return QString( oss.str().c_str() );
154 	else
155 		return "Error";
156 }
157 
158 //_______________________________________________________________________________
pitch(const QString & str1,const QString & str2)159 QString QGuidoAR::pitch( const QString& str1 , const QString& str2 )
160 {
161 	std::ostringstream oss;
162 	guido::garErr err = guido::guidoApplyPitch( str1.toUtf8().constData() , str2.toUtf8().constData() , guido::kApplyForwardLoop , guido::kUseLowest , oss );
163 	if ( err == guido::kNoErr )
164 		return QString( oss.str().c_str() );
165 	else
166 		return "Error";
167 }
168 
169 #ifdef USEMidiShare
170 
171 #include "guidoparser.h"
172 #include "midicontextvisitor.h"
173 #include "midiconverter.h"
174 #include <Player.h>
175 
176 //_______________________________________________________________________________
midiExport(const QString & gmnCode,const QString & outMidiFile)177 void QGuidoAR::midiExport( const QString& gmnCode , const QString& outMidiFile )
178 {
179 	assert( outMidiFile.length() );
180 
181 	guido::guidoparser r;
182 	guido::Sguidoelement score = r.parseString( gmnCode.toUtf8().data() );
183 	if (score)
184 	{
185 		guido::midiconverter mc;
186 
187 		int err = mc.score2midifile(score, outMidiFile.toUtf8().data() );
188 		if (err != noErr)
189 			std::cerr << "error " << err << " while converting GMN to midifile " << outMidiFile.toUtf8().data() << std::endl;
190 	}
191 	else
192 	{
193 		std::cerr << "error : can't convert GMN to a valid score" << std::endl;
194 	}
195 }
196 
197 //_______________________________________________________________________________
getMidiRef(const QString & gmnCode)198 int	QGuidoAR::getMidiRef( const QString& gmnCode )
199 {
200 	guido::guidoparser r;
201 
202 	guido::Sguidoelement score = r.parseString( gmnCode.toUtf8().data() );
203 	if (score)
204 	{
205 		guido::midiconverter mc;
206 		short ref = mc.score2player(score, gmnCode.toUtf8().data());
207 		if (ref > 0) {
208 			MidiConnect (ref, 0, true);
209 			return ref;
210 		}
211 	}
212 	return -1;
213 }
214 
215 //_______________________________________________________________________________
midiPlay(int midiRef)216 void QGuidoAR::midiPlay( int midiRef )		{ StartPlayer(midiRef); }
midiPause(int midiRef)217 void QGuidoAR::midiPause( int midiRef )		{ PausePlayer(midiRef); }
midiResume(int midiRef)218 void QGuidoAR::midiResume( int midiRef )	{ ContPlayer(midiRef); }
midiStop(int midiRef)219 void QGuidoAR::midiStop( int midiRef )		{ StopPlayer (midiRef); }
midiClose(int midiRef)220 void QGuidoAR::midiClose( int midiRef )
221 {
222 	MidiConnect	(midiRef, 0, false);
223 	ClosePlayer	(midiRef);
224 }
225 #endif
226