~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/song/managers/MeasureManager.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.herac.tuxguitar.song.managers;
2
 
 
3
 
import java.util.ArrayList;
4
 
import java.util.Iterator;
5
 
import java.util.List;
6
 
 
7
 
import org.herac.tuxguitar.gui.editors.tab.TablatureUtil;
8
 
import org.herac.tuxguitar.song.models.Component;
9
 
import org.herac.tuxguitar.song.models.Duration;
10
 
import org.herac.tuxguitar.song.models.InstrumentString;
11
 
import org.herac.tuxguitar.song.models.Measure;
12
 
import org.herac.tuxguitar.song.models.Note;
13
 
import org.herac.tuxguitar.song.models.Silence;
14
 
import org.herac.tuxguitar.song.models.SongTrack;
15
 
import org.herac.tuxguitar.song.models.Tupleto;
16
 
import org.herac.tuxguitar.song.models.effects.BendEffect;
17
 
import org.herac.tuxguitar.song.models.effects.GraceEffect;
18
 
import org.herac.tuxguitar.song.models.effects.HarmonicEffect;
19
 
import org.herac.tuxguitar.song.models.effects.TremoloBarEffect;
20
 
import org.herac.tuxguitar.song.models.effects.TremoloPickingEffect;
21
 
import org.herac.tuxguitar.song.models.effects.TrillEffect;
22
 
 
23
 
public class MeasureManager {
24
 
        private SongManager songManager;
25
 
        
26
 
        public MeasureManager(SongManager songManager){
27
 
                this.songManager = songManager;
28
 
        }
29
 
        
30
 
    public void orderNotes(Measure measure){
31
 
        for(int i = 0;i < measure.getNotes().size();i++){
32
 
            Note minNote = null;
33
 
            for(int noteIdx = i;noteIdx < measure.getNotes().size();noteIdx++){
34
 
                Note note = (Note)measure.getNotes().get(noteIdx);
35
 
                if(minNote == null || note.getStart() < minNote.getStart()){
36
 
                    minNote = note;
37
 
                }
38
 
            }
39
 
            measure.getNotes().remove(minNote);
40
 
            measure.getNotes().add(i,minNote);
41
 
        }
42
 
    }
43
 
        
44
 
    public void addNote(Measure measure,Note note){                          
45
 
        //Verifico si entra en el compas
46
 
        if(canInsert(measure,note,true,false)){
47
 
            //Borro lo que haya en la misma posicion
48
 
            removeComponentsAt(measure,note.getStart(),note.getString(),false);            
49
 
            
50
 
            //Agrego la nota
51
 
            measure.addNote(note);            
52
 
            
53
 
            //trato de agregar un silencio similar al lado
54
 
            tryChangeSilenceAfter(measure,note);
55
 
        }                
56
 
    }
57
 
        
58
 
        
59
 
        public void removeNote(Measure measure,Note note){
60
 
                measure.removeNote(note);
61
 
        }
62
 
 
63
 
    /**
64
 
     * Agrega un silencio al compas
65
 
     */
66
 
    public void addSilence(Measure measure,Silence silence){                
67
 
        //Verifico si entra en el compas
68
 
        if(canInsert(measure,silence,true,false)){
69
 
            //Borro lo que haya en la misma posicion
70
 
            removeAllComponentsAt(measure,silence.getStart());             
71
 
            
72
 
            //Agrego el silencio
73
 
            measure.addSilence(silence);           
74
 
        }
75
 
    }  
76
 
        
77
 
        public void removeSilence(Measure measure,Silence silence){
78
 
                measure.removeSilence(silence);
79
 
        }
80
 
        
81
 
    /**
82
 
     * Elimina un silencio del compas.
83
 
     * si se asigna moveNextComponents = true. los componentes que le siguen 
84
 
     * se moveran para completar el espacio vacio que dejo el silencio
85
 
     */         
86
 
    public void removeSilence(Measure measure,Silence silence,boolean moveNextComponents){
87
 
        //TODO sacar el calculo de length con el nextComponent cuando en autosilences se agreguen tresillos
88
 
        List components = getComponents(measure);
89
 
        Component component = getComponent(components,silence.getStart());
90
 
        Component nextComponent = getNextComponent(components,component);
91
 
        
92
 
        
93
 
        removeSilence(measure,silence);
94
 
        if(moveNextComponents){
95
 
            long start = silence.getStart();
96
 
            long length = silence.getDuration().getTime();
97
 
            
98
 
            if(nextComponent != null){
99
 
                length = nextComponent.getStart() - start;                
100
 
            }
101
 
            
102
 
            moveComponents(measure,start + length,-length);
103
 
        }
104
 
    }   
105
 
        
106
 
    public void removeNotesAfterString(Measure measure,int string){  
107
 
        List notesToRemove = new ArrayList();
108
 
        Iterator it = measure.getNotes().iterator();
109
 
        while(it.hasNext()){
110
 
            Note note = (Note)it.next();            
111
 
            if(note.getString() > string){
112
 
                notesToRemove.add(note);                
113
 
            }            
114
 
        } 
115
 
        it = notesToRemove.iterator();
116
 
        while(it.hasNext()){
117
 
            Note note = (Note)it.next();            
118
 
            removeNote(measure,note);        
119
 
        } 
120
 
    }
121
 
    
122
 
    /**
123
 
     * Retorna Todas las Notas en la posicion Start
124
 
     */
125
 
