1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 863 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
|
/*
Copyright (C) 2006-2007 Remon Sijrier
This file is part of Traverso
Traverso is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "ReadSource.h"
#include "ResampleAudioReader.h"
#include "ProjectManager.h"
#include "Project.h"
#include "AudioClip.h"
#include "DiskIO.h"
#include "Utils.h"
#include "Sheet.h"
#include "AudioDevice.h"
#include <QFile>
#include "Config.h"
#include <limits.h>
// Always put me below _all_ includes, this is needed
// in case we run with memory leak detection enabled!
#include "Debugger.h"
/**
* \class ReadSource
\brief A class for (buffered) reading of audio files.
*/
// This constructor is called for existing (recorded/imported) audio sources
ReadSource::ReadSource(const QDomNode node)
: AudioSource()
{
set_state(node);
private_init();
Project* project = pm().get_project();
// FIXME The check below no longer makes sense!!!!!
// Check if the audiofile exists in our project audiosources dir
// and give it priority over the dir as given by the project.tpf file
// This makes it possible to move project directories without Traverso being
// unable to find it's audiosources!
if ( QFile::exists(project->get_root_dir() "/audiosources/" m_name) ||
QFile::exists(project->get_root_dir() "/audiosources/" m_name "-ch0.wav") ) {
set_dir(project->get_root_dir() "/audiosources/");
}
m_silent = (m_channelCount == 0);
}
// constructor for file import
ReadSource::ReadSource(const QString& dir, const QString& name)
: AudioSource(dir, name)
{
private_init();
AbstractAudioReader* reader = AbstractAudioReader::create_audio_reader(m_fileName);
if (reader) {
m_channelCount = reader->get_num_channels();
delete reader;
} else {
m_channelCount = 0;
}
m_silent = false;
}
// Constructor for recorded audio.
ReadSource::ReadSource(const QString& dir, const QString& name, int channelCount)
: AudioSource(dir, name)
{
private_init();
m_channelCount = channelCount;
m_silent = false;
m_name = name "-" QString::number(m_id);
m_fileName = m_dir m_name;
m_rate = pm().get_project()->get_rate();
m_wasRecording = true;
m_shortName = m_name.left(m_name.length() - 20);
}
// Constructor for silent clips
ReadSource::ReadSource()
: AudioSource("", tr("Silence"))
{
private_init();
m_channelCount = 0;
m_silent = true;
}
void ReadSource::private_init()
{
m_refcount = 0;
m_error = 0;
m_clip = 0;
m_audioReader = 0;
m_bufferstatus = 0;
}
ReadSource::~ReadSource()
{
PENTERDES;
for(int i=0; i<m_buffers.size(); i) {
delete m_buffers.at(i);
}
if (m_audioReader) {
delete m_audioReader;
}
if (m_bufferstatus) {
delete m_bufferstatus;
}
}
QDomNode ReadSource::get_state( QDomDocument doc )
{
QDomElement node = doc.createElement("Source");
node.setAttribute("channelcount", m_channelCount);
node.setAttribute("origsheetid", m_origSheetId);
node.setAttribute("dir", m_dir);
node.setAttribute("id", m_id);
node.setAttribute("name", m_name);
node.setAttribute("origbitdepth", m_origBitDepth);
node.setAttribute("wasrecording", m_wasRecording);
node.setAttribute("length", m_length.universal_frame());
node.setAttribute("rate", m_rate);
node.setAttribute("decoder", m_decodertype);
return node;
}
int ReadSource::set_state( const QDomNode & node )
{
PENTER;
QDomElement e = node.toElement();
m_channelCount = e.attribute("channelcount", "0").toInt();
m_origSheetId = e.attribute("origsheetid", "0").toLongLong();
set_dir( e.attribute("dir", "" ));
m_id = e.attribute("id", "").toLongLong();
m_rate = m_outputRate = e.attribute("rate", "0").toUInt();
bool ok;
m_length = TimeRef(e.attribute("length", "0").toLongLong(&ok));
m_origBitDepth = e.attribute("origbitdepth", "0").toInt();
m_wasRecording = e.attribute("wasrecording", "0").toInt();
m_decodertype = e.attribute("decoder", "");
// For older project files, this should properly detect if the
// audio source was a recording or not., in fact this should suffice
// and the flag wasrecording would be unneeded, but oh well....
if (m_origSheetId != 0) {
m_wasRecording = true;
}
set_name( e.attribute("name", "No name supplied?" ));
return 1;
}
int ReadSource::init( )
{
PENTER;
Q_ASSERT(m_refcount);
Project* project = pm().get_project();
m_bufferstatus = new BufferStatus;
// Fake the samplerate, until it's set by an AudioReader!
if (project) {
m_rate = m_outputRate = project->get_rate();
} else {
m_rate = 44100;
}
if (m_silent) {
m_length = TimeRef(LLONG_MAX);
m_channelCount = 0;
m_origBitDepth = 16;
m_bufferstatus->fillStatus = 100;
m_bufferstatus->needSync = false;
return 1;
}
if (m_channelCount == 0) {
PERROR("ReadSource channel count is 0");
return (m_error = INVALID_CHANNEL_COUNT);
}
if ( ! QFile::exists(m_fileName)) {
return (m_error = FILE_DOES_NOT_EXIST);
}
m_rbReady = 0;
m_needSync = 1;
m_syncInProgress = 0;
m_bufferUnderRunDetected = m_wasActivated = 0;
m_active = 0;
// There should be another config option for ConverterType to use for export (higher quality)
//converter_type = config().get_property("Conversion", "ExportResamplingConverterType", 0).toInt();
m_audioReader = new ResampleAudioReader(m_fileName, m_decodertype);
if (!m_audioReader->is_valid()) {
PERROR("ReadSource:: audio reader is not valid! (reader channel count: %d, nframes: %d", m_audioReader->get_num_channels(), m_audioReader->get_nframes());
delete m_audioReader;
m_audioReader = 0;
return (m_error = COULD_NOT_OPEN_FILE);
}
int converter_type = config().get_property("Conversion", "RTResamplingConverterType", DEFAULT_RESAMPLE_QUALITY).toInt();
m_audioReader->set_converter_type(converter_type);
set_output_rate(m_audioReader->get_file_rate());
// (re)set the decoder type
m_decodertype = m_audioReader->decoder_type();
m_channelCount = m_audioReader->get_num_channels();
// @Ben: I thought we support any channel count now ??
if (m_channelCount > 2) {
PERROR("ReadAudioSource: file contains %d channels; only 2 channels are supported", m_channelCount);
delete m_audioReader;
m_audioReader = 0;
return (m_error = INVALID_CHANNEL_COUNT);
}
// Never reached, it's already checked in AbstractAudioReader::is_valid() which was already called!
if (m_channelCount == 0) {
PERROR("ReadAudioSource: not a valid channel count: %d", m_channelCount);
delete m_audioReader;
m_audioReader = 0;
return (m_error = ZERO_CHANNELS);
}
m_rate = m_audioReader->get_file_rate();
m_length = m_audioReader->get_length();
return 1;
}
void ReadSource::set_output_rate(int rate)
{
Q_ASSERT(rate > 0);
if (! m_audioReader) {
printf("ReadSource::set_output_rate: No audioreader!\n");
return;
}
bool useResampling = config().get_property("Conversion", "DynamicResampling", true).toBool();
if (useResampling) {
m_audioReader->set_output_rate(rate);
} else {
m_audioReader->set_output_rate(m_audioReader->get_file_rate());
}
m_outputRate = rate;
// The length could have become slightly smaller/larger due
// rounding issues involved with converting to one samplerate to another.
// Should be at the order of one - two samples at most, but for reading purposes we
// need sample accurate information!
m_length = m_audioReader->get_length();
}
int ReadSource::file_read(DecodeBuffer* buffer, const TimeRef& start, nframes_t cnt) const
{
// PROFILE_START;
Q_ASSERT(m_audioReader);
nframes_t result = m_audioReader->read_from(buffer, start, cnt);
// PROFILE_END("ReadSource::fileread");
return result;
}
int ReadSource::file_read(DecodeBuffer * buffer, nframes_t start, nframes_t cnt)
{
Q_ASSERT(m_audioReader);
return m_audioReader->read_from(buffer, start, cnt);
}
ReadSource * ReadSource::deep_copy( )
{
PENTER;
QDomDocument doc("ReadSource");
QDomNode rsnode = get_state(doc);
ReadSource* source = new ReadSource(rsnode);
return source;
}
void ReadSource::set_audio_clip(AudioClip* clip)
{
PENTER;
Q_ASSERT(clip);
m_clip = clip;
}
nframes_t ReadSource::get_nframes( ) const
{
if (!m_audioReader) {
return 0;
}
return m_audioReader->get_nframes();
}
int ReadSource::set_file(const QString & filename)
{
PENTER;
Q_ASSERT(m_clip);
m_error = 0;
int splitpoint = filename.lastIndexOf("/") 1;
int length = filename.length();
QString dir = filename.left(splitpoint - 1) "/";
QString name = filename.right(length - splitpoint);
set_dir(dir);
set_name(name);
if (init() < 0) {
return -1;
}
set_audio_clip(m_clip);
emit stateChanged();
return 1;
}
int ReadSource::rb_read(audio_sample_t** dst, TimeRef& start, nframes_t count)
{
if (m_channelCount == 0) {
return count;
}
if ( ! m_rbReady ) {
// printf("ringbuffer not ready\n");
return 0;
}
TimeRef diff = m_rbRelativeFileReadPos - start;
// In universal frame positioning, it is possible (somehow) that the start
// location and m_rbRelativeFileReadPos differ very very slighly, and when
// converted to frames, the difference is much smaller then 1 frame.
// To catch these kind of I think 'rounding issues' we convert to frames here
// and see if the diff in frames == 0, in which case it should be fine to make
// our m_rbRelativeFileReadPos equal to start, and thus avoiding unwanted resync actions!
if ( (diff.universal_frame() > 0) && (diff.to_frame(m_outputRate) == 0) ) {
m_rbRelativeFileReadPos = start;
}
if (start != m_rbRelativeFileReadPos) {
TimeRef availabletime(nframes_t(m_buffers.at(0)->read_space()), m_outputRate);
/* printf("rb_read:: m_rbRelativeFileReadPos, start: %lld, %lld\n", m_rbRelativeFileReadPos.universal_frame(), start.universal_frame());
printf("rb_read:: availabletime %d\n", availabletime.to_frame(m_outputRate));*/
if ( (start > m_rbRelativeFileReadPos) && ((m_rbRelativeFileReadPos availabletime) > (start TimeRef(count, m_outputRate))) ) {
TimeRef advance = start - m_rbRelativeFileReadPos;
if (availabletime < advance) {
printf("available < advance !!!!!!!\n");
}
for (int i=m_buffers.size()-1; i>=0; --i) {
m_buffers.at(i)->increment_read_ptr(advance.to_frame(m_outputRate));
}
m_rbRelativeFileReadPos = advance;
/* printf("rb_read:: advance %d\n", advance.to_frame(m_outputRate));
printf("rb_read:: m_rbRelativeFileReadPos after advance %d\n", m_rbRelativeFileReadPos.to_frame(m_outputRate));*/
} else {
TimeRef synclocation = start m_clip->get_track_start_location() m_clip->get_source_start_location();
start_resync(synclocation);
return 0;
}
}
nframes_t readcount = 0;
for (int chan=0; chan<m_channelCount; chan) {
readcount = m_buffers.at(chan)->read(dst[chan], count);
if (readcount != count) {
PMESG("readcount, count: %d, %d", readcount, count);
// Hmm, not sure what to do in this case....
}
}
m_rbRelativeFileReadPos.add_frames(readcount, m_outputRate);
return readcount;
}
int ReadSource::rb_file_read(DecodeBuffer* buffer, nframes_t cnt)
{
nframes_t readFrames = file_read(buffer, m_rbFileReadPos, cnt);
if (readFrames == cnt) {
m_rbFileReadPos.add_frames(readFrames, m_outputRate);
} else {
// We either passed the end of the file, or our audio reader
// is doing weird things, is broken, invalid or something else
// Set the rinbuffer file readpos to m_length so processing stops here!
m_rbFileReadPos = m_length;
}
return readFrames;
}
void ReadSource::rb_seek_to_file_position(TimeRef& position)
{
Q_ASSERT(m_clip);
// printf("rb_seek_to_file_position:: seeking to %d\n", position);
// calculate position relative to the file!
TimeRef fileposition = position - m_clip->get_track_start_location() - m_clip->get_source_start_location();
// Do nothing if we are already at the seek position
if (m_rbFileReadPos == fileposition) {
// printf("ringbuffer already at position %d\n", position);
return;
}
// check if the clip's start position is within the range
// if not, fill the buffer from the earliest point this clip
// will come into play.
if (fileposition < TimeRef()) {
// printf("not seeking to %ld, but too %d\n\n", fileposition,m_clip->get_source_start_location());
fileposition = m_clip->get_source_start_location();
}
// printf("rb_seek_to_file_position:: seeking to relative pos: %d\n", fileposition);
// The content of our buffers is no longer valid, so we empty them
for (int i=0; i<m_buffers.size(); i) {
m_buffers.at(i)->reset();
}
m_rbFileReadPos = fileposition;
m_rbRelativeFileReadPos = fileposition;
// printf("rb_seek_to_file_position:: m_rbRelativeFileReadPos, synclocation: %d, %d\n", m_rbRelativeFileReadPos.to_frame(m_outputRate), fileposition.to_frame(m_outputRate));
}
void ReadSource::process_ringbuffer(DecodeBuffer* buffer, bool seeking)
{
if (m_channelCount == 0) {
return;
}
// Do nothing if we passed the lenght of the AudioFile.
if (m_rbFileReadPos >= m_length) {
// printf("returning, m_rbFileReadPos > m_length! (%d > %d)\n", m_rbFileReadPos.to_frame(get_rate()), m_audioReader->get_nframes());
if (m_syncInProgress) {
finish_resync();
}
return;
}
// Calculate the number of samples we can write into the buffer
int writeSpace = m_buffers.at(0)->write_space();
// The amount of chunks which can be 'read'
int chunkCount = (int)(writeSpace / m_chunkSize);
int toRead = m_chunkSize;
if (seeking) {
toRead = writeSpace;
// printf("doing a full seek buffer fill\n");
} else if (m_syncInProgress) {
// Currently, we fill the buffer completely.
// For some reason, filling it with 1/4 at a time
// doesn't fill it consitently, and thus giving audible artifacts.
/* toRead = m_chunkSize * 2;*/
toRead = writeSpace;
} else if (chunkCount == 0) {
// If we are nearing the end of the source file it could be possible
// we only need to read the last samples which is smaller in size then
// chunksize. If so, set toRead to m_source->m_length - rbFileReasPos
nframes_t available = (m_length - m_rbFileReadPos).to_frame(m_outputRate);
if (available <= m_chunkSize) {
toRead = available;
} else {
printf("ReadSource:: chunkCount == 0, but not at end of file, this shouldn't happen!!\n");
return;
}
}
// Check if the resample quality has changed, it's a safe place here
// to reconfigure the audioreaders resample quality.
// This allows on the fly changing of the resample quality :)
if (m_diskio->get_resample_quality() != m_audioReader->get_convertor_type()) {
m_audioReader->set_converter_type(m_diskio->get_resample_quality());
}
// Read in the samples from source
nframes_t toWrite = rb_file_read(buffer, toRead);
// and write it to the ringbuffer
if (toWrite) {
for (int i=m_buffers.size()-1; i>=0; --i) {
m_buffers.at(i)->write(buffer->destination[i], toWrite);
}
}
}
void ReadSource::start_resync(TimeRef& position)
{
// printf("starting resync!\n");
if (m_needSync || m_syncInProgress) {
// printf("start_resync still in progress!\n");
return;
}
m_syncPos = position;
m_rbReady = 0;
m_needSync = 1;
}
void ReadSource::finish_resync()
{
// printf("sync finished\n");
m_needSync = 0;
m_bufferUnderRunDetected = 0;
m_rbReady = 1;
m_syncInProgress = 0;
}
void ReadSource::sync(DecodeBuffer* buffer)
{
PENTER2;
// printf("source::sync: %s\n", QS_C(m_fileName));
if (!m_audioReader) {
return;
}
if (!m_needSync) {
return;
}
if (!m_syncInProgress) {
rb_seek_to_file_position(m_syncPos);
m_syncInProgress = 1;
}
// Currently, we fill the buffer completely.
// For some reason, filling it with 1/4 at a time
// doesn't fill it consitently, and thus giving audible artifacts.
process_ringbuffer(buffer);
if (m_buffers.at(0)->write_space() == 0) {
finish_resync();
}
// PWARN("Resyncing ringbuffer finished");
}
void ReadSource::prepare_rt_buffers( )
{
PENTER;
Q_ASSERT(m_clip);
for (int i=0; i<m_buffers.size(); i) {
delete m_buffers.at(i);
}
m_buffers.clear();
float size = config().get_property("Hardware", "readbuffersize", 1.0).toDouble();
m_bufferSize = (int) (size * m_outputRate);
m_chunkSize = m_bufferSize / DiskIO::bufferdividefactor;
for (int i=0; i<m_channelCount; i) {
m_buffers.append(new RingBufferNPT<float>(m_bufferSize));
}
TimeRef synclocation = m_clip->get_sheet()->get_work_location();
start_resync(synclocation);
}
BufferStatus* ReadSource::get_buffer_status()
{
if (m_channelCount == 0) {
return m_bufferstatus;
}
int freespace = m_buffers.at(0)->write_space();
// printf("m_rbFileReadPos, m_length %lld, %lld\n", m_rbFileReadPos.universal_frame(), m_length.universal_frame());
TimeRef transport = m_clip->get_sheet()->get_transport_location();
TimeRef syncstartlocation = m_clip->get_track_start_location();
bool transportBeforeSyncStartLocation = transport < (syncstartlocation - (3 * UNIVERSAL_SAMPLE_RATE));
bool transportAfterClipEndLocation = transport > (m_clip->get_track_end_location() (3 * UNIVERSAL_SAMPLE_RATE));
if (m_rbFileReadPos >= m_length || !m_active || transportBeforeSyncStartLocation || transportAfterClipEndLocation) {
m_bufferstatus->fillStatus = 100;
freespace = 0;
m_bufferstatus->needSync = false;
} else {
m_bufferstatus->fillStatus = (int) (((float)freespace / m_bufferSize) * 100);
m_bufferstatus->needSync = m_needSync;
}
m_bufferstatus->bufferUnderRun = m_bufferUnderRunDetected;
m_bufferstatus->priority = (int) (freespace / m_chunkSize);
return m_bufferstatus;
}
void ReadSource::set_active(bool active)
{
if (m_active == active)
return;
if (active) {
m_active = 1;
} else {
m_active = 0;
}
}
int ReadSource::get_file_rate() const
{
if (m_audioReader) {
return m_audioReader->get_file_rate();
} else {
PERROR("ReadSource::get_file_rate(), but no audioreader available!!");
}
return pm().get_project()->get_rate();
}
void ReadSource::set_diskio(DiskIO * diskio)
{
m_diskio = diskio;
set_output_rate(m_diskio->get_output_rate());
if (m_audioReader) {
m_audioReader->set_resample_decode_buffer(m_diskio->get_resample_decode_buffer());
m_audioReader->set_converter_type(m_diskio->get_resample_quality());
}
prepare_rt_buffers();
}
QString ReadSource::get_error_string() const
{
switch(m_error) {
case COULD_NOT_OPEN_FILE: return tr("Could not open file");
case INVALID_CHANNEL_COUNT: return tr("Invalid channel count");
case ZERO_CHANNELS: return tr("File has zero channels");
case FILE_DOES_NOT_EXIST: return tr("The file does not exist!");
default: return tr("No ReadSource error set");
}
return tr("No ReadSource error set");
}
|