comparison data/fileio/WavFileReader.cpp @ 148:1a42221a1522

* Reorganising code base. This revision will not compile.
author Chris Cannam
date Mon, 31 Jul 2006 11:49:58 +0000
parents
children c03ec31005e1
comparison
equal deleted inserted replaced
147:3a13b0d4934e 148:1a42221a1522
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006 Chris Cannam.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
14 */
15
16 #include "WavFileReader.h"
17
18 #include <iostream>
19
20 WavFileReader::WavFileReader(QString path) :
21 m_file(0),
22 m_path(path),
23 m_buffer(0),
24 m_bufsiz(0),
25 m_lastStart(0),
26 m_lastCount(0)
27 {
28 m_frameCount = 0;
29 m_channelCount = 0;
30 m_sampleRate = 0;
31
32 m_fileInfo.format = 0;
33 m_fileInfo.frames = 0;
34 m_file = sf_open(m_path.toLocal8Bit(), SFM_READ, &m_fileInfo);
35
36 if (!m_file || m_fileInfo.frames <= 0 || m_fileInfo.channels <= 0) {
37 std::cerr << "WavFileReader::initialize: Failed to open file ("
38 << sf_strerror(m_file) << ")" << std::endl;
39
40 if (m_file) {
41 m_error = QString("Couldn't load audio file '%1':\n%2")
42 .arg(m_path).arg(sf_strerror(m_file));
43 } else {
44 m_error = QString("Failed to open audio file '%1'")
45 .arg(m_path);
46 }
47 return;
48 }
49
50 m_frameCount = m_fileInfo.frames;
51 m_channelCount = m_fileInfo.channels;
52 m_sampleRate = m_fileInfo.samplerate;
53 }
54
55 WavFileReader::~WavFileReader()
56 {
57 if (m_file) sf_close(m_file);
58 }
59
60 void
61 WavFileReader::getInterleavedFrames(size_t start, size_t count,
62 SampleBlock &results) const
63 {
64 results.clear();
65 if (!m_file || !m_channelCount) return;
66 if (count == 0) return;
67
68 if ((long)start >= m_fileInfo.frames) {
69 return;
70 }
71
72 if (long(start + count) > m_fileInfo.frames) {
73 count = m_fileInfo.frames - start;
74 }
75
76 sf_count_t readCount = 0;
77
78 m_mutex.lock();
79
80 if (start != m_lastStart || count != m_lastCount) {
81
82 if (sf_seek(m_file, start, SEEK_SET) < 0) {
83 m_mutex.unlock();
84 return;
85 }
86
87 if (count * m_fileInfo.channels > m_bufsiz) {
88 // std::cerr << "WavFileReader: Reallocating buffer for " << count
89 // << " frames, " << m_fileInfo.channels << " channels: "
90 // << m_bufsiz << " floats" << std::endl;
91 m_bufsiz = count * m_fileInfo.channels;
92 delete[] m_buffer;
93 m_buffer = new float[m_bufsiz];
94 }
95
96 if ((readCount = sf_readf_float(m_file, m_buffer, count)) < 0) {
97 m_mutex.unlock();
98 return;
99 }
100
101 m_lastStart = start;
102 m_lastCount = readCount;
103 }
104
105 for (size_t i = 0; i < count * m_fileInfo.channels; ++i) {
106 results.push_back(m_buffer[i]);
107 }
108
109 m_mutex.unlock();
110 return;
111 }
112