amplitude.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include <stdio.h>
  21. #include <malloc.h>
  22. #include <string.h>
  23. #include <math.h>
  24. #include <stdlib.h>
  25. static const int SAMPLE_RATE = 60;
  26. enum errorCodes_t {
  27. E_OK = 0,
  28. E_ARGS,
  29. E_OPEN_IN,
  30. E_OPEN_OUT,
  31. E_PROCESSING
  32. };
  33. struct chunk_t {
  34. unsigned int id;
  35. unsigned int size;
  36. unsigned int offset;
  37. };
  38. static unsigned short FORMAT_PCM = 0x0001;
  39. struct format_t {
  40. unsigned short formatTag;
  41. unsigned short numChannels;
  42. unsigned int samplesPerSec;
  43. unsigned int avgBytesPerSec;
  44. unsigned short sampleSize;
  45. unsigned short bitsPerSample;
  46. };
  47. #define SwapBytes( x, y ) { unsigned char t = (x); (x) = (y); (y) = t; }
  48. template<class type> static void Swap( type &c ) {
  49. if ( sizeof( type ) == 1 ) {
  50. } else if ( sizeof( type ) == 2 ) {
  51. unsigned char * b = (unsigned char *)&c;
  52. SwapBytes( b[0], b[1] );
  53. } else if ( sizeof( type ) == 4 ) {
  54. unsigned char * b = (unsigned char *)&c;
  55. SwapBytes( b[0], b[3] );
  56. SwapBytes( b[1], b[2] );
  57. } else if ( sizeof( type ) == 8 ) {
  58. unsigned char * b = (unsigned char *)&c;
  59. SwapBytes( b[0], b[7] );
  60. SwapBytes( b[1], b[6]);
  61. SwapBytes( b[2], b[5] );
  62. SwapBytes( b[3], b[4] );
  63. } else {
  64. int * null = 0;
  65. c = *null;
  66. }
  67. }
  68. int WAVE_ReadHeader( FILE * f ) {
  69. struct header_t {
  70. unsigned int id;
  71. unsigned int size;
  72. unsigned int format;
  73. } header;
  74. fread( &header, sizeof( header ), 1, f );
  75. Swap( header.id );
  76. Swap( header.format );
  77. if ( header.id != 'RIFF' || header.format != 'WAVE' || header.size < 4 ) {
  78. return 0;
  79. }
  80. return header.size;
  81. }
  82. int WAVE_ReadChunks( FILE * f, unsigned int fileSize, chunk_t * chunks, int maxChunks ) {
  83. unsigned int offset = ftell( f );
  84. int numChunks = 0;
  85. while ( offset < fileSize ) {
  86. struct chuckHeader_t {
  87. unsigned int id;
  88. unsigned int size;
  89. } chunkHeader;
  90. if ( fread( &chunkHeader, sizeof( chunkHeader ), 1, f ) != 1 ) {
  91. return numChunks;
  92. }
  93. Swap( chunkHeader.id );
  94. offset += sizeof( chunkHeader );
  95. if ( numChunks == maxChunks ) {
  96. return maxChunks + 1;
  97. }
  98. chunks[numChunks].id = chunkHeader.id;
  99. chunks[numChunks].size = chunkHeader.size;
  100. chunks[numChunks].offset = offset;
  101. numChunks++;
  102. offset += chunkHeader.size;
  103. fseek( f, offset, SEEK_SET );
  104. }
  105. return numChunks;
  106. }
  107. bool Process( FILE * in, FILE * out ) {
  108. int headerSize = WAVE_ReadHeader( in );
  109. if ( headerSize == 0 ) {
  110. printf( "Header invalid\n" );
  111. return false;
  112. }
  113. static const int MAX_CHUNKS = 32;
  114. chunk_t chunks[MAX_CHUNKS] = {};
  115. int numChunks = WAVE_ReadChunks( in, headerSize + 8, chunks, MAX_CHUNKS );
  116. if ( numChunks == 0 ) {
  117. printf( "No chunks\n" );
  118. return false;
  119. }
  120. if ( numChunks > MAX_CHUNKS ) {
  121. printf( "Too many chunks\n" );
  122. return false;
  123. }
  124. format_t format;
  125. bool foundFormat = false;
  126. unsigned int dataOffset = 0;
  127. unsigned int dataSize = 0;
  128. for ( int i = 0; i < numChunks; i++ ) {
  129. if ( chunks[i].id == 'fmt ' ) {
  130. if ( foundFormat ) {
  131. printf( "Found multiple format chunks\n" );
  132. return false;
  133. }
  134. if ( chunks[i].size < sizeof( format ) ) {
  135. printf( "Format chunk too small\n" );
  136. return false;
  137. }
  138. fseek( in, chunks[i].offset, SEEK_SET );
  139. fread( &format, sizeof( format ), 1, in );
  140. foundFormat = true;
  141. }
  142. if ( chunks[i].id == 'data' ) {
  143. if ( dataOffset > 0 ) {
  144. printf( "Found multiple data chunks\n" );
  145. return false;
  146. }
  147. dataOffset = chunks[i].offset;
  148. dataSize = chunks[i].size;
  149. }
  150. }
  151. if ( dataOffset == 0 ) {
  152. printf( "Colud not find data chunk\n" );
  153. return false;
  154. }
  155. if ( !foundFormat ) {
  156. printf( "Could not find fmt chunk\n" );
  157. return false;
  158. }
  159. if ( format.formatTag != FORMAT_PCM ) {
  160. printf( "Only PCM files supported (%d)\n", format.formatTag );
  161. return false;
  162. }
  163. if ( format.bitsPerSample != 8 && format.bitsPerSample != 16 ) {
  164. printf( "Only 8 or 16 bit files supported (%d)\n", format.bitsPerSample );
  165. return false;
  166. }
  167. if ( format.numChannels != 1 && format.numChannels != 2 ) {
  168. printf( "Only stereo or mono files supported (%d)\n", format.numChannels );
  169. return false;
  170. }
  171. unsigned short expectedSampleSize = format.numChannels * format.bitsPerSample / 8;
  172. if ( format.sampleSize != expectedSampleSize ) {
  173. printf( "Invalid sampleSize (%d, expected %d)\n", format.sampleSize, expectedSampleSize );
  174. return false;
  175. }
  176. unsigned int numSamples = dataSize / expectedSampleSize;
  177. void * inputData = malloc( dataSize );
  178. if ( inputData == NULL ) {
  179. printf( "Out of memory\n" );
  180. return false;
  181. }
  182. fseek( in, dataOffset, SEEK_SET );
  183. fread( inputData, dataSize, 1, in );
  184. int numOutputSamples = 1 + ( numSamples * SAMPLE_RATE / format.samplesPerSec );
  185. float * min = (float *)malloc( numOutputSamples * sizeof( float ) );
  186. float * max = (float *)malloc( numOutputSamples * sizeof( float ) );
  187. unsigned char * outputData = (unsigned char *)malloc( numOutputSamples );
  188. if ( min == NULL || max == NULL || outputData == NULL ) {
  189. printf( "Out of memory\n" );
  190. free( inputData );
  191. free( min );
  192. free( max );
  193. free( outputData );
  194. return false;
  195. }
  196. for ( int i = 0; i < numOutputSamples; i++ ) {
  197. max[i] = -1.0f;
  198. min[i] = 1.0f;
  199. }
  200. if ( format.bitsPerSample == 16 ) {
  201. short * sdata = (short *)inputData;
  202. if ( format.numChannels == 1 ) {
  203. for ( unsigned int i = 0; i < numSamples; i++ ) {
  204. unsigned int index = i * SAMPLE_RATE / format.samplesPerSec;
  205. float fdata = (float)sdata[i] / 32767.0f;
  206. min[index] = __min( min[index], fdata );
  207. max[index] = __max( max[index], fdata );
  208. }
  209. } else {
  210. unsigned int j = 0;
  211. for ( unsigned int i = 0; i < numSamples; i++ ) {
  212. unsigned int index = i * SAMPLE_RATE / format.samplesPerSec;
  213. for ( unsigned int c = 0; c < format.numChannels; c++ ) {
  214. float fdata = (float)sdata[j++] / 32767.0f;
  215. min[index] = __min( min[index], fdata );
  216. max[index] = __max( max[index], fdata );
  217. }
  218. }
  219. }
  220. } else {
  221. unsigned char * bdata = (unsigned char *)inputData;
  222. if ( format.numChannels == 1 ) {
  223. for ( unsigned int i = 0; i < numSamples; i++ ) {
  224. unsigned int index = i * SAMPLE_RATE / format.samplesPerSec;
  225. float fdata = ( (float)bdata[i] - 128.0f ) / 127.0f;
  226. min[index] = __min( min[index], fdata );
  227. max[index] = __max( max[index], fdata );
  228. }
  229. } else {
  230. unsigned int j = 0;
  231. for ( unsigned int i = 0; i < numSamples; i++ ) {
  232. unsigned int index = i * SAMPLE_RATE / format.samplesPerSec;
  233. for ( unsigned int c = 0; c < format.numChannels; c++ ) {
  234. float fdata = ( (float)bdata[j++] - 128.0f ) / 127.0f;
  235. min[index] = __min( min[index], fdata );
  236. max[index] = __max( max[index], fdata );
  237. }
  238. }
  239. }
  240. }
  241. for ( int i = 0; i < numOutputSamples; i++ ) {
  242. float amp = atan( max[i] - min[i] ) / 0.7853981633974483f;
  243. int o = (int)( amp * 255.0f );
  244. if ( o > 255 ) {
  245. outputData[i] = 255;
  246. } else if ( o < 0 ) {
  247. outputData[i] = 0;
  248. } else {
  249. outputData[i] = (unsigned char)o;
  250. }
  251. }
  252. fwrite( outputData, numOutputSamples, 1, out );
  253. free( inputData );
  254. free( min );
  255. free( max );
  256. free( outputData );
  257. printf( "Success\n" );
  258. return true;
  259. }
  260. int main(int argc, char * argv[]) {
  261. if ( argc < 2 ) {
  262. printf( "Usage: %s <wav>\n", argv[0] );
  263. return E_ARGS;
  264. }
  265. const char * inputFileName = argv[1];
  266. printf( "Processing %s: ", inputFileName );
  267. FILE * in = NULL;
  268. if ( fopen_s( &in, inputFileName, "rb" ) != 0 ) {
  269. printf( "Could not open input file\n" );
  270. return E_OPEN_IN;
  271. }
  272. char outputFileName[1024] = {0};
  273. if ( strcpy_s( outputFileName, inputFileName ) != 0 ) {
  274. printf( "Filename too long\n" );
  275. return E_ARGS;
  276. }
  277. char * dot = strrchr( outputFileName, '.' );
  278. if ( dot == NULL ) {
  279. dot = outputFileName + strlen( outputFileName );
  280. }
  281. if ( strcpy_s( dot, sizeof( outputFileName ) - ( dot - outputFileName ), ".amp" ) != 0 ) {
  282. printf( "Filename too long\n" );
  283. return E_ARGS;
  284. }
  285. FILE * out = NULL;
  286. if ( fopen_s( &out, outputFileName, "wb" ) != 0 ) {
  287. printf( "Could not open output file %s\n", outputFileName );
  288. return E_OPEN_OUT;
  289. }
  290. bool success = Process( in, out );
  291. fclose( in );
  292. fclose( out );
  293. if ( !success ) {
  294. remove( outputFileName );
  295. return E_PROCESSING;
  296. }
  297. return E_OK;
  298. }