~s-cecilio/lenmus/v5.3

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// RCS-ID: $Id: Score.cpp,v 1.3 2006/02/23 19:23:54 cecilios Exp $
//--------------------------------------------------------------------------------------
//    LenMus Phonascus: The teacher of music
//    Copyright (c) 2002-2006 Cecilio Salmeron
//
//    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) any later version.
//
//    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, write to the Free Software Foundation, Inc., 51 Franklin Street, 
//    Fifth Floor, Boston, MA  02110-1301, USA.
//
//    For any comment, suggestion or feature request, please contact the manager of 
//    the project at cecilios@users.sourceforge.net
//
//-------------------------------------------------------------------------------------
/*! @file Score.cpp
    @brief Implementation file for class lmScore
    @ingroup score_kernel
*/
//--------------------------------------------------------------------------------------------------
/*! @class lmScore
    @ingroup score_kernel

    A score is a collection of instruments (\<parts\> in MusicXML).
    An instrument is, normally, one VStaff, but more VStaves are posible (for what?)
    And an VStaff is a collection of StaffObjs.

*/
//--------------------------------------------------------------------------------------------------

#ifdef __GNUG__
// #pragma implementation
#endif

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

#include "wx/debug.h"
#include "wx/list.h"
#include "Score.h"
#include "../app/global.h"

// global unique variables used during score building
lmNoteRest*    g_pLastNoteRest;
lmBeam*        g_pCurBeam;            // lmBeam object that is being built ot NULL if none in process. See lmNote constructor

// access to colors
#include "../globals/Colors.h"
extern lmColors* g_pColors;

//---------------------------------------------------------------------------------------
// lmScore constructors and destructor
//---------------------------------------------------------------------------------------

lmScore::lmScore()
{
    //Set up an empty score, that is, without any lmInstrument.
    
    m_pTitle = (lmText*)NULL;        //no title
    m_pSubtitle = (lmText*)NULL;    //no subtitle

    //create the renderer object
    m_pFormatter = new lmFormatter3();
    m_pFormatter->SetScore(this);

    //initializations
    m_pSoundMngr = (lmSoundManager*)NULL;

    //! @todo fill this not with constants
    m_nSystemsDistance = 20000;            // 2 cm = 20 mm = 20,000 microns
    m_nTopSystemDistance = 20000;        // 2 cm = 20 mm = 20,000 microns
    m_nSystemsLeftMargin = 0;
    m_nSystemsRightMargin = 0;

}

lmScore::~lmScore()
{
    //wxLogMessage(_T("[lmScore::~lmScore] Deleting lmScore object"));
    if (m_pFormatter) delete m_pFormatter;
    m_cInstruments.DeleteContents(true);
    m_cInstruments.Clear();

    m_cGlobalStaffobjs.DeleteContents(true);
    m_cGlobalStaffobjs.Clear();

    if (m_pSoundMngr) {
        m_pSoundMngr->DeleteEventsTable();
        delete m_pSoundMngr;
    }

    m_cHighlighted.DeleteContents(false);    //Staffobjs must not be deleted, only the list
    m_cHighlighted.Clear();

    if (m_pTitle) delete m_pTitle;
    if (m_pSubtitle) delete m_pSubtitle;

}

//---------------------------------------------------------------------------------------
// score object methods
//---------------------------------------------------------------------------------------

void lmScore::SetTitle(wxString title)
{
    if (!m_pTitle) {
        m_pTitle = new lmText(this, title, 0, 0, false, false, _T("Garamond"), 14, true, false );
        IncludeInGlobalList(m_pTitle);
    }
    else {
        m_pTitle->SetText(title);
    }
}

void lmScore::SetSubtitle(wxString subtitle)
{
    if (!m_pSubtitle) {
        m_pSubtitle = new lmText(this, subtitle, 0, 0, false, false, _T("Garamond"), 11, false, true );
        IncludeInGlobalList(m_pSubtitle);
    }
    else {
        m_pSubtitle->SetText(subtitle);
    }
}

