Mercurial > hg > hv
comparison EventBoxUnit.cpp @ 0:a6a46af64546
first upload
author | wenx <xue.wen@eecs.qmul.ac.uk> |
---|---|
date | Wed, 10 Aug 2011 14:55:38 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:a6a46af64546 |
---|---|
1 /* | |
2 Harmonic Visualiser | |
3 | |
4 An audio file viewer and editor. | |
5 Centre for Digital Music, Queen Mary, University of London. | |
6 This file copyright 2011 Wen Xue. | |
7 | |
8 This program is free software; you can redistribute it and/or | |
9 modify it under the terms of the GNU General Public License as | |
10 published by the Free Software Foundation; either version 2 of the | |
11 License, or (at your option) any later version. | |
12 */ | |
13 //--------------------------------------------------------------------------- | |
14 | |
15 #include <vcl.h> | |
16 #pragma hdrstop | |
17 | |
18 #include "EventBoxUnit.h" | |
19 #include "WaveView.h" | |
20 #include "Unit1.h" | |
21 #include "EditorPanelUnit.h" | |
22 #include <Math.hpp> | |
23 #include <math.h> | |
24 //--------------------------------------------------------------------------- | |
25 #pragma package(smart_init) | |
26 #pragma resource "*.dfm" | |
27 TEventBox *EventBox; | |
28 //--------------------------------------------------------------------------- | |
29 __fastcall TEventBox::TEventBox(TComponent* Owner) | |
30 : TForm(Owner) | |
31 { | |
32 HSCapacity=100; | |
33 HSCount=0; | |
34 HS=(THS**)malloc(sizeof(THS*)*HSCapacity); | |
35 memset(HS, 0, sizeof(THS*)*HSCapacity); | |
36 } | |
37 | |
38 __fastcall TEventBox::~TEventBox() | |
39 { | |
40 Clear(); | |
41 free(HS); | |
42 } | |
43 | |
44 void __fastcall TEventBox::Clear() | |
45 { | |
46 ListBox1->Clear(); | |
47 for(int i=0; i<HSCount; i++) delete HS[i]; | |
48 HSCount=0; | |
49 } | |
50 //--------------------------------------------------------------------------- | |
51 THS* __fastcall TEventBox::NewHS(int M, int Fr) | |
52 { | |
53 if (HSCount>=HSCapacity) | |
54 { | |
55 HSCapacity+=100; | |
56 HS=(THS**)realloc(HS, sizeof(THS*)*HSCapacity); | |
57 memset(&HS[HSCount], 0, sizeof(THS*)*(HSCapacity-HSCount)); | |
58 } | |
59 THS* newhs; | |
60 if (M>0 && Fr>0) newhs=new THS(M, Fr); | |
61 else newhs=new THS; | |
62 HS[HSCount++]=newhs; | |
63 if (ListBox1->Enabled) | |
64 { | |
65 ListBox1->Items->Add(""); | |
66 ListBox1->ItemIndex=ListBox1->Count-1; | |
67 } | |
68 return newhs; | |
69 } | |
70 | |
71 //--------------------------------------------------------------------------- | |
72 void __fastcall TEventBox::ListBox1MouseDown(TObject *Sender, | |
73 TMouseButton Button, TShiftState Shift, int X, int Y) | |
74 { | |
75 int c=ListBox1->ItemAtPos(TPoint(X, Y), true); | |
76 SetItemIndex(c); | |
77 } | |
78 //--------------------------------------------------------------------------- | |
79 void ClearObjectByShortTag0(TWaveView* WV, int tag0); | |
80 void __fastcall TEventBox::SetItemIndex(int index) | |
81 { | |
82 ListBox1->ItemIndex=index; | |
83 ClearObjectByShortTag0(Form1->WaveView1, 1); | |
84 if (index>=0) | |
85 { | |
86 Form1->HS=HS[index]; | |
87 Form1->AddHSObject(Form1->HS); | |
88 } | |
89 else | |
90 { | |
91 Form1->HS=0; | |
92 } | |
93 | |
94 bool alreadyinvalidated=false; | |
95 if (CheckBox1->Checked && Form1->HS) | |
96 { | |
97 TWaveView* WaveView1=Form1->WaveView1; | |
98 int eventst=Form1->HS->Partials[0][0].t, eventen=Form1->HS->Partials[0][Form1->HS->Fr-1].t, | |
99 wvlen=WaveView1->EndPos-WaveView1->StartPos; | |
100 if (eventst>WaveView1->EndPos || eventen<WaveView1->StartPos) | |
101 { | |
102 int newst=(eventst+eventen)/2-wvlen/2, newen=newst+wvlen; | |
103 if (newst<0) newst=0, newen=wvlen; | |
104 else if (newen>WaveView1->Length) newen=WaveView1->Length, newst=WaveView1->Length-wvlen; | |
105 WaveView1->SetStartAndEndPos(newst, newen); | |
106 alreadyinvalidated=true; | |
107 } | |
108 } | |
109 if (!alreadyinvalidated) Form1->WaveView1->Invalidate(); | |
110 } | |
111 //--------------------------------------------------------------------------- | |
112 void __fastcall TEventBox::Save(TObject *Sender) | |
113 { | |
114 Sort(); | |
115 AnsiString FileName=ChangeFileExt(Form1->WaveAudio1->FileName, ".evt"); | |
116 if (FileExists(FileName)) | |
117 { | |
118 AnsiString BakName=ChangeFileExt(FileName, ".evt.bak."+Now().FormatString("yymmddhhnnss")); | |
119 RenameFile(FileName, BakName); | |
120 } | |
121 SaveToFile(FileName); | |
122 } | |
123 //--------------------------------------------------------------------------- | |
124 void __fastcall TEventBox::Load(TObject *Sender) | |
125 { | |
126 LoadFromFile(ChangeFileExt(Form1->WaveAudio1->FileName, ".evt")); | |
127 } | |
128 //--------------------------------------------------------------------------- | |
129 void __fastcall TEventBox::Sort() | |
130 { | |
131 for (int i=0; i<HSCount-1; i++) | |
132 { | |
133 int indmint=i; | |
134 for (int j=i+1; j<HSCount; j++) if (HS[j]->Partials[0][0].t<HS[indmint]->Partials[0][0].t) indmint=j; | |
135 | |
136 if (indmint!=i) | |
137 { | |
138 THS* tmphs=HS[i]; | |
139 HS[i]=HS[indmint]; | |
140 HS[indmint]=tmphs; | |
141 AnsiString tmpstr=ListBox1->Items->Strings[i]; | |
142 ListBox1->Items->Strings[i]=ListBox1->Items->Strings[indmint]; | |
143 ListBox1->Items->Strings[indmint]=tmpstr; | |
144 } | |
145 } | |
146 } | |
147 | |
148 void __fastcall TEventBox::SaveToFile(AnsiString FileName) | |
149 { | |
150 TFileStream* File=new TFileStream(FileName, fmCreate); | |
151 File->Write(&HSCount, sizeof(int)); | |
152 for (int i=0; i<HSCount; i++) HS[i]->WriteToStream(File); | |
153 delete File; | |
154 } | |
155 | |
156 void __fastcall TEventBox::LoadFromFile(AnsiString FileName) | |
157 { | |
158 Clear(); SetItemIndex(-1); | |
159 if (!FileExists(FileName)) return; | |
160 TFileStream* File=new TFileStream(FileName, fmOpenRead); | |
161 __int32 hsc; File->Read(&hsc, sizeof(__int32)); | |
162 bool counted=memcmp(&hsc, "EVT", 4); | |
163 if (!counted) File->Seek(-4, soFromCurrent); | |
164 ListBox1->Enabled=false; | |
165 | |
166 int i=0; | |
167 while (!counted || i<hsc) | |
168 { | |
169 THS* hs=NewHS(0, 0); | |
170 if (!hs->ReadFromStream(File)) | |
171 { | |
172 if (!counted) | |
173 { | |
174 delete HS[i]; | |
175 HSCount--; | |
176 break; | |
177 } | |
178 else | |
179 { //load old format file | |
180 char c[5]; c[4]=0; | |
181 int Channel, M, Fr; | |
182 File->Read(c, 4); | |
183 File->Read(&Channel, sizeof(int)); | |
184 File->Read(&M, sizeof(int)); | |
185 File->Read(&Fr, sizeof(int)); | |
186 hs->Resize(M, Fr); | |
187 hs->Channel=Channel; | |
188 File->Read(hs->Partials[0], sizeof(atom)*M*Fr); | |
189 File->Read(c, 4); | |
190 if (strcmp(c, "EVT ")) hs->isconstf=*(int*)c; | |
191 else File->Seek(-4, soFromCurrent); | |
192 } | |
193 } | |
194 i++; | |
195 } | |
196 delete File; | |
197 TStringList* List=new TStringList; | |
198 | |
199 for (int i=0; i<HSCount; i++) | |
200 { | |
201 List->Add(AnsiString(i)+" "+ | |
202 (HS[i]->Channel==0?"left: ":"right: ") | |
203 +AnsiString().sprintf("%.2fs, ", HS[i]->Partials[0][0].t*1.0/Form1->WaveView1->SamplesPerSec) | |
204 +SemitoneToPitch(Log2(HS[i]->Partials[0][0].f*Form1->WaveView1->SamplesPerSec/C4)*12) | |
205 ); | |
206 } | |
207 ListBox1->Items=List; | |
208 delete List; | |
209 ListBox1->Enabled=true; | |
210 SetItemIndex(-1); | |
211 } | |
212 //--------------------------------------------------------------------------- | |
213 | |
214 | |
215 void __fastcall TEventBox::ListBox1KeyUp(TObject *Sender, WORD &Key, | |
216 TShiftState Shift) | |
217 { | |
218 int LII=ListBox1->ItemIndex; | |
219 if (Key==VK_DELETE) | |
220 { | |
221 if (LII>=0) | |
222 { | |
223 delete HS[LII]; | |
224 HSCount-=1; | |
225 memcpy(&HS[LII], &HS[LII+1], sizeof(THS*)*(HSCount-LII)); | |
226 ListBox1->Items->Delete(LII); | |
227 ClearObjectByShortTag0(Form1->WaveView1, 1); | |
228 Form1->HS=0; | |
229 Form1->WaveView1->Invalidate(); | |
230 } | |
231 } | |
232 else if (Key=='L') Load(NULL); | |
233 else if (Key=='S') Save(NULL); | |
234 else | |
235 { | |
236 } | |
237 LII=ListBox1->ItemIndex; | |
238 if (LII>=0 && Form1->HS!=HS[LII]) SetItemIndex(LII); | |
239 else if (LII<0 && Form1->HS!=0) SetItemIndex(LII); | |
240 } | |
241 //--------------------------------------------------------------------------- | |
242 | |
243 | |
244 void __fastcall TEventBox::Vibratowizard1Click(TObject *Sender) | |
245 { | |
246 if (Form1->Vibratowizard1->Enabled) Form1->Vibratowizard1Click(Sender); | |
247 } | |
248 //--------------------------------------------------------------------------- | |
249 | |
250 void __fastcall TEventBox::ListBox1DblClick(TObject *Sender) | |
251 { | |
252 if (Form1->Vibratowizard1->Enabled) | |
253 { | |
254 if (GetKeyState(VK_SHIFT)>=0) | |
255 Form1->Sourcefilter1Click(Vibratowizard1); | |
256 else | |
257 Form1->Vibratowizard1Click(Vibratowizard1); | |
258 } | |
259 } | |
260 //--------------------------------------------------------------------------- | |
261 | |
262 //--------------------------------------------------------------------------- | |
263 | |
264 void __fastcall TEventBox::FormClick(TObject *Sender) | |
265 { | |
266 SetItemIndex(-1); | |
267 } | |
268 //--------------------------------------------------------------------------- | |
269 | |
270 | |
271 void __fastcall TEventBox::Cut1Click(TObject *Sender) | |
272 { | |
273 if (Form1->Cut1->Enabled) Form1->Cut1Click(Sender); | |
274 } | |
275 //--------------------------------------------------------------------------- | |
276 | |
277 | |
278 void __fastcall TEventBox::PopupMenu1Popup(TObject *Sender) | |
279 { | |
280 bool hasselection=ListBox1->ItemIndex>=0; | |
281 Vibratowizard1->Visible=hasselection; | |
282 Sourcefilter1->Visible=hasselection; | |
283 Extract1->Visible=hasselection; | |
284 Cut1->Visible=hasselection;; | |
285 } | |
286 //--------------------------------------------------------------------------- | |
287 | |
288 void __fastcall TEventBox::Extract1Click(TObject *Sender) | |
289 { | |
290 if (Form1->Extract1->Enabled) Form1->Extract1Click(Sender); | |
291 } | |
292 //--------------------------------------------------------------------------- | |
293 | |
294 | |
295 void __fastcall TEventBox::Sourcefilter1Click(TObject *Sender) | |
296 { | |
297 if (Form1->Vibratowizard1->Enabled) | |
298 Form1->Sourcefilter1Click(Vibratowizard1); | |
299 } | |
300 //--------------------------------------------------------------------------- | |
301 |