    public List getNotes(Measure measure,long start) {
126
 
        List notes = new ArrayList();        
127
 
        Iterator it = measure.getNotes().iterator();
128
 
        while(it.hasNext()){           
129
 
            Note note = (Note)it.next();
130
 
            if (note.getStart() == start) {
131
 
                notes.add(note);
132
 
            }
133
 
        }
134
 
        return notes;
135
 
    }    
136
 
    
137
 
    /**
138
 
     * Retorna las Nota en la posicion y cuerda
139
 
     */
140
 
    public Note getNote(Measure measure,long start,int string) {        
141
 
        Iterator it = measure.getNotes().iterator();
142
 
        while(it.hasNext()){           
143
 
            Note note = (Note)it.next();
144
 
            if (note.getStart() == start && note.getString() == string) {
145
 
                return note;
146
 
            }
147
 
        }
148
 
        return null;
149
 
    }    
150
 
    
151
 
    /**
152
 
     * Mueve todas las notas
153
 
     */
154
 
    public void moveAllNotes(Measure measure,long theMove){
155
 
        moveComponents(measure.getNotes(),theMove);
156
 
    }  
157
 
    
158
 
    /**
159
 
     * Mueve todos los silencios
160
 
     */
161
 
    public void moveAllSilences(Measure measure,long theMove){
162
 
        moveComponents(measure.getSilences(),theMove);
163
 
    }  
164
 
    
165
 
    /**
166
 
     * Mueve todos los componentes
167
 
     */
168
 
    public void moveAllComponents(Measure measure,long theMove){
169
 
        moveComponents(getComponents(measure),theMove);
170
 
    } 
171
 
    
172
 
    
173
 
    /**
174
 
     * Mueve los componentes ubicados en start
175
 
     */    
176
 
    public boolean moveComponents(Measure measure,long start,long theMove){
177
 
        //obtengo los datos del compas
178
 
        long measureStart = measure.getStart();
179
 
        long measureLength = measure.getLength();
180
 
        
181
 
        //muevo los componentes
182
 
        List components = getComponentsBeforeEnd(getComponents(measure),start);
183
 
        moveComponents(components,theMove);
184
 
        
185
 
        //si el compas no quedo correctamente vuelvo a dejar todo como estaba
186
 
        Component first = getFirstComponent(getComponents(measure));
187
 
        while(first instanceof Silence){
188
 
            removeSilence(measure,(Silence)first);
189
 
            first = getNextComponent(getComponents(measure),first);
190
 
        }
191
 
        Component last = getLastComponent(getComponents(measure));
192
 
        while(last instanceof Silence){
193
 
            removeSilence(measure,(Silence)last);
194
 
            last = getPreviousComponent(getComponents(measure),last);
195
 
        }
196
 
        if(first != null && last != null){
197
 
            if(first.getStart() < measureStart || (last.getStart() + last.getDuration().getTime()) > (measureStart + measureLength)){
198
 
                moveComponents(components,-theMove);
199
 
                return false;
200
 
            }
201
 
        }
202
 
        return true;
203
 
    }       
204
 
    
205
 
    /**
206
 
     * Mueve los componentes
207
 
     */    
208
 
    private void moveComponents(List components,long theMove){
209
 
        Iterator it = components.iterator();
210
 
        while(it.hasNext()){
211
 
            Component component = (Component)it.next();
212
 
            moveComponent(component,theMove);             
213
 
        }       
214
 
    }
215
 
    
216
 
    /**
217
 
     * Mueve el componente
218
 
     */    
219
 
    private void moveComponent(Component component,long theMove){
220
 
        //obtengo el start viejo
221
 
        long start = component.getStart();
222
 
        
223
 
        //asigno el nuevo start
224
 
        component.setStart(start + theMove);                                   
225
 
    }
226
 
   
227
 
    /**
228
 
     * Retorna los silencios en la posicion start
229
 
     */
230
 
    public List getSilences(Measure measure,long start) {
231
 
        List silences = new ArrayList();
232
 
        for (int i = 0; i < measure.getSilences().size(); i++) {
233
 
            Silence silence = (Silence) measure.getSilences().get(i);            
234
 
            if (silence.getStart() == start) {
235
 
                silences.add(silence);
236
 
            }
237
 
        }
238
 
        return silences;
239
 
    }  
240
 
    
241
 
    /**
242
 
     * Retorna el Siguiente Componente que sea de un silencio
243
 
     */
244
 
    public Silence getNextSilence(Measure measure,Component component) {
245
 
        Silence nextSilence = null;
246
 
        for (int i = 0; i < measure.getSilences().size(); i++) {
247
 
                Silence currSilence = (Silence) measure.getSilences().get(i);
248
 
            
249
 
            if (currSilence.getStart() > component.getStart()) {
250
 
                if (nextSilence == null) {
251
 
                        nextSilence = currSilence;
252
 
                } else if (currSilence.getStart() < nextSilence.getStart()) {
253
 
                        nextSilence = currSilence;
254
 
                } else if (currSilence.getStart() == nextSilence.getStart()
255
 
                        && currSilence.getDuration().getTime() <= nextSilence.getDuration().getTime()) {
256
 
                        nextSilence = currSilence;
257
 
                }
258
 
            }
259
 
            
260
 
        }
261
 
        return nextSilence;
262
 
    }  
263
 
    
264
 
    public List getComponents(Measure measure){
265
 
        List components = new ArrayList();
266
 
        components.addAll(measure.getNotes());
267
 
        components.addAll(measure.getSilences());
268
 
        return components;
269
 
    }
270
 
    
271
 