wxInt32 lmScore::GetNumMeasures()
{
    //! @limit it is being assumed that all instruments and staves have the same number of bars
    InstrumentsList::Node *node = m_cInstruments.GetFirst();
    lmInstrument *pInstr = node->GetData();
    lmVStaff *pStaff = pInstr->GetVStaff(1);
    return(pStaff->GetNumMeasures());
}

lmInstrument* lmScore::AddInstrument(wxInt32 nVStaves, wxInt32 nMIDIChannel, wxInt32 nMIDIInstr)
{
    //add an lmInstrument with nVStaves (1..m) empty VStaves.
    //nMIDIChannel is the MIDI channel to use for playing this instrument
    lmInstrument* pInstr = new lmInstrument(this, nVStaves, nMIDIChannel, nMIDIInstr);
    m_cInstruments.Append(pInstr);
    return pInstr;
    
}

//returns lmVStaff number nVStaff (1..n), of lmInstrument nInstr (1..m)
lmVStaff* lmScore::GetVStaff(wxInt32 nInstr, wxInt32 nVStaff)
{
    wxInt32 i;
    InstrumentsList::Node *node;
    lmInstrument *pInstr;
    //iterate over the list to locate lmInstrument nInstr
    for (i=1, node = m_cInstruments.GetFirst(); 
        node && i < nInstr; node = node->GetNext(), i++ ) {}
//    wxASSERT_MSG{pInstr != 0, _T("No existe el Instrumento num. nInstr"));
    pInstr = node->GetData();
    return(pInstr->GetVStaff(nVStaff));
}

lmInstrument* lmScore::XML_FindInstrument(wxString sId)
{
    // iterate over instrument list to find the one with id == sId
    wxInstrumentsListNode *node;
    lmInstrument* pInstr = (lmInstrument*)NULL;
    for (node = m_cInstruments.GetFirst(); node; node = node->GetNext()) {
        pInstr = (lmInstrument*)node->GetData();
        if (pInstr->XML_GetId() == sId) break;
    }
    return pInstr;
}

void lmScore::Draw(lmPaper *pPaper)
{
//            Optional fMetodoJustificado As Boolean = True, _
//            Optional nTipoEspaciado As ESpacingMethod = esm_PropConstantShortNote, _
//            Optional fJustificada As Boolean = True, _
//            Optional fTruncarUltimoSistema As Boolean = False, _
//            Optional rFactorAjuste As Single = 1#)
//


    ////DEBUG: draw green lines to show page borders
    //wxDC* pDC = pPaper->GetDC();
    //wxASSERT(pDC);
    //lmMicrons yTop = pPaper->GetCursorY();
    //lmMicrons xLeft = pPaper->GetLeftMarginXPos();
    //lmMicrons xRight = pPaper->GetRightMarginXPos();
    //pDC->SetPen(*wxGREEN_PEN);
    //pDC->DrawLine(xLeft, yTop, xRight, yTop);    //top horizontal
    //pDC->DrawLine(xLeft, yTop, xLeft, (pPaper->GetPaperSize()).GetHeight());    //left vertical
    //pDC->DrawLine(xRight, yTop, xRight, (pPaper->GetPaperSize()).GetHeight());    //right vertical
    ////End DEBUG --------------------------------------------

    //WriteTitles(DO_MEASURE, pPaper);
    //WriteTitles(DO_DRAW, pPaper);
    m_pFormatter->RenderScore(pPaper);

}

