Chris@148
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@148
|
2
|
Chris@148
|
3 /*
|
Chris@148
|
4 Sonic Visualiser
|
Chris@148
|
5 An audio file viewer and annotation editor.
|
Chris@148
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@148
|
7 This file copyright 2006 Chris Cannam.
|
Chris@148
|
8
|
Chris@148
|
9 This program is free software; you can redistribute it and/or
|
Chris@148
|
10 modify it under the terms of the GNU General Public License as
|
Chris@148
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@148
|
12 License, or (at your option) any later version. See the file
|
Chris@148
|
13 COPYING included with this distribution for more information.
|
Chris@148
|
14 */
|
Chris@148
|
15
|
Chris@148
|
16 #include "WavFileReader.h"
|
Chris@148
|
17
|
Chris@148
|
18 #include <iostream>
|
Chris@148
|
19
|
Chris@175
|
20 #include <QMutexLocker>
|
Chris@175
|
21
|
Chris@148
|
22 WavFileReader::WavFileReader(QString path) :
|
Chris@148
|
23 m_file(0),
|
Chris@148
|
24 m_path(path),
|
Chris@148
|
25 m_buffer(0),
|
Chris@148
|
26 m_bufsiz(0),
|
Chris@148
|
27 m_lastStart(0),
|
Chris@148
|
28 m_lastCount(0)
|
Chris@148
|
29 {
|
Chris@148
|
30 m_frameCount = 0;
|
Chris@148
|
31 m_channelCount = 0;
|
Chris@148
|
32 m_sampleRate = 0;
|
Chris@148
|
33
|
Chris@148
|
34 m_fileInfo.format = 0;
|
Chris@148
|
35 m_fileInfo.frames = 0;
|
Chris@148
|
36 m_file = sf_open(m_path.toLocal8Bit(), SFM_READ, &m_fileInfo);
|
Chris@148
|
37
|
Chris@175
|
38 if (!m_file || m_fileInfo.channels <= 0) {
|
Chris@148
|
39 std::cerr << "WavFileReader::initialize: Failed to open file ("
|
Chris@148
|
40 << sf_strerror(m_file) << ")" << std::endl;
|
Chris@148
|
41
|
Chris@148
|
42 if (m_file) {
|
Chris@148
|
43 m_error = QString("Couldn't load audio file '%1':\n%2")
|
Chris@148
|
44 .arg(m_path).arg(sf_strerror(m_file));
|
Chris@148
|
45 } else {
|
Chris@148
|
46 m_error = QString("Failed to open audio file '%1'")
|
Chris@148
|
47 .arg(m_path);
|
Chris@148
|
48 }
|
Chris@148
|
49 return;
|
Chris@148
|
50 }
|
Chris@148
|
51
|
Chris@148
|
52 m_frameCount = m_fileInfo.frames;
|
Chris@148
|
53 m_channelCount = m_fileInfo.channels;
|
Chris@148
|
54 m_sampleRate = m_fileInfo.samplerate;
|
Chris@175
|
55
|
Chris@175
|
56 std::cerr << "WavFileReader: Frame count " << m_frameCount << ", channel count " << m_channelCount << ", sample rate " << m_sampleRate << std::endl;
|
Chris@175
|
57
|
Chris@148
|
58 }
|
Chris@148
|
59
|
Chris@148
|
60 WavFileReader::~WavFileReader()
|
Chris@148
|
61 {
|
Chris@148
|
62 if (m_file) sf_close(m_file);
|
Chris@148
|
63 }
|
Chris@148
|
64
|
Chris@148
|
65 void
|
Chris@175
|
66 WavFileReader::updateFrameCount()
|
Chris@175
|
67 {
|
Chris@175
|
68 QMutexLocker locker(&m_mutex);
|
Chris@175
|
69
|
Chris@175
|
70 size_t prevCount = m_fileInfo.frames;
|
Chris@175
|
71
|
Chris@175
|
72 if (m_file) {
|
Chris@175
|
73 sf_close(m_file);
|
Chris@175
|
74 m_file = sf_open(m_path.toLocal8Bit(), SFM_READ, &m_fileInfo);
|
Chris@175
|
75 if (!m_file || m_fileInfo.channels <= 0) {
|
Chris@175
|
76 std::cerr << "WavFileReader::updateFrameCount: Failed to open file ("
|
Chris@175
|
77 << sf_strerror(m_file) << ")" << std::endl;
|
Chris@175
|
78 }
|
Chris@175
|
79 }
|
Chris@175
|
80
|
Chris@175
|
81 std::cerr << "WavFileReader::updateFrameCount: now " << m_fileInfo.frames << std::endl;
|
Chris@175
|
82
|
Chris@175
|
83 if (m_fileInfo.frames != prevCount) emit frameCountChanged();
|
Chris@175
|
84 }
|
Chris@175
|
85
|
Chris@175
|
86 void
|
Chris@148
|
87 WavFileReader::getInterleavedFrames(size_t start, size_t count,
|
Chris@148
|
88 SampleBlock &results) const
|
Chris@148
|
89 {
|
Chris@175
|
90 if (count == 0) return;
|
Chris@148
|
91 results.clear();
|
Chris@175
|
92
|
Chris@175
|
93 QMutexLocker locker(&m_mutex);
|
Chris@175
|
94
|
Chris@175
|
95 if (!m_file || !m_channelCount) {
|
Chris@175
|
96 return;
|
Chris@175
|
97 }
|
Chris@148
|
98
|
Chris@148
|
99 if ((long)start >= m_fileInfo.frames) {
|
Chris@175
|
100 // std::cerr << "WavFileReader::getInterleavedFrames: " << start
|
Chris@175
|
101 // << " > " << m_fileInfo.frames << std::endl;
|
Chris@148
|
102 return;
|
Chris@148
|
103 }
|
Chris@148
|
104
|
Chris@148
|
105 if (long(start + count) > m_fileInfo.frames) {
|
Chris@148
|
106 count = m_fileInfo.frames - start;
|
Chris@148
|
107 }
|
Chris@148
|
108
|
Chris@148
|
109 sf_count_t readCount = 0;
|
Chris@148
|
110
|
Chris@148
|
111 if (start != m_lastStart || count != m_lastCount) {
|
Chris@148
|
112
|
Chris@148
|
113 if (sf_seek(m_file, start, SEEK_SET) < 0) {
|
Chris@175
|
114 // std::cerr << "sf_seek failed" << std::endl;
|
Chris@148
|
115 return;
|
Chris@148
|
116 }
|
Chris@148
|
117
|
Chris@148
|
118 if (count * m_fileInfo.channels > m_bufsiz) {
|
Chris@148
|
119 // std::cerr << "WavFileReader: Reallocating buffer for " << count
|
Chris@148
|
120 // << " frames, " << m_fileInfo.channels << " channels: "
|
Chris@148
|
121 // << m_bufsiz << " floats" << std::endl;
|
Chris@148
|
122 m_bufsiz = count * m_fileInfo.channels;
|
Chris@148
|
123 delete[] m_buffer;
|
Chris@148
|
124 m_buffer = new float[m_bufsiz];
|
Chris@148
|
125 }
|
Chris@148
|
126
|
Chris@148
|
127 if ((readCount = sf_readf_float(m_file, m_buffer, count)) < 0) {
|
Chris@175
|
128 // std::cerr << "sf_readf_float failed" << std::endl;
|
Chris@148
|
129 return;
|
Chris@148
|
130 }
|
Chris@148
|
131
|
Chris@148
|
132 m_lastStart = start;
|
Chris@148
|
133 m_lastCount = readCount;
|
Chris@148
|
134 }
|
Chris@148
|
135
|
Chris@148
|
136 for (size_t i = 0; i < count * m_fileInfo.channels; ++i) {
|
Chris@148
|
137 results.push_back(m_buffer[i]);
|
Chris@148
|
138 }
|
Chris@148
|
139
|
Chris@148
|
140 return;
|
Chris@148
|
141 }
|
Chris@148
|
142
|
Chris@157
|
143 void
|
Chris@157
|
144 WavFileReader::getSupportedExtensions(std::set<QString> &extensions)
|
Chris@157
|
145 {
|
Chris@157
|
146 int count;
|
Chris@157
|
147
|
Chris@157
|
148 if (sf_command(0, SFC_GET_FORMAT_MAJOR_COUNT, &count, sizeof(count))) {
|
Chris@157
|
149 extensions.insert("wav");
|
Chris@157
|
150 extensions.insert("aiff");
|
Chris@157
|
151 extensions.insert("aif");
|
Chris@157
|
152 return;
|
Chris@157
|
153 }
|
Chris@157
|
154
|
Chris@157
|
155 SF_FORMAT_INFO info;
|
Chris@157
|
156 for (int i = 0; i < count; ++i) {
|
Chris@157
|
157 info.format = i;
|
Chris@157
|
158 if (!sf_command(0, SFC_GET_FORMAT_MAJOR, &info, sizeof(info))) {
|
Chris@157
|
159 extensions.insert(info.extension);
|
Chris@157
|
160 }
|
Chris@157
|
161 }
|
Chris@157
|
162 }
|