    /**
272
 
     * Retorna el Siguiente Componente
273
 
     */
274
 
    public Component getNextComponent(List components,Component component) {
275
 
        Component nextComponent = null;
276
 
        for (int noteIdx = 0; noteIdx < components.size(); noteIdx++) {
277
 
            Component currComponent = (Component) components.get(noteIdx);
278
 
            
279
 
            if (currComponent.getStart() > component.getStart()) {
280
 
                if (nextComponent == null) {
281
 
                    nextComponent = currComponent;
282
 
                } else if (currComponent.getStart() < nextComponent.getStart()) {
283
 
                    nextComponent = currComponent;
284
 
                } else if (currComponent.getStart() == nextComponent.getStart()
285
 
                        && currComponent.getDuration().getTime() <= nextComponent.getDuration().getTime()) {
286
 
                    nextComponent = currComponent;
287
 
                }
288
 
            }
289
 
        }
290
 
        return nextComponent;
291
 
    }
292
 
    
293
 
    /**
294
 
     * Retorna el Componente Anterior
295
 
     */
296
 
    public Component getPreviousComponent(List components,Component component) {
297
 
        Component prevComponent = null;
298
 
        for (int noteIdx = 0; noteIdx < components.size(); noteIdx++) {
299
 
            Component currComponent = (Component) components.get(noteIdx);
300
 
 
301
 
            if (currComponent.getStart() < component.getStart()) {
302
 
                if (prevComponent == null) {
303
 
                    prevComponent = currComponent;
304
 
                } else if (currComponent.getStart() > prevComponent.getStart()) {
305
 
                    prevComponent = currComponent;
306
 
                } else if (currComponent.getStart() == prevComponent.getStart()
307
 
                        && currComponent.getDuration().getTime() <= prevComponent.getDuration().getTime()) {
308
 
                    prevComponent = currComponent;
309
 
                }
310
 
            }
311
 
        }
312
 
        return prevComponent;
313
 
    }
314
 
    
315
 
    /**
316
 
     * Retorna el Primer Componente
317
 
     */
318
 
    public Component getFirstComponent(List components) {
319
 
        Component firstComponent = null;
320
 
        for (int i = 0; i < components.size(); i++) {
321
 
            Component currComponent = (Component) components.get(i);            
322
 
            if (firstComponent == null || currComponent.getStart() < firstComponent.getStart()) {
323
 
                firstComponent = currComponent;
324
 
            }
325
 
        }
326
 
        return firstComponent;
327
 
    }    
328
 
    
329
 
    /**
330
 
     * Retorna el Ultimo Componente
331
 
     */
332
 
    public Component getLastComponent(List components) {
333
 
        Component lastComponent = null;
334
 
        for (int i = 0; i < components.size(); i++) {
335
 
            Component currComponent = (Component) components.get(i);            
336
 
            if (lastComponent == null || lastComponent.getStart() < currComponent.getStart()) {
337
 
                lastComponent = currComponent;
338
 
            }
339
 
        }
340
 
        return lastComponent;
341
 
    }     
342
 
    
343
 
    /**
344
 
     * Retorna los componentes en la posicion start
345
 
     */
346
 
    public List getComponents(List components,long start) {
347
 
        List componentAtStart = new ArrayList();
348
 
        for (int i = 0; i < components.size(); i++) {
349
 
            Component currComponent = (Component) components.get(i);            
350
 
            if (currComponent.getStart() == start) {
351
 
                componentAtStart.add(currComponent);
352
 
            }
353
 
        }
354
 
        return componentAtStart;
355
 
    }  
356
 
    
357
 
    /**
358
 
     * Retorna Un Componente en la posicion start
359
 
     */
360
 
    public Component getComponent(List components,long start) {
361
 
        Component component = null;
362
 
        for (int noteIdx = 0; noteIdx < components.size(); noteIdx++) {
363
 
            Component currComponent = (Component) components.get(noteIdx);            
364
 
            if (currComponent.getStart() == start) {
365
 
                component = currComponent;
366
 
                break;
367
 
            }
368
 
        }
369
 
        return component;
370
 
    }         
371
 
    
372
 
    /**
373
 
     * Retorna Todos los desde Start hasta el final del compas
374
 
     */
375
 
    public List getComponentsBeforeEnd(List components,long fromStart) {
376
 
        List componentBeforeEnd = new ArrayList();        
377
 
        Iterator it = components.iterator();
378
 
        while(it.hasNext()){           
379
 
            Component currComponent = (Component)it.next();
380
 
            if (currComponent.getStart() >= fromStart) {
381
 
                componentBeforeEnd.add(currComponent);
382
 
            }
383
 
        }
384
 
        return componentBeforeEnd;
385
 
    } 
386
 
    
387
 
    /**
388
 
     * Retorna Un Componente diferente en el mismo beat
389
 
     */
390
 
    public Component getComponentAtSameBeat(List components,Component beatComponent) {
391
 
        Component component = null;
392
 
        for (int noteIdx = 0; noteIdx < components.size(); noteIdx++) {
393
 
            Component currComponent = (Component) components.get(noteIdx);            
394
 
            if (currComponent.getStart() == beatComponent.getStart() && !currComponent.equals(beatComponent)) {
395
 
                component = currComponent;
396
 
                break;
397
 
            }
398
 
        }
399
 
        return component;
400
 
    }       
401
 
    
402
 
