kwave  18.07.70
AsciiEncoder.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  AsciiEncoder.cpp - encoder for ASCII data
3  -------------------
4  begin : Sun Nov 26 2006
5  copyright : (C) 2006 by Thomas Eschenbacher
6  email : 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 <math.h>
21 #include <stdlib.h>
22 
23 #include <QApplication>
24 #include <QList>
25 #include <QTextCodec>
26 #include <QVariant>
27 
28 #include <KLocalizedString>
29 
30 #include "libkwave/FileInfo.h"
31 #include "libkwave/Label.h"
32 #include "libkwave/LabelList.h"
33 #include "libkwave/MessageBox.h"
34 #include "libkwave/MetaDataList.h"
36 #include "libkwave/Parser.h"
37 #include "libkwave/Sample.h"
38 #include "libkwave/SampleReader.h"
39 
40 #include "AsciiCodecPlugin.h"
41 #include "AsciiEncoder.h"
42 
43 /***************************************************************************/
45  :Kwave::Encoder(), m_dst()
46 {
47  m_dst.setCodec(QTextCodec::codecForName("UTF-8"));
50 }
51 
52 /***************************************************************************/
54 {
55 }
56 
57 /***************************************************************************/
59 {
60  return new Kwave::AsciiEncoder();
61 }
62 
63 /***************************************************************************/
64 QList<Kwave::FileProperty> Kwave::AsciiEncoder::supportedProperties()
65 {
66  // default is to support all known properties
67  Kwave::FileInfo info;
68  return info.allKnownProperties();
69 }
70 
71 /***************************************************************************/
72 bool Kwave::AsciiEncoder::encode(QWidget *widget,
74  QIODevice &dst,
75  const Kwave::MetaDataList &meta_data)
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 }
176 
177 /***************************************************************************/
178 /***************************************************************************/
virtual Kwave::Encoder * instance() Q_DECL_OVERRIDE
Definition: App.h:33
double rate() const
Definition: FileInfo.cpp:415
QString name(FileProperty key) const
Definition: FileInfo.h:227
bool canLoadSave(FileProperty key) const
Definition: FileInfo.h:220
virtual sample_index_t pos() const
Definition: Label.cpp:56
#define LOAD_MIME_TYPES
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
QList< FileProperty > allKnownProperties() const
Definition: FileInfo.cpp:383
sample_index_t length() const
Definition: FileInfo.cpp:400
const QMap< FileProperty, QVariant > properties() const
Definition: FileInfo.cpp:389
virtual QString name() const
Definition: Label.cpp:74
#define META_PREFIX
unsigned int tracks() const
Definition: FileInfo.cpp:445
#define REGISTER_COMPRESSION_TYPES
virtual QList< Kwave::FileProperty > supportedProperties() Q_DECL_OVERRIDE
virtual bool encode(QWidget *widget, Kwave::MultiTrackReader &src, QIODevice &dst, const Kwave::MetaDataList &meta_data) Q_DECL_OVERRIDE
unsigned int bits() const
Definition: FileInfo.cpp:430
static QString escape(const QString &text)
Definition: Parser.cpp:277
FileProperty
Definition: FileInfo.h:45
virtual ~AsciiEncoder() Q_DECL_OVERRIDE
qint32 sample_t
Definition: Sample.h:37