void lmScore::WriteTitles(bool fMeasuring, lmPaper *pPaper)
{
    long nWidth, nHeight;
    lmMicrons xPos=0;

    if (fMeasuring) {
        // Measurement phase ---------------------------------------------------

        m_nHeadersHeight = 0;

        // measure Title if exits
        if (m_pTitle) {
            // center Title
            if (!m_pTitle->IsFixed()) {
                m_pTitle->Draw(DO_MEASURE, pPaper);
                nWidth = m_pTitle->GetSelRect().width;
                nHeight = m_pTitle->GetSelRect().height;
                xPos = (pPaper->GetRightMarginXPos() - pPaper->GetLeftMarginXPos() - nWidth)/2;
                pPaper->SetCursorX(pPaper->GetLeftMarginXPos() + xPos);
            }
            m_pTitle->Draw(DO_MEASURE, pPaper);
            m_pTitle->SetFixed(true);
            nWidth = m_pTitle->GetSelRect().width;
            nHeight = m_pTitle->GetSelRect().height;

            // advance paper and update headers total height
            pPaper->IncrementCursorY(nHeight);
            m_nHeadersHeight += nHeight;
        }

        // measure Subtitle if exits
        if (m_pSubtitle) {
            // center subtitle
            if (!m_pSubtitle->IsFixed()) {
                m_pSubtitle->Draw(DO_MEASURE, pPaper);
                nWidth = m_pSubtitle->GetSelRect().width;
                nHeight = m_pSubtitle->GetSelRect().height;
                xPos = (pPaper->GetRightMarginXPos() - pPaper->GetLeftMarginXPos() - nWidth)/2;
                pPaper->SetCursorX(pPaper->GetLeftMarginXPos() + xPos);
            }

            //render subtitle
            m_pSubtitle->Draw(DO_MEASURE, pPaper);
            m_pSubtitle->SetFixed(true);
            nWidth = m_pSubtitle->GetSelRect().width;
            nHeight = m_pSubtitle->GetSelRect().height;

            //update headers total height
            m_nHeadersHeight += nHeight;
        }

    }

    else {
        // Drawing phase ---------------------------------------------------
        if (m_pTitle) m_pTitle->Draw(DO_DRAW, pPaper);
        if (m_pSubtitle) m_pSubtitle->Draw(DO_DRAW, pPaper);
    }

}

void lmScore::IncludeInGlobalList(lmStaffObj* pSO)
{
    m_cGlobalStaffobjs.Append(pSO);
}

void lmScore::RemoveFromGlobalList(lmStaffObj* pSO)
{
    m_cGlobalStaffobjs.DeleteObject(pSO);
}

//=========================================================================================
// Methods for finding StaffObjs
//=========================================================================================

lmScoreObj* lmScore::FindSelectableObject(wxPoint& pt)
{
    wxInt32 iVStaff;
    lmInstrument *pInstr;
    lmVStaff *pStaff;
    InstrumentsList::Node *node;
    lmScoreObj* pScO;

    //Look up in the global StaffObjs list
    wxStaffObjsListNode* pNode;
    for(pNode = m_cGlobalStaffobjs.GetFirst(); pNode; pNode = pNode->GetNext()) {
        pScO = (lmScoreObj*)pNode->GetData();
        if (pScO->IsAtPoint(pt)) {
            return pScO;
        }
    }

    //Not found in global list. Look up in the VStaffs' lists

    //for each instrument
    for (node = m_cInstruments.GetFirst(); node; node=node->GetNext()) {
        pInstr = node->GetData();
        
        //for each lmVStaff
        for (iVStaff=1; iVStaff <= pInstr->GetNumStaves(); iVStaff++) {
            pStaff = pInstr->GetVStaff(iVStaff);
            
            //look for posible lmStaffObj and exit if any found
            pScO = pStaff->FindSelectableObject(pt);
            if (pScO) return(pScO);
        }
    }
    return (lmScoreObj *)NULL;    //none found
}

////Friend methods for lmFormatter object and for internal use
////=========================================================================================

lmInstrument* lmScore::GetFirstInstrument()
{
    m_pNode = m_cInstruments.GetFirst();
    return (m_pNode ? (lmInstrument *)m_pNode->GetData() : (lmInstrument *)m_pNode);
}

lmInstrument* lmScore::GetNextInstrument()
{
    wxASSERT(m_pNode);
    m_pNode = m_pNode->GetNext();
    return (m_pNode ? (lmInstrument *)m_pNode->GetData() : (lmInstrument *)m_pNode);
}

lmInstrument* lmScore::GetLastInstrument()
{
    wxASSERT(m_pNode);
    m_pNode = m_cInstruments.GetLast();
    return (m_pNode ? (lmInstrument *)m_pNode->GetData() : (lmInstrument *)m_pNode);
}

////Returns the lmInstrument number nInstr (1..n)
//Friend Property Get lmInstrument(nInstr As Long) As CInstrumento
//    Set lmInstrument = m_cInstruments.Item(nInstr)
//    
//}
//
////Returns the number of Instruments in the Score
//Friend Property Get InstrumentsCount() As Long
//    InstrumentsCount = m_cInstruments.Count
//    
//}
//
////== End of Friend methods for lmFormatter object ============================================

