summaryrefslogtreecommitdiff
path: root/muse2/muse/steprec.cpp
blob: c1fc23b1760c3004752f00eb7287e825146bf87a (plain)
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
//=========================================================
//  MusE
//  Linux Music Editor
//  steprec.cpp
//  (C) Copyright 2011 Florian Jung (flo93@users.sourceforge.net)
//=========================================================

#include "steprec.h"
#include "part.h"
#include "event.h"
#include "globals.h"
#include "functions.h"

#include "song.h"
#include "audio.h"

#include <set>

#define CHORD_TIMEOUT 75

StepRec::StepRec(bool* note_held_down_array)
{
	note_held_down=note_held_down_array;

	chord_timer=new QTimer(this);
	chord_timer->setSingleShot(true);
	chord_timer->setInterval(CHORD_TIMEOUT);
	chord_timer->stop();
	connect(chord_timer, SIGNAL(timeout()), SLOT(timeout()));
}

void StepRec::timeout()
{
	if (chord_timer_set_to_tick != song->cpos())
	{
		Pos p(chord_timer_set_to_tick, true);
		song->setPos(0, p, true, false, true);
	}
}

void StepRec::record(Part* part, int pitch, int len, int step, int velo, bool ctrl, bool shift)
{
	unsigned tick = song->cpos();
	unsigned lasttick=0;
	Undo operations;
	
	if (pitch!=rcSteprecNote) 
	{
		chord_timer->stop();

		// extend len of last note?
		EventList* events = part->events();
		if (ctrl)
		{
			for (iEvent i = events->begin(); i != events->end(); ++i)
			{
				Event ev = i->second;
				if (ev.isNote() && ev.pitch() == pitch && ((ev.tick() + ev.lenTick() + part->tick()) == tick))
				{
					Event e = ev.clone();
					e.setLenTick(ev.lenTick() + len);
					operations.push_back(UndoOp(UndoOp::ModifyEvent, e,ev, part, false, false));
					
					if (!shift)
					{
						chord_timer_set_to_tick = tick + step;
						chord_timer->start();
					}
					
					lasttick=tick+len - part->tick();
					goto steprec_record_foot;
				}
			}
		}

		if (tick<=part->endTick())
		{
			// if we already entered the note, delete it
			// if we would find a note after part->lenTick(), the above "if"
			// avoids this. this has to be avoided because then part->hasHiddenNotes() is true
			// which results in forbidding any action beyond its end
			EventRange range = events->equal_range(tick - part->tick());
			for (iEvent i = range.first; i != range.second; ++i)
			{
				Event ev = i->second;
				if (ev.isNote() && ev.pitch() == pitch)
				{
					audio->msgDeleteEvent(ev, part, true, false, false);

					if (!shift)
					{
						chord_timer_set_to_tick = tick + step;
						chord_timer->start();
					}
					
					return;
				}
			}
		}
		
				
		Event e(Note);
		e.setTick(tick - part->tick());
		e.setPitch(pitch);
		e.setVelo(velo);
		e.setLenTick(len);
		operations.push_back(UndoOp(UndoOp::AddEvent, e, part, false, false));
		lasttick=e.endTick();

		if (! (globalKeyState & Qt::ShiftModifier))
		{
			chord_timer_set_to_tick = tick + step;
			chord_timer->start();
		}
		
		goto steprec_record_foot; // this is actually unneccessary, but for clarity
	}
	else  // equals if (pitch==rcSteprecNote)
	{
		bool held_notes=false;
		if (note_held_down!=NULL)
		{
			for (int i=0;i<128;i++)
			if (note_held_down[i]) { held_notes=true; break; }
		}
		else
			held_notes=false;
			 

		if (held_notes)
		{
			chord_timer->stop();
			
			// extend len of last note(s)
			using std::set;
			
			set<Event*> extend_set;
			EventList* events = part->events();
			for (iEvent i = events->begin(); i != events->end(); ++i)
			{
				Event& ev = i->second;
				if (ev.isNote() && note_held_down[ev.pitch()] && ((ev.tick() + ev.lenTick() + part->tick()) == tick))
					extend_set.insert(&ev);
			}
			
			for (set<Event*>::iterator it=extend_set.begin(); it!=extend_set.end(); it++)
			{
				Event& ev=**it;
				Event e = ev.clone();
				e.setLenTick(ev.lenTick() + len);
				operations.push_back(UndoOp(UndoOp::ModifyEvent,e, ev, part, false, false));
			}

			if (!shift)
			{
				chord_timer_set_to_tick = tick + step;
				chord_timer->start();
			}
			
			lasttick=tick+len - part->tick();
			goto steprec_record_foot; // this is actually unneccessary, but for clarity
		}
		else // equals if (!held_notes)
		{
			chord_timer->stop();

			// simply proceed, inserting a rest
			Pos p(song->cpos() + step, true);
			song->setPos(0, p, true, false, true);
			
			return;
		}
	}
	
	steprec_record_foot:
	if (!((lasttick > part->lenTick()) && part->hasHiddenNotes())) // allowed?
	{
		if (lasttick > part->lenTick()) // we have to expand the part?
			schedule_resize_all_same_len_clone_parts(part, lasttick, operations);
		
		song->applyOperationGroup(operations);
	}
}