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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
|
//=========================================================
// MusE
// Linux Music Editor
// $Id: waveview.cpp,v 1.10.2.16 2009/11/14 03:37:48 terminator356 Exp $
// (C) Copyright 2000 Werner Schweer (ws@seh.de)
//=========================================================
#include <stdio.h>
#include <values.h>
#include <sys/wait.h>
#include <QPainter>
#include <QDir>
#include <QFileInfo>
#include <QMessageBox>
#include <QMouseEvent>
#include "editgain.h"
#include "globals.h"
#include "wave.h"
#include "waveview.h"
#include "song.h"
#include "event.h"
#include "waveedit.h"
#include "audio.h"
#include "gconfig.h"
bool modifyWarnedYet = false;
//---------------------------------------------------------
// WaveView
//---------------------------------------------------------
WaveView::WaveView(MidiEditor* pr, QWidget* parent, int xscale, int yscale)
: View(parent, xscale, 1)
{
editor = pr;
setVirt(true);
pos[0] = tempomap.tick2frame(song->cpos());
pos[1] = tempomap.tick2frame(song->lpos());
pos[2] = tempomap.tick2frame(song->rpos());
yScale = yscale;
mode = NORMAL;
selectionStart = 0;
selectionStop = 0;
lastGainvalue = 100;
setMouseTracking(true);
setBg(QColor(192, 208, 255));
if (editor->parts()->empty()) {
curPart = 0;
curPartId = -1;
}
else {
curPart = (WavePart*)(editor->parts()->begin()->second);
curPartId = curPart->sn();
}
connect(song, SIGNAL(posChanged(int,unsigned,bool)), SLOT(setPos(int,unsigned,bool)));
connect(song, SIGNAL(songChanged(int)), SLOT(songChanged(int)));
songChanged(SC_SELECTION);
}
//---------------------------------------------------------
// setYScale
//---------------------------------------------------------
void WaveView::setYScale(int val)
{
yScale = val;
redraw();
}
//---------------------------------------------------------
// draw
//---------------------------------------------------------
void WaveView::pdraw(QPainter& p, const QRect& rr)
{
int x1 = rr.x();
int x2 = rr.right() + 1;
if (x1 < 0)
x1 = 0;
if (x2 > width())
x2 = width();
int hh = height();
int h = hh/2;
int y = rr.y() + h;
// Added by T356.
int xScale = xmag;
if (xScale < 0)
xScale = -xScale;
for (iPart ip = editor->parts()->begin(); ip != editor->parts()->end(); ++ip) {
WavePart* wp = (WavePart*)(ip->second);
int channels = wp->track()->channels();
int px = wp->frame();
EventList* el = wp->events();
for (iEvent e = el->begin(); e != el->end(); ++e) {
Event event = e->second;
if (event.empty())
continue;
SndFileR f = event.sndFile();
if (f.isNull())
continue;
unsigned peoffset = px + event.frame() - event.spos();
int sx, ex;
sx = event.frame() + px + xScale/2;
ex = sx + event.lenFrame();
sx = sx / xScale - xpos;
ex = ex / xScale - xpos;
if (sx < x1)
sx = x1;
if (ex > x2)
ex = x2;
int pos = (xpos + sx) * xScale + event.spos() - event.frame() - px;
//printf("pos=%d xpos=%d sx=%d ex=%d xScale=%d event.spos=%d event.frame=%d px=%d\n",
// pos, xpos, sx, ex, xScale, event.spos(), event.frame(), px);
h = hh / (channels * 2);
int cc = hh % (channels * 2) ? 0 : 1;
for (int i = sx; i < ex; i++) {
y = rr.y() + h;
SampleV sa[f.channels()];
f.read(sa, xScale, pos);
pos += xScale;
if (pos < event.spos())
continue;
int selectionStartPos = selectionStart - peoffset; // Offset transformed to event coords
int selectionStopPos = selectionStop - peoffset;
for (int k = 0; k < channels; ++k) {
int kk = k % f.channels();
int peak = (sa[kk].peak * (h - 1)) / yScale;
int rms = (sa[kk].rms * (h - 1)) / yScale;
if (peak > h)
peak = h;
if (rms > h)
rms = h;
QColor peak_color = QColor(Qt::darkGray);
QColor rms_color = QColor(Qt::black);
// Changed by T356. Reduces (but not eliminates) drawing artifacts.
//if (pos > selectionStartPos && pos < selectionStopPos) {
if (pos > selectionStartPos && pos <= selectionStopPos) {
peak_color = QColor(Qt::lightGray);
rms_color = QColor(Qt::white);
// Draw inverted
p.setPen(QColor(Qt::black));
p.drawLine(i, y - h + cc, i, y + h - cc );
}
p.setPen(peak_color);
p.drawLine(i, y - peak - cc, i, y + peak);
p.setPen(rms_color);
p.drawLine(i, y - rms - cc, i, y + rms);
y += 2 * h;
}
}
}
}
View::pdraw(p, rr);
}
//---------------------------------------------------------
// draw
//---------------------------------------------------------
void WaveView::draw(QPainter& p, const QRect& r)
{
unsigned x = r.x() < 0 ? 0 : r.x();
unsigned y = r.y() < 0 ? 0 : r.y();
int w = r.width();
int h = r.height();
unsigned x2 = x + w;
unsigned y2 = y + h;
//
// draw marker & centerline
//
p.setPen(Qt::red);
if (pos[0] >= x && pos[0] < x2) {
p.drawLine(pos[0], y, pos[0], y2);
}
p.setPen(Qt::blue);
if (pos[1] >= x && pos[1] < x2) {
p.drawLine(pos[1], y, pos[1], y2);
}
if (pos[2] >= x && pos[2] < x2)
p.drawLine(pos[2], y, pos[2], y2);
// Changed by T356. Support multiple (or none) selected parts.
//int n = curPart->track()->channels();
int n = 1;
if(curPart)
n = curPart->track()->channels();
int hn = h / n;
int hh = hn / 2;
for (int i = 0; i < n; ++i) {
int h2 = hn * i;
int center = hh + h2;
p.setPen(QColor(i & i ? Qt::red : Qt::blue));
p.drawLine(x, center, x2, center);
p.setPen(QColor(Qt::black));
p.drawLine(x, h2, x2, h2);
}
}
//---------------------------------------------------------
// getCaption
//---------------------------------------------------------
QString WaveView::getCaption() const
{
// Changed by T356. Support multiple (or none) selected parts.
//return QString("Part ") + curPart->name();
if(curPart)
return QString("Part ") + curPart->name();
else
return QString("Part ");
}
//---------------------------------------------------------
// songChanged
//---------------------------------------------------------
void WaveView::songChanged(int flags)
{
// Is it simply a midi controller value adjustment? Forget it.
if(flags == SC_MIDI_CONTROLLER)
return;
if (flags & SC_SELECTION) {
startSample = MAXINT;
endSample = 0;
curPart = 0;
for (iPart p = editor->parts()->begin(); p != editor->parts()->end(); ++p) {
WavePart* part = (WavePart*)(p->second);
if (part->sn() == curPartId)
curPart = part;
int ssample = part->frame();
int esample = ssample + part->lenFrame();
if (ssample < startSample) {
startSample = ssample;
//printf("startSample = %d\n", startSample);
}
if (esample > endSample) {
endSample = esample;
//printf("endSample = %d\n", endSample);
}
}
}
if (flags & SC_CLIP_MODIFIED) {
redraw(); // Boring, but the only thing possible to do
}
if (flags & SC_TEMPO) {
setPos(0, song->cpos(), false);
setPos(1, song->lpos(), false);
setPos(2, song->rpos(), false);
}
redraw();
}
//---------------------------------------------------------
// setPos
// set one of three markers
// idx - 0-cpos 1-lpos 2-rpos
// flag - emit followEvent()
//---------------------------------------------------------
void WaveView::setPos(int idx, unsigned val, bool adjustScrollbar)
{
val = tempomap.tick2frame(val);
if (pos[idx] == val)
return;
int opos = mapx(pos[idx]);
int npos = mapx(val);
if (adjustScrollbar && idx == 0) {
switch (song->follow()) {
case Song::NO:
break;
case Song::JUMP:
if (npos >= width()) {
int ppos = val - xorg - rmapxDev(width()/4);
if (ppos < 0)
ppos = 0;
emit followEvent(ppos);
opos = mapx(pos[idx]);
npos = mapx(val);
}
else if (npos < 0) {
int ppos = val - xorg - rmapxDev(width()*3/4);
if (ppos < 0)
ppos = 0;
emit followEvent(ppos);
opos = mapx(pos[idx]);
npos = mapx(val);
}
break;
case Song::CONTINUOUS:
if (npos > (width()*5)/8) {
int ppos = pos[idx] - xorg - rmapxDev(width()*5/8);
if (ppos < 0)
ppos = 0;
emit followEvent(ppos);
opos = mapx(pos[idx]);
npos = mapx(val);
}
else if (npos < (width()*3)/8) {
int ppos = pos[idx] - xorg - rmapxDev(width()*3/8);
if (ppos < 0)
ppos = 0;
emit followEvent(ppos);
opos = mapx(pos[idx]);
npos = mapx(val);
}
break;
}
}
int x;
int w = 1;
if (opos > npos) {
w += opos - npos;
x = npos;
}
else {
w += npos - opos;
x = opos;
}
pos[idx] = val;
redraw(QRect(x, 0, w, height()));
}
//---------------------------------------------------------
// viewMousePressEvent
//---------------------------------------------------------
void WaveView::viewMousePressEvent(QMouseEvent* event)
{
button = event->button();
unsigned x = event->x();
switch (button) {
case Qt::LeftButton:
if (mode == NORMAL) {
// redraw and reset:
if (selectionStart != selectionStop) {
selectionStart = selectionStop = 0;
redraw();
}
mode = DRAG;
dragstartx = x;
selectionStart = selectionStop = x;
}
break;
case Qt::MidButton:
case Qt::RightButton:
default:
break;
}
viewMouseMoveEvent(event);
}
//---------------------------------------------------------
// wheelEvent
//---------------------------------------------------------
void WaveView::wheelEvent(QWheelEvent* event)
{
emit mouseWheelMoved(event->delta() / 10);
}
//---------------------------------------------------------
// viewMouseReleaseEvent
//---------------------------------------------------------
void WaveView::viewMouseReleaseEvent(QMouseEvent* /*event*/)
{
button = Qt::NoButton;
if (mode == DRAG) {
mode = NORMAL;
//printf("selectionStart=%d selectionStop=%d\n", selectionStart, selectionStop);
}
}
//---------------------------------------------------------
// viewMouseMoveEvent
//---------------------------------------------------------
void WaveView::viewMouseMoveEvent(QMouseEvent* event)
{
unsigned x = event->x();
emit timeChanged(x);
int i;
switch (button) {
case Qt::LeftButton:
i = 0;
if (mode == DRAG) {
if (x < dragstartx) {
selectionStart = x;
selectionStop = dragstartx;
}
else {
selectionStart = dragstartx;
selectionStop = x;
}
}
break;
case Qt::MidButton:
i = 1;
break;
case Qt::RightButton:
i = 2;
break;
default:
return;
}
Pos p(tempomap.frame2tick(x), true);
song->setPos(i, p);
}
//---------------------------------------------------------
// range
// returns range in samples
//---------------------------------------------------------
void WaveView::range(int* s, int *e)
{
PartList* lst = editor->parts();
if(lst->empty())
{
*s = 0;
*e = tempomap.tick2frame(song->len());
return;
}
int ps = song->len(), pe = 0;
int tps, tpe;
for(iPart ip = lst->begin(); ip != lst->end(); ++ip)
{
tps = ip->second->tick();
if(tps < ps)
ps = tps;
tpe = tps + ip->second->lenTick();
if(tpe > pe)
pe = tpe;
}
*s = tempomap.tick2frame(ps);
*e = tempomap.tick2frame(pe);
}
//---------------------------------------------------------
// cmd
//---------------------------------------------------------
void WaveView::cmd(int n)
{
int modifyoperation = -1;
double paramA = 0.0;
switch(n) {
case WaveEdit::CMD_SELECT_ALL:
if (!editor->parts()->empty()) {
iPart iBeg = editor->parts()->begin();
iPart iEnd = editor->parts()->end();
iEnd--;
WavePart* beg = (WavePart*) iBeg->second;
WavePart* end = (WavePart*) iEnd->second;
selectionStart = beg->frame();
selectionStop = end->frame() + end->lenFrame();
redraw();
}
break;
case WaveEdit::CMD_EDIT_EXTERNAL:
modifyoperation = EDIT_EXTERNAL;
break;
case WaveEdit::CMD_SELECT_NONE:
selectionStart = selectionStop = 0;
redraw();
break;
case WaveEdit::CMD_MUTE:
modifyoperation = MUTE;
break;
case WaveEdit::CMD_NORMALIZE:
modifyoperation = NORMALIZE;
break;
case WaveEdit::CMD_FADE_IN:
modifyoperation = FADE_IN;
break;
case WaveEdit::CMD_FADE_OUT:
modifyoperation = FADE_OUT;
break;
case WaveEdit::CMD_REVERSE:
modifyoperation = REVERSE;
break;
case WaveEdit::CMD_GAIN_FREE: {
EditGain* editGain = new EditGain(this, lastGainvalue);
if (editGain->exec() == QDialog::Accepted) {
lastGainvalue = editGain->getGain();
modifyoperation = GAIN;
paramA = (double)lastGainvalue / 100.0;
}
delete editGain;
}
break;
case WaveEdit::CMD_GAIN_200:
modifyoperation = GAIN;
paramA = 2.0;
break;
case WaveEdit::CMD_GAIN_150:
modifyoperation = GAIN;
paramA = 1.5;
break;
case WaveEdit::CMD_GAIN_75:
modifyoperation = GAIN;
paramA = 0.75;
break;
case WaveEdit::CMD_GAIN_50:
modifyoperation = GAIN;
paramA = 0.5;
break;
case WaveEdit::CMD_GAIN_25:
modifyoperation = GAIN;
paramA = 0.25;
break;
default:
break;
}
if (modifyoperation != -1) {
if (selectionStart == selectionStop) {
printf("No selection. Ignoring\n"); //@!TODO: Disable menu options when no selection
QMessageBox::information(this,
QString("MusE"),
QWidget::tr("No selection. Ignoring"));
return;
}
//if(!modifyWarnedYet)
//{
// modifyWarnedYet = true;
// if(QMessageBox::warning(this, QString("Muse"),
// tr("Warning! Muse currently operates directly on the sound file.\n"
// "Undo is supported, but NOT after exit, WITH OR WITHOUT A SAVE!\n"
// "If you are stuck, try deleting the associated .wca file and reloading."), tr("&Ok"), tr("&Cancel"),
// QString::null, 0, 1 ) != 0)
// return;
//}
modifySelection(modifyoperation, selectionStart, selectionStop, paramA);
}
}
//---------------------------------------------------------
// getSelection
//---------------------------------------------------------
WaveSelectionList WaveView::getSelection(unsigned startpos, unsigned stoppos)
{
WaveSelectionList selection;
for (iPart ip = editor->parts()->begin(); ip != editor->parts()->end(); ++ip) {
WavePart* wp = (WavePart*)(ip->second);
unsigned part_offset = wp->frame();
EventList* el = wp->events();
//printf("eventlist length=%d\n",el->size());
for (iEvent e = el->begin(); e != el->end(); ++e) {
Event event = e->second;
if (event.empty())
continue;
SndFileR file = event.sndFile();
if (file.isNull())
continue;
unsigned event_offset = event.frame() + part_offset;
unsigned event_startpos = event.spos();
unsigned event_length = event.lenFrame() + event.spos();
unsigned event_end = event_offset + event_length;
//printf("startpos=%d stoppos=%d part_offset=%d event_offset=%d event_startpos=%d event_length=%d event_end=%d\n", startpos, stoppos, part_offset, event_offset, event_startpos, event_length, event_end);
if (!(event_end <= startpos || event_offset > stoppos)) {
int tmp_sx = startpos - event_offset + event_startpos;
int tmp_ex = stoppos - event_offset + event_startpos;
unsigned sx;
unsigned ex;
tmp_sx < (int)event_startpos ? sx = event_startpos : sx = tmp_sx;
tmp_ex > (int)event_length ? ex = event_length : ex = tmp_ex;
//printf("Event data affected: %d->%d filename:%s\n", sx, ex, file.name().latin1());
WaveEventSelection s;
s.file = file;
s.startframe = sx;
s.endframe = ex+1;
//printf("sx=%d ex=%d\n",sx,ex);
selection.push_back(s);
}
}
}
return selection;
}
//---------------------------------------------------------
// modifySelection
//---------------------------------------------------------
void WaveView::modifySelection(int operation, unsigned startpos, unsigned stoppos, double paramA)
{
song->startUndo();
WaveSelectionList selection = getSelection(startpos, stoppos);
for (iWaveSelection i = selection.begin(); i != selection.end(); i++) {
WaveEventSelection w = *i;
SndFileR& file = w.file;
unsigned sx = w.startframe;
unsigned ex = w.endframe;
unsigned file_channels = file.channels();
QString tmpWavFile = QString::null;
if (!getUniqueTmpfileName(tmpWavFile)) {
break;
}
audio->msgIdle(true); // Not good with playback during operations
SndFile tmpFile(tmpWavFile);
tmpFile.setFormat(file.format(), file_channels, file.samplerate());
if (tmpFile.openWrite()) {
audio->msgIdle(false);
printf("Could not open temporary file...\n");
break;
}
//
// Write out data that will be changed to temp file
//
unsigned tmpdatalen = ex - sx;
off_t tmpdataoffset = sx;
float* tmpdata[file_channels];
for (unsigned i=0; i<file_channels; i++) {
tmpdata[i] = new float[tmpdatalen];
}
file.seek(tmpdataoffset, 0);
file.readWithHeap(file_channels, tmpdata, tmpdatalen);
file.close();
tmpFile.write(file_channels, tmpdata, tmpdatalen);
tmpFile.close();
switch(operation)
{
case MUTE:
muteSelection(file_channels, tmpdata, tmpdatalen);
break;
case NORMALIZE:
normalizeSelection(file_channels, tmpdata, tmpdatalen);
break;
case FADE_IN:
fadeInSelection(file_channels, tmpdata, tmpdatalen);
break;
case FADE_OUT:
fadeOutSelection(file_channels, tmpdata, tmpdatalen);
break;
case REVERSE:
reverseSelection(file_channels, tmpdata, tmpdatalen);
break;
case GAIN:
applyGain(file_channels, tmpdata, tmpdatalen, paramA);
break;
case EDIT_EXTERNAL:
editExternal(file.format(), file.samplerate(), file_channels, tmpdata, tmpdatalen);
break;
default:
printf("Error: Default state reached in modifySelection\n");
break;
}
file.openWrite();
file.seek(tmpdataoffset, 0);
file.write(file_channels, tmpdata, tmpdatalen);
file.update();
file.close();
file.openRead();
for (unsigned i=0; i<file_channels; i++) {
delete[] tmpdata[i];
}
// Undo handling
song->cmdChangeWave(file.dirPath() + "/" + file.name(), tmpWavFile, sx, ex);
audio->msgIdle(false); // Not good with playback during operations
}
song->endUndo(SC_CLIP_MODIFIED);
redraw();
}
//---------------------------------------------------------
// muteSelection
//---------------------------------------------------------
void WaveView::muteSelection(unsigned channels, float** data, unsigned length)
{
// Set everything to 0!
for (unsigned i=0; i<channels; i++) {
for (unsigned j=0; j<length; j++) {
data[i][j] = 0;
}
}
}
//---------------------------------------------------------
// normalizeSelection
//---------------------------------------------------------
void WaveView::normalizeSelection(unsigned channels, float** data, unsigned length)
{
float loudest = 0.0;
for (unsigned i=0; i<channels; i++) {
for (unsigned j=0; j<length; j++) {
if (data[i][j] > loudest)
loudest = data[i][j];
}
}
double scale = 0.99 / (double)loudest;
for (unsigned i=0; i<channels; i++) {
for (unsigned j=0; j<length; j++) {
data[i][j] = (float) ((double)data[i][j] * scale);
}
}
}
//---------------------------------------------------------
// fadeInSelection
//---------------------------------------------------------
void WaveView::fadeInSelection(unsigned channels, float** data, unsigned length)
{
for (unsigned i=0; i<channels; i++) {
for (unsigned j=0; j<length; j++) {
double scale = (double) j / (double)length ;
data[i][j] = (float) ((double)data[i][j] * scale);
}
}
}
//---------------------------------------------------------
// fadeOutSelection
//---------------------------------------------------------
void WaveView::fadeOutSelection(unsigned channels, float** data, unsigned length)
{
for (unsigned i=0; i<channels; i++) {
for (unsigned j=0; j<length; j++) {
double scale = (double) (length - j) / (double)length ;
data[i][j] = (float) ((double)data[i][j] * scale);
}
}
}
//---------------------------------------------------------
// reverseSelection
//---------------------------------------------------------
void WaveView::reverseSelection(unsigned channels, float** data, unsigned length)
{
if(length <= 1)
return;
for (unsigned i=0; i<channels; i++) {
for (unsigned j=0; j<length/2; j++) {
float tmpl = data[i][j];
float tmpr = data[i][length - j - 1];
data[i][j] = tmpr;
data[i][length - j - 1] = tmpl;
}
}
}
//---------------------------------------------------------
// applyGain
//---------------------------------------------------------
void WaveView::applyGain(unsigned channels, float** data, unsigned length, double gain)
{
for (unsigned i=0; i<channels; i++) {
for (unsigned j=0; j<length; j++) {
data[i][j] = (float) ((double)data[i][j] * gain);
}
}
}
//---------------------------------------------------------
// editExternal
//---------------------------------------------------------
void WaveView::editExternal(unsigned file_format, unsigned file_samplerate, unsigned file_channels, float** tmpdata, unsigned tmpdatalen)
{
// Create yet another tmp-file
QString exttmpFileName;
if (!getUniqueTmpfileName(exttmpFileName)) {
printf("Could not create temp file - aborting...\n");
return;
}
SndFile exttmpFile(exttmpFileName);
exttmpFile.setFormat(file_format, file_channels, file_samplerate);
if (exttmpFile.openWrite()) {
printf("Could not open temporary file...\n");
return;
}
// Write out change-data to this file:
exttmpFile.write(file_channels, tmpdata, tmpdatalen);
exttmpFile.close();
// Forkaborkabork
int pid = fork();
if (pid == 0) {
if (execlp(config.externalWavEditor.latin1(), config.externalWavEditor.latin1(), exttmpFileName.latin1(), NULL) == -1) {
perror("Failed to launch external editor");
// Get out of here
// cannot report error through gui, we are in another fork!
//@!TODO: Handle unsuccessful attempts
exit(99);
}
exit(0);
}
else if (pid == -1) {
perror("fork failed");
}
else {
int status;
waitpid(pid, &status, 0);
//printf ("status=%d\n",status);
if( WEXITSTATUS(status) != 0 ){
QMessageBox::warning(this, tr("MusE - external editor failed"),
tr("MusE was unable to launch the external editor\ncheck if the editor setting in:\n"
"Global Settings->Audio:External Waveditor\nis set to a valid editor."));
}
if (exttmpFile.openRead()) {
printf("Could not reopen temporary file!\n");
}
else {
// Re-read file again
exttmpFile.seek(0, 0);
size_t sz = exttmpFile.readWithHeap(file_channels, tmpdata, tmpdatalen);
if (sz != tmpdatalen) {
// File must have been shrunken - not good. Alert user.
QMessageBox::critical(this, tr("MusE - file size changed"),
tr("When editing in external editor - you should not change the filesize\nsince it must fit the selected region.\n\nMissing data is muted"));
for (unsigned i=0; i<file_channels; i++) {
for (unsigned j=sz; j<tmpdatalen; j++) {
tmpdata[i][j] = 0;
}
}
}
}
QDir dir = exttmpFile.dirPath();
dir.remove(exttmpFileName);
dir.remove(exttmpFile.basename() + ".wca");
}
}
//---------------------------------------------------------
// getUniqueTmpfileName
//---------------------------------------------------------
bool WaveView::getUniqueTmpfileName(QString& newFilename)
{
// Check if tmp-directory exists under project path
QString tmpWavDir = museProject + "/tmp_musewav"; //!@TODO: Don't hardcode like this
QFileInfo tmpdirfi(tmpWavDir);
if (!tmpdirfi.isDir()) {
// Try to create a tmpdir
QDir projdir(museProject);
if (!projdir.mkdir("tmp_musewav")) {
printf("Could not create undo dir!\n");
return false;
}
}
tmpdirfi.setFile(tmpWavDir);
if (!tmpdirfi.isWritable()) {
printf("Temp directory is not writable - aborting\n");
return false;
}
QDir tmpdir = tmpdirfi.dir();
// Find a new filename
for (int i=0; i<10000; i++) {
QString filename = "muse_tmp";
filename.append(QString::number(i));
filename.append(".wav");
if (!tmpdir.exists(tmpWavDir +"/" + filename)) {
newFilename = tmpWavDir + "/" + filename;
return true;
}
}
printf("Could not find a suitable tmpfilename (more than 10000 tmpfiles in tmpdir - clean up!\n");
return false;
}
|