wxString lmScore::Dump()
{
    wxString sDump = _T("Global objects:\n");

    //loop to dump global StaffObjs
    lmStaffObj* pSO;
    wxStaffObjsListNode* pNode;
    for(pNode = m_cGlobalStaffobjs.GetFirst(); pNode; pNode = pNode->GetNext()) {
        pSO = (lmStaffObj*)pNode->GetData();
        sDump += pSO->Dump();
    }

    //loop to dump all instruments
    sDump += _T("\nLocal objects:\n");
    lmInstrument *pInstr = GetFirstInstrument();
    for (int i=1; i<= (int)m_cInstruments.GetCount(); i++, pInstr = GetNextInstrument())
    {
        sDump += wxString::Format(_T("\nInstrument %d\n"), i );
        sDump += pInstr->Dump();
    }
    return sDump;
}

wxString lmScore::SourceLDP()
{
    wxString sSource = 
        wxString::Format(_T("Partitura\n   (Vers 1.3)\n   (NumInstrumentos %d)\n"),
                    m_cInstruments.GetCount() );

    //loop for each instrument
     lmInstrument *pInstr = GetFirstInstrument();
    for (int i=1; i<= (int)m_cInstruments.GetCount(); i++, pInstr = GetNextInstrument())
    {
        sSource += wxString::Format(_T("   (Instrumento %d\n"), i);
        sSource += pInstr->SourceLDP();
        sSource += _T("   )\n");
    }
    sSource += _T(")");
    return sSource;
}

wxString lmScore::SourceXML()
{
    wxString sSource = _T("TODO: lmScore XML Source code generation methods");

//    Dim i As Long, sFuente As String
//    
//    sFuente = "<?xml version=""1.0"" standalone=""no""?>" & sCrLf & _
//            "<!DOCTYPE score-partwise PUBLIC ""-//Recordare//DTD MusicXML 0.7 Partwise//EN"" " & _
//            """http://www.musicxml.org/dtds/partwise.dtd"">" & sCrLf & _
//            "<score-partwise>" & sCrLf & _
//            "  <part-list>" & sCrLf
////        <score-part id="P1">
////            <part-name>Voice</part-name>
////        </score-part>
//
////    sFuente = sFuente & "<Partitura" & sCrLf & "   (Vers 1.3)" & sCrLf & "   (NumInstrumentos " & _
////        m_cInstruments.Count & ")" & sCrLf
//    for (i = 1 To m_cInstruments.Count
//        sFuente = sFuente & "    <score-part id=""P" & i & """ >" & sCrLf
////        sFuente = sFuente & m_cInstruments.Item(i).FuenteXML
////        sFuente = sFuente & "   )" & sCrLf
//        sFuente = sFuente & "    </score-part>" & sCrLf
//    }    // i
//    sFuente = sFuente & "  </part-list>" & sCrLf
//    for (i = 1 To m_cInstruments.Count
//        sFuente = sFuente & "  <part id=""P" & i & """ >" & sCrLf
//        sFuente = sFuente & m_cInstruments.Item(i).FuenteXML
//        sFuente = sFuente & "  </part>" & sCrLf
//    }    // i
//    FuenteXML = sFuente & "</score-partwise>" & sCrLf
    return sSource;
        
}

////Toca el compas nCompas (1 .. n)
//void lmScore::PlayMeasure(nCompas As Long, fVisualTracking As Boolean, _
//                fRecuadrarCompas As Boolean, nPlayMode As EPlayMode)
//{
//
//    if (!fMIDIEnabled) return;
//    if (m_pSoundMngr Is Nothing) { ComputeMidiEvents
//
//    m_pSoundMngr->PlayMeasure nCompas, fVisualTracking, fRecuadrarCompas, nPlayMode
//
//}

void lmScore::Play(bool fVisualTracking, bool fMarcarCompasPrevio, EPlayMode nPlayMode, 
                 long nMM, wxWindow* pWindow)
{
    if (!m_pSoundMngr) {
        m_pSoundMngr = new lmSoundManager();
        ComputeMidiEvents();
    }

    m_pSoundMngr->Play(fVisualTracking, fMarcarCompasPrevio, nPlayMode, nMM, pWindow);

}

