diff data/fileio/WavFileReader.cpp @ 1041:843f67be0ed9 cxx11

Replace the get*Frames calls in AudioFileReader with less stupid API
author Chris Cannam
date Wed, 04 Mar 2015 12:30:41 +0000
parents a1cd5abcb38b
children 48e4ffa9fb48
line wrap: on
line diff
--- a/data/fileio/WavFileReader.cpp	Wed Mar 04 12:01:04 2015 +0000
+++ b/data/fileio/WavFileReader.cpp	Wed Mar 04 12:30:41 2015 +0000
@@ -25,8 +25,6 @@
     m_source(source),
     m_path(source.getLocalFilename()),
     m_seekable(false),
-    m_buffer(0),
-    m_bufsiz(0),
     m_lastStart(0),
     m_lastCount(0),
     m_updating(fileUpdating)
@@ -81,7 +79,6 @@
 WavFileReader::~WavFileReader()
 {
     if (m_file) sf_close(m_file);
-    delete[] m_buffer;
 }
 
 void
@@ -122,24 +119,21 @@
     m_updating = false;
 }
 
-void
-WavFileReader::getInterleavedFrames(sv_frame_t start, sv_frame_t count,
-				    SampleBlock &results) const
+SampleBlock
+WavFileReader::getInterleavedFrames(sv_frame_t start, sv_frame_t count) const
 {
-    if (count == 0) return;
-    results.clear();
-    results.reserve(count * m_fileInfo.channels);
+    if (count == 0) return SampleBlock();
 
     QMutexLocker locker(&m_mutex);
 
     if (!m_file || !m_channelCount) {
-        return;
+        return SampleBlock();
     }
 
     if (start >= m_fileInfo.frames) {
 //        SVDEBUG << "WavFileReader::getInterleavedFrames: " << start
 //                  << " > " << m_fileInfo.frames << endl;
-	return;
+	return SampleBlock();
     }
 
     if (start + count > m_fileInfo.frames) {
@@ -151,36 +145,21 @@
     if (start != m_lastStart || count != m_lastCount) {
 
 	if (sf_seek(m_file, start, SEEK_SET) < 0) {
-//            cerr << "sf_seek failed" << endl;
-	    return;
+	    return SampleBlock();
 	}
+
+        sv_frame_t n = count * m_fileInfo.channels;
+        m_buffer.resize(n);
 	
-	if (count * m_fileInfo.channels > m_bufsiz) {
-//	    cerr << "WavFileReader: Reallocating buffer for " << count
-//		      << " frames, " << m_fileInfo.channels << " channels: "
-//		      << m_bufsiz << " floats" << endl;
-	    m_bufsiz = count * m_fileInfo.channels;
-	    delete[] m_buffer;
-	    m_buffer = new float[m_bufsiz];
-	}
-	
-	if ((readCount = sf_readf_float(m_file, m_buffer, count)) < 0) {
-//            cerr << "sf_readf_float failed" << endl;
-	    return;
+	if ((readCount = sf_readf_float(m_file, m_buffer.data(), count)) < 0) {
+	    return SampleBlock();
 	}
 
 	m_lastStart = start;
 	m_lastCount = readCount;
     }
 
-    for (sv_frame_t i = 0; i < count * m_fileInfo.channels; ++i) {
-        if (i >= m_bufsiz) {
-            cerr << "INTERNAL ERROR: WavFileReader::getInterleavedFrames: " << i << " >= " << m_bufsiz << endl;
-        }
-	results.push_back(m_buffer[i]);
-    }
-
-    return;
+    return m_buffer;
 }
 
 void