PS.C 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. Copyright (C) 1994-1995 Apogee Software, Ltd.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. /**********************************************************************
  16. module: PS.C
  17. author: James R. Dose
  18. date: December 12, 1995
  19. Simple sound player.
  20. (c) Copyright 1995 James R. Dose. All Rights Reserved.
  21. **********************************************************************/
  22. #include <conio.h>
  23. #include <dos.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include "fx_man.h"
  28. /*---------------------------------------------------------------------
  29. Function prototypes
  30. ---------------------------------------------------------------------*/
  31. char *LoadFile( char *filename, int *length );
  32. char *GetUserText( const char *parameter );
  33. int CheckUserParm( const char *parameter );
  34. void DefaultExtension( char *path, char *extension );
  35. #define TRUE ( 1 == 1 )
  36. #define FALSE ( !TRUE )
  37. #define NUMCARDS 8
  38. char *SoundCardNames[] =
  39. {
  40. "Sound Blaster", "Awe 32", "Pro AudioSpectrum",
  41. "Sound Man 16", "Ensoniq SoundScape", "Gravis UltraSound",
  42. "Disney SoundSource", "Tandy SoundSource"
  43. };
  44. int SoundCardNums[] =
  45. {
  46. SoundBlaster, Awe32, ProAudioSpectrum, SoundMan16,
  47. SoundScape, UltraSound, SoundSource, TandySoundSource
  48. };
  49. /*---------------------------------------------------------------------
  50. Function: main
  51. Sets up sound cards, calls the demo, and then cleans up.
  52. ---------------------------------------------------------------------*/
  53. void main
  54. (
  55. int argc,
  56. char *argv[]
  57. )
  58. {
  59. int card;
  60. int voices;
  61. int rate;
  62. int bits;
  63. int channels;
  64. int reverb;
  65. int status;
  66. int length;
  67. int voice;
  68. fx_device device;
  69. char *SoundPtr = NULL;
  70. char *ptr;
  71. char filename[ 128 ];
  72. char ch;
  73. printf( "\nPS Version 1.0 by Jim Dose\n" );
  74. if ( ( CheckUserParm( "?" ) ) || ( argc < 2 ) )
  75. {
  76. int index;
  77. printf(
  78. "Usage: PS [ soundfile ] CARD=[ card # ] VOICES=[ # of voices ]\n"
  79. " BITS=[ 8 or 16 ] [MONO/STEREO] RATE=[ mixing rate ]\n"
  80. " REVERB=[ reverb amount]\n\n"
  81. " sound card # = \n" );
  82. for( index = 0; index < NUMCARDS; index++ )
  83. {
  84. printf( " %d : %s\n", index, SoundCardNames[ index ] );
  85. }
  86. printf( "\nDefault: PS [ soundfile ] CARD=0 VOICES=4 "
  87. "BITS=8 MONO RATE=11000\n\n" );
  88. exit( 0 );
  89. }
  90. // Default is Sound Blaster
  91. card = 0;
  92. voices = 4;
  93. bits = 8;
  94. channels = 1;
  95. reverb = 0;
  96. rate = 11000;
  97. ptr = GetUserText( "VOICES" );
  98. if ( ptr != NULL )
  99. {
  100. sscanf( ptr, "%d", &voices );
  101. }
  102. ptr = GetUserText( "BITS" );
  103. if ( ptr != NULL )
  104. {
  105. sscanf( ptr, "%d", &bits );
  106. }
  107. ptr = GetUserText( "RATE" );
  108. if ( ptr != NULL )
  109. {
  110. sscanf( ptr, "%d", &rate );
  111. }
  112. ptr = GetUserText( "REVERB" );
  113. if ( ptr != NULL )
  114. {
  115. sscanf( ptr, "%d", &reverb );
  116. }
  117. ptr = GetUserText( "MONO" );
  118. if ( ptr != NULL )
  119. {
  120. channels = 1;
  121. }
  122. ptr = GetUserText( "STEREO" );
  123. if ( ptr != NULL )
  124. {
  125. channels = 2;
  126. }
  127. ptr = GetUserText( "CARD" );
  128. if ( ptr != NULL )
  129. {
  130. sscanf( ptr, "%d", &card );
  131. }
  132. if ( ( card < 0 ) || ( card >= NUMCARDS ) )
  133. {
  134. printf( "Value out of range for sound card #: %d\n", card );
  135. exit( 1 );
  136. }
  137. strcpy( filename, argv[ 1 ] );
  138. DefaultExtension( filename, ".wav" );
  139. SoundPtr = LoadFile( filename, &length );
  140. if ( !SoundPtr )
  141. {
  142. strcpy( filename, argv[ 1 ] );
  143. DefaultExtension( filename, ".voc" );
  144. SoundPtr = LoadFile( filename, &length );
  145. if ( !SoundPtr )
  146. {
  147. strcpy( filename, argv[ 1 ] );
  148. SoundPtr = LoadFile( filename, &length );
  149. if ( !SoundPtr )
  150. {
  151. printf( "Cannot open '%s' for read.\n", argv[ 1 ] );
  152. exit( 1 );
  153. }
  154. }
  155. }
  156. status = FX_SetupCard( card, &device );
  157. if ( status != FX_Ok )
  158. {
  159. printf( "%s\n", FX_ErrorString( status ) );
  160. exit( 1 );
  161. }
  162. status = FX_Init( card, voices, channels, bits, rate );
  163. if ( status != FX_Ok )
  164. {
  165. printf( "%s\n", FX_ErrorString( status ) );
  166. exit( 1 );
  167. }
  168. FX_SetReverb( reverb );
  169. FX_SetVolume( 255 );
  170. printf( "Playing file '%s'.\n\n", filename );
  171. printf( "Press any key to play the sound.\n"
  172. "Press ESCape to end.\n\n" );
  173. ch = 0;
  174. while( ch != 27 )
  175. {
  176. if ( stricmp( &filename[ strlen( filename ) - 3 ], "WAV" ) == 0 )
  177. {
  178. voice = FX_PlayWAV( SoundPtr, 0, 255, 255, 255, 0, 0 );
  179. }
  180. else if ( stricmp( &filename[ strlen( filename ) - 3 ], "VOC" ) == 0 )
  181. {
  182. voice = FX_PlayVOC( SoundPtr, 0, 255, 255, 255, 0, 0 );
  183. }
  184. else
  185. {
  186. voice = FX_PlayRaw( SoundPtr, length, rate, 0, 255, 255, 255, 0, 0 );
  187. }
  188. if ( voice < FX_Ok )
  189. {
  190. printf( "Sound error - %s\n", FX_ErrorString( status ) );
  191. }
  192. ch = getch();
  193. }
  194. FX_StopAllSounds();
  195. free( SoundPtr );
  196. FX_Shutdown();
  197. printf( "\n" );
  198. }
  199. /*---------------------------------------------------------------------
  200. Function: LoadFile
  201. Loads a file from disk.
  202. ---------------------------------------------------------------------*/
  203. char *LoadFile
  204. (
  205. char *filename,
  206. int *length
  207. )
  208. {
  209. FILE *in;
  210. long size;
  211. char *ptr;
  212. if ( ( in = fopen( filename, "rb" ) ) == NULL )
  213. {
  214. return NULL;
  215. }
  216. fseek( in, 0, SEEK_END );
  217. size = ftell( in );
  218. fseek( in, 0, SEEK_SET );
  219. ptr = ( char * )malloc( size );
  220. if ( ptr == NULL )
  221. {
  222. printf( "Out of memory while reading '%s'.\n", filename );
  223. exit( 1 );
  224. }
  225. if ( fread( ptr, size, 1, in ) != 1 )
  226. {
  227. printf( "Unexpected end of file while reading '%s'.\n", filename );
  228. exit(1);
  229. }
  230. fclose( in );
  231. *length = size;
  232. return( ptr );
  233. }
  234. /*---------------------------------------------------------------------
  235. Function: GetUserText
  236. Checks if the specified string is present in the command line
  237. and returns a pointer to the text following it.
  238. ---------------------------------------------------------------------*/
  239. char *GetUserText
  240. (
  241. const char *parameter
  242. )
  243. {
  244. int i;
  245. int length;
  246. char *text;
  247. char *ptr;
  248. extern int _argc;
  249. extern char **_argv;
  250. text = NULL;
  251. length = strlen( parameter );
  252. i = 1;
  253. while( i < _argc )
  254. {
  255. ptr = _argv[ i ];
  256. if ( ( strnicmp( parameter, ptr, length ) == 0 ) &&
  257. ( *( ptr + length ) == '=' ) )
  258. {
  259. text = ptr + length + 1;
  260. break;
  261. }
  262. i++;
  263. }
  264. return( text );
  265. }
  266. /*---------------------------------------------------------------------
  267. Function: CheckUserParm
  268. Checks if the specified string is present in the command line.
  269. ---------------------------------------------------------------------*/
  270. int CheckUserParm
  271. (
  272. const char *parameter
  273. )
  274. {
  275. int i;
  276. int found;
  277. char *ptr;
  278. extern int _argc;
  279. extern char **_argv;
  280. found = FALSE;
  281. i = 1;
  282. while( i < _argc )
  283. {
  284. ptr = _argv[ i ];
  285. // Only check parameters preceded by - or /
  286. if ( ( *ptr == '-' ) || ( *ptr == '/' ) )
  287. {
  288. ptr++;
  289. if ( stricmp( parameter, ptr ) == 0 )
  290. {
  291. found = TRUE;
  292. break;
  293. }
  294. }
  295. i++;
  296. }
  297. return( found );
  298. }
  299. /*---------------------------------------------------------------------
  300. Function: DefaultExtension
  301. Checks if the specified filename contains an extension and adds
  302. one if it doesn't.
  303. ---------------------------------------------------------------------*/
  304. void DefaultExtension
  305. (
  306. char *path,
  307. char *extension
  308. )
  309. {
  310. char *src;
  311. //
  312. // if path doesn't have a .EXT, append extension
  313. // (extension should include the .)
  314. //
  315. src = path + strlen( path ) - 1;
  316. while( ( *src != '\\' ) && ( src != path ) )
  317. {
  318. if ( *src == '.' )
  319. {
  320. // it has an extension
  321. return;
  322. }
  323. src--;
  324. }
  325. strcat( path, extension );
  326. }