Subversion Repositories spk

Rev

Rev 290 | Rev 325 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
270 cycrow 1
/*
2
 Voice file Creation V1.00 Created by Cycrow (Matthew Gravestock)
3
*/
4
 
5
// Main Spk File Library Include
6
#ifdef _WIN32
7
#include <spk.h>
8
#include <StringList.h>
9
#else
10
#include "../spk/spk.h"
11
#endif
12
#include <time.h>
13
 
14
#ifdef _WIN32
15
#include <windows.h>
16
#include <direct.h>
17
#include <shlobj.h>
18
#endif
19
 
20
#include "sndfile.hh"
304 cycrow 21
#include "Utils/CommandLine.h"
270 cycrow 22
 
23
 
24
void PrintSyntax (const Utils::WString &cmd)
287 cycrow 25
{	
26
	wprintf ( L"Syntax: %s </command> [options]\n", cmd.c_str());
27
	wprintf ( L"\nCommands:\n" );
270 cycrow 28
	/*
29
	printf ( "   /list\n\t- Lists installed packages\n" );
30
	printf ( "   /install <file>\n\t- Installs a package file\n" );
31
	printf ( "   /uninstall <package#>\n\t- Uninstalls a package, use id number from list\n" );
32
	printf ( "   /enable <package#>\n\t- Enables an installed package\n" );
33
	printf ( "   /disable <package#>\n\t- Disables an installed package\n" );
34
	printf ( "   /removeuninstall\n\t- Removes uninstall scripts\n" );
35
	printf ( "   /removeshared\n\t- Removes unused sharded files\n" );
36
	printf ( "\nSwitchs:\n" );
37
	printf ( "   -d (--directory) [directory]\n\tChange destination directory, otherwise uses current\n\n" );
38
	printf ( "   -v (--verbose)\n\tTurns on verbose mode\n\n" );
39
	printf ( "   -o (--override)\n\tOverride install warnings\n\tIE. allows you to install older scripts\n\n" );
40
	printf ( "   -t (--textrename)\n\tForces text file renaming for installed packages\n\n" );
41
	printf ( "   -l (--language) <language>\n\tSets the language id for text file renaming\n\totheriwse reads game lang.dat\n\n" );
42
	printf ( "   -c (--enablechild)\n\tAuto Enabled all children when parent is enabled\n\n" );
287 cycrow 43
	printf ( "   -m (--forcemod\n\tForces a mod enabled even if theres one already enabled\n\n" );	
270 cycrow 44
	*/
45
}
46
 
47
void Pause()
48
{
49
#ifdef _DEBUG
50
	char pause;
51
	scanf ( "%s", &pause );
52
#endif
53
}
54
 
55
#define BUFFER_LEN 4096
287 cycrow 56
 
57
 
304 cycrow 58
void DoOffset(const Utils::CommandLine &cmd)
287 cycrow 59
{
304 cycrow 60
	if (cmd.argCount() < 3)
287 cycrow 61
	{
304 cycrow 62
		wprintf(L"Syntax: %s offset [options] <offset> <file> [--out:<file>]\n", cmd.cmdName().c_str());
287 cycrow 63
		wprintf(L"	<offset>\t\tOffset time to adjust by\n");
64
		wprintf(L"	<file>\t\tThe file to adjust\n");
65
		wprintf(L"\nOptions:\n");
66
		wprintf(L"  --out:<file>\t\tThe output filename, otherwise, will write to the input\n");
67
		wprintf(L"  --page:<id>\t\tThe page id to read (otherwise will do all of them)\n");
68
	}
69
	else
70
	{
304 cycrow 71
		Utils::WString offsetStr = cmd.arg(1);
72
		Utils::WString fileStr = cmd.arg(2);
73
		Utils::WString outFileStr = cmd.hasSwitch(L"out") ? cmd.switchData(L"out") : fileStr;
74
		unsigned int pageID = cmd.hasSwitch(L"page") ? cmd.switchData(L"page").toInt() : 0;
287 cycrow 75
		long offset = offsetStr.toLong();
304 cycrow 76
		bool doIgnore = cmd.hasSwitch(L"ignore");
287 cycrow 77
 
78
		CFileIO f;
79
		if (f.open(fileStr))
80
		{
81
			bool inPage = false;
289 cycrow 82
			bool ignore = false;
287 cycrow 83
			Utils::WStringList list;
84
			Utils::WStringList outList;
85
			if (f.readLines(list))
86
			{
87
				for (auto itr = list.begin(); itr != list.end(); itr++)
88
				{
89
					Utils::WString line = (*itr)->str;
90
					if (line.contains(L"<page id=\""))
91
					{
92
						long id = line.between(L"<page id=\"", L"\"").toLong();
93
						if (pageID == 0 || pageID == id)
94
							inPage = true;
289 cycrow 95
						else
96
							ignore = true;
287 cycrow 97
					}
98
					else if (line.contains(L"</page>"))
99
						inPage = false;
100
					else if (inPage)
101
					{
102
						long pos = line.findPos(L"s=\"");
103
						if (pos != -1)
104
						{
105
							Utils::WString s = line.substr(pos + 3);
106
							s = s.token(L"\"", 1);
289 cycrow 107
							long long time = s.toLong64();
108
							long long toTime = time + offset;
287 cycrow 109
							line = line.findReplace(time, toTime);
110
						}
111
					}
289 cycrow 112
 
113
					if(!ignore || !doIgnore)
114
						outList.pushBack(line);
115
 
116
					if (line.contains(L"</page>"))
117
						ignore = false;
287 cycrow 118
				}
119
				CFileIO outFile(outFileStr);
120
				outFile.writeFile(&outList);
121
				outFile.close();
122
			}
123
		}
124
		else
125
			wprintf(L"Error: Unable to open file, %s\n", fileStr.c_str());
126
	}
127
 
128
}
129
 
