summaryrefslogtreecommitdiffstats
path: root/src/sound/PeakFileManager.cpp
blob: ec51458155a238d54f09fc17f5118acb887fe6b7 (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
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
/*
    Rosegarden
    A sequencer and musical notation editor.
 
    This program is Copyright 2000-2008
        Guillaume Laurent   <glaurent@telegraph-road.org>,
        Chris Cannam        <cannam@all-day-breakfast.com>,
        Richard Bown        <bownie@bownie.com>
 
    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.
*/


// Accepts a file handle positioned somewhere in sample data (could
// be at the start) along with the necessary meta information for
// decoding (channels, bits per sample) and turns the sample data
// into peak data and generates a BWF format peak chunk file.  This
// file can exist by itself (in the case this is being generated
// by a WAV) or be accomodated inside a BWF format file.
//
//

#include <string>
#include <vector>

#include <tqobject.h>

#include "PeakFileManager.h"
#include "AudioFile.h"
#include "RealTime.h"
#include "PeakFile.h"

namespace Rosegarden
{


PeakFileManager::PeakFileManager():
        m_updatePercentage(0),
        m_currentPeakFile(0)
{}

PeakFileManager::~PeakFileManager()
{}

// Inserts PeakFile based on AudioFile if it doesn't already exist
bool
PeakFileManager::insertAudioFile(AudioFile *audioFile)
{
    std::vector<PeakFile*>::iterator it;

    for (it = m_peakFiles.begin(); it != m_peakFiles.end(); it++) {
        if ((*it)->getAudioFile()->getId() == audioFile->getId())
            return false;
    }

    /*
    std::cout << "PeakFileManager::insertAudioFile - creating peak file "
              << m_peakFiles.size() + 1
              << " for \"" << audioFile->getFilename()
              << "\"" << std::endl;
    */

    // Insert
    m_peakFiles.push_back(new PeakFile(audioFile));

    return true;
}

// Removes peak file from PeakFileManager - doesn't affect audioFile
//
bool
PeakFileManager::removeAudioFile(AudioFile *audioFile)
{
    std::vector<PeakFile*>::iterator it;

    for (it = m_peakFiles.begin(); it != m_peakFiles.end(); it++) {
        if ((*it)->getAudioFile()->getId() == audioFile->getId()) {
            if (m_currentPeakFile == *it)
                m_currentPeakFile = 0;
            delete *it;
            m_peakFiles.erase(it);
            return true;
        }
    }

    return false;
}

// Auto-insert PeakFile into manager if it doesn't already exist
//
PeakFile*
PeakFileManager::getPeakFile(AudioFile *audioFile)
{
    std::vector<PeakFile*>::iterator it;
    PeakFile *ptr = 0;

    while (ptr == 0) {
        for (it = m_peakFiles.begin(); it != m_peakFiles.end(); it++)
            if ((*it)->getAudioFile()->getId() == audioFile->getId())
                ptr = *it;

        // If nothing is found then insert and retry
        //
        if (ptr == 0) {
            // Insert - if we fail we return as empty
            //
            if (insertAudioFile(audioFile) == false)
                return 0;
        }
    }

    return ptr;
}


// Does a given AudioFile have a valid peak file or peak chunk?
//
bool
PeakFileManager::hasValidPeaks(AudioFile *audioFile)
{
    if (audioFile->getType() == WAV) {
        // Check external peak file
        PeakFile *peakFile = getPeakFile(audioFile);

        if (peakFile == 0) {
#ifdef DEBUG_PEAKFILEMANAGER
            std::cerr << "PeakFileManager::hasValidPeaks - no peak file found"
            << std::endl;
#endif

            return false;
        }
        // If it doesn't open and parse correctly
        if (peakFile->open() == false)
            return false;

        // or if the data is old or invalid
        if (peakFile->isValid() == false)
            return false;

    } else if (audioFile->getType() == BWF) {
        // check internal peak chunk
    } else {
#ifdef DEBUG_PEAKFILEMANAGER
        std::cout << "PeakFileManager::hasValidPeaks - unsupported file type"
        << std::endl;
#endif

        return false;
    }

    return true;

}

// Generate the peak file.  Checks to see if peak file exists
// already and if so if it's up to date.  If it isn't then we
// regenerate.
//
void
PeakFileManager::generatePeaks(AudioFile *audioFile,
                               unsigned short updatePercentage)
{
#ifdef DEBUG_PEAKFILEMANAGER
    std::cout << "PeakFileManager::generatePeaks - generating peaks for \""
    << audioFile->getFilename() << "\"" << std::endl;
#endif

    if (audioFile->getType() == WAV) {
        m_currentPeakFile = getPeakFile(audioFile);

        TQObject::connect(m_currentPeakFile, TQ_SIGNAL(setProgress(int)),
                         this, TQ_SIGNAL(setProgress(int)));

        // Just write out a peak file
        //
        if (m_currentPeakFile->write(updatePercentage) == false) {
            std::cerr << "Can't write peak file for " << audioFile->getFilename() << " - no preview generated" << std::endl;
            throw BadPeakFileException
            (audioFile->getFilename(), __FILE__, __LINE__);
        }

        // The m_currentPeakFile might have been cancelled (see stopPreview())
        //
        if (m_currentPeakFile) {
            // close writes out important things
            m_currentPeakFile->close();
            m_currentPeakFile->disconnect();
        }
    } else if (audioFile->getType() == BWF) {
        // write the file out and incorporate the peak chunk
    } else {
#ifdef DEBUG_PEAKFILEMANAGER
        std::cerr << "PeakFileManager::generatePeaks - unsupported file type"
        << std::endl;
#endif

        return ;
    }

    m_currentPeakFile = 0;

}

std::vector<float>
PeakFileManager::getPreview(AudioFile *audioFile,
                            const RealTime &startTime,
                            const RealTime &endTime,
                            int width,
                            bool showMinima)
{
    std::vector<float> rV;

    // If we've got no channels then the audio file hasn't
    // completed (recording) - so don't generate a preview
    //
    if (audioFile->getChannels() == 0)
        return rV;

    if (audioFile->getType() == WAV) {
        PeakFile *peakFile = getPeakFile(audioFile);

        // just write out a peak file
        try {
            peakFile->open();
            rV = peakFile->getPreview(startTime,
                                      endTime,
                                      width,
                                      showMinima);
        } catch (SoundFile::BadSoundFileException e) {
#ifdef DEBUG_PEAKFILEMANAGER
            std::cout << "PeakFileManager::getPreview "
            << "\"" << e << "\"" << std::endl;
#else

            ;
#endif

            throw BadPeakFileException(e);
        }
    } else if (audioFile->getType() == BWF) {
        // write the file out and incorporate the peak chunk
    }
#ifdef DEBUG_PEAKFILEMANAGER
    else {
        std::cerr << "PeakFileManager::getPreview - unsupported file type"
        << std::endl;
    }
#endif

    return rV;
}

void
PeakFileManager::clear()
{
    std::vector<PeakFile*>::iterator it;

    for (it = m_peakFiles.begin(); it != m_peakFiles.end(); it++)
        delete (*it);

    m_peakFiles.erase(m_peakFiles.begin(), m_peakFiles.end());

    m_currentPeakFile = 0;
}


std::vector<SplitPointPair>
PeakFileManager::getSplitPoints(AudioFile *audioFile,
                                const RealTime &startTime,
                                const RealTime &endTime,
                                int threshold,
                                const RealTime &minTime)
{
    PeakFile *peakFile = getPeakFile(audioFile);

    if (peakFile == 0)
        return std::vector<SplitPointPair>();

    return peakFile->getSplitPoints(startTime,
                                    endTime,
                                    threshold,
                                    minTime);

}

void
PeakFileManager::stopPreview()
{
    if (m_currentPeakFile) {
        // Stop processing
        //
        TQString fileName = TQString(m_currentPeakFile->getFilename().data());
        m_currentPeakFile->setProcessingPeaks(false);
        m_currentPeakFile->disconnect();

        TQFile file(fileName);
        bool removed = file.remove();

#ifdef DEBUG_PEAKFILEMANAGER

        if (removed) {
            std::cout << "PeakFileManager::stopPreview() - removed preview"
            << std::endl;
        }
#endif
        //delete m_currentPeakFile;
        m_currentPeakFile = 0;
    }
}




}


#include "PeakFileManager.moc"