    /**
403
 
     * Elimina los Componentes que empiecen en Start y esten en la misma cuerda
404
 
     * Si hay un Silencio lo borra sin importar la cuerda
405
 
     */
406
 
    public void removeComponentsAt(Measure measure,long start,int string,boolean addSilence){
407
 
        if(string != -1){
408
 
                List notes = getNotes(measure,start);
409
 
                Iterator it = notes.iterator();
410
 
                while(it.hasNext()){
411
 
                        Note note = (Note)it.next();
412
 
                        if(note.getString() == string){
413
 
                                removeNote(measure,note);
414
 
                    
415
 
                                //si era el unico componente agrego un silencio
416
 
                                if(addSilence && notes.size() == 1){
417
 
                                        addSilence(measure,new Silence(note.getStart(),(Duration)note.getDuration().clone()));
418
 
                                }                
419
 
                        }
420
 
                }    
421
 
        }
422
 
        List silences = getSilences(measure,start);
423
 
        
424
 
        Iterator it = silences.iterator();
425
 
        while(it.hasNext()){
426
 
            Silence silence = (Silence)it.next();
427
 
            removeSilence(measure,silence);                            
428
 
        }        
429
 
    }
430
 
    
431
 
    /**
432
 
     * Elimina todos los Componentes 
433
 
     */
434
 
    public void removeAllComponents(Measure measure){
435
 
        measure.getNotes().clear();
436
 
        measure.getSilences().clear();
437
 
    }    
438
 
    
439
 
    /**
440
 
     * Elimina los Componentes que empiecen en Start 
441
 
     */
442
 
    public void removeAllComponentsAt(Measure measure,long start){
443
 
        List notes = getNotes(measure,start);
444
 
        Iterator it = notes.iterator();
445
 
        while(it.hasNext()){
446
 
            Note note = (Note)it.next();                
447
 
            removeNote(measure,note);                            
448
 
        }       
449
 
        
450
 
        List silences = getSilences(measure,start);
451
 
        it = silences.iterator();
452
 
        while(it.hasNext()){
453
 
            Silence silence = (Silence)it.next();            
454
 
            removeSilence(measure,silence);            
455
 
        }        
456
 
    }
457
 
    
458
 
    public void orderComponents(List components){
459
 
        for(int i = 0;i < components.size();i++){
460
 
            Component minComponent = null;
461
 
            for(int j = i;j < components.size();j++){
462
 
                Component component = (Component)components.get(j);
463
 
                if(minComponent == null || component.getStart() < minComponent.getStart()){
464
 
                    minComponent = component;
465
 
                }
466
 
            }
467
 
            components.remove(minComponent);
468
 
            components.add(i,minComponent);
469
 
        }
470
 
    }
471
 
    
472
 
  
473
 
    public void transposeNotes(Measure measure,List strings,int transpose){
474
 
        Iterator it = measure.getNotes().iterator();
475
 
        while(it.hasNext()){
476
 
                Note note = (Note)it.next();
477
 
                int value = note.getValue() + transpose;
478
 
                int string = note.getString();
479
 
                if(value < 0){
480
 
                        while(strings.size() > string){                                 
481
 
                                InstrumentString current = (InstrumentString)strings.get((string - 1));
482
 
                                InstrumentString next = (InstrumentString)strings.get(string);
483
 
                                string ++;
484
 
                                value += (current.getValue() - next.getValue());
485
 
                                if(value >= 0){
486
 
                                        break;
487
 
                                }
488
 
                        }
489
 
                }                       
490
 
                if(value >= 0){
491
 
                        note.setValue(value);
492
 
                        note.setString(string);
493
 
                }
494
 
        }
495
 
    }
496
 
    
497
 
    
498
 
    /**
499
 
     * Mueve la nota a la cuerda de arriba
500
 
     */
501
 
    public int shiftNoteUp(Measure measure,long start,int string){
502
 
        return shiftNote(measure, start, string,-1);
503
 
    }
504
 
 
505
 
    /**
506
 
     * Mueve la nota a la cuerda de abajo
507
 
     */
508
 
    public int shiftNoteDown(Measure measure,long start,int string){            
509
 
        return shiftNote(measure, start, string,1);
510
 
    }    
511
 
    
512
 
    /**
513
 
     * Mueve la nota a la siguiente cuerda
514
 
     */
515
 
    private int shiftNote(Measure measure,long start,int string,int move){
516
 
        Note note = getNote(measure,start,string);
517
 
        SongTrack track = this.songManager.getTrack(measure);
518
 
        if(note != null && track != null){
519
 
                int nextStringNumber = (note.getString() + move);
520
 
                while(getNote(measure,start,nextStringNumber) != null){
521
 
                        nextStringNumber += move;
522
 
                }
523
 
                if(nextStringNumber >= 1 && nextStringNumber <= track.stringCount()){
524
 
                        InstrumentString currentString = track.getString(note.getString());     
525
 
                        InstrumentString nextString = track.getString(nextStringNumber);                        
526
 
                        int noteValue = (note.getValue() + currentString.getValue());
527
 
                        if(noteValue >= nextString.getValue() && ((nextString.getValue() + 30 > noteValue) || track.isPercussionTrack()) ){
528
 
                                note.setValue(noteValue - nextString.getValue());
529
 
                                note.setString(nextString.getNumber());
530
 
                                return note.getString();
531
 
                        }
532
 
                }
533
 
        }       
534
 
        return 0;
535
 
    }  
536
 
    
537
 
