summaryrefslogtreecommitdiff
path: root/poppler/Sound.cc
blob: db74713e3514044434559650e271e1df51d4e9d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* Sound.cc - an object that holds the sound structure
 * Copyright (C) 2006-2007, Pino Toscano <pino@kde.org>
 * Copyright (C) 2009, 2017-2019, Albert Astals Cid <aacid@kde.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "GooString.h"
#include "Object.h"
#include "Sound.h"
#include "Stream.h"
#include "FileSpec.h"

Sound *Sound::parseSound(Object *obj)
{
  // let's try to see if this Object is a Sound, according to the PDF specs
  // (section 9.2)
  Stream *str = nullptr;
  // the Object must be a Stream
  if (obj->isStream()) {
    str = obj->getStream();
  } else {
    return nullptr;
  }
  // the Stream must have a Dict
  Dict *dict = str->getDict();
  if (dict == nullptr)
    return nullptr;
  // the Dict must have the 'R' key of type num
  Object tmp = dict->lookup("R");
  if (tmp.isNum()) {
    return new Sound(obj);
  } else {
    return nullptr;
  }
}

Sound::Sound(const Object *obj, bool readAttrs)
{
  streamObj = obj->copy();

  fileName = nullptr;
  samplingRate = 0.0;
  channels = 1;
  bitsPerSample = 8;
  encoding = soundRaw;

  if (readAttrs)
  {
    Dict *dict = streamObj.getStream()->getDict();
    Object tmp = dict->lookup("F");
    if (!tmp.isNull()) {
      // valid 'F' key -> external file
      kind = soundExternal;
      Object obj1 = getFileSpecNameForPlatform (&tmp);
      if (obj1.isString()) {
        fileName = obj1.getString()->copy();
      }
    } else {
      // no file specification, then the sound data have to be
      // extracted from the stream
      kind = soundEmbedded;
    }
    // sampling rate
    samplingRate = dict->lookup("R").getNumWithDefaultValue(0);
    // sound channels
    tmp = dict->lookup("C");
    if (tmp.isInt()) {
      channels = tmp.getInt();
    }
    // bits per sample
    tmp = dict->lookup("B");
    if (tmp.isInt()) {
      bitsPerSample = tmp.getInt();
    }
    // encoding format
    tmp = dict->lookup("E");
    if (tmp.isName())
    {
      const char *enc = tmp.getName();
      if (strcmp("Raw", enc) == 0) {
        encoding = soundRaw;
      } else if (strcmp("Signed", enc) == 0) {
        encoding = soundSigned;
      } else if (strcmp("muLaw", enc) == 0) {
        encoding = soundMuLaw;
      } else if (strcmp("ALaw", enc) == 0) {
        encoding = soundALaw;
      }
    }
  }
}

Sound::~Sound()
{
  delete fileName;
}

Stream *Sound::getStream()
{
  return streamObj.getStream();
}

Sound *Sound::copy() const
{
  Sound *newsound = new Sound(&streamObj, false);

  newsound->kind = kind;
  if (fileName) {
    newsound->fileName = fileName->copy();
  }
  newsound->samplingRate = samplingRate;
  newsound->channels = channels;
  newsound->bitsPerSample = bitsPerSample;
  newsound->encoding = encoding;

  return newsound;
}