270 cycrow 130
/*
131
	Main entry point to program
132
*/
133
int main ( int argc, char **argv )
134
{
135
 	// display program header to command prompt
136
	printf ( "\nX3 Voice Create V1.00 (SPK Library Version %.2f) 15/12/2023 Created by Cycrow\n\n", GetLibraryVersion() );
137
 
304 cycrow 138
	Utils::CommandLine cmd(argc, argv);
139
	Utils::WString command = cmd.argCount() >= 1 ? cmd.arg(0) : L"";
270 cycrow 140
 
287 cycrow 141
	if (command == L"offset")
304 cycrow 142
		DoOffset(cmd);
287 cycrow 143
	else if (command == L"help")
304 cycrow 144
		PrintSyntax(cmd.cmdName());
287 cycrow 145
	else
146
	{
270 cycrow 147
 
287 cycrow 148
		Utils::String mainFileStr = "d:/x/X3 Terran Conflict - Guilds/addon2/mov/00344.wav";
149
		Utils::WString dir = L"d:/x/X3 Terran Conflict - Guilds/addon2/mov/working";
150
		Utils::String output = "d:/x/X3 Terran Conflict - Guilds/addon2/mov/working/00344.wav";
151
		Utils::WString outputXML = L"d:/x/X3 Terran Conflict - Guilds/addon2/mov/working/00044.xml";
270 cycrow 152
 
287 cycrow 153
		SF_INFO info;
154
		SNDFILE* mainFile = sf_open(mainFileStr.c_str(), SFM_READ, &info);
155
		if (mainFile)
270 cycrow 156
		{
287 cycrow 157
			double duration = static_cast<double>(info.frames) / static_cast<double>(info.samplerate);
158
			size_t currentPos = static_cast<size_t>((duration * 1000.0) + 0.5);
270 cycrow 159
 
287 cycrow 160
			CFileIO(mainFileStr).copy(output);
270 cycrow 161
 
162
 
287 cycrow 163
			SF_INFO outInfo;
164
			SNDFILE* outputFile = sf_open(output.c_str(), SFM_RDWR, &outInfo);
165
			if (outputFile)
270 cycrow 166
			{
287 cycrow 167
				double buffer[BUFFER_LEN];
168
 
169
				int check = sf_format_check(&outInfo);
170
				sf_count_t seek = sf_seek(outputFile, 0, SEEK_END);
171
 
172
				Utils::WStringList xmlOut;
173
				xmlOut.pushBack(L"<?xml version=\"1.0\" ?>");
174
				xmlOut.pushBack(L"<language id=\"44\">");
175
 
176
				CDirIO D(dir);
177
				Utils::WStringList list;
178
				if (D.dirList(list))
270 cycrow 179
				{
287 cycrow 180
					for (auto itr = list.begin(); itr != list.end(); itr++)
270 cycrow 181
					{
287 cycrow 182
						if (D.isDir((*itr)->str))
183
						{
184
							CDirIO curDir(D.dir((*itr)->str));
185
							long pageID = (*itr)->str.toInt();
270 cycrow 186
 
287 cycrow 187
							xmlOut.pushBack(Utils::WString(L"<page id=\"") + pageID + L"\" stream=\"3\">");
270 cycrow 188
 
287 cycrow 189
							Utils::WStringList idList;
190
							if (curDir.dirList(idList))
270 cycrow 191
							{
287 cycrow 192
								for (auto itr2 = idList.begin(); itr2 != idList.end(); itr2++)
270 cycrow 193
								{
287 cycrow 194
									if (curDir.isFile((*itr2)->str))
270 cycrow 195
									{
287 cycrow 196
										CFileIO F(curDir.file((*itr2)->str));
197
										long id = F.baseName().toInt();
198
										size_t len = 0;
270 cycrow 199
 
287 cycrow 200
										SF_INFO inInfo;
201
										SNDFILE* inputFile = sf_open(F.fullFilename().toString().c_str(), SFM_READ, &inInfo);
202
										if (inputFile)
203
										{
204
											double duration = static_cast<double>(inInfo.frames) / static_cast<double>(inInfo.samplerate);
205
											size_t len = static_cast<size_t>((duration * 1000.0) + 0.5);
270 cycrow 206
 
207
 
287 cycrow 208
											sf_count_t readcount;
209
											while ((readcount = sf_read_double(inputFile, buffer, BUFFER_LEN)) > 0)
210
												sf_write_double(outputFile, buffer, readcount);
211
											//int* data = new int[inInfo.frames * inInfo.channels];
212
											//sf_count_t readCount = sf_readf_int(inputFile, data, inInfo.frames);
213
											//sf_count_t writeCount = sf_writef_int(outputFile, data, inInfo.frames);
214
 
215
											xmlOut.pushBack(Utils::WString(L"\t<t id=\"") + id + L"\" s=\"" + (long)currentPos + L"\" l=\"" + (long)len + "\"/>");
216
											currentPos += len;
217
											sf_close(inputFile);
218
										}
270 cycrow 219
									}
220
								}
221
							}
287 cycrow 222
 
223
							xmlOut.pushBack(L"</page>");
270 cycrow 224
						}
225
					}
226
				}
227
 
287 cycrow 228
				xmlOut.pushBack("</language>");
270 cycrow 229
 
287 cycrow 230
				sf_close(outputFile);
270 cycrow 231
 
287 cycrow 232
				CFileIO xml(outputXML);
233
				xml.writeFile(&xmlOut);
234
			}
235
 
236
			sf_close(mainFile);
270 cycrow 237
		}
238
	}
239
	//Pause();
240
 
241
	return 0;
242
}