    /**
538
 
     * Mueve la nota 1 semitono arriba
539
 
     */        
540
 
    public boolean moveSemitoneUp(Measure measure,long start,int string){
541
 
        return moveSemitone(measure, start, string,1);
542
 
    }
543
 
    
544
 
    /**
545
 
     * Mueve la nota 1 semitono abajo
546
 
     */    
547
 
    public boolean moveSemitoneDown(Measure measure,long start,int string){
548
 
        return moveSemitone(measure, start, string,-1);
549
 
    }
550
 
    
551
 
    /**
552
 
     * Mueve la nota los semitonos indicados
553
 
     */
554
 
    private boolean moveSemitone(Measure measure,long start,int string,int semitones){
555
 
        Note note = getNote(measure,start,string);
556
 
        SongTrack track = this.songManager.getTrack(measure);
557
 
        if(note != null && track != null){
558
 
                int newValue = (note.getValue() + semitones);
559
 
                if(newValue >= 0 && (newValue < 30 || track.isPercussionTrack()) ){
560
 
                        note.setValue(newValue);
561
 
                        return true;                    
562
 
                }
563
 
        }       
564
 
        return false;
565
 
    }     
566
 
    
567
 
    /**
568
 
     * Verifica si el componente se puede insertar en el compas.
569
 
     * si no puede, con la opcion removeSilences, verifica si el motivo por el
570
 
     * cual no entra es que lo siguen silencios. de ser asi los borra. 
571
 
     */
572
 
    public boolean canInsert(Measure measure,Component component,boolean removeSilences,boolean tryMove){        
573
 
        boolean canInsert = true;
574
 
        int errorMargin = 10;
575
 
        
576
 
        List components = getComponents(measure);
577
 
        orderComponents(components);             
578
 
        
579
 
        //Si es una nota, Verifico si hay un componente en el mismo lugar, y comparo las duraciones.
580
 
        if(component instanceof Note){
581
 
                Component componentAtBeat = getComponentAtSameBeat(components,component);
582
 
                if(componentAtBeat instanceof Note){
583
 
                        if(component.getDuration().getTime() <= componentAtBeat.getDuration().getTime()){
584
 
                                return true;
585
 
                        }
586
 
                } 
587
 
        }
588
 
        
589
 
        //Verifico si hay lugar para meter la nota-----------------        
590
 
        Component nextComponent = getNextComponent(components,component);
591
 
        //si el componente es null, verifico el fin del compas
592
 
        
593
 
        long componentEnd = component.getStart() + component.getDuration().getTime();
594
 
        if(nextComponent == null){
595
 
            if(componentEnd > (measure.getStart() + measure.getLength() + errorMargin)){
596
 
                canInsert = false;
597
 
            }
598
 
        }else if(componentEnd > (nextComponent.getStart() + errorMargin)){
599
 
            canInsert = false;            
600
 
        }
601
 
        
602
 
        //---Busca si hay espacio disponible de silencios entre el componente y el el que le sigue.. si encuentra lo borra
603
 
        if(removeSilences && !canInsert && nextComponent instanceof Silence){
604
 
            //Verifico si lo que sigue es un silencio. y lo borro     
605
 
            long nextComponentEnd = 0;
606
 
            List nextSilences = new ArrayList();
607
 
            while(nextComponent instanceof Silence){  
608
 
                nextSilences.add(nextComponent);        
609
 
                nextComponentEnd = nextComponent.getStart() + nextComponent.getDuration().getTime();                  
610
 
                nextComponent = getNextComponent(components,nextComponent);
611
 
            }
612
 
            if(nextComponent == null){
613
 
                nextComponentEnd = measure.getStart() + measure.getLength();
614
 
            }else if(nextComponent instanceof Note){
615
 
                nextComponentEnd = nextComponent.getStart();
616
 
            }
617
 
            if(componentEnd <= (nextComponentEnd + errorMargin)){
618
 
                while(!nextSilences.isEmpty()){
619
 
                    Silence currSilence = (Silence)nextSilences.get(0);
620
 
                    measure.removeSilence(currSilence);
621
 
                    nextSilences.remove(currSilence);
622
 
                }
623
 
                canInsert = true;                
624
 
            }             
625
 
        }
626
 
        
627
 
        //---Busca si hay espacio disponible de silencios entre el componente y el final.. si encuentra mueve todo
628
 
        if(!canInsert && removeSilences && tryMove){
629
 
            nextComponent = getNextComponent(components,component);
630
 
            if(nextComponent != null){
631
 
                long requiredLength = (component.getDuration().getTime()  - (nextComponent.getStart() - component.getStart()));
632
 
                
633
 
                
634
 
                long nextSilenceLength = 0;
635
 
                List nextSilences = new ArrayList();
636
 
                Silence nextSilence = getNextSilence(measure,component);
637
 
                while(nextSilence != null){  
638
 
                        nextSilences.add(nextSilence);  
639
 
                        nextSilenceLength += nextSilence.getDuration().getTime();                  
640
 
                        nextSilence = getNextSilence(measure,nextSilence);
641
 
                }       
642
 
                if(requiredLength <= (nextSilenceLength + errorMargin)){
643
 
                        components = getComponentsBeforeEnd(components,nextComponent.getStart());
644
 
                    while(!components.isEmpty()){
645
 
                        Component currComponent = (Component)components.get(0);
646
 
                        if(currComponent instanceof Silence){
647
 
                            Silence currSilence = (Silence)currComponent;                                                    
648
 
                            requiredLength -= currSilence.getDuration().getTime();                            
649
 
                            measure.removeSilence(currSilence);                                
650
 
                        }else if(requiredLength > 0){
651
 
                            moveComponent(currComponent,requiredLength);      
652
 
                        }
653
 
                        
654
 
                        
655
 
                        components.remove(0);                        
656
 
                    }
657
 
                    canInsert = true;     
658
 
                }
659
 
                
660
 
            }
661
 
        }
662
 
        
663
 
        return canInsert;
664
 
    }           
665
 
    
666
 
    
667
 
