mirror of
https://foundry.openuru.org/gitblit/r/CWE-ou-minkata.git
synced 2025-07-19 03:39:08 +00:00
Initial pass at getting subtitle support in. Doesn't compile for unknown reasons
This commit is contained in:
@ -48,6 +48,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
|
||||
|
||||
#include "plgDispatch.h"
|
||||
#include "hsResMgr.h"
|
||||
#include "plSrtFileReader.h"
|
||||
#include "../pnMessage/plRefMsg.h"
|
||||
#include "../plFile/plFileUtils.h"
|
||||
#include "../plFile/hsFiles.h"
|
||||
@ -117,20 +118,35 @@ static void LoadCallback(void *)
|
||||
else
|
||||
{
|
||||
plAudioFileReader *reader = nil;
|
||||
plSrtFileReader* srtReader = nullptr;
|
||||
|
||||
while(plSoundBuffer *buffer = templist.Head())
|
||||
{
|
||||
if(buffer->GetData())
|
||||
{
|
||||
reader = CreateReader(true, buffer->GetFileName(), buffer->GetAudioReaderType(), buffer->GetReaderSelect());
|
||||
const char* srcFilename = buffer->GetFileName();
|
||||
reader = CreateReader(true, srcFilename, buffer->GetAudioReaderType(), buffer->GetReaderSelect());
|
||||
|
||||
if( reader )
|
||||
{
|
||||
unsigned readLen = buffer->GetAsyncLoadLength() ? buffer->GetAsyncLoadLength() : buffer->GetDataLength();
|
||||
reader->Read( readLen, buffer->GetData() );
|
||||
buffer->SetAudioReader(reader); // give sound buffer reader, since we may need it later
|
||||
|
||||
plSrtFileReader* srtReader = buffer->GetSrtReader();
|
||||
if (srtReader != nullptr && srtReader->GetCurrentAudioFileName() == srcFilename) {
|
||||
// same file we were playing before, so start the SRT feed over instead of deleting and reloading
|
||||
srtReader->StartOver();
|
||||
} else {
|
||||
std::unique_ptr<plSrtFileReader> newSrtFileReader(new plSrtFileReader(srcFilename));
|
||||
if (newSrtFileReader->ReadFile())
|
||||
buffer->SetSrtReader(newSrtFileReader.release());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer->SetError();
|
||||
}
|
||||
}
|
||||
|
||||
templist.Unlink(buffer);
|
||||
@ -194,6 +210,8 @@ plSoundBuffer::~plSoundBuffer()
|
||||
ASSERT(!link.IsLinked());
|
||||
delete [] fFileName;
|
||||
UnLoad();
|
||||
|
||||
delete fSrtReader;
|
||||
}
|
||||
|
||||
void plSoundBuffer::IInitBuffer()
|
||||
@ -206,6 +224,7 @@ void plSoundBuffer::IInitBuffer()
|
||||
fFlags = 0;
|
||||
fDataRead = 0;
|
||||
fReader = nil;
|
||||
fSrtReader = nullptr;
|
||||
fLoaded = 0;
|
||||
fLoading = false;
|
||||
fHeader.fFormatTag = 0;
|
||||
@ -458,6 +477,12 @@ void plSoundBuffer::SetLoaded(bool loaded)
|
||||
fLoaded = loaded;
|
||||
}
|
||||
|
||||
void plSoundBuffer::SetSrtReader(plSrtFileReader* reader)
|
||||
{
|
||||
delete fSrtReader;
|
||||
fSrtReader = reader;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
*
|
||||
|
@ -61,6 +61,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
|
||||
|
||||
class plUnifiedTime;
|
||||
class plAudioFileReader;
|
||||
class plSrtFileReader;
|
||||
class plSoundBuffer : public hsKeyedObject
|
||||
{
|
||||
public:
|
||||
@ -118,6 +119,8 @@ public:
|
||||
plAudioFileReader * GetAudioReader(); // transfers ownership to caller
|
||||
void SetAudioReader(plAudioFileReader *reader);
|
||||
void SetLoaded(bool loaded);
|
||||
plSrtFileReader* GetSrtReader() const { return fSrtReader; } // does not transfer ownership
|
||||
void SetSrtReader(plSrtFileReader* reader);
|
||||
|
||||
plAudioFileReader::StreamType GetAudioReaderType() { return fStreamType; }
|
||||
unsigned GetAsyncLoadLength() { return fAsyncLoadLength ? fAsyncLoadLength : fDataLength; }
|
||||
@ -148,7 +151,8 @@ protected:
|
||||
bool fLoading;
|
||||
bool fError;
|
||||
|
||||
plAudioFileReader * fReader;
|
||||
plAudioFileReader * fReader;
|
||||
plSrtFileReader* fSrtReader;
|
||||
UInt8 * fData;
|
||||
plWAVHeader fHeader;
|
||||
UInt32 fDataLength;
|
||||
|
192
Sources/Plasma/PubUtilLib/plAudioCore/plSrtFileReader.cpp
Normal file
192
Sources/Plasma/PubUtilLib/plAudioCore/plSrtFileReader.cpp
Normal file
@ -0,0 +1,192 @@
|
||||
/*==LICENSE==*
|
||||
|
||||
CyanWorlds.com Engine - MMOG client, server and tools
|
||||
Copyright (C) 2011 Cyan Worlds, Inc.
|
||||
|
||||
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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Additional permissions under GNU GPL version 3 section 7
|
||||
|
||||
If you modify this Program, or any covered work, by linking or
|
||||
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
|
||||
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
|
||||
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
|
||||
(or a modified version of those libraries),
|
||||
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
|
||||
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
|
||||
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
|
||||
licensors of this Program grant you additional
|
||||
permission to convey the resulting work. Corresponding Source for a
|
||||
non-source form of such a combination shall include the source code for
|
||||
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
|
||||
work.
|
||||
|
||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com
|
||||
or by snail mail at:
|
||||
Cyan Worlds, Inc.
|
||||
14617 N Newport Hwy
|
||||
Mead, WA 99021
|
||||
|
||||
*==LICENSE==*/
|
||||
|
||||
#include "plSrtFileReader.h"
|
||||
|
||||
#include "hsStream.h"
|
||||
|
||||
#include "../plStatusLog/plStatusLog.h"
|
||||
#include "../pnUtils/Private/pnUtPath.h"
|
||||
#include "../pnUtils/Private/pnUtStr.h"
|
||||
|
||||
#include <regex>
|
||||
|
||||
static const std::regex speakerTextRegex("^((([\\w.]+(\\s\\w+)?):) )?(.*)");
|
||||
static const std::regex timingsRegex("^(\\d{2}):(\\d{2}):(\\d{2}),(\\d{3}) --> (\\d{2}):(\\d{2}):(\\d{2}),(\\d{3})$");
|
||||
|
||||
bool plSrtFileReader::ReadFile()
|
||||
{
|
||||
wchar srtPathUnicode[MAX_PATH];
|
||||
StrToUnicode(srtPathUnicode, fAudioFileName, arrsize(srtPathUnicode));
|
||||
PathSetExtension(srtPathUnicode, srtPathUnicode, L".sub", arrsize(srtPathUnicode));
|
||||
|
||||
if (PathDoesFileExist(srtPathUnicode)) {
|
||||
// read sets of SRT data until end of file
|
||||
hsUNIXStream srtFileStream;
|
||||
|
||||
// if file exists and was opened successfully
|
||||
if (srtFileStream.Open(srtPathUnicode, L"r")) {
|
||||
plStatusLog::AddLineS("audio.log", "Successfully opened subtitle file {}", srtPathUnicode);
|
||||
|
||||
uint32_t subtitleNumber = 0;
|
||||
uint32_t subtitleStartTimeMs = 0;
|
||||
uint32_t subtitleEndTimeMs = 0;
|
||||
char* line;
|
||||
std::string speakerName;
|
||||
std::string subtitleText;
|
||||
std::cmatch matches;
|
||||
|
||||
for (unsigned int lnCounter = 0; !srtFileStream.AtEnd(); lnCounter++) {
|
||||
if (lnCounter % 4 == 0 && srtFileStream.ReadLn(line)) {
|
||||
subtitleNumber = std::stoul(line);
|
||||
}
|
||||
else if (lnCounter % 4 == 1 && srtFileStream.ReadLn(line)) {
|
||||
if (std::regex_match(line, matches, timingsRegex)) {
|
||||
if (matches.size() < 9) {
|
||||
// add error message and ensure this subtitle line won't be added to the entries
|
||||
plStatusLog::AddLineS("audio.log", plStatusLog::kRed, " Subtitle timings '{}' are formatted incorrectly.", line);
|
||||
subtitleNumber = 0;
|
||||
}
|
||||
else {
|
||||
// matches[0] is the entire match, we don't do anything with it
|
||||
// matches[1] is the first group -- the start hour number
|
||||
subtitleStartTimeMs += std::stoul(matches[1].str()) * 3600000;
|
||||
// matches[2] is the second group -- the start minute number
|
||||
subtitleStartTimeMs += std::stoul(matches[2].str()) * 60000;
|
||||
// matches[3] is the third group -- the start seconds number
|
||||
subtitleStartTimeMs += std::stoul(matches[3].str()) * 1000;
|
||||
// matches[4] is the fourth group -- the start milliseconds number
|
||||
subtitleStartTimeMs += std::stoul(matches[4].str());
|
||||
|
||||
// matches[5] is the fifth group -- the end hour number
|
||||
subtitleEndTimeMs += std::stoul(matches[5].str()) * 3600000;
|
||||
// matches[6] is the sixth group -- the end minute number
|
||||
subtitleEndTimeMs += std::stoul(matches[6].str()) * 60000;
|
||||
// matches[7] is the seventh group -- the end seconds number
|
||||
subtitleEndTimeMs += std::stoul(matches[7].str()) * 1000;
|
||||
// matches[8] is the eighth group -- the end milliseconds number
|
||||
subtitleEndTimeMs += std::stoul(matches[8].str());
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (lnCounter % 4 == 2 && srtFileStream.ReadLn(line)) {
|
||||
if (std::regex_match(line, matches, speakerTextRegex)) {
|
||||
if (matches.size() < 5) {
|
||||
// add error message and ensure this subtitle line won't be added to the entries
|
||||
plStatusLog::AddLineS("audio.log", plStatusLog::kRed, " Subtitle text and/or speaker name '{}' are formatted incorrectly.", line);
|
||||
subtitleNumber = 0;
|
||||
}
|
||||
else {
|
||||
// matches[0] is the entire match, we don't do anything with it
|
||||
// matches[2] is the second group (the optional subtitle speaker with colon but no space at the end)
|
||||
speakerName = matches[2];
|
||||
|
||||
// matches[5] is the fourth group (the subtitle text)
|
||||
subtitleText = matches[5];
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (lnCounter % 4 == 3 && subtitleNumber > 0 && subtitleStartTimeMs >= 0 && subtitleEndTimeMs >= 0 && !subtitleText.empty()) {
|
||||
// entry is complete, add to the queue and reset our temp variables
|
||||
if (!speakerName.empty())
|
||||
fEntries.emplace_back(plSrtEntry(subtitleNumber, subtitleStartTimeMs, subtitleEndTimeMs, std::move(subtitleText), std::move(speakerName)));
|
||||
else
|
||||
fEntries.emplace_back(plSrtEntry(subtitleNumber, subtitleStartTimeMs, subtitleEndTimeMs, std::move(subtitleText)));
|
||||
|
||||
subtitleNumber = 0;
|
||||
subtitleStartTimeMs = 0;
|
||||
subtitleEndTimeMs = 0;
|
||||
subtitleText.clear();
|
||||
speakerName.clear();
|
||||
}
|
||||
}
|
||||
|
||||
if (subtitleNumber > 0 && subtitleStartTimeMs >= 0 && subtitleEndTimeMs >= 0 && !subtitleText.empty()) {
|
||||
// enqueue the last subtitle from the file if we didn't have an extra blank line at the end
|
||||
if (!speakerName.empty())
|
||||
fEntries.emplace_back(plSrtEntry(subtitleNumber, subtitleStartTimeMs, subtitleEndTimeMs, std::move(subtitleText), std::move(speakerName)));
|
||||
else
|
||||
fEntries.emplace_back(plSrtEntry(subtitleNumber, subtitleStartTimeMs, subtitleEndTimeMs, std::move(subtitleText)));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void plSrtFileReader::AdvanceToTime(uint32_t timeMs)
|
||||
{
|
||||
while (fCurrentEntryIndex < fEntries.size() && fEntries.at(fCurrentEntryIndex).GetEndTimeMs() <= timeMs) {
|
||||
fCurrentEntryIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
plSrtEntry* plSrtFileReader::GetNextEntryStartingBeforeTime(uint32_t timeMs)
|
||||
{
|
||||
if (fCurrentEntryIndex < fEntries.size()) {
|
||||
plSrtEntry& nextEntry = fEntries.at(fCurrentEntryIndex);
|
||||
|
||||
if (nextEntry.GetStartTimeMs() <= timeMs) {
|
||||
fCurrentEntryIndex++;
|
||||
return &nextEntry;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
plSrtEntry* plSrtFileReader::GetNextEntryEndingBeforeTime(uint32_t timeMs)
|
||||
{
|
||||
if (fCurrentEntryIndex < fEntries.size()) {
|
||||
plSrtEntry& nextEntry = fEntries.at(fCurrentEntryIndex);
|
||||
|
||||
if (nextEntry.GetEndTimeMs() <= timeMs) {
|
||||
fCurrentEntryIndex++;
|
||||
return &nextEntry;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
105
Sources/Plasma/PubUtilLib/plAudioCore/plSrtFileReader.h
Normal file
105
Sources/Plasma/PubUtilLib/plAudioCore/plSrtFileReader.h
Normal file
@ -0,0 +1,105 @@
|
||||
/*==LICENSE==*
|
||||
|
||||
CyanWorlds.com Engine - MMOG client, server and tools
|
||||
Copyright (C) 2011 Cyan Worlds, Inc.
|
||||
|
||||
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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Additional permissions under GNU GPL version 3 section 7
|
||||
|
||||
If you modify this Program, or any covered work, by linking or
|
||||
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
|
||||
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
|
||||
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
|
||||
(or a modified version of those libraries),
|
||||
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
|
||||
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
|
||||
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
|
||||
licensors of this Program grant you additional
|
||||
permission to convey the resulting work. Corresponding Source for a
|
||||
non-source form of such a combination shall include the source code for
|
||||
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
|
||||
work.
|
||||
|
||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com
|
||||
or by snail mail at:
|
||||
Cyan Worlds, Inc.
|
||||
14617 N Newport Hwy
|
||||
Mead, WA 99021
|
||||
|
||||
*==LICENSE==*/
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// //
|
||||
// plSrtFileReader - Class for reading an SRT format file and //
|
||||
// storing the read entries for access later //
|
||||
// //
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef _plSrtFileReader_h
|
||||
#define _plSrtFileReader_h
|
||||
|
||||
#include "hsTypes.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
class plSrtEntry
|
||||
{
|
||||
public:
|
||||
|
||||
plSrtEntry(uint32_t entryNum, uint32_t startTimeMs, uint32_t endTimeMs, std::string subtitleText, std::string speakerName)
|
||||
: fEntryNum(entryNum), fStartTimeMs(startTimeMs), fEndTimeMs(endTimeMs), fSubtitleText(std::move(subtitleText)),
|
||||
fSpeakerName(std::move(speakerName)) { }
|
||||
|
||||
plSrtEntry(uint32_t entryNum, uint32_t startTimeMs, uint32_t endTimeMs, std::string subtitleText)
|
||||
: fEntryNum(entryNum), fStartTimeMs(startTimeMs), fEndTimeMs(endTimeMs), fSubtitleText(std::move(subtitleText)) { }
|
||||
|
||||
std::string GetSubtitleText() const { return fSubtitleText; }
|
||||
std::string GetSpeakerName() const { return fSpeakerName; }
|
||||
uint32_t GetStartTimeMs() const { return fStartTimeMs; }
|
||||
uint32_t GetEndTimeMs() const { return fEndTimeMs; }
|
||||
|
||||
protected:
|
||||
|
||||
uint32_t fEntryNum;
|
||||
uint32_t fStartTimeMs;
|
||||
uint32_t fEndTimeMs;
|
||||
std::string fSubtitleText;
|
||||
std::string fSpeakerName;
|
||||
|
||||
};
|
||||
|
||||
class plSrtFileReader
|
||||
{
|
||||
public:
|
||||
|
||||
plSrtFileReader(const char* audioFileName)
|
||||
: fAudioFileName(std::move(audioFileName)), fEntries(), fCurrentEntryIndex() { }
|
||||
|
||||
bool ReadFile();
|
||||
void StartOver() { fCurrentEntryIndex = 0; }
|
||||
const char* GetCurrentAudioFileName() const { return fAudioFileName; }
|
||||
void AdvanceToTime(uint32_t timeMs);
|
||||
plSrtEntry* GetNextEntryStartingBeforeTime(uint32_t timeMs);
|
||||
plSrtEntry* GetNextEntryEndingBeforeTime(uint32_t timeMs);
|
||||
|
||||
protected:
|
||||
|
||||
const char* fAudioFileName;
|
||||
std::vector<plSrtEntry> fEntries;
|
||||
uint32_t fCurrentEntryIndex;
|
||||
|
||||
};
|
||||
|
||||
#endif //_plSrtFileReader_h
|
Reference in New Issue
Block a user