Subversion Repositories spk

Rev

Rev 325 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 325 Rev 326
Line 502... Line 502...
502
		wprintf(L"Extracted %lldms from page %d to %s\n", length, page, CFileIO(outputfile).filename().c_str());
502
		wprintf(L"Extracted %lldms from page %d to %s\n", length, page, CFileIO(outputfile).filename().c_str());
503
}
503
}
504
 
504
 
505
void Split(const Utils::CommandLine& cmd)
505
void Split(const Utils::CommandLine& cmd)
506
{
506
{
-
 
507
	if (cmd.argCount() < 4)
-
 
508
	{
-
 
509
		wprintf(L"Syntax: %s split [--stream:<#>] <file.xml> <file.wav> <dir>\n", cmd.cmdName().c_str());
-
 
510
		wprintf(L"\t<file.wav>\tThe main wav file to split\n");
-
 
511
		wprintf(L"\t<file.xml>\tThe main xml file to split\n");
-
 
512
		wprintf(L"\t<dir>\t\tThe directory to write the files to\n");
-
 
513
		return;
-
 
514
	}
-
 
515
	bool quiet = cmd.hasSwitch(L"quiet");
-
 
516
	bool verbose = cmd.hasSwitch(L"verbose");
-
 
517
	Utils::WString xmlfile = cmd.fullFilename(cmd.arg(1));
-
 
518
	Utils::WString wavfile = cmd.fullFilename(cmd.arg(2));
-
 
519
	Utils::WString dir = cmd.fullFilename(cmd.arg(3));
-
 
520
	int stream = cmd.hasSwitch(L"stream") ? cmd.switchData(L"stream").toInt() : 1;
-
 
521
	if (!CFileIO(wavfile).exists())
-
 
522
	{
-
 
523
		wprintf(L"Error: Unable to open wav file, %s\n", wavfile.c_str());
-
 
524
		return;
-
 
525
	}
-
 
526
	CFileIO xml(xmlfile);
-
 
527
	if (!xml.exists())
-
 
528
	{
-
 
529
		wprintf(L"Error: Unable to open xml file, %s\n", xmlfile.c_str());
-
 
530
		return;
-
 
531
	}
-
 
532
	Utils::WStringList xmlLines;
-
 
533
	if (!xml.readLines(xmlLines))
-
 
534
	{
-
 
535
		wprintf(L"Error: Unable to read xml file, %s\n", xmlfile.c_str());
-
 
536
		return;
-
 
537
	}
-
 
538
	int filesWritten = 0;
507
 
539
 
-
 
540
	SF_INFO inInfo;
-
 
541
	SNDFILE* inputFile = sf_open(wavfile.toFileUTF8().c_str(), SFM_READ, &inInfo);
-
 
542
	if (inputFile)
-
 
543
	{
-
 
544
		CDirIO D(dir);
-
 
545
		if (!D.exists())
-
 
546
		{
-
 
547
			if (!D.create())
-
 
548
			{
-
 
549
				wprintf(L"Error: Unable to create directory, %s\n", dir.c_str());
-
 
550
				return;
-
 
551
			}
-
 
552
		}
-
 
553
 
-
 
554
		double buffer[BUFFER_LEN];
-
 
555
		sf_count_t seek = sf_seek(inputFile, 0, SEEK_SET);
-
 
556
		long inPage = 0;
-
 
557
		for (auto itr = xmlLines.begin(); itr != xmlLines.end(); itr++)
-
 
558
		{
-
 
559
			if ((*itr)->str.contains(L"</page>"))
-
 
560
				inPage = 0;
-
 
561
			else if ((*itr)->str.contains(L"<page id=\""))
-
 
562
			{
-
 
563
				long strm = (*itr)->str.between(L" stream=\"", L"\"").toLong();
-
 
564
				if (strm == stream)
-
 
565
					inPage = (*itr)->str.between(L"<page id=\"", L"\"").toLong();
-
 
566
			}
-
 
567
			else if (inPage > 0)
-
 
568
			{
-
 
569
				if ((*itr)->str.contains(L"<t id="))
-
 
570
				{
-
 
571
					long i = (*itr)->str.between(L" id=\"", L"\"").toLong();
-
 
572
					long long s = (*itr)->str.between(L" s=\"", L"\"").toLong64();
-
 
573
					long long l = (*itr)->str.between(L" l=\"", L"\"").toLong64();
-
 
574
 
-
 
575
					long long startFrame = static_cast<long long>((static_cast<double>(s) / 1000.0) * inInfo.samplerate);
-
 
576
					sf_count_t seek = sf_seek(inputFile, startFrame, SEEK_SET);
-
 
577
 
-
 
578
					CDirIO writeDir(D.file(Utils::WString(inPage)));
-
 
579
					if (!writeDir.exists())
-
 
580
					{
-
 
581
						if (!writeDir.create())
-
 
582
						{
-
 
583
							wprintf(L"Error: Unable to create directory, %s\n", writeDir.dir().c_str());
-
 
584
							return;
-
 
585
						}
-
 
586
					}
-
 
587
 
-
 
588
					Utils::WString outFileName = writeDir.file(Utils::WString(i) + L".wav");
-
 
589
					// now we need to write the wav file
-
 
590
					SF_INFO outInfo;
-
 
591
					outInfo.channels = 1;
-
 
592
					outInfo.samplerate = inInfo.samplerate;
-
 
593
					outInfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
-
 
594
 
-
 
595
					SNDFILE* outputFile = sf_open(outFileName.toFileUTF8().c_str(), SFM_WRITE, &outInfo);
-
 
596
					if (outputFile)
-
 
597
					{
-
 
598
						long long endFrame = static_cast<long long>((static_cast<double>(s + l) / 1000.0) * inInfo.samplerate);
-
 
599
						long long readRemaining = endFrame - startFrame;
-
 
600
						while (readRemaining > 0)
-
 
601
						{
-
 
602
							long long readAmount = readRemaining > BUFFER_LEN ? BUFFER_LEN : readRemaining;
-
 
603
							sf_count_t readcount = sf_read_double(inputFile, buffer, readAmount);
-
 
604
							sf_write_double(outputFile, buffer, readcount);
-
 
605
 
-
 
606
							readRemaining -= readcount;
-
 
607
						}
-
 
608
						sf_close(outputFile);
-
 
609
						if (verbose)
-
 
610
							wprintf(L"Wrote %lldms to %d\\%s\n", l, inPage, CFileIO(outFileName).filename().c_str());
-
 
611
						++filesWritten;
-
 
612
					}
-
 
613
				}
-
 
614
			}
-
 
615
		}
-
 
616
 
-
 
617
		sf_close(inputFile);
-
 
618
	}
-
 
619
 
-
 
620
	if(filesWritten > 0)
-
 
621
	{
-
 
622
		if (!quiet || cmd.hasSwitch(L"showresult"))
-
 
623
			wprintf(L"Wrote %d files to %s\n", filesWritten, dir.c_str());
-
 
624
	}
-
 
625
	else
-
 
626
	{
-
 
627
		wprintf(L"Error: Unable to write any files to %s\n", dir.c_str());
-
 
628
	}
508
}
629
}
509
 
630
 
510
 
631
 
511
void Compile(const Utils::CommandLine& cmd)
632
void Compile(const Utils::CommandLine& cmd)
512
{
633
{