    /**
668
 
     * Cambia la Duracion del componente.
669
 
     */
670
 
    public void changeDuration(Measure measure,Component component,Duration duration){
671
 
        changeDuration(measure,component,duration,true);
672
 
    }
673
 
    
674
 
    /**
675
 
     * Cambia la Duracion del componente.
676
 
     */
677
 
    public void changeDuration(Measure measure,Component component,Duration duration,boolean tryMove){        
678
 
        //obtengo la duracion vieja
679
 
        Duration oldDuration = (Duration)component.getDuration().clone();
680
 
        
681
 
        //asigno la nueva duracion
682
 
        component.setDuration((Duration)duration.clone());
683
 
        
684
 
        //si no entra vuelvo a dejar la vieja
685
 
        //if(canInsert(measure,component,true,true)){            
686
 
        if(canInsert(measure,component,true,tryMove)){
687
 
            //se lo agrego a todas las notas en la posicion
688
 
            List components = getNotes(measure,component.getStart());
689
 
            Iterator it = components.iterator();
690
 
            while(it.hasNext()){
691
 
                Component currComponent = (Component)it.next();
692
 
                currComponent.setDuration((Duration)duration.clone());
693
 
            }            
694
 
            
695
 
            //trato de agregar un silencio similar al lado            
696
 
            boolean move = (component.getDuration().getTime() > oldDuration.getTime());
697
 
            tryChangeSilenceAfter(measure,component,move);
698
 
            
699
 
        }else{
700
 
            component.setDuration(oldDuration);
701
 
        }
702
 
        
703
 
 
704
 
    }
705
 
    
706
 
    public void tryChangeSilenceAfter(Measure measure,Component component){
707
 
        tryChangeSilenceAfter(measure,component,true);
708
 
    }
709
 
    
710
 
    public void tryChangeSilenceAfter(Measure measure,Component component,boolean tryMove){
711
 
        List components = getComponents(measure);
712
 
        autoCompleteSilences(measure,components);
713
 
        Component nextComponent = getNextComponent(getComponents(measure),component);
714
 
        
715
 
        long componentEnd = (component.getStart() + component.getDuration().getTime());
716
 
        long measureEnd = (measure.getStart() + measure.getLength());
717
 
        if(nextComponent instanceof Silence && componentEnd <= measureEnd){
718
 
            
719
 
            long theMove = (getRealStart(measure,componentEnd)) - getRealStart(measure,nextComponent.getStart());
720
 
                //long theMove = (componentEnd - nextComponent.getStart());
721
 
            
722
 
            if((nextComponent.getStart() + theMove) < measureEnd && (nextComponent.getStart() + nextComponent.getDuration().getTime() + theMove) <= measureEnd){            
723
 
                moveComponent(nextComponent,theMove);                        
724
 
                changeDuration(measure,nextComponent,(Duration)component.getDuration().clone(),tryMove);
725
 
            }
726
 
        }     
727
 
    }
728
 
    
729
 
    public void autoCompleteSilences(Measure measure){ 
730
 
        autoCompleteSilences(measure,getComponents(measure));
731
 
    }
732
 
    
733
 
    /**
734
 
     * Calcula si hay espacios libres. y crea nuevos silencios
735
 
     */   
736
 
    public void autoCompleteSilences(Measure measure,List components){    
737
 
 
738
 
        long start = measure.getStart();
739
 
        long end = 0;
740
 
        long diff = 0;
741
 
        Component component = getFirstComponent(components);
742
 
        
743
 
        while (component != null) {
744
 
            end = component.getStart() + component.getDuration().getTime();  
745
 
            if(component.getStart() > start){
746
 
                diff = component.getStart() - start;
747
 
                if(diff > 0){                                                            
748
 
                    createSilences(measure,start,diff);
749
 
                }                
750
 
            }            
751
 
            start = end;       
752
 
            component = getNextComponent(components,component);            
753
 
        }        
754
 
        end = measure.getStart() + measure.getLength();
755
 
        diff = end - start;
756
 
        if(diff > 0){       
757
 
            createSilences(measure,start,diff);         
758
 
        }
759
 
    }    
760
 
    
761
 
    /**
762
 
     * Crea Silencios temporarios en base a length
763
 
     */    
764
 
    public void createSilences(Measure measure,long start,long length){
765
 
        List durations = TablatureUtil.createDurations(length);
766
 
        Iterator it = durations.iterator();
767
 
        while(it.hasNext()){
768
 
            Duration duration = (Duration)it.next();
769
 
            Silence silence = new Silence(start,duration);
770
 
            addSilence(measure,silence);
771
 
            start += duration.getTime();
772
 
        }
773
 
    }  
774
 
    
775
 
    
776
 
