kwave  18.07.70
TrackWriter.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  TrackWriter.cpp - stream for inserting samples into a track
3  -------------------
4  begin : Feb 11 2001
5  copyright : (C) 2001 by Thomas Eschenbacher
6  email : Thomas Eschenbacher <thomas.eschenbacher@gmx.de>
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "config.h"
19 
20 #include <QApplication>
21 
22 #include "libkwave/InsertMode.h"
23 #include "libkwave/Track.h"
24 #include "libkwave/TrackWriter.h"
25 #include "libkwave/Utils.h"
26 #include "libkwave/memcpy.h"
27 
29 #define MIN_PROGRESS_INTERVAL 500
30 
31 //***************************************************************************
33  sample_index_t left, sample_index_t right)
34  :Kwave::Writer(mode, left, right),
35  m_track(track), m_progress_time()
36 {
37  m_track.use();
38  m_progress_time.start();
39 }
40 
41 //***************************************************************************
43 {
44  flush();
45  m_track.release();
46 }
47 
48 //***************************************************************************
50  unsigned int &count)
51 {
52  if (count == 0) return true; // nothing to write
53 
54  if ((m_mode == Kwave::Overwrite) && (m_position + count > m_last + 1)) {
55  // need clipping
56  count = Kwave::toUint(m_last + 1 - m_position);
57 // qDebug("TrackWriter::write() clipped to count=%u", count);
58  }
59 
60  Q_ASSERT(count <= buffer.size());
61 // qDebug("TrackWriter[%p,%llu...%llu]::write(%llu ... %llu) (total=%llu)",
62 // static_cast<void *>(this),
63 // m_first, m_last,
64 // m_position, m_position + count - 1,
65 // m_position + count - m_first);
66 
67  if (!m_track.writeSamples(m_mode, m_position, buffer, 0, count)) {
68  count = 0;
69  return false; /* out of memory */
70  }
71 
72  m_position += count;
73 
74  // fix m_last, this might be needed in Append and Insert mode
75  Q_ASSERT(m_position >= 1);
76  if ((m_mode == Kwave::Append) || (m_mode == Kwave::Insert)) {
77  if ((m_position - 1) > m_last) m_last = (m_position - 1);
78  }
79  count = 0;
80 
81  // inform others that we proceeded
82  if (m_progress_time.elapsed() > MIN_PROGRESS_INTERVAL) {
83  m_progress_time.restart();
84  emit proceeded();
85  }
86 
87  return true;
88 }
89 
90 //***************************************************************************
91 //***************************************************************************
Kwave::Track & m_track
Definition: TrackWriter.h:81
Definition: App.h:33
bool flush()
Definition: Writer.h:94
void use()
Definition: Track.cpp:763
quint64 sample_index_t
Definition: Sample.h:28
virtual bool write(const Kwave::SampleArray &buffer, unsigned int &count) Q_DECL_OVERRIDE
Definition: TrackWriter.cpp:49
void release()
Definition: Track.cpp:769
sample_index_t m_last
Definition: Writer.h:152
sample_index_t m_position
Definition: Writer.h:158
bool writeSamples(Kwave::InsertMode mode, sample_index_t offset, const Kwave::SampleArray &buffer, unsigned int buf_offset, unsigned int length)
Definition: Track.cpp:561
virtual ~TrackWriter() Q_DECL_OVERRIDE
Definition: TrackWriter.cpp:42
TrackWriter(Kwave::Track &track, Kwave::InsertMode mode, sample_index_t left=0, sample_index_t right=0)
Definition: TrackWriter.cpp:32
void proceeded()
unsigned int size() const
#define MIN_PROGRESS_INTERVAL
Definition: TrackWriter.cpp:29
unsigned int toUint(T x)
Definition: Utils.h:109
InsertMode
Definition: InsertMode.h:26
Kwave::InsertMode mode() const
Definition: Writer.h:125
Kwave::InsertMode m_mode
Definition: Writer.h:155