1 /* poppler-sound.cc: qt interface to poppler
2  * Copyright (C) 2006-2007, Pino Toscano <pino@kde.org>
3  * Copyright (C) 2008, 2018, 2020, Albert Astals Cid <aacid@kde.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 #include "poppler-qt5.h"
21 
22 #include "Object.h"
23 #include "Stream.h"
24 #include "Sound.h"
25 
26 namespace Poppler {
27 
28 class SoundData
29 {
30 public:
SoundData()31     SoundData() : m_soundObj(nullptr) { }
32 
~SoundData()33     ~SoundData() { delete m_soundObj; }
34 
35     SoundData(const SoundData &) = delete;
36     SoundData &operator=(const SoundData &) = delete;
37 
38     SoundObject::SoundType m_type;
39     Sound *m_soundObj;
40 };
41 
SoundObject(Sound * popplersound)42 SoundObject::SoundObject(Sound *popplersound)
43 {
44     m_soundData = new SoundData();
45     switch (popplersound->getSoundKind()) {
46     case soundEmbedded:
47         m_soundData->m_type = SoundObject::Embedded;
48         break;
49     case soundExternal:
50     default:
51         m_soundData->m_type = SoundObject::External;
52         break;
53     }
54 
55     m_soundData->m_soundObj = popplersound->copy();
56 }
57 
~SoundObject()58 SoundObject::~SoundObject()
59 {
60     delete m_soundData;
61 }
62 
soundType() const63 SoundObject::SoundType SoundObject::soundType() const
64 {
65     return m_soundData->m_type;
66 }
67 
url() const68 QString SoundObject::url() const
69 {
70     if (m_soundData->m_type != SoundObject::External)
71         return QString();
72 
73     return QString(m_soundData->m_soundObj->getFileName().c_str());
74 }
75 
data() const76 QByteArray SoundObject::data() const
77 {
78     if (m_soundData->m_type != SoundObject::Embedded)
79         return QByteArray();
80 
81     Stream *stream = m_soundData->m_soundObj->getStream();
82     stream->reset();
83     int dataLen = 0;
84     QByteArray fileArray;
85     int i;
86     while ((i = stream->getChar()) != EOF) {
87         fileArray[dataLen] = (char)i;
88         ++dataLen;
89     }
90     fileArray.resize(dataLen);
91 
92     return fileArray;
93 }
94 
samplingRate() const95 double SoundObject::samplingRate() const
96 {
97     return m_soundData->m_soundObj->getSamplingRate();
98 }
99 
channels() const100 int SoundObject::channels() const
101 {
102     return m_soundData->m_soundObj->getChannels();
103 }
104 
bitsPerSample() const105 int SoundObject::bitsPerSample() const
106 {
107     return m_soundData->m_soundObj->getBitsPerSample();
108 }
109 
soundEncoding() const110 SoundObject::SoundEncoding SoundObject::soundEncoding() const
111 {
112     switch (m_soundData->m_soundObj->getEncoding()) {
113     case soundRaw:
114         return SoundObject::Raw;
115     case soundSigned:
116         return SoundObject::Signed;
117     case soundMuLaw:
118         return SoundObject::muLaw;
119     case soundALaw:
120         return SoundObject::ALaw;
121     }
122     return SoundObject::Raw;
123 }
124 
125 }
126