PDF4QT/PdfTool/pdftoolaudiobook.cpp

480 lines
16 KiB
C++
Raw Normal View History

2021-04-30 20:12:10 +02:00
// Copyright (C) 2020-2021 Jakub Melka
2020-10-18 17:49:16 +02:00
//
2020-12-20 19:03:58 +01:00
// This file is part of Pdf4Qt.
2020-10-18 17:49:16 +02:00
//
2020-12-20 19:03:58 +01:00
// Pdf4Qt is free software: you can redistribute it and/or modify
2020-10-18 17:49:16 +02:00
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
2021-04-30 20:12:10 +02:00
// with the written consent of the copyright owner, any later version.
2020-10-18 17:49:16 +02:00
//
2020-12-20 19:03:58 +01:00
// Pdf4Qt is distributed in the hope that it will be useful,
2020-10-18 17:49:16 +02:00
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
2020-12-20 19:03:58 +01:00
// along with Pdf4Qt. If not, see <https://www.gnu.org/licenses/>.
2020-10-18 17:49:16 +02:00
#include "pdftoolaudiobook.h"
#ifdef Q_OS_WIN
2020-10-21 18:32:04 +02:00
#include <QFileInfo>
2020-10-18 17:49:16 +02:00
#include <sapi.h>
#pragma comment(lib, "ole32")
namespace pdftool
{
static PDFToolAudioBook s_audioBookApplication;
2020-10-19 18:51:30 +02:00
static PDFToolAudioBookVoices s_audioBookVoicesApplication;
2020-10-18 17:49:16 +02:00
2020-10-22 18:49:56 +02:00
PDFVoiceInfo::PDFVoiceInfo(std::map<QString, QString> properties, ISpObjectToken* voiceToken) :
2020-10-18 17:49:16 +02:00
m_properties(qMove(properties)),
2020-10-22 18:49:56 +02:00
m_voiceToken(voiceToken)
2020-10-18 17:49:16 +02:00
{
2020-10-22 18:49:56 +02:00
if (m_voiceToken)
{
m_voiceToken->AddRef();
}
}
PDFVoiceInfo::PDFVoiceInfo(PDFVoiceInfo&& other)
{
std::swap(m_properties, other.m_properties);
std::swap(m_voiceToken, other.m_voiceToken);
}
2020-10-18 17:49:16 +02:00
2020-10-22 18:49:56 +02:00
PDFVoiceInfo& PDFVoiceInfo::operator=(PDFVoiceInfo&& other)
{
std::swap(m_properties, other.m_properties);
std::swap(m_voiceToken, other.m_voiceToken);
return *this;
2020-10-18 17:49:16 +02:00
}
PDFVoiceInfo::~PDFVoiceInfo()
{
2020-10-22 18:49:56 +02:00
if (m_voiceToken)
2020-10-18 17:49:16 +02:00
{
2020-10-22 18:49:56 +02:00
m_voiceToken->Release();
2020-10-18 17:49:16 +02:00
}
}
QLocale PDFVoiceInfo::getLocale() const
{
bool ok = false;
LCID locale = getLanguage().toInt(&ok, 16);
if (ok)
{
2020-10-19 18:51:30 +02:00
// Language name
2020-10-18 17:49:16 +02:00
int count = GetLocaleInfoW(locale, LOCALE_SISO639LANGNAME, NULL, 0);
2020-10-19 18:51:30 +02:00
std::vector<wchar_t> buffer(count, wchar_t());
GetLocaleInfoW(locale, LOCALE_SISO639LANGNAME, buffer.data(), int(buffer.size()));
QString languageCode = QString::fromWCharArray(buffer.data());
// Country name
count = GetLocaleInfoW(locale, LOCALE_SISO3166CTRYNAME, NULL, 0);
buffer.resize(count, wchar_t());
GetLocaleInfoW(locale, LOCALE_SISO3166CTRYNAME, buffer.data(), int(buffer.size()));
QString countryCode = QString::fromWCharArray(buffer.data());
return QLocale(QString("%1_%2").arg(languageCode, countryCode));
2020-10-18 17:49:16 +02:00
}
return QLocale();
}
QString PDFVoiceInfo::getStringValue(QString key) const
{
auto it = m_properties.find(key);
if (it != m_properties.cend())
{
return it->second;
}
return QString();
}
2020-10-22 18:49:56 +02:00
int PDFToolAudioBookBase::fillVoices(const PDFToolOptions& options, PDFVoiceInfoList& list, bool fillVoiceTokenPointers)
2020-10-18 17:49:16 +02:00
{
2020-10-19 18:51:30 +02:00
int result = ExitSuccess;
2020-10-18 17:49:16 +02:00
2020-10-19 18:51:30 +02:00
QStringList voiceSelector;
if (!options.textVoiceName.isEmpty())
{
voiceSelector << QString("Name=%1").arg(options.textVoiceName);
2020-10-18 17:49:16 +02:00
}
2020-10-19 18:51:30 +02:00
if (!options.textVoiceGender.isEmpty())
2020-10-18 17:49:16 +02:00
{
2020-10-19 18:51:30 +02:00
voiceSelector << QString("Gender=%1").arg(options.textVoiceGender);
2020-10-18 17:49:16 +02:00
}
2020-10-19 18:51:30 +02:00
if (!options.textVoiceAge.isEmpty())
{
voiceSelector << QString("Age=%1").arg(options.textVoiceAge);
}
if (!options.textVoiceLangCode.isEmpty())
{
voiceSelector << QString("Language=%1").arg(options.textVoiceLangCode);
}
QString voiceSelectorString = voiceSelector.join(";");
LPCWSTR requiredAttributes = !voiceSelectorString.isEmpty() ? (LPCWSTR)voiceSelectorString.utf16() : nullptr;
2020-10-18 17:49:16 +02:00
ISpObjectTokenCategory* category = nullptr;
if (!SUCCEEDED(::CoCreateInstance(CLSID_SpObjectTokenCategory, NULL, CLSCTX_ALL, __uuidof(ISpObjectTokenCategory), (LPVOID*)&category)))
{
PDFConsole::writeError(PDFToolTranslationContext::tr("SAPI Error: Cannot enumerate SAPI voices."), options.outputCodec);
return ErrorSAPI;
}
if (!SUCCEEDED(category->SetId(SPCAT_VOICES, FALSE)))
{
PDFConsole::writeError(PDFToolTranslationContext::tr("SAPI Error: Cannot enumerate SAPI voices."), options.outputCodec);
return ErrorSAPI;
}
IEnumSpObjectTokens* enumTokensObject = nullptr;
2020-10-19 18:51:30 +02:00
if (SUCCEEDED(category->EnumTokens(requiredAttributes, NULL, &enumTokensObject)))
2020-10-18 17:49:16 +02:00
{
ISpObjectToken* token = nullptr;
while (SUCCEEDED(enumTokensObject->Next(1, &token, NULL)))
{
if (token)
{
/* Attributes can be for example:
* Version,
* Language,
* Gender,
* Age,
* Name
* Vendor */
std::map<QString, QString> properties;
ISpDataKey* attributes = nullptr;
if (SUCCEEDED(token->OpenKey(L"Attributes", &attributes)))
{
for (ULONG i = 0; ; ++i)
{
LPWSTR valueName = NULL;
if (SUCCEEDED(attributes->EnumValues(i, &valueName)))
{
LPWSTR data = NULL;
if (SUCCEEDED(attributes->GetStringValue(valueName, &data)))
{
QString propertyName = QString::fromWCharArray(valueName);
QString propertyValue = QString::fromWCharArray(data);
if (!propertyValue.isEmpty())
{
properties[propertyName] = propertyValue;
}
::CoTaskMemFree(data);
}
::CoTaskMemFree(valueName);
}
else
{
break;
}
}
attributes->Release();
}
2020-10-22 18:49:56 +02:00
if (fillVoiceTokenPointers)
2020-10-18 17:49:16 +02:00
{
2020-10-22 18:49:56 +02:00
list.emplace_back(qMove(properties), token);
}
else
{
list.emplace_back(qMove(properties), nullptr);
2020-10-18 17:49:16 +02:00
}
token->Release();
}
else
{
break;
}
}
}
else
{
PDFConsole::writeError(PDFToolTranslationContext::tr("SAPI Error: Cannot enumerate SAPI voices."), options.outputCodec);
result = ErrorSAPI;
}
if (enumTokensObject)
{
enumTokensObject->Release();
}
if (category)
{
category->Release();
}
return result;
}
2020-10-19 18:51:30 +02:00
int PDFToolAudioBookBase::showVoiceList(const PDFToolOptions& options)
2020-10-18 17:49:16 +02:00
{
PDFVoiceInfoList voices;
int result = fillVoices(options, voices, false);
PDFOutputFormatter formatter(options.outputStyle, options.outputCodec);
formatter.beginDocument("voices", PDFToolTranslationContext::tr("Available voices for given settings:"));
formatter.endl();
formatter.beginTable("voices", PDFToolTranslationContext::tr("Voice list"));
formatter.beginTableHeaderRow("header");
formatter.writeTableHeaderColumn("name", PDFToolTranslationContext::tr("Name"), Qt::AlignLeft);
formatter.writeTableHeaderColumn("gender", PDFToolTranslationContext::tr("Gender"), Qt::AlignLeft);
formatter.writeTableHeaderColumn("age", PDFToolTranslationContext::tr("Age"), Qt::AlignLeft);
2020-10-19 18:51:30 +02:00
formatter.writeTableHeaderColumn("language-code", PDFToolTranslationContext::tr("Lang. Code"), Qt::AlignLeft);
2020-10-18 17:49:16 +02:00
formatter.writeTableHeaderColumn("locale", PDFToolTranslationContext::tr("Locale"), Qt::AlignLeft);
2020-10-19 18:51:30 +02:00
formatter.writeTableHeaderColumn("language", PDFToolTranslationContext::tr("Language"), Qt::AlignLeft);
formatter.writeTableHeaderColumn("country", PDFToolTranslationContext::tr("Country"), Qt::AlignLeft);
2020-10-18 17:49:16 +02:00
formatter.writeTableHeaderColumn("vendor", PDFToolTranslationContext::tr("Vendor"), Qt::AlignLeft);
formatter.writeTableHeaderColumn("version", PDFToolTranslationContext::tr("Version"), Qt::AlignLeft);
formatter.endTableHeaderRow();
for (const PDFVoiceInfo& voice : voices)
{
2020-10-19 18:51:30 +02:00
QLocale locale = voice.getLocale();
2020-10-18 17:49:16 +02:00
formatter.beginTableRow("voice");
2020-10-19 18:51:30 +02:00
formatter.writeTableColumn("name", voice.getName(), Qt::AlignLeft);
formatter.writeTableColumn("gender", voice.getGender(), Qt::AlignLeft);
formatter.writeTableColumn("age", voice.getAge(), Qt::AlignLeft);
formatter.writeTableColumn("language", voice.getLanguage(), Qt::AlignLeft);
formatter.writeTableColumn("locale", locale.name(), Qt::AlignLeft);
formatter.writeTableColumn("language", locale.nativeLanguageName(), Qt::AlignLeft);
formatter.writeTableColumn("country", locale.nativeCountryName(), Qt::AlignLeft);
formatter.writeTableColumn("vendor", voice.getVendor(), Qt::AlignLeft);
formatter.writeTableColumn("version", voice.getVersion(), Qt::AlignLeft);
2020-10-18 17:49:16 +02:00
formatter.endTableRow();
}
formatter.endTable();
formatter.endDocument();
PDFConsole::writeText(formatter.getString(), options.outputCodec);
return result;
}
2020-10-19 18:51:30 +02:00
QString PDFToolAudioBookVoices::getStandardString(PDFToolAbstractApplication::StandardString standardString) const
{
switch (standardString)
{
case Command:
return "audio-book-voices";
case Name:
return PDFToolTranslationContext::tr("Audio book voices");
case Description:
return PDFToolTranslationContext::tr("List of available voices for audio book conversion.");
default:
Q_ASSERT(false);
break;
}
return QString();
}
int PDFToolAudioBookVoices::execute(const PDFToolOptions& options)
{
if (!SUCCEEDED(::CoInitializeEx(nullptr, COINIT_MULTITHREADED | COINIT_SPEED_OVER_MEMORY)))
{
return ErrorCOM;
}
int returnCode = showVoiceList(options);
::CoUninitialize();
return returnCode;
}
PDFToolAbstractApplication::Options PDFToolAudioBookVoices::getOptionsFlags() const
{
return ConsoleFormat | VoiceSelector;
}
QString PDFToolAudioBook::getStandardString(StandardString standardString) const
{
switch (standardString)
{
case Command:
return "audio-book";
case Name:
return PDFToolTranslationContext::tr("Audio book convertor");
case Description:
return PDFToolTranslationContext::tr("Convert your document to a simple audio book.");
default:
Q_ASSERT(false);
break;
}
return QString();
}
2020-10-21 18:32:04 +02:00
int PDFToolAudioBook::getDocumentTextFlow(const PDFToolOptions& options, pdf::PDFDocumentTextFlow& flow)
{
pdf::PDFDocument document;
QByteArray sourceData;
if (!readDocument(options, document, &sourceData))
{
return ErrorDocumentReading;
}
QString parseError;
std::vector<pdf::PDFInteger> pages = options.getPageRange(document.getCatalog()->getPageCount(), parseError, true);
if (!parseError.isEmpty())
{
PDFConsole::writeError(parseError, options.outputCodec);
return ErrorInvalidArguments;
}
pdf::PDFDocumentTextFlowFactory factory;
flow = factory.create(&document, pages, options.textAnalysisAlgorithm);
return ExitSuccess;
}
int PDFToolAudioBook::createAudioBook(const PDFToolOptions& options, pdf::PDFDocumentTextFlow& flow)
{
QString audioString;
QTextStream textStream(&audioString);
for (const pdf::PDFDocumentTextFlow::Item& item : flow.getItems())
{
if (item.flags.testFlag(pdf::PDFDocumentTextFlow::PageStart) && options.textSpeechMarkPageNumbers)
{
textStream << QString("<bookmark mark=\"%1\"/>").arg(item.text) << endl;
}
if (!item.text.isEmpty())
{
bool showText = (item.flags.testFlag(pdf::PDFDocumentTextFlow::Text)) ||
(item.flags.testFlag(pdf::PDFDocumentTextFlow::PageStart) && options.textSpeechSayPageNumbers) ||
(item.flags.testFlag(pdf::PDFDocumentTextFlow::PageEnd) && options.textSpeechSayPageNumbers) ||
(item.flags.testFlag(pdf::PDFDocumentTextFlow::StructureTitle) && options.textSpeechSayStructTitles) ||
(item.flags.testFlag(pdf::PDFDocumentTextFlow::StructureAlternativeDescription) && options.textSpeechSayStructAlternativeDescription) ||
(item.flags.testFlag(pdf::PDFDocumentTextFlow::StructureExpandedForm) && options.textSpeechSayStructExpandedForm) ||
(item.flags.testFlag(pdf::PDFDocumentTextFlow::StructureActualText) && options.textSpeechSayStructActualText);
if (showText)
{
textStream << item.text << endl;
}
}
}
PDFVoiceInfoList voices;
fillVoices(options, voices, true);
// Do we have any voice?
if (voices.empty())
{
2020-10-22 18:49:56 +02:00
PDFConsole::writeError(PDFToolTranslationContext::tr("No suitable voice found."), options.outputCodec);
2020-10-21 18:32:04 +02:00
return ErrorSAPI;
}
2020-10-22 18:49:56 +02:00
if (!voices.front().getVoiceToken())
2020-10-21 18:32:04 +02:00
{
PDFConsole::writeError(PDFToolTranslationContext::tr("Invalid voice."), options.outputCodec);
return ErrorSAPI;
}
QFileInfo info(options.document);
2020-10-23 16:32:56 +02:00
QString outputFile = QString("%1/%2.%3").arg(info.path(), info.completeBaseName(), options.textSpeechAudioFormat);
2020-10-21 18:32:04 +02:00
BSTR outputFileName = (BSTR)outputFile.utf16();
ISpeechFileStream* stream = nullptr;
if (!SUCCEEDED(::CoCreateInstance(CLSID_SpFileStream, NULL, CLSCTX_ALL, __uuidof(ISpeechFileStream), (LPVOID*)&stream)))
{
PDFConsole::writeError(PDFToolTranslationContext::tr("Cannot create output stream '%1'.").arg(outputFile), options.outputCodec);
return ErrorSAPI;
}
2020-10-22 18:49:56 +02:00
ISpVoice* voice = nullptr;
if (!SUCCEEDED(::CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, __uuidof(ISpVoice), (LPVOID*)&voice)))
{
PDFConsole::writeError(PDFToolTranslationContext::tr("Cannot create voice."), options.outputCodec);
stream->Release();
return ErrorSAPI;
}
2020-10-21 18:32:04 +02:00
if (!SUCCEEDED(stream->Open(outputFileName, SSFMCreateForWrite)))
{
PDFConsole::writeError(PDFToolTranslationContext::tr("Cannot create output stream '%1'.").arg(outputFile), options.outputCodec);
2020-10-22 18:49:56 +02:00
voice->Release();
2020-10-21 18:32:04 +02:00
stream->Release();
return ErrorSAPI;
}
2020-10-22 18:49:56 +02:00
ISpObjectToken* voiceToken = voices.front().getVoiceToken();
if (!SUCCEEDED(voice->SetVoice(voiceToken)))
{
PDFConsole::writeError(PDFToolTranslationContext::tr("Failed to set requested voice. Default voice will be used."), options.outputCodec);
}
2020-10-21 18:32:04 +02:00
voices.clear();
LPCWSTR stringToSpeak = (LPCWSTR)audioString.utf16();
voice->SetOutput(stream, FALSE);
voice->Speak(stringToSpeak, SPF_PURGEBEFORESPEAK | SPF_PARSE_SAPI, NULL);
voice->Release();
stream->Release();
return ExitSuccess;
}
2020-10-19 18:51:30 +02:00
int PDFToolAudioBook::execute(const PDFToolOptions& options)
{
2020-10-21 18:32:04 +02:00
pdf::PDFDocumentTextFlow textFlow;
int result = getDocumentTextFlow(options, textFlow);
if (result != ExitSuccess)
{
return result;
}
if (textFlow.isEmpty())
{
PDFConsole::writeError(PDFToolTranslationContext::tr("No text extracted to be converted to audio book."), options.outputCodec);
return ErrorNoText;
}
2020-10-19 18:51:30 +02:00
if (!SUCCEEDED(::CoInitializeEx(nullptr, COINIT_MULTITHREADED | COINIT_SPEED_OVER_MEMORY)))
{
return ErrorCOM;
}
2020-10-21 18:32:04 +02:00
result = createAudioBook(options, textFlow);
2020-10-19 18:51:30 +02:00
::CoUninitialize();
2020-10-21 18:32:04 +02:00
return result;
2020-10-19 18:51:30 +02:00
}
PDFToolAbstractApplication::Options PDFToolAudioBook::getOptionsFlags() const
{
2020-10-21 18:32:04 +02:00
return ConsoleFormat | OpenDocument | PageSelector | VoiceSelector | TextAnalysis | TextSpeech;
2020-10-19 18:51:30 +02:00
}
2020-10-18 17:49:16 +02:00
} // namespace pdftool
#endif