kwave  18.07.70
Kwave::AsciiEncoder Class Reference

#include <AsciiEncoder.h>

Inheritance diagram for Kwave::AsciiEncoder:
Inheritance graph
Collaboration diagram for Kwave::AsciiEncoder:
Collaboration graph

Public Member Functions

 AsciiEncoder ()
 
virtual ~AsciiEncoder () Q_DECL_OVERRIDE
 
virtual Kwave::Encoderinstance () Q_DECL_OVERRIDE
 
virtual bool encode (QWidget *widget, Kwave::MultiTrackReader &src, QIODevice &dst, const Kwave::MetaDataList &meta_data) Q_DECL_OVERRIDE
 
virtual QList< Kwave::FilePropertysupportedProperties () Q_DECL_OVERRIDE
 
- Public Member Functions inherited from Kwave::Encoder
 Encoder ()
 
virtual ~Encoder ()
 
virtual QList< Kwave::FilePropertyunsupportedProperties (const QList< Kwave::FileProperty > &properties_to_check)
 
- Public Member Functions inherited from Kwave::CodecBase
 CodecBase ()
 
virtual ~CodecBase ()
 
virtual bool supports (const QMimeType &mimetype)
 
virtual bool supports (const QString &mimetype_name)
 
virtual QStringList extensions (const QString &mimetype_name) const
 
virtual const QList< CodecBase::MimeTypemimeTypes ()
 
virtual const QList< Kwave::Compression::TypecompressionTypes ()
 
virtual void addMimeType (const char *name, const QString &description, const char *patterns)
 
virtual void addCompression (Kwave::Compression::Type compression)
 
virtual QString mimeTypeOf (const QUrl &url)
 

Private Attributes

QTextStream m_dst
 

Detailed Description

Definition at line 35 of file AsciiEncoder.h.

Constructor & Destructor Documentation

◆ AsciiEncoder()

Kwave::AsciiEncoder::AsciiEncoder ( )

Constructor

Definition at line 44 of file AsciiEncoder.cpp.

References LOAD_MIME_TYPES, m_dst, and REGISTER_COMPRESSION_TYPES.

45  :Kwave::Encoder(), m_dst()
46 {
47  m_dst.setCodec(QTextCodec::codecForName("UTF-8"));
50 }
#define LOAD_MIME_TYPES
QTextStream m_dst
Definition: AsciiEncoder.h:68
#define REGISTER_COMPRESSION_TYPES

◆ ~AsciiEncoder()

Kwave::AsciiEncoder::~AsciiEncoder ( )
virtual

Destructor

Definition at line 53 of file AsciiEncoder.cpp.

54 {
55 }

Member Function Documentation

◆ encode()

bool Kwave::AsciiEncoder::encode ( QWidget *  widget,
Kwave::MultiTrackReader src,
QIODevice &  dst,
const Kwave::MetaDataList meta_data 
)
virtual

Encodes a signal into a stream of bytes.

Parameters
widgeta widget that can be used for displaying message boxes or dialogs
srcMultiTrackReader used as source of the audio data
dstfile or other source to receive a stream of bytes
meta_datameta information about the file to be saved
Returns
true if succeeded, false on errors

Implements Kwave::Encoder.

Definition at line 72 of file AsciiEncoder.cpp.

References Kwave::FileInfo::bits(), Kwave::FileInfo::canLoadSave(), Kwave::SampleReader::eof(), Kwave::MessageBox::error(), Kwave::Parser::escape(), Kwave::MultiTrackReader::isCanceled(), Kwave::FileInfo::length(), m_dst, META_PREFIX, Kwave::Label::name(), Kwave::FileInfo::name(), Kwave::Label::pos(), Kwave::FileInfo::properties(), Kwave::FileInfo::rate(), supportedProperties(), and Kwave::FileInfo::tracks().

76 {
77  bool result = true;
78 
79  qDebug("AsciiEncoder::encode()");
80 
81  // get info: tracks, sample rate
82  const Kwave::FileInfo info(meta_data);
83  unsigned int tracks = info.tracks();
84  unsigned int bits = info.bits();
85  sample_index_t length = info.length();
86 
87  do {
88  // open the output device
89  if (!dst.open(QIODevice::ReadWrite | QIODevice::Truncate)) {
91  i18n("Unable to open the file for saving."));
92  result = false;
93  break;
94  }
95 
96  // output device successfully opened
97  m_dst.setDevice(&dst);
98 
99  // write out the default properties:
100  // sample rate, bits, tracks, length
101  m_dst << META_PREFIX << "'rate'=" << info.rate() << endl;
102  m_dst << META_PREFIX << "'tracks'=" << tracks << endl;
103  m_dst << META_PREFIX << "'bits'=" << bits << endl;
104  m_dst << META_PREFIX << "'length'=" << length << endl;
105 
106  // write out all other, non-standard properties that we have
107  QMap<Kwave::FileProperty, QVariant> properties = info.properties();
108  QMap<Kwave::FileProperty, QVariant>::Iterator it;
109  QList<Kwave::FileProperty> supported = supportedProperties();
110  for (it=properties.begin(); it != properties.end(); ++it) {
111  Kwave::FileProperty p = it.key();
112  QVariant v = it.value();
113 
114  if (!supported.contains(p))
115  continue;
116  if (!info.canLoadSave(p))
117  continue;
118 
119  // write the property
120  m_dst << META_PREFIX << "'" << info.name(p) << "'='"
121  << Kwave::Parser::escape(v.toString()).toUtf8()
122  << "'" << endl;
123  }
124 
125  // write out all labels
126  Kwave::LabelList labels(meta_data);
127  foreach (const Kwave::Label &label, labels) {
128  m_dst << META_PREFIX << "'label["
129  << QString::number(label.pos()) << "]'='"
130  << Kwave::Parser::escape(label.name()).toUtf8()
131  << "'" << endl;
132  }
133 
134  sample_index_t rest = length;
135  sample_index_t pos = 0;
136  while (rest-- && !src.isCanceled()) {
137  // write out one track per line
138  for (unsigned int track=0; track < tracks; track++) {
139  Kwave::SampleReader *reader = src[track];
140  Q_ASSERT(reader);
141  if (!reader) break;
142 
143  // read one single sample
144  sample_t sample = 0;
145  if (!reader->eof()) (*reader) >> sample;
146 
147  // print out the sample value
148  m_dst.setFieldWidth(9);
149  m_dst << sample;
150 
151  // comma as separator between the samples
152  if (track != tracks-1)
153  m_dst << ", ";
154  }
155 
156  // as comment: current position [samples]
157  m_dst << " # ";
158  m_dst.setFieldWidth(12);
159  m_dst << pos;
160  pos++;
161 
162  // end of line
163  m_dst << endl;
164  }
165 
166  } while (false);
167 
168  // end of file
169  m_dst << "# EOF " << endl << endl;
170 
171  m_dst.setDevice(Q_NULLPTR);
172  dst.close();
173 
174  return result;
175 }
virtual sample_index_t pos() const
Definition: Label.cpp:56
quint64 sample_index_t
Definition: Sample.h:28
bool eof() const
Definition: SampleReader.h:66
QTextStream m_dst
Definition: AsciiEncoder.h:68
static int error(QWidget *widget, QString message, QString caption=QString())
Definition: MessageBox.cpp:126
virtual QString name() const
Definition: Label.cpp:74
#define META_PREFIX
virtual QList< Kwave::FileProperty > supportedProperties() Q_DECL_OVERRIDE
static QString escape(const QString &text)
Definition: Parser.cpp:277
FileProperty
Definition: FileInfo.h:45
qint32 sample_t
Definition: Sample.h:37
Here is the call graph for this function:

◆ instance()

Kwave::Encoder * Kwave::AsciiEncoder::instance ( )
virtual

Returns a new instance of the encoder

Implements Kwave::Encoder.

Definition at line 58 of file AsciiEncoder.cpp.

59 {
60  return new Kwave::AsciiEncoder();
61 }

◆ supportedProperties()

QList< Kwave::FileProperty > Kwave::AsciiEncoder::supportedProperties ( )
virtual

Returns a list of supported file properties

Reimplemented from Kwave::Encoder.

Definition at line 64 of file AsciiEncoder.cpp.

References Kwave::FileInfo::allKnownProperties().

Referenced by encode().

65 {
66  // default is to support all known properties
67  Kwave::FileInfo info;
68  return info.allKnownProperties();
69 }
QList< FileProperty > allKnownProperties() const
Definition: FileInfo.cpp:383
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_dst

QTextStream Kwave::AsciiEncoder::m_dst
private

pointer to the QIODevice for storing, used while encoding

Definition at line 68 of file AsciiEncoder.h.

Referenced by AsciiEncoder(), and encode().


The documentation for this class was generated from the following files: