deprecated/wveconv/wveconv.cpp
changeset 600 6d08f4a05d93
equal deleted inserted replaced
599:fa7a3cc6effd 600:6d08f4a05d93
       
     1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Psion3a WVE to Series5 sound file convertor
       
    15 // 
       
    16 //
       
    17 
       
    18 #if defined(__MSVCDOTNET__) || defined(__TOOLS2__)
       
    19 #include <iostream>
       
    20 #include <fstream>
       
    21 using namespace std;
       
    22 #else  //!__MSVCDOTNET__
       
    23 #include <iostream.h>
       
    24 #include <fstream.h>
       
    25 #ifdef __CW32__
       
    26 using std::streampos;
       
    27 #endif //__CW32__
       
    28 #endif //__MSVCDOTNET__
       
    29 
       
    30 #include <string.h>
       
    31 
       
    32 #if defined(__VC32__) && !defined(__MSVCDOTNET__)
       
    33 #pragma warning( disable : 4710 )	// 'fn': function not inlined
       
    34 #endif // old MSVC
       
    35 
       
    36 #define KUidRecordApp 0x1000007e
       
    37 
       
    38 #define KUid1 0x10000037
       
    39 #define KUid2 0x1000006d
       
    40 #define KUid3 KUidRecordApp
       
    41 #define KUidCheckSum 0x5508accf
       
    42 
       
    43 #define KUidAppStream 0x10000089
       
    44 #define KUidSampleStream 0x10000052
       
    45 
       
    46 
       
    47 int ErrorNotWveFile()
       
    48 	{
       
    49 	cout << "The specified input file is not a Series 3a WVE file" << endl ;
       
    50 	return(0);
       
    51 	}
       
    52 
       
    53 int ErrorNotFound()
       
    54 	{
       
    55 	cout << "Unable to open input file" << endl ;
       
    56 	return(0);
       
    57 	}
       
    58 
       
    59 int ReadInt32(ifstream& inStream)
       
    60 	{
       
    61     char ch[4];
       
    62     inStream.read(&ch[0],4);
       
    63     return *(int*)&ch[0];
       
    64 	}
       
    65 
       
    66 int ReadInt16(ifstream& inStream)
       
    67 	{
       
    68     char ch[4];
       
    69     inStream.read(&ch[0],2);
       
    70     ch[2]=0;
       
    71     ch[3]=0;
       
    72     return *(int*)&ch[0];
       
    73 	}
       
    74 
       
    75 int WriteWord(ofstream& outStream,unsigned int aWord)
       
    76 	{
       
    77     outStream.write((char*)&aWord,4);
       
    78 	return 0;
       
    79 	}
       
    80 
       
    81 int WriteByte(ofstream& outStream,const char aByte)
       
    82 	{
       
    83     outStream.write((char*)&aByte,1);
       
    84 	return 0;
       
    85 	}
       
    86 
       
    87 int WriteText(ofstream& outStream,const char* aText)
       
    88 	{
       
    89 	char ch;
       
    90 	while((ch=*aText++)!=0)
       
    91 		outStream << ch;
       
    92 	return 0;
       
    93 	}
       
    94 
       
    95 void ShowHelp()
       
    96 	{
       
    97 	cout << "WveConv 0.04";
       
    98 	cout << endl ;
       
    99 	cout << "Converts a Psion 3a WVE file to an EPOC sound file" << endl ;
       
   100 	cout << endl ;
       
   101 	cout << "Usage: WVECONV S3AFILE.WVE [EPOCFILE]" << endl ;
       
   102 	cout << endl ;
       
   103 	cout << "If [EPOCFILE] is omitted, the input filename" << endl ;
       
   104     cout << "without the extension will be used" << endl ;
       
   105 	}
       
   106 
       
   107 int ConvertFile(const char* inputFile,const char* outputFile)
       
   108 	{
       
   109 	ifstream inStream(inputFile,ios::in|ios::binary);
       
   110 	ofstream outStream(outputFile,ios::out|ios::binary);
       
   111 
       
   112 	if(!inStream.is_open())
       
   113 		return(ErrorNotFound());
       
   114 
       
   115 	const char* appName="Record.app";
       
   116 	char header[16];
       
   117 	
       
   118 	inStream.read(&header[0],16);
       
   119 
       
   120 	if(strcmp(header,"ALawSoundFile**")!=0)
       
   121 		return(ErrorNotWveFile());
       
   122 
       
   123 	int compressedLength=0;
       
   124 	int trailingSilence=0;
       
   125 	int compressorUid=0;
       
   126 	int sampleLength=0;
       
   127 	int repeatCount=0;
       
   128     int versionNumber=0;
       
   129 //
       
   130 // extract the sample length, repeat count and trailing silence...
       
   131 //
       
   132 	versionNumber=ReadInt16(inStream);
       
   133 	sampleLength=ReadInt32(inStream);
       
   134 	trailingSilence=ReadInt16(inStream);
       
   135 	repeatCount=ReadInt16(inStream);
       
   136     if(repeatCount==0) // 0 and 1 are the same on Series 3 
       
   137         repeatCount=1; 
       
   138 
       
   139 	trailingSilence*=31250;
       
   140 
       
   141 	compressedLength=sampleLength;
       
   142 
       
   143 	cout << "Converting " << inputFile << " to " << outputFile << endl << endl;
       
   144     cout << "Version number  :" << versionNumber << endl ;
       
   145 	cout << "Sample length   :" << sampleLength << " bytes" << endl ;
       
   146 	cout << "Repeat count    :" << repeatCount << endl ;
       
   147 	cout << "Trailing silence:" << trailingSilence << " uS" << endl ;
       
   148 
       
   149 	inStream.seekg((streampos)0x20);
       
   150 //
       
   151 // Write out the header...
       
   152 //
       
   153 	unsigned int rootstreamid=0x14;
       
   154 	unsigned int appstreamid=0x25;
       
   155 	unsigned int samplestreamid=0x34;
       
   156 
       
   157 // checked uid header
       
   158 
       
   159 	WriteWord(outStream,KUid1);
       
   160 	WriteWord(outStream,KUid2);
       
   161 	WriteWord(outStream,KUidRecordApp);
       
   162 	WriteWord(outStream,KUidCheckSum);
       
   163 
       
   164 // root stream id
       
   165 
       
   166 	WriteWord(outStream,rootstreamid);
       
   167 
       
   168 //stream dictionary @ 0x14 root stream
       
   169 
       
   170 	WriteByte(outStream,4);		// two entries in dictionary
       
   171 	WriteWord(outStream,KUidSampleStream);	// sample stream
       
   172 	WriteWord(outStream,samplestreamid);
       
   173 	WriteWord(outStream,KUidAppStream);	// appid stream
       
   174 	WriteWord(outStream,appstreamid);
       
   175 
       
   176 // record app identifier stream @ 0x25
       
   177 
       
   178 	WriteWord(outStream,KUidRecordApp);
       
   179 	WriteByte(outStream,42);
       
   180 	WriteText(outStream,appName);
       
   181 
       
   182 //  sample header @ 0x34
       
   183 
       
   184 	WriteWord(outStream,sampleLength);
       
   185 	WriteWord(outStream,compressorUid);
       
   186 	WriteWord(outStream,repeatCount-1); // repeats are zero based on Series 5 
       
   187 	WriteWord(outStream,trailingSilence);
       
   188 	WriteWord(outStream,compressedLength);
       
   189 //
       
   190 // Copy the sample data...
       
   191 //
       
   192 	streampos newPos=0x20; // start of sample data in 3a file...
       
   193 	inStream.seekg(newPos);
       
   194 
       
   195 	char buffer[256];
       
   196 	int count;
       
   197 	int actualLength=0;
       
   198 	do
       
   199 		{
       
   200 		inStream.read(&buffer[0],256);
       
   201 		if((count=inStream.gcount())!=0)
       
   202 			{
       
   203 			outStream.write(&buffer[0],count);
       
   204 			actualLength+=count;
       
   205 			}
       
   206 		} while(count);
       
   207 //
       
   208 // should check actualLength==sampleLength...but what the heck
       
   209 //
       
   210 	outStream.close();
       
   211 	inStream.close();
       
   212 	return 0;
       
   213 	}
       
   214 
       
   215 int main(int aNumArgs,char* aArgs[])
       
   216 	{
       
   217 	if(aNumArgs<=1 || aArgs[1][0]=='?' || aArgs[1][0]=='/')
       
   218 		{
       
   219 		ShowHelp();
       
   220 		return 0;
       
   221 		}
       
   222 	char inputFile[255];
       
   223 	char outputFile[255];
       
   224 
       
   225 	strcpy(inputFile,aArgs[1]);
       
   226 
       
   227 	if(aNumArgs==3)
       
   228 		strcpy(outputFile,aArgs[2]);
       
   229 
       
   230 	if(aNumArgs==2 || outputFile[0]==0)
       
   231 		{
       
   232 		strcpy(outputFile,inputFile);
       
   233 //
       
   234 // remove the extension
       
   235 //
       
   236 		int len=strlen(outputFile);
       
   237 		for(;;)
       
   238 			{
       
   239 			if(--len<0)
       
   240 				break;
       
   241 			if(outputFile[len]=='.')
       
   242 				{
       
   243 				outputFile[len]=0;
       
   244 				break;
       
   245 				}
       
   246 			}
       
   247 		}
       
   248 	return(ConvertFile(inputFile,outputFile));
       
   249 	}
       
   250