    public long getRealStart(Measure measure,long currStart){        
777
 
        long beatLength = TablatureUtil.getBeatLength(measure.getTimeSignature());        
778
 
        long start = currStart;
779
 
        
780
 
        boolean startBeat = (start % beatLength == 0); 
781
 
        if(!startBeat){
782
 
 
783
 
            
784
 
            Duration minDuration = new Duration(Duration.SIXTY_FOURTH,false,false,new Tupleto(3,2));
785
 
            for(int i = 0;i < minDuration.getTime();i++){
786
 
                start ++;    
787
 
                startBeat = (start % beatLength == 0);     
788
 
                if(startBeat){
789
 
                   break; 
790
 
                }
791
 
            }
792
 
            if(!startBeat){
793
 
                start = currStart;
794
 
            }
795
 
        }
796
 
        
797
 
        return start;
798
 
    }
799
 
 
800
 
    
801
 
    public boolean areInSameBeat(Measure measure,Component arg0,Component arg1){
802
 
        long measureEnd = measure.getStart() + measure.getLength();
803
 
        long beatLength = TablatureUtil.getBeatLength(measure.getTimeSignature());  
804
 
        long start1 = getRealStart(measure,arg0.getStart());        
805
 
        long start2 = getRealStart(measure,arg1.getStart());
806
 
        
807
 
        long currStart = measure.getStart();
808
 
        
809
 
        
810
 
        boolean finish = false;
811
 
        while(!finish){
812
 
            if(start1 >= currStart && start1 < currStart + beatLength && start2 >= currStart && start2 < currStart + beatLength){
813
 
                return true;
814
 
            }
815
 
            currStart += beatLength;
816
 
            if(currStart > measureEnd){
817
 
                finish = true;
818
 
            }
819
 
        }
820
 
        return false;          
821
 
    }
822
 
    
823
 
    
824
 
    
825
 
    /** 
826
 
     * Liga la nota
827
 
     */
828
 
    public void changeTieNote(Measure measure,long start,int string){
829
 
        Note note = getNote(measure,start,string);
830
 
        if(note != null){
831
 
            changeTieNote(note);
832
 
        }
833
 
    }
834
 
    
835
 
    /** 
836
 
     * Liga la nota
837
 
     */
838
 
    public void changeTieNote(Note note){        
839
 
        note.setTiedNote(!note.isTiedNote());
840
 
        note.getEffect().setDeadNote(false);
841
 
    }
842
 
    
843
 
 
844
 
    
845
 
    /** 
846
 
     * Agrega un vibrato
847
 
     */
848
 
    public void changeVibratoNote(Measure measure,long start,int string){
849
 
        Note note = getNote(measure,start,string);
850
 
        if(note != null){            
851
 
                note.getEffect().setVibrato(!note.getEffect().isVibrato());
852
 
        }
853
 
    }    
854
 
 
855
 
    /** 
856
 
     * Agrega una nota muerta
857
 
     */
858
 
    public void changeDeadNote(Note note){
859
 
        //note.getEffect().set(note.getEffect().isVibrato(),null,!note.getEffect().isDeadNote(),false,false);
860
 
        note.getEffect().setDeadNote(!note.getEffect().isDeadNote());
861
 
        note.setTiedNote(false);        
862
 
    }
863
 
    
864
 
    /** 
865
 
     * Agrega un slide
866
 
     */
867
 
    public void changeSlideNote(Measure measure,long start,int string){
868
 
        Note note = getNote(measure,start,string);
869
 
        if(note != null){    
870
 
                note.getEffect().setSlide(!note.getEffect().isSlide());
871
 
            //note.getEffect().set(note.getEffect().isVibrato(),null,false,!note.getEffect().isSlide(),false);
872
 
        }
873
 
    }     
874
 
   
875
 
    
876
 
    /** 
877
 
     * Agrega un hammer
878
 
     */
879
 
    public void changeHammerNote(Measure measure,long start,int string){
880
 
        Note note = getNote(measure,start,string);
881
 
        if(note != null){
882
 
                note.getEffect().setHammer(!note.getEffect().isHammer());
883
 
            //note.getEffect().set(note.getEffect().isVibrato(),null,false,false,!note.getEffect().isHammer());            
884
 
        }
885
 
    }            
886
 
    
887
 
    
888
 
    /** 
889
 
     * Agrega un palm-mute
890
 
     */
891
 
    public void changePalmMute(Measure measure,long start,int string){
892
 
        Note note = getNote(measure,start,string);
893
 
        if(note != null){
894
 
                note.getEffect().setPalmMute(!note.getEffect().isPalmMute());            
895
 
        }
896
 
    }         
897
 
    
898
 
    /** 
899
 
     * Agrega un staccato
900
 
     */
901
 
    public void changeStaccato(Measure measure,long start,int string){
902
 
        Note note = getNote(measure,start,string);
903
 
        if(note != null){
904
 
                note.getEffect().setStaccato(!note.getEffect().isStaccato());            
905
 
        }
906
 
    }             
907
 
 
908
 
    /** 
909
 
     * Agrega un tapping
910
 
     */
911
 
    public void changeTapping(Measure measure,long start,int string){
912
 
        Note note = getNote(measure,start,string);
913
 
        if(note != null){
914
 
                note.getEffect().setTapping(!note.getEffect().isTapping());            
915
 
        }
916
 
    }            
917
 
    
918
 
    /** 
919
 
     * Agrega un slapping
920
 
     */
921
 