void lmScore::PlayMeasure(int nMeasure, bool fVisualTracking, EPlayMode nPlayMode,
                          long nMM, wxWindow* pWindow)
{
    if (!m_pSoundMngr) {
        m_pSoundMngr = new lmSoundManager();
        ComputeMidiEvents();
    }

    m_pSoundMngr->PlayMeasure(nMeasure, fVisualTracking, nPlayMode, nMM, pWindow);
}

void lmScore::Pause()
{
    if (!m_pSoundMngr) return;
    m_pSoundMngr->Pause();

}

void lmScore::Stop()
{
    if (!m_pSoundMngr) return;
    m_pSoundMngr->Stop();

}

void lmScore::WaitForTermination()
{
    if (!m_pSoundMngr) return;
    m_pSoundMngr->WaitForTermination();

}

void lmScore::ScoreHighlight(lmStaffObj* pSO, lmPaper* pPaper, EHighlightType nHighlightType)
{
    switch (nHighlightType) {
        case eVisualOn:
            m_cHighlighted.Append(pSO);
            pSO->Draw(DO_DRAW, pPaper, g_pColors->ScoreHighlight() );
            break;

        case eVisualOff:
            m_cHighlighted.DeleteObject(pSO);
            RemoveHighlight(pSO, pPaper);
            break;

        case eRemoveAllHighlight:
            //remove highlight from all staffobjs in m_cHighlighted list
            wxStaffObjsListNode* pNode;
            for(pNode = m_cHighlighted.GetFirst(); pNode; pNode = pNode->GetNext()) {
                RemoveHighlight( (lmStaffObj*)pNode->GetData(), pPaper );
            }
            //clear the list
            m_cHighlighted.DeleteContents(false);    //Staffobjs must not be deleted, only the list
            m_cHighlighted.Clear();
            break;

        default:
            wxASSERT(false);
    }

}

void lmScore::RemoveHighlight(lmStaffObj* pSO, lmPaper* pPaper)
{
    /*! @todo
        If we paint in black it remains a red aureole around
        the note. By painting it first in white the size of the aureole
        is smaller but still visible. A posible better solution is to
        modify Draw() method to accept an additional parameter: a flag
        to signal that XOR draw mode in RED followed by a normal
        draw in BLACK must be done.
    */
    pSO->Draw(DO_DRAW, pPaper, *wxWHITE);
    pSO->Draw(DO_DRAW, pPaper, g_pColors->ScoreNormal() );
}

void lmScore::ComputeMidiEvents()
{
    int nChannel, nInstr;        //MIDI info. for instrument in process
    lmSoundManager* pSM;            
    
    if (m_pSoundMngr)
        m_pSoundMngr->DeleteEventsTable();
    else
        m_pSoundMngr = new lmSoundManager();

    //Loop to generate Midi events for each instrument
    lmVStaff* pVStaff;
     lmInstrument *pInstr = GetFirstInstrument();
    for (int i=1; i<= (int)m_cInstruments.GetCount(); i++, pInstr = GetNextInstrument())
    {       
        nChannel = pInstr->GetMIDIChannel();
        nInstr = pInstr->GetMIDIInstrument();

       //for each lmVStaff
        for (int iVStaff=1; iVStaff <= pInstr->GetNumStaves(); iVStaff++) {
            pVStaff = pInstr->GetVStaff(iVStaff);
            pSM = pVStaff->ComputeMidiEvents(nChannel);
            m_pSoundMngr->Append(pSM);
            delete pSM;
        }

        //Add an event to program sound for this instrument
        m_pSoundMngr->StoreEvent(0, eSET_ProgInstr, nChannel, nInstr, 0, (lmStaffObj*)NULL, 0);
        
    }
    
    //End up Midi events table and sort it by time
    m_pSoundMngr->CloseTable();
    
}

wxString lmScore::DumpMidiEvents()
{
    if (!m_pSoundMngr) ComputeMidiEvents();
    return m_pSoundMngr->DumpMidiEvents();
}