Subversion Repositories spk

Rev

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