Commit 7331bc46 authored by Albert Astals Cid's avatar Albert Astals Cid

* poppler/Sound.h:

        * poppler/Sound.cc:
        * qt4/src/poppler-sound.cc: Move most of the sound reading code
        into the Sound class, so frontends can use it easily.
        Patch by Pino Toscano <pino@kde.org>.
parent 841bee94
2007-01-17 Albert Astals Cid <aacid@kde.org>
* poppler/Sound.h:
* poppler/Sound.cc:
* qt4/src/poppler-sound.cc: Move most of the sound reading code
into the Sound class, so frontends can use it easily.
Patch by Pino Toscano <pino@kde.org>.
2007-01-13 Albert Astals Cid <aacid@kde.org>
* poppler/Stream.h:
......@@ -18,7 +26,7 @@
* qt4/src/poppler-link.cc
* qt4/src/poppler-page.cc
* qt4/src/poppler-private.h
* qt4/src/poppler-qt4.h: Make the Qt4 backend compilable even with
* qt4/src/poppler-qt4.h: Make the Qt4 frontend compilable even with
no Splash backend. Patch by Pino Toscano <pino@kde.org>.
2007-01-13 Albert Astals Cid <aacid@kde.org>
......
/* Sound.cc - an object that holds the sound structure
* Copyright (C) 2006, Pino Toscano <pino@kde.org>
* Copyright (C) 2006-2007, Pino Toscano <pino@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
......@@ -16,11 +16,11 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <string.h>
#include "GooString.h"
#include "Object.h"
#include "Sound.h"
#include "Stream.h"
#include "Link.h"
Sound *Sound::parseSound(Object *obj)
{
......@@ -47,15 +47,73 @@ Sound *Sound::parseSound(Object *obj)
}
}
Sound::Sound(Object *obj)
Sound::Sound(Object *obj, bool readAttrs)
{
streamObj = new Object();
streamObj->initNull();
obj->copy(streamObj);
fileName = NULL;
samplingRate = 0.0;
channels = 1;
bitsPerSample = 8;
encoding = soundRaw;
if (readAttrs)
{
Object tmp;
Dict *dict = streamObj->getStream()->getDict();
dict->lookup("F", &tmp);
if (!tmp.isNull()) {
// valid 'F' key -> external file
kind = soundExternal;
fileName = LinkAction::getFileSpecName(&tmp);
} else {
// no file specification, then the sound data have to be
// extracted from the stream
kind = soundEmbedded;
}
tmp.free();
// sampling rate
dict->lookup("R", &tmp);
if (tmp.isNum()) {
samplingRate = tmp.getNum();
}
tmp.free();
// sound channels
dict->lookup("C", &tmp);
if (tmp.isInt()) {
channels = tmp.getInt();
}
tmp.free();
// bits per sample
dict->lookup("B", &tmp);
if (tmp.isInt()) {
bitsPerSample = tmp.getInt();
}
tmp.free();
// encoding format
dict->lookup("E", &tmp);
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;
}
}
tmp.free();
}
}
Sound::~Sound()
{
delete fileName;
streamObj->free();
delete streamObj;
}
......@@ -64,3 +122,19 @@ Stream *Sound::getStream()
{
return streamObj->getStream();
}
Sound *Sound::copy()
{
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;
}
/* Sound.h - an object that holds the sound structure
* Copyright (C) 2006, Pino Toscano <pino@kde.org>
* Copyright (C) 2006-2007, Pino Toscano <pino@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
......@@ -19,11 +19,24 @@
#ifndef Sound_H
#define Sound_H
class GooString;
class Object;
class Stream;
//------------------------------------------------------------------------
enum SoundKind {
soundEmbedded, // embedded sound
soundExternal // external sound
};
enum SoundEncoding {
soundRaw, // raw encoding
soundSigned, // twos-complement values
soundMuLaw, // mu-law-encoded samples
soundALaw // A-law-encoded samples
};
class Sound
{
public:
......@@ -36,11 +49,26 @@ public:
Object *getObject() { return streamObj; }
Stream *getStream();
SoundKind getSoundKind() { return kind; }
GooString *getFileName() { return fileName; }
double getSamplingRate() { return samplingRate; }
int getChannels() { return channels; }
int getBitsPerSample() { return bitsPerSample; }
SoundEncoding getEncoding() { return encoding; }
Sound *copy();
private:
// Create a sound. The Object obj is ensured to be a Stream with a Dict
Sound(Object *obj);
Sound(Object *obj, bool readAttrs = true);
Object *streamObj;
SoundKind kind;
GooString *fileName;
double samplingRate;
int channels;
int bitsPerSample;
SoundEncoding encoding;
};
#endif
/* poppler-sound.cc: qt interface to poppler
* Copyright (C) 2006, Pino Toscano <pino@kde.org>
* Copyright (C) 2006-2007, Pino Toscano <pino@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
......@@ -18,12 +18,7 @@
#define UNSTABLE_POPPLER_QT4
#include <QtCore/QByteArray>
#include <QtCore/QString>
#include <QtCore/QVariant>
#include "Object.h"
#include "Dict.h"
#include "Stream.h"
#include "Sound.h"
......@@ -36,107 +31,41 @@ class SoundData
{
public:
SoundData()
: m_channels( 1 ), m_bitsPerSample( 8 ), m_soundEncoding( SoundObject::Raw ), m_soundObj( new Object() )
{ m_soundObj->initNull(); }
: m_soundObj( 0 )
{
}
~SoundData()
{
m_soundObj->free();
delete m_soundObj;
}
QVariant m_data;
SoundObject::SoundType m_type;
double m_samplingRate;
int m_channels;
int m_bitsPerSample;
SoundObject::SoundEncoding m_soundEncoding;
Object *m_soundObj;
Sound *m_soundObj;
};
SoundObject::SoundObject(Sound *popplersound)
{
m_soundData = new SoundData();
Dict *dict = popplersound->getStream()->getDict();
Object tmp;
// file specs / data
dict->lookup("F", &tmp);
if ( !tmp.isNull() )
{
// valid 'F' key -> external file
m_soundData->m_type = SoundObject::External;
// TODO read the file specifications
m_soundData->m_data = QVariant( QString() );
}
else
{
// no file specification, then the sound data have to be
// extracted from the stream
m_soundData->m_type = SoundObject::Embedded;
Stream *stream = popplersound->getStream();
stream->reset();
int dataLen = 0;
QByteArray fileArray;
int i;
while ( (i = stream->getChar()) != EOF) {
fileArray[dataLen] = (char)i;
++dataLen;
}
fileArray.resize(dataLen);
m_soundData->m_data = QVariant( fileArray );
}
tmp.free();
// sampling rate
dict->lookup( "R", &tmp );
if ( tmp.isNum() )
{
m_soundData->m_samplingRate = tmp.getNum();
}
tmp.free();
// sound channels
dict->lookup( "C", &tmp );
if ( tmp.isInt() )
{
m_soundData->m_channels = tmp.getInt();
}
tmp.free();
// sound channels
dict->lookup( "B", &tmp );
if ( tmp.isInt() )
{
m_soundData->m_bitsPerSample = tmp.getInt();
}
tmp.free();
// encoding format
dict->lookup( "E", &tmp );
if ( tmp.isName() )
switch ( popplersound->getSoundKind() )
{
const char *enc = tmp.getName();
if ( !strcmp( "Raw", enc ) )
m_soundData->m_soundEncoding = SoundObject::Raw;
else if ( !strcmp( "Signed", enc ) )
m_soundData->m_soundEncoding = SoundObject::Signed;
if ( !strcmp( "muLaw", enc ) )
m_soundData->m_soundEncoding = SoundObject::muLaw;
if ( !strcmp( "ALaw", enc ) )
m_soundData->m_soundEncoding = SoundObject::ALaw;
case soundEmbedded:
m_soundData->m_type = SoundObject::Embedded;
break;
case soundExternal:
default:
m_soundData->m_type = SoundObject::External;
break;
}
tmp.free();
// at the end, copying the object
popplersound->getObject()->copy(m_soundData->m_soundObj);
m_soundData->m_soundObj = popplersound->copy();
}
SoundObject::SoundObject(const SoundObject &s)
{
m_soundData = new SoundData();
m_soundData->m_type = s.m_soundData->m_type;
m_soundData->m_data = s.m_soundData->m_data;
m_soundData->m_type = s.m_soundData->m_type;
m_soundData->m_samplingRate = s.m_soundData->m_samplingRate;
m_soundData->m_channels = s.m_soundData->m_channels;
m_soundData->m_bitsPerSample = s.m_soundData->m_bitsPerSample;
m_soundData->m_soundEncoding = s.m_soundData->m_soundEncoding;
s.m_soundData->m_soundObj->copy(m_soundData->m_soundObj);
m_soundData->m_soundObj = s.m_soundData->m_soundObj->copy();
}
SoundObject::~SoundObject()
......@@ -151,32 +80,61 @@ SoundObject::SoundType SoundObject::soundType() const
QString SoundObject::url() const
{
return m_soundData->m_type == SoundObject::External ? m_soundData->m_data.toString() : QString();
if ( m_soundData->m_type != SoundObject::External )
return QString();
GooString * goo = m_soundData->m_soundObj->getFileName();
return goo ? QString( goo->getCString() ) : QString();
}
QByteArray SoundObject::data() const
{
return m_soundData->m_type == SoundObject::Embedded ? m_soundData->m_data.toByteArray() : QByteArray();
if ( m_soundData->m_type != SoundObject::Embedded )
return QByteArray();
Stream *stream = m_soundData->m_soundObj->getStream();
stream->reset();
int dataLen = 0;
QByteArray fileArray;
int i;
while ( (i = stream->getChar()) != EOF) {
fileArray[dataLen] = (char)i;
++dataLen;
}
fileArray.resize(dataLen);
return fileArray;
};
double SoundObject::samplingRate() const
{
return m_soundData->m_samplingRate;
return m_soundData->m_soundObj->getSamplingRate();
}
int SoundObject::channels() const
{
return m_soundData->m_channels;
return m_soundData->m_soundObj->getChannels();
}
int SoundObject::bitsPerSample() const
{
return m_soundData->m_bitsPerSample;
return m_soundData->m_soundObj->getBitsPerSample();
}
SoundObject::SoundEncoding SoundObject::soundEncoding() const
{
return m_soundData->m_soundEncoding;
switch ( m_soundData->m_soundObj->getEncoding() )
{
case soundRaw:
return SoundObject::Raw;
case soundSigned:
return SoundObject::Signed;
case soundMuLaw:
return SoundObject::muLaw;
case soundALaw:
return SoundObject::ALaw;
}
return SoundObject::Raw;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment