beta-0.89.2
[luatex.git] / source / libs / poppler / poppler-src / poppler / Sound.cc
blob6129fdcb47d80978d01030950e060dc5b073c548
1 /* Sound.cc - an object that holds the sound structure
2 * Copyright (C) 2006-2007, Pino Toscano <pino@kde.org>
3 * Copyright (C) 2009, Albert Astals Cid <aacid@kde.org>
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.
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.
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.
20 #include "GooString.h"
21 #include "Object.h"
22 #include "Sound.h"
23 #include "Stream.h"
24 #include "FileSpec.h"
26 Sound *Sound::parseSound(Object *obj)
28 // let's try to see if this Object is a Sound, according to the PDF specs
29 // (section 9.2)
30 Stream *str = NULL;
31 // the Object must be a Stream
32 if (obj->isStream()) {
33 str = obj->getStream();
34 } else {
35 return NULL;
37 // the Stream must have a Dict
38 Dict *dict = str->getDict();
39 if (dict == NULL)
40 return NULL;
41 Object tmp;
42 // the Dict must have the 'R' key of type num
43 dict->lookup("R", &tmp);
44 if (tmp.isNum()) {
45 return new Sound(obj);
46 } else {
47 return NULL;
51 Sound::Sound(Object *obj, bool readAttrs)
53 streamObj = new Object();
54 streamObj->initNull();
55 obj->copy(streamObj);
57 fileName = NULL;
58 samplingRate = 0.0;
59 channels = 1;
60 bitsPerSample = 8;
61 encoding = soundRaw;
63 if (readAttrs)
65 Object tmp;
66 Dict *dict = streamObj->getStream()->getDict();
67 dict->lookup("F", &tmp);
68 if (!tmp.isNull()) {
69 Object obj1;
70 // valid 'F' key -> external file
71 kind = soundExternal;
72 if (getFileSpecNameForPlatform (&tmp, &obj1)) {
73 fileName = obj1.getString()->copy();
74 obj1.free();
76 } else {
77 // no file specification, then the sound data have to be
78 // extracted from the stream
79 kind = soundEmbedded;
81 tmp.free();
82 // sampling rate
83 dict->lookup("R", &tmp);
84 if (tmp.isNum()) {
85 samplingRate = tmp.getNum();
87 tmp.free();
88 // sound channels
89 dict->lookup("C", &tmp);
90 if (tmp.isInt()) {
91 channels = tmp.getInt();
93 tmp.free();
94 // bits per sample
95 dict->lookup("B", &tmp);
96 if (tmp.isInt()) {
97 bitsPerSample = tmp.getInt();
99 tmp.free();
100 // encoding format
101 dict->lookup("E", &tmp);
102 if (tmp.isName())
104 const char *enc = tmp.getName();
105 if (strcmp("Raw", enc) == 0) {
106 encoding = soundRaw;
107 } else if (strcmp("Signed", enc) == 0) {
108 encoding = soundSigned;
109 } else if (strcmp("muLaw", enc) == 0) {
110 encoding = soundMuLaw;
111 } else if (strcmp("ALaw", enc) == 0) {
112 encoding = soundALaw;
115 tmp.free();
119 Sound::~Sound()
121 delete fileName;
122 streamObj->free();
123 delete streamObj;
126 Stream *Sound::getStream()
128 return streamObj->getStream();
131 Sound *Sound::copy()
133 Sound *newsound = new Sound(streamObj, false);
135 newsound->kind = kind;
136 if (fileName) {
137 newsound->fileName = fileName->copy();
139 newsound->samplingRate = samplingRate;
140 newsound->channels = channels;
141 newsound->bitsPerSample = bitsPerSample;
142 newsound->encoding = encoding;
144 return newsound;