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
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
|
// -*- c-indentation-style:"stroustrup" c-basic-offset: 4 -*- /*
/*
Rosegarden
A sequencer and musical notation editor.
This program is Copyright 2000-2008
Guillaume Laurent <[email protected]>,
Chris Cannam <[email protected]>,
Richard Bown <[email protected]>
The moral right of the authors to claim authorship of this work
has been asserted.
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. See the file
COPYING included with this distribution for more information.
*/
#include <cmath>
#include <cstdlib>
#include <kapplication.h>
#include <tqdatetime.h>
#include <tqstringlist.h>
#include <tqpalette.h>
#include <kapp.h>
#include "PeakFile.h"
#include "AudioFile.h"
#include "Profiler.h"
using std::cout;
using std::cerr;
using std::endl;
//#define DEBUG_PEAKFILE 1
//#define DEBUG_PEAKFILE_BRIEF 1
//#define DEBUG_PEAKFILE_CACHE 1
#ifdef DEBUG_PEAKFILE
#define DEBUG_PEAKFILE_BRIEF 1
#endif
namespace Rosegarden
{
PeakFile::PeakFile(AudioFile *audioFile):
SoundFile(audioFile->getPeakFilename()),
m_audioFile(audioFile),
m_version( -1), // -1 defines new file - start at 0
m_format(1), // default is 8-bit peak format
m_pointsPerValue(0),
m_blockSize(256), // default block size is 256 samples
m_channels(0),
m_numberOfPeaks(0),
m_positionPeakOfPeaks(0),
m_offsetToPeaks(0),
m_modificationTime(TQDate(1970, 1, 1), TQTime(0, 0, 0)),
m_chunkStartPosition(0),
m_lastPreviewStartTime(0, 0),
m_lastPreviewEndTime(0, 0),
m_lastPreviewWidth( -1),
m_lastPreviewShowMinima(false)
{}
PeakFile::~PeakFile()
{}
bool
PeakFile::open()
{
// Set the file size
//
TQFileInfo info(TQString(m_fileName.c_str()));
m_fileSize = info.size();
// If we're already open then don't open again
//
if (m_inFile && m_inFile->is_open())
return true;
// Open
//
m_inFile = new std::ifstream(m_fileName.c_str(),
std::ios::in | std::ios::binary);
// Check we're open
//
if (!(*m_inFile))
return false;
try {
parseHeader();
} catch (BadSoundFileException s) {
#ifdef DEBUG_PEAKFILE
cerr << "PeakFile::open - EXCEPTION \"" << s.getMessage() << "\""
<< endl;
#endif
return false;
}
return true;
}
void
PeakFile::parseHeader()
{
if (!(*m_inFile))
return ;
m_inFile->seekg(0, std::ios::beg);
// get full header length
//
std::string header = getBytes(128);
#if (__GNUC__ < 3)
if (header.compare(AUDIO_BWF_PEAK_ID, 0, 4) != 0)
#else
if (header.compare(0, 4, AUDIO_BWF_PEAK_ID) != 0)
#endif
{
throw(BadSoundFileException(m_fileName, "PeakFile::parseHeader - can't find LEVL identifier"));
}
int length = getIntegerFromLittleEndian(header.substr(4, 4));
// Get the length of the header minus the first 8 bytes
//
if (length == 0)
throw(BadSoundFileException(m_fileName, "PeakFile::parseHeader - can't get header length"));
// Get the file information
//
m_version = getIntegerFromLittleEndian(header.substr(8, 4));
m_format = getIntegerFromLittleEndian(header.substr(12, 4));
m_pointsPerValue = getIntegerFromLittleEndian(header.substr(16, 4));
m_blockSize = getIntegerFromLittleEndian(header.substr(20, 4));
m_channels = getIntegerFromLittleEndian(header.substr(24, 4));
m_numberOfPeaks = getIntegerFromLittleEndian(header.substr(28, 4));
m_positionPeakOfPeaks = getIntegerFromLittleEndian(header.substr(32, 4));
// Read in date string and convert it up to TQDateTime
//
TQString dateString = TQString(header.substr(40, 28).c_str());
TQStringList dateTime = TQStringList::split(":", dateString);
m_modificationTime.setDate(TQDate(dateTime[0].toInt(),
dateTime[1].toInt(),
dateTime[2].toInt()));
m_modificationTime.setTime(TQTime(dateTime[3].toInt(),
dateTime[4].toInt(),
dateTime[5].toInt(),
dateTime[6].toInt()));
//printStats();
}
void
PeakFile::printStats()
{
cout << endl;
cout << "STATS for PeakFile \"" << m_fileName << "\"" << endl
<< "-----" << endl << endl;
cout << " VERSION = " << m_version << endl
<< " FORMAT = " << m_format << endl
<< " BYTES/VALUE = " << m_pointsPerValue << endl
<< " BLOCKSIZE = " << m_blockSize << endl
<< " CHANNELS = " << m_channels << endl
<< " PEAK FRAMES = " << m_numberOfPeaks << endl
<< " PEAK OF PKS = " << m_positionPeakOfPeaks << endl
<< endl;
cout << "DATE" << endl
<< "----" << endl << endl
<< " YEAR = " << m_modificationTime.date().year() << endl
<< " MONTH = " << m_modificationTime.date().month() << endl
<< " DAY = " << m_modificationTime.date().day() << endl
<< " HOUR = " << m_modificationTime.time().hour() << endl
<< " MINUTE = " << m_modificationTime.time().minute()
<< endl
<< " SECOND = " << m_modificationTime.time().second()
<< endl
<< " MSEC = " << m_modificationTime.time().msec()
<< endl << endl;
}
bool
PeakFile::write()
{
return write(5); // default update every 5%
}
bool
PeakFile::write(unsigned short updatePercentage)
{
if (m_outFile) {
m_outFile->close();
delete m_outFile;
}
// Attempt to open AudioFile so that we can extract sample data
// for preview file generation
//
try {
if (!m_audioFile->open())
return false;
} catch (BadSoundFileException e) {
#ifdef DEBUG_PEAKFILE
std::cerr << "PeakFile::write - \"" << e.getMessage() << "\"" << std::endl;
#endif
return false;
}
// create and test that we've made it
m_outFile = new std::ofstream(m_fileName.c_str(),
std::ios::out | std::ios::binary);
if (!(*m_outFile))
return false;
// write out the header
writeHeader(m_outFile);
// and now the peak values
writePeaks(updatePercentage, m_outFile);
return true;
}
// Close the peak file and tidy up
//
void
PeakFile::close()
{
// Close any input file handle
//
if (m_inFile && m_inFile->is_open()) {
m_inFile->close();
delete m_inFile;
m_inFile = 0;
}
if (m_outFile == 0)
return ;
// Seek to start of chunk
//
m_outFile->seekp(m_chunkStartPosition, std::ios::beg);
// Seek to size field at set it
//
m_outFile->seekp(4, std::ios::cur);
putBytes(m_outFile, getLittleEndianFromInteger(m_bodyBytes + 120, 4));
// Seek to format and set it (m_format is only set at the
// end of writePeaks()
//
m_outFile->seekp(4, std::ios::cur);
putBytes(m_outFile, getLittleEndianFromInteger(m_format, 4));
// Seek to number of peak frames and write value
//
m_outFile->seekp(12, std::ios::cur);
putBytes(m_outFile,
getLittleEndianFromInteger(m_numberOfPeaks, 4));
// Peak of peaks
//
putBytes(m_outFile,
getLittleEndianFromInteger(m_positionPeakOfPeaks, 4));
// Seek to date field
//
m_outFile->seekp(4, std::ios::cur);
// Set modification time to now
//
m_modificationTime = m_modificationTime.tqcurrentDateTime();
TQString fDate;
fDate.sprintf("%04d:%02d:%02d:%02d:%02d:%02d:%03d",
m_modificationTime.date().year(),
m_modificationTime.date().month(),
m_modificationTime.date().day(),
m_modificationTime.time().hour(),
m_modificationTime.time().minute(),
m_modificationTime.time().second(),
m_modificationTime.time().msec());
std::string dateString(fDate.ascii());
// Pad with spaces to make up to 28 bytes long and output
//
dateString += " ";
putBytes(m_outFile, dateString);
// Ok, now close and tidy up
//
m_outFile->close();
delete m_outFile;
m_outFile = 0;
}
// If the audio file is more recently modified that the modification time
// on this peak file then we're invalid. The action to rectify this is
// usually to regenerate the peak data.
//
bool
PeakFile::isValid()
{
if (m_audioFile->getModificationDateTime() > m_modificationTime)
return false;
return true;
}
bool
PeakFile::writeToHandle(std::ofstream *file,
unsigned short /*updatePercentage*/)
{
// Remember the position where we pass in the ofstream pointer
// so we can return there to write close() information.
//
m_chunkStartPosition = file->tellp();
return false;
}
// Build up a header string and then pump it out to the file handle
//
void
PeakFile::writeHeader(std::ofstream *file)
{
if (!file || !(*file))
return ;
std::string header;
// The "levl" identifer for this chunk
//
header += AUDIO_BWF_PEAK_ID;
// Add a four byte version of the size of the header chunk (120
// bytes from this point onwards)
//
header += getLittleEndianFromInteger(120, 4);
// A four byte version number (incremented every time)
//
header += getLittleEndianFromInteger(++m_version, 4);
// Format of the peak points - 1 = unsigned char
// 2 = unsigned short
//
header += getLittleEndianFromInteger(m_format, 4);
// Points per value - 1 = 1 peak and has vertical about x-axis
// 2 = 2 peaks so differs above and below x-axis
//
// .. hardcode to 2 for the mo
m_pointsPerValue = 2;
header += getLittleEndianFromInteger(m_pointsPerValue, 4);
// Block size - default and recommended is 256
//
header += getLittleEndianFromInteger(m_blockSize, 4);
// Set channels up if they're currently empty
//
if (m_channels == 0 && m_audioFile)
m_channels = m_audioFile->getChannels();
// Peak channels - same as AudioFile channels
//
header += getLittleEndianFromInteger(m_channels, 4);
// Number of peak frames - we write this at close() and so
// for the moment put spacing 0's in.
header += getLittleEndianFromInteger(0, 4);
// Position of peak of peaks - written at close()
//
header += getLittleEndianFromInteger(0, 4);
// Offset to start of peaks - usually the total size of this header
//
header += getLittleEndianFromInteger(128, 4);
// Creation timestamp - fill in on close() so just use spacing
// of 28 bytes for the moment.
//
header += getLittleEndianFromInteger(0, 28);
// reserved space - 60 bytes
header += getLittleEndianFromInteger(0, 60);
//cout << "HEADER LENGTH = " << header.length() << endl;
// write out the header
//
putBytes(file, header);
}
bool
PeakFile::scanToPeak(int peak)
{
if (!m_inFile)
return false;
if (!m_inFile->is_open())
return false;
// Scan to start of chunk and then seek to peak number
//
ssize_t pos = (ssize_t)m_chunkStartPosition + 128 +
peak * m_format * m_channels * m_pointsPerValue;
ssize_t off = pos - m_inFile->tellg();
if (off == 0) {
return true;
} else if (off < 0) {
// std::cerr << "PeakFile::scanToPeak: warning: seeking backwards for peak " << peak << " (" << m_inFile->tellg() << " -> " << pos << ")" << std::endl;
m_inFile->seekg(pos);
} else {
m_inFile->seekg(off, std::ios::cur);
}
// Ensure we re-read the input buffer if we're
// doing buffered reads as it's now meaningless
//
m_loseBuffer = true;
if (m_inFile->eof()) {
m_inFile->clear();
return false;
}
return true;
}
bool
PeakFile::scanForward(int numberOfPeaks)
{
if (!m_inFile)
return false;
if (!m_inFile->is_open())
return false;
// Seek forward and number of peaks
//
m_inFile->seekg(numberOfPeaks * m_format * m_channels * m_pointsPerValue,
std::ios::cur);
// Ensure we re-read the input buffer
m_loseBuffer = true;
if (m_inFile->eof()) {
m_inFile->clear();
return false;
}
return true;
}
void
PeakFile::writePeaks(unsigned short /*updatePercentage*/,
std::ofstream *file)
{
if (!file || !(*file))
return ;
m_keepProcessing = true;
#ifdef DEBUG_PEAKFILE
cout << "PeakFile::writePeaks - calculating peaks" << endl;
#endif
// Scan to beginning of audio data
m_audioFile->scanTo(RealTime(0, 0));
// Store our samples
//
std::vector<std::pair<int, int> > channelPeaks;
std::string samples;
unsigned char *samplePtr;
int sampleValue;
int sampleMax = 0 ;
int sampleFrameCount = 0;
int channels = m_audioFile->getChannels();
int bytes = m_audioFile->getBitsPerSample() / 8;
m_format = bytes;
if (bytes == 3 || bytes == 4) // 24-bit PCM or 32-bit float
m_format = 2; // write 16-bit PCM instead
// for the progress dialog
unsigned int apprxTotalBytes = m_audioFile->getSize();
unsigned int byteCount = 0;
for (int i = 0; i < channels; i++)
channelPeaks.push_back(std::pair<int, int>());
// clear down info
m_numberOfPeaks = 0;
m_bodyBytes = 0;
m_positionPeakOfPeaks = 0;
while (m_keepProcessing) {
try {
samples = m_audioFile->
getBytes(m_blockSize * channels * bytes);
} catch (BadSoundFileException e) {
std::cerr << "PeakFile::writePeaks: " << e.getMessage()
<< std::endl;
break;
}
// If no bytes or less than the total number of bytes are returned
// then break out
//
if (samples.length() == 0 ||
samples.length() < (m_blockSize * m_audioFile->getChannels()
* bytes))
break;
byteCount += samples.length();
emit setProgress((int)(double(byteCount) /
double(apprxTotalBytes) * 100.0));
kapp->processEvents();
samplePtr = (unsigned char *)samples.c_str();
for (int i = 0; i < m_blockSize; i++) {
for (unsigned int ch = 0; ch < m_audioFile->getChannels(); ch++) {
// Single byte format values range from 0-255 and then
// shifted down about the x-axis. Double byte and above
// are already centred about x-axis.
//
if (bytes == 1) {
// get value
sampleValue = int(*samplePtr) - 128;
samplePtr++;
} else if (bytes == 2) {
unsigned char b2 = samplePtr[0];
unsigned char b1 = samplePtr[1];
unsigned int bits = (b1 << 8) + b2;
sampleValue = (short)bits;
samplePtr += 2;
} else if (bytes == 3) {
unsigned char b3 = samplePtr[0];
unsigned char b2 = samplePtr[1];
unsigned char b1 = samplePtr[2];
unsigned int bits = (b1 << 24) + (b2 << 16) + (b3 << 8);
// write out as 16-bit (m_format == 2)
sampleValue = int(bits) / 65536;
samplePtr += 3;
} else if (bytes == 4) // IEEE float (enforced by RIFFAudioFile)
{
// write out as 16-bit (m_format == 2)
float val = *(float *)samplePtr;
sampleValue = (int)(32767.0 * val);
samplePtr += 4;
} else {
throw(BadSoundFileException(m_fileName, "PeakFile::writePeaks - unsupported bit depth"));
}
// First time for each channel
//
if (i == 0) {
channelPeaks[ch].first = sampleValue;
channelPeaks[ch].second = sampleValue;
} else {
// Compare and store
//
if (sampleValue > channelPeaks[ch].first)
channelPeaks[ch].first = sampleValue;
if (sampleValue < channelPeaks[ch].second)
channelPeaks[ch].second = sampleValue;
}
// Store peak of peaks if it fits
//
if (abs(sampleValue) > sampleMax) {
sampleMax = abs(sampleValue);
m_positionPeakOfPeaks = sampleFrameCount;
}
}
// for peak of peaks as well as frame count
sampleFrameCount++;
}
// Write absolute peak data in channel order
//
for (unsigned int i = 0; i < m_audioFile->getChannels(); i++) {
putBytes(file, getLittleEndianFromInteger(channelPeaks[i].first,
m_format));
putBytes(file, getLittleEndianFromInteger(channelPeaks[i].second,
m_format));
m_bodyBytes += m_format * 2;
}
// increment number of peak frames
m_numberOfPeaks++;
}
#ifdef DEBUG_PEAKFILE
cout << "PeakFile::writePeaks - "
<< "completed peaks" << endl;
#endif
}
// Get a normalised vector for the preview at a given horizontal resolution.
// We return a value for each channel and if returnLow is set we also return
// an interleaved low value for each channel.
//
//
std::vector<float>
PeakFile::getPreview(const RealTime &startTime,
const RealTime &endTime,
int width,
bool showMinima)
{
#ifdef DEBUG_PEAKFILE_BRIEF
std::cout << "PeakFile::getPreview - "
<< "startTime = " << startTime
<< ", endTime = " << endTime
<< ", width = " << width
<< ", showMinima = " << showMinima << std::endl;
#endif
if (getSize() == 0) {
std::cout << "PeakFile::getPreview - PeakFile size == 0" << std::endl;
return std::vector<float>();
}
// Regenerate cache on these conditions
//
if (!m_peakCache.length()) {
#ifdef DEBUG_PEAKFILE_CACHE
std::cerr << "PeakFile::getPreview - no peak cache" << std::endl;
#endif
if (getSize() < (256 *1024)) // if less than 256K PeakFile
{
// Scan to start of peak data
scanToPeak(0);
try
{
m_peakCache = getBytes(m_inFile, getSize() - 128);
} catch (BadSoundFileException e)
{
std::cerr << "PeakFile::getPreview: " << e.getMessage()
<< std::endl;
}
#ifdef DEBUG_PEAKFILE_CACHE
std::cout << "PeakFile::getPreview - generated peak cache - "
<< "size = " << m_peakCache.length() << std::endl;
#endif
} else {
#ifdef DEBUG_PEAKFILE_CACHE
std::cout << "PeakFile::getPreview - file size = " << getSize()
<< ", not generating cache" << std::endl;
#endif
}
}
// Check to see if we hit the "lastPreview" cache by comparing the last
// query parameters we used.
//
if (startTime == m_lastPreviewStartTime && endTime == m_lastPreviewEndTime
&& width == m_lastPreviewWidth && showMinima == m_lastPreviewShowMinima) {
#ifdef DEBUG_PEAKFILE_CACHE
std::cout << "PeakFile::getPreview - hit last preview cache" << std::endl;
#endif
return m_lastPreviewCache;
} else {
#ifdef DEBUG_PEAKFILE_CACHE
std::cout << "PeakFile::getPreview - last preview " << m_lastPreviewStartTime
<< " -> " << m_lastPreviewEndTime << ", w " << m_lastPreviewWidth << "; this " << startTime << " -> " << endTime << ", w " << width << std::endl;
#endif
}
// Clear the cache - we need to regenerate it
//
m_lastPreviewCache.clear();
int startPeak = getPeak(startTime);
int endPeak = getPeak(endTime);
// Sanity check
if (startPeak > endPeak)
return m_lastPreviewCache;
// Actual possible sample length in RealTime
//
double step = double(endPeak - startPeak) / double(width);
std::string peakData;
int peakNumber;
#ifdef DEBUG_PEAKFILE_BRIEF
std::cout << "PeakFile::getPreview - getting preview for \""
<< m_audioFile->getFilename() << "\"" << endl;
#endif
// Get a divisor
//
float divisor = 0.0f;
switch (m_format) {
case 1:
divisor = SAMPLE_MAX_8BIT;
break;
case 2:
divisor = SAMPLE_MAX_16BIT;
break;
default:
#ifdef DEBUG_PEAKFILE_BRIEF
std::cout << "PeakFile::getPreview - "
<< "unsupported peak length format (" << m_format << ")"
<< endl;
#endif
return m_lastPreviewCache;
}
float *hiValues = new float[m_channels];
float *loValues = new float[m_channels];
for (int i = 0; i < width; i++) {
peakNumber = startPeak + int(double(i) * step);
int nextPeakNumber = startPeak + int(double(i + 1) * step);
// Seek to value
//
if (!m_peakCache.length()) {
if (scanToPeak(peakNumber) == false) {
#ifdef DEBUG_PEAKFILE
std::cout << "PeakFile::getPreview: scanToPeak(" << peakNumber << ") failed" << std::endl;
#endif
m_lastPreviewCache.push_back(0.0f);
}
}
#ifdef DEBUG_PEAKFILE
std::cout << "PeakFile::getPreview: step is " << step << ", format * pointsPerValue * chans is " << (m_format * m_pointsPerValue * m_channels) << std::endl;
std::cout << "i = " << i << ", peakNumber = " << peakNumber << ", nextPeakNumber = " << nextPeakNumber << std::endl;
#endif
for (int ch = 0; ch < m_channels; ch++) {
hiValues[ch] = 0.0f;
loValues[ch] = 0.0f;
}
// Get peak value over channels
//
for (int k = 0; peakNumber < nextPeakNumber; ++k) {
for (int ch = 0; ch < m_channels; ch++) {
if (!m_peakCache.length()) {
try {
peakData = getBytes(m_inFile, m_format * m_pointsPerValue);
} catch (BadSoundFileException e) {
// Problem with the get - probably an EOF
// return the results so far.
//
#ifdef DEBUG_PEAKFILE
std::cout << "PeakFile::getPreview - \"" << e.getMessage() << "\"\n"
<< endl;
#endif
goto done;
}
#ifdef DEBUG_PEAKFILE
std::cout << "PeakFile::getPreview - "
<< "read from file" << std::endl;
#endif
} else {
int valueNum = peakNumber * m_channels + ch;
int charNum = valueNum * m_format * m_pointsPerValue;
int charLength = m_format * m_pointsPerValue;
// Get peak value from the cached string if
// the value is valid.
//
if (charNum + charLength <= m_peakCache.length()) {
peakData = m_peakCache.substr(charNum, charLength);
#ifdef DEBUG_PEAKFILE
std::cout << "PeakFile::getPreview - "
<< "hit peakCache" << std::endl;
#endif
}
}
if (peakData.length() != (unsigned int)(m_format *
m_pointsPerValue)) {
// We didn't get the whole peak block - return what
// we've got so far
//
#ifdef DEBUG_PEAKFILE
std::cout << "PeakFile::getPreview - "
<< "failed to get complete peak block"
<< endl;
#endif
goto done;
}
int intDivisor = int(divisor);
int inValue =
getIntegerFromLittleEndian(peakData.substr(0, m_format));
while (inValue > intDivisor) {
inValue -= (1 << (m_format * 8));
}
#ifdef DEBUG_PEAKFILE
std::cout << "found potential hivalue " << inValue << std::endl;
#endif
if (k == 0 || inValue > hiValues[ch]) {
hiValues[ch] = float(inValue);
}
if (m_pointsPerValue == 2) {
inValue =
getIntegerFromLittleEndian(
peakData.substr(m_format, m_format));
while (inValue > intDivisor) {
inValue -= (1 << (m_format * 8));
}
if (k == 0 || inValue < loValues[ch]) {
loValues[ch] = inValue;
}
}
}
++peakNumber;
}
for (int ch = 0; ch < m_channels; ++ch) {
float value = hiValues[ch] / divisor;
#ifdef DEBUG_PEAKFILE_BRIEF
std::cout << "VALUE = " << hiValues[ch] / divisor << std::endl;
#endif
if (showMinima) {
m_lastPreviewCache.push_back(loValues[ch] / divisor);
} else {
value = fabs(value);
if (m_pointsPerValue == 2) {
value = std::max(value, fabsf(loValues[ch] / divisor));
}
m_lastPreviewCache.push_back(value);
}
}
}
done:
resetStream();
delete[] hiValues;
delete[] loValues;
// We have a good preview in the cache so store our parameters
//
m_lastPreviewStartTime = startTime;
m_lastPreviewEndTime = endTime;
m_lastPreviewWidth = width;
m_lastPreviewShowMinima = showMinima;
#ifdef DEBUG_PEAKFILE_BRIEF
std::cout << "Returning " << m_lastPreviewCache.size() << " items" << std::endl;
#endif
return m_lastPreviewCache;
}
int
PeakFile::getPeak(const RealTime &time)
{
double frames = ((time.sec * 1000000.0) + time.usec()) *
m_audioFile->getSampleRate() / 1000000.0;
return int(frames / double(m_blockSize));
}
RealTime
PeakFile::getTime(int peak)
{
int usecs = int((double)peak * (double)m_blockSize *
double(1000000.0) / double(m_audioFile->getSampleRate()));
return RealTime(usecs / 1000000, (usecs % 1000000) * 1000);
}
// Get pairs of split points for areas that exceed a percentage
// threshold
//
std::vector<SplitPointPair>
PeakFile::getSplitPoints(const RealTime &startTime,
const RealTime &endTime,
int threshold,
const RealTime &minLength)
{
std::vector<SplitPointPair> points;
std::string peakData;
int startPeak = getPeak(startTime);
int endPeak = getPeak(endTime);
if (endPeak < startPeak)
return std::vector<SplitPointPair>();
scanToPeak(startPeak);
float divisor = 0.0f;
switch (m_format) {
case 1:
divisor = SAMPLE_MAX_8BIT;
break;
case 2:
divisor = SAMPLE_MAX_16BIT;
break;
default:
return points;
}
float value;
float fThreshold = float(threshold) / 100.0;
bool belowThreshold = true;
RealTime startSplit = RealTime::zeroTime;
bool inSplit = false;
for (int i = startPeak; i < endPeak; i++) {
value = 0.0;
for (int ch = 0; ch < m_channels; ch++) {
try {
peakData = getBytes(m_inFile, m_format * m_pointsPerValue);
} catch (BadSoundFileException e) {
std::cerr << "PeakFile::getSplitPoints: "
<< e.getMessage() << std::endl;
break;
}
if (peakData.length() == (unsigned int)(m_format *
m_pointsPerValue)) {
int peakValue =
getIntegerFromLittleEndian(peakData.substr(0, m_format));
value += fabs(float(peakValue) / divisor);
}
}
value /= float(m_channels);
if (belowThreshold) {
if (value > fThreshold) {
startSplit = getTime(i);
inSplit = true;
belowThreshold = false;
}
} else {
if (value < fThreshold && getTime(i) - startSplit > minLength) {
// insert values
if (inSplit) {
points.push_back(SplitPointPair(startSplit, getTime(i)));
}
inSplit = false;
belowThreshold = true;
}
}
}
// if we've got a split point open the close it
if (inSplit) {
points.push_back(SplitPointPair(startSplit,
getTime(endPeak)));
}
return points;
}
}
#include "PeakFile.moc"
|