    public void changeSlapping(Measure measure,long start,int string){
922
 
        Note note = getNote(measure,start,string);
923
 
        if(note != null){
924
 
                note.getEffect().setSlapping(!note.getEffect().isSlapping());            
925
 
        }
926
 
    }     
927
 
    
928
 
    /** 
929
 
     * Agrega un popping
930
 
     */
931
 
    public void changePopping(Measure measure,long start,int string){
932
 
        Note note = getNote(measure,start,string);
933
 
        if(note != null){
934
 
                note.getEffect().setPopping(!note.getEffect().isPopping());            
935
 
        }
936
 
    }
937
 
    
938
 
    /** 
939
 
     * Agrega un bend
940
 
     */
941
 
    public void changeBendNote(Measure measure,long start,int string,BendEffect bend){
942
 
        Note note = getNote(measure,start,string);
943
 
        if(note != null){
944
 
                note.getEffect().setBend(bend);            
945
 
        }
946
 
    }  
947
 
 
948
 
    
949
 
    /** 
950
 
     * Agrega un tremoloBar
951
 
     */
952
 
    public void changeTremoloBar(Measure measure,long start,int string,TremoloBarEffect tremoloBar){
953
 
        Note note = getNote(measure,start,string);
954
 
        if(note != null){
955
 
                note.getEffect().setTremoloBar(tremoloBar);            
956
 
        }
957
 
    }  
958
 
    
959
 
    /** 
960
 
     * Agrega un GhostNote
961
 
     */
962
 
    public void changeGhostNote(Measure measure,long start,int string){
963
 
        Note note = getNote(measure,start,string);
964
 
        if(note != null){ 
965
 
                note.getEffect().setGhostNote(!note.getEffect().isGhostNote());     
966
 
        }
967
 
    }                
968
 
 
969
 
    
970
 
    /** 
971
 
     * Agrega un AccentuatedNote
972
 
     */
973
 
    public void changeAccentuatedNote(Measure measure,long start,int string){
974
 
        Note note = getNote(measure,start,string);
975
 
        if(note != null){ 
976
 
                note.getEffect().setAccentuatedNote(!note.getEffect().isAccentuatedNote());     
977
 
        }
978
 
    }
979
 
    
980
 
    /** 
981
 
     * Agrega un GhostNote
982
 
     */
983
 
    public void changeHeavyAccentuatedNote(Measure measure,long start,int string){
984
 
        Note note = getNote(measure,start,string);
985
 
        if(note != null){ 
986
 
                note.getEffect().setHeavyAccentuatedNote(!note.getEffect().isHeavyAccentuatedNote());     
987
 
        }
988
 
    }    
989
 
    
990
 
    /** 
991
 
     * Agrega un harmonic
992
 
     */
993
 
    public void changeHarmonicNote(Measure measure,long start,int string,HarmonicEffect harmonic){
994
 
        Note note = getNote(measure,start,string);
995
 
        if(note != null){
996
 
                note.getEffect().setHarmonic(harmonic);            
997
 
        }
998
 
    }      
999
 
    
1000
 
    /** 
1001
 
     * Agrega un grace
1002
 
     */
1003
 
    public void changeGraceNote(Measure measure,long start,int string,GraceEffect grace){
1004
 
        Note note = getNote(measure,start,string);
1005
 
        if(note != null){
1006
 
                note.getEffect().setGrace(grace);            
1007
 
        }
1008
 
    }          
1009
 
    
1010
 
    /** 
1011
 
     * Agrega un trill
1012
 
     */
1013
 
    public void changeTrillNote(Measure measure,long start,int string,TrillEffect trill){
1014
 
        Note note = getNote(measure,start,string);
1015
 
        if(note != null){
1016
 
                note.getEffect().setTrill(trill);         
1017
 
        }
1018
 
    }         
1019
 
    
1020
 
    /** 
1021
 
     * Agrega un tremolo picking
1022
 
     */
1023
 
    public void changeTremoloPicking(Measure measure,long start,int string,TremoloPickingEffect tremoloPicking){
1024
 
        Note note = getNote(measure,start,string);
1025
 
        if(note != null){
1026
 
                note.getEffect().setTremoloPicking(tremoloPicking);         
1027
 
        }
1028
 
    }      
1029
 
    
1030
 
    /** 
1031
 
     * Agrega un fadeIn
1032
 
     */
1033
 
    public void changeFadeIn(Measure measure,long start,int string){
1034
 
        Note note = getNote(measure,start,string);
1035
 
        if(note != null){
1036
 
                note.getEffect().setFadeIn(!note.getEffect().isFadeIn());         
1037
 
        }
1038
 
    }       
1039
 
    
1040
 
    /** 
1041
 
     * Cambia el Velocity
1042
 
     */
1043
 
    public void changeVelocity(int velocity,Measure measure,long start,int string){
1044
 
        Note note = getNote(measure,start,string);
1045
 
        if(note != null){ 
1046
 
                note.setVelocity(velocity);     
1047
 
        }
1048
 
    }                
1049
 
    
1050
 
    public void calculateNoteStartWithRepetitions(Measure measure,long startMove) {
1051
 
        for (int noteIdx = 0; noteIdx < measure.getNotes().size(); noteIdx++) {
1052
 
            Note note = (Note) measure.getNotes().get(noteIdx);
1053
 
            //asigno el start con repeticiones           
1054
 
            note.setStartWithRepetitions(note.getStart() + startMove);
1055
 
        }
1056
 
    }
1057
 
 
1058
 
}