File: WavFileReader.cpp

package info (click to toggle)
sonic-visualiser 2.5~repack1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,596 kB
  • ctags: 9,853
  • sloc: cpp: 93,843; ansic: 1,138; sh: 1,012; xml: 64; makefile: 35
file content (219 lines) | stat: -rw-r--r-- 6,085 bytes parent folder | download
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
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    Sonic Visualiser
    An audio file viewer and annotation editor.
    Centre for Digital Music, Queen Mary, University of London.
    This file copyright 2006 Chris Cannam and QMUL.
    
    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 "WavFileReader.h"

#include <iostream>

#include <QMutexLocker>
#include <QFileInfo>

WavFileReader::WavFileReader(FileSource source, bool fileUpdating) :
    m_file(0),
    m_source(source),
    m_path(source.getLocalFilename()),
    m_seekable(false),
    m_lastStart(0),
    m_lastCount(0),
    m_updating(fileUpdating)
{
    m_frameCount = 0;
    m_channelCount = 0;
    m_sampleRate = 0;

    m_fileInfo.format = 0;
    m_fileInfo.frames = 0;
    m_file = sf_open(m_path.toLocal8Bit(), SFM_READ, &m_fileInfo);

    if (!m_file || (!fileUpdating && m_fileInfo.channels <= 0)) {
	cerr << "WavFileReader::initialize: Failed to open file at \""
                  << m_path << "\" ("
		  << sf_strerror(m_file) << ")" << endl;

	if (m_file) {
	    m_error = QString("Couldn't load audio file '%1':\n%2")
		.arg(m_path).arg(sf_strerror(m_file));
	} else {
	    m_error = QString("Failed to open audio file '%1'")
		.arg(m_path);
	}
	return;
    }

    if (m_fileInfo.channels > 0) {

        m_frameCount = m_fileInfo.frames;
        m_channelCount = m_fileInfo.channels;
        m_sampleRate = m_fileInfo.samplerate;

        m_seekable = (m_fileInfo.seekable != 0);

        // Our m_seekable reports whether a file is rapidly seekable,
        // so things like Ogg don't qualify. We cautiously report
        // every file type of "at least" the historical period of Ogg
        // or FLAC as non-seekable.
        int type = m_fileInfo.format & SF_FORMAT_TYPEMASK;
//        cerr << "WavFileReader: format type is " << type << " (flac, ogg are " << SF_FORMAT_FLAC << ", " << SF_FORMAT_OGG << ")" << endl;
        if (type >= SF_FORMAT_FLAC || type >= SF_FORMAT_OGG) {
//            cerr << "WavFileReader: Recording as non-seekable" << endl;
            m_seekable = false;
        }
    }

//    cerr << "WavFileReader: Frame count " << m_frameCount << ", channel count " << m_channelCount << ", sample rate " << m_sampleRate << ", seekable " << m_seekable << endl;

}

WavFileReader::~WavFileReader()
{
    if (m_file) sf_close(m_file);
}

void
WavFileReader::updateFrameCount()
{
    QMutexLocker locker(&m_mutex);

    sv_frame_t prevCount = m_fileInfo.frames;

    if (m_file) {
        sf_close(m_file);
        m_file = sf_open(m_path.toLocal8Bit(), SFM_READ, &m_fileInfo);
        if (!m_file || m_fileInfo.channels <= 0) {
            cerr << "WavFileReader::updateFrameCount: Failed to open file at \"" << m_path << "\" ("
                      << sf_strerror(m_file) << ")" << endl;
        }
    }

//    SVDEBUG << "WavFileReader::updateFrameCount: now " << m_fileInfo.frames << endl;

    m_frameCount = m_fileInfo.frames;

    if (m_channelCount == 0) {
        m_channelCount = m_fileInfo.channels;
        m_sampleRate = m_fileInfo.samplerate;
    }

    if (m_frameCount != prevCount) {
//        cerr << "frameCountChanged" << endl;
        emit frameCountChanged();
    }
}

void
WavFileReader::updateDone()
{
    updateFrameCount();
    m_updating = false;
}

SampleBlock
WavFileReader::getInterleavedFrames(sv_frame_t start, sv_frame_t count) const
{
    if (count == 0) return SampleBlock();

    QMutexLocker locker(&m_mutex);

    if (!m_file || !m_channelCount) {
        return SampleBlock();
    }

    if (start >= m_fileInfo.frames) {
//        SVDEBUG << "WavFileReader::getInterleavedFrames: " << start
//                  << " > " << m_fileInfo.frames << endl;
	return SampleBlock();
    }

    if (start + count > m_fileInfo.frames) {
	count = m_fileInfo.frames - start;
    }

    if (start != m_lastStart || count != m_lastCount) {

	if (sf_seek(m_file, start, SEEK_SET) < 0) {
	    return SampleBlock();
	}

        sv_frame_t n = count * m_fileInfo.channels;
        m_buffer.resize(size_t(n));
	
        sf_count_t readCount = 0;

	if ((readCount = sf_readf_float(m_file, m_buffer.data(), count)) < 0) {
	    return SampleBlock();
	}

        m_buffer.resize(size_t(readCount * m_fileInfo.channels));
        
	m_lastStart = start;
	m_lastCount = readCount;
    }

    return m_buffer;
}

void
WavFileReader::getSupportedExtensions(std::set<QString> &extensions)
{
    int count;

    if (sf_command(0, SFC_GET_FORMAT_MAJOR_COUNT, &count, sizeof(count))) {
        extensions.insert("wav");
        extensions.insert("aiff");
        extensions.insert("aifc");
        extensions.insert("aif");
        return;
    }

    SF_FORMAT_INFO info;
    for (int i = 0; i < count; ++i) {
        info.format = i;
        if (!sf_command(0, SFC_GET_FORMAT_MAJOR, &info, sizeof(info))) {
            QString ext = QString(info.extension).toLower();
            extensions.insert(ext);
            if (ext == "oga") {
                // libsndfile is awfully proper, it says it only
                // supports .oga but lots of Ogg audio files in the
                // wild are .ogg and it will accept that
                extensions.insert("ogg");
            }
        }
    }
}

bool
WavFileReader::supportsExtension(QString extension)
{
    std::set<QString> extensions;
    getSupportedExtensions(extensions);
    return (extensions.find(extension.toLower()) != extensions.end());
}

bool
WavFileReader::supportsContentType(QString type)
{
    return (type == "audio/x-wav" ||
            type == "audio/x-aiff" ||
            type == "audio/basic");
}

bool
WavFileReader::supports(FileSource &source)
{
    return (supportsExtension(source.getExtension()) ||
            supportsContentType(source.getContentType()));
}