annotate runner/MultiplexedReader.cpp @ 218:dc82579c9a26

Add test for multiplexed audio
author Chris Cannam
date Fri, 20 Nov 2015 09:31:55 +0000
parents 3f7c65f26559
children d0fe861bb116
rev   line source
Chris@107 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@107 2
Chris@107 3 /*
Chris@107 4 Sonic Annotator
Chris@107 5 A utility for batch feature extraction from audio files.
Chris@107 6 Mark Levy, Chris Sutton and Chris Cannam, Queen Mary, University of London.
Chris@107 7 Copyright 2007-2014 QMUL.
Chris@107 8
Chris@107 9 This program is free software; you can redistribute it and/or
Chris@107 10 modify it under the terms of the GNU General Public License as
Chris@107 11 published by the Free Software Foundation; either version 2 of the
Chris@107 12 License, or (at your option) any later version. See the file
Chris@107 13 COPYING included with this distribution for more information.
Chris@107 14 */
Chris@107 15
Chris@107 16 #include "MultiplexedReader.h"
Chris@107 17
Chris@107 18 MultiplexedReader::MultiplexedReader(QList<AudioFileReader *> readers) :
Chris@107 19 m_readers(readers)
Chris@107 20 {
Chris@107 21 m_channelCount = readers.size();
Chris@107 22 m_sampleRate = readers[0]->getSampleRate();
Chris@107 23
Chris@107 24 m_frameCount = 0;
Chris@107 25 m_quicklySeekable = true;
Chris@107 26
Chris@107 27 foreach (AudioFileReader *r, m_readers) {
Chris@107 28 if (!r->isOK()) {
Chris@107 29 m_channelCount = 0;
Chris@107 30 m_error = r->getError();
Chris@185 31 } else if (r->getSampleRate() != m_sampleRate) {
Chris@185 32 m_channelCount = 0;
Chris@185 33 m_error = "Readers provided to MultiplexedReader must have the same sample rate";
Chris@107 34 } else {
Chris@107 35 if (r->getFrameCount() > m_frameCount) {
Chris@107 36 m_frameCount = r->getFrameCount();
Chris@107 37 }
Chris@107 38 if (!r->isQuicklySeekable()) {
Chris@107 39 m_quicklySeekable = false;
Chris@107 40 }
Chris@107 41 }
Chris@107 42 }
Chris@107 43 }
Chris@107 44
Chris@107 45 MultiplexedReader::~MultiplexedReader()
Chris@107 46 {
Chris@107 47 foreach (AudioFileReader *r, m_readers) {
Chris@107 48 delete r;
Chris@107 49 }
Chris@107 50 }
Chris@107 51
Chris@195 52 SampleBlock
Chris@195 53 MultiplexedReader::getInterleavedFrames(sv_frame_t start, sv_frame_t frameCount) const
Chris@107 54 {
Chris@114 55 int out_chans = m_readers.size();
Chris@107 56
Chris@114 57 // Allocate and zero
Chris@195 58 SampleBlock block(frameCount * out_chans, 0.f);
Chris@107 59
Chris@114 60 for (int out_chan = 0; out_chan < out_chans; ++out_chan) {
Chris@107 61
Chris@114 62 AudioFileReader *reader = m_readers[out_chan];
Chris@195 63 SampleBlock readerBlock = getInterleavedFrames(start, frameCount);
Chris@107 64
Chris@114 65 int in_chans = reader->getChannelCount();
Chris@107 66
Chris@114 67 for (int frame = 0; frame < frameCount; ++frame) {
Chris@114 68
Chris@114 69 int out_index = frame * out_chans + out_chan;
Chris@114 70
Chris@114 71 for (int in_chan = 0; in_chan < in_chans; ++in_chan) {
Chris@114 72 int in_index = frame * in_chans + in_chan;
Chris@114 73 if (in_index >= (int)readerBlock.size()) break;
Chris@114 74 block[out_index] += readerBlock[in_index];
Chris@107 75 }
Chris@114 76
Chris@114 77 if (in_chans > 1) {
Chris@114 78 block[out_index] /= float(in_chans);
Chris@114 79 }
Chris@107 80 }
Chris@107 81 }
Chris@195 82
Chris@195 83 return block;
Chris@107 84 }
Chris@107 85
Chris@107 86 int
Chris@107 87 MultiplexedReader::getDecodeCompletion() const
Chris@107 88 {
Chris@107 89 int completion = 100;
Chris@107 90 foreach (AudioFileReader *r, m_readers) {
Chris@107 91 int c = r->getDecodeCompletion();
Chris@107 92 if (c < 100) {
Chris@107 93 completion = c;
Chris@107 94 }
Chris@107 95 }
Chris@107 96 return completion;
Chris@107 97 }
Chris@107 98
Chris@107 99 bool
Chris@107 100 MultiplexedReader::isUpdating() const
Chris@107 101 {
Chris@107 102 foreach (AudioFileReader *r, m_readers) {
Chris@107 103 if (r->isUpdating()) return true;
Chris@107 104 }
Chris@107 105 return false;
Chris@107 106 }
Chris@107 107
Chris@107 108
Chris@107 109