Chris@107: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@107: Chris@107: /* Chris@107: Sonic Annotator Chris@107: A utility for batch feature extraction from audio files. Chris@107: Mark Levy, Chris Sutton and Chris Cannam, Queen Mary, University of London. Chris@107: Copyright 2007-2014 QMUL. Chris@107: Chris@107: This program is free software; you can redistribute it and/or Chris@107: modify it under the terms of the GNU General Public License as Chris@107: published by the Free Software Foundation; either version 2 of the Chris@107: License, or (at your option) any later version. See the file Chris@107: COPYING included with this distribution for more information. Chris@107: */ Chris@107: Chris@107: #include "MultiplexedReader.h" Chris@107: Chris@107: MultiplexedReader::MultiplexedReader(QList readers) : Chris@107: m_readers(readers) Chris@107: { Chris@107: m_channelCount = readers.size(); Chris@107: m_sampleRate = readers[0]->getSampleRate(); Chris@107: Chris@263: SVDEBUG << "MultiplexedReader: channel count: " << m_channelCount Chris@263: << " (i.e. " << m_channelCount << " reader(s) to multiplex)" << endl; Chris@263: SVDEBUG << "MultiplexedReader: sample rate from first reader: " Chris@263: << m_sampleRate << endl; Chris@263: Chris@107: m_frameCount = 0; Chris@107: m_quicklySeekable = true; Chris@107: Chris@107: foreach (AudioFileReader *r, m_readers) { Chris@107: if (!r->isOK()) { Chris@107: m_channelCount = 0; Chris@107: m_error = r->getError(); Chris@185: } else if (r->getSampleRate() != m_sampleRate) { Chris@185: m_channelCount = 0; Chris@185: m_error = "Readers provided to MultiplexedReader must have the same sample rate"; Chris@107: } else { Chris@107: if (r->getFrameCount() > m_frameCount) { Chris@107: m_frameCount = r->getFrameCount(); Chris@107: } Chris@107: if (!r->isQuicklySeekable()) { Chris@107: m_quicklySeekable = false; Chris@107: } Chris@107: } Chris@107: } Chris@107: } Chris@107: Chris@107: MultiplexedReader::~MultiplexedReader() Chris@107: { Chris@107: foreach (AudioFileReader *r, m_readers) { Chris@107: delete r; Chris@107: } Chris@107: } Chris@107: Chris@258: std::vector Chris@195: MultiplexedReader::getInterleavedFrames(sv_frame_t start, sv_frame_t frameCount) const Chris@107: { Chris@114: int out_chans = m_readers.size(); Chris@107: Chris@114: // Allocate and zero Chris@258: std::vector block(frameCount * out_chans, 0.f); Chris@107: Chris@114: for (int out_chan = 0; out_chan < out_chans; ++out_chan) { Chris@107: Chris@114: AudioFileReader *reader = m_readers[out_chan]; Chris@258: auto readerBlock = reader->getInterleavedFrames(start, frameCount); Chris@107: Chris@114: int in_chans = reader->getChannelCount(); Chris@107: Chris@114: for (int frame = 0; frame < frameCount; ++frame) { Chris@114: Chris@114: int out_index = frame * out_chans + out_chan; Chris@114: Chris@114: for (int in_chan = 0; in_chan < in_chans; ++in_chan) { Chris@114: int in_index = frame * in_chans + in_chan; Chris@114: if (in_index >= (int)readerBlock.size()) break; Chris@114: block[out_index] += readerBlock[in_index]; Chris@107: } Chris@114: Chris@114: if (in_chans > 1) { Chris@114: block[out_index] /= float(in_chans); Chris@114: } Chris@107: } Chris@107: } Chris@195: Chris@195: return block; Chris@107: } Chris@107: Chris@107: int Chris@107: MultiplexedReader::getDecodeCompletion() const Chris@107: { Chris@107: int completion = 100; Chris@107: foreach (AudioFileReader *r, m_readers) { Chris@107: int c = r->getDecodeCompletion(); Chris@107: if (c < 100) { Chris@107: completion = c; Chris@107: } Chris@107: } Chris@107: return completion; Chris@107: } Chris@107: Chris@107: bool Chris@107: MultiplexedReader::isUpdating() const Chris@107: { Chris@107: foreach (AudioFileReader *r, m_readers) { Chris@107: if (r->isUpdating()) return true; Chris@107: } Chris@107: return false; Chris@107: } Chris@107: Chris@107: Chris@107: