123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538 |
- /*
- Copyright (C) 1994-1995 Apogee Software, Ltd.
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License
- as published by the Free Software Foundation; either version 2
- of the License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- See the GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
- /**********************************************************************
- module: PM.C
- author: James R. Dose
- date: January 16, 1994
- Simple music player.
- (c) Copyright 1994 James R. Dose. All Rights Reserved.
- **********************************************************************/
- #include <conio.h>
- #include <dos.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include "music.h"
- /*---------------------------------------------------------------------
- Function prototypes
- ---------------------------------------------------------------------*/
- void LoadTimbres( char *timbrefile );
- char *LoadMidi( char *filename );
- char *GetUserText( const char *parameter );
- int CheckUserParm( const char *parameter );
- void DefaultExtension( char *path, char *extension );
- void TurnOffTextCursor( void );
- void TurnOnTextCursor( void );
- #define TRUE ( 1 == 1 )
- #define FALSE ( !TRUE )
- #define NUMCARDS 10
- char *SoundCardNames[] =
- {
- "General Midi", "Sound Canvas", "Awe 32", "WaveBlaster",
- "SoundBlaster", "Pro AudioSpectrum", "Sound Man 16", "Adlib",
- "Ensoniq SoundScape", "Gravis UltraSound"
- };
- int SoundCardNums[] =
- {
- GenMidi, SoundCanvas, Awe32, WaveBlaster,
- SoundBlaster, ProAudioSpectrum, SoundMan16,
- Adlib, SoundScape, UltraSound
- };
- /*---------------------------------------------------------------------
- Function: main
- Sets up sound cards, calls the demo, and then cleans up.
- ---------------------------------------------------------------------*/
- void main
- (
- int argc,
- char *argv[]
- )
- {
- int card;
- int address;
- int status;
- char *SongPtr = NULL;
- char *ptr;
- char filename[ 128 ];
- char timbrefile[ 128 ];
- int gotopos = 0;
- int measure = 0;
- int beat = 0;
- int tick = 0;
- int time = 0;
- printf( "\nPM EMIDI Music Player Version 1.21 Copyright (c) 1996 by Jim Dose\n" );
- if ( ( CheckUserParm( "?" ) ) || ( argc < 2 ) )
- {
- int index;
- printf(
- "Usage: PM [ midifile ] CARD=[ card number ] MPU=[ port address in hex ]\n"
- " TIMBRE=[ timbre file ] TIME=[minutes:seconds:milliseconds]\n"
- " POSITION=[measure:beat:tick]\n\n"
- " card number = \n" );
- for( index = 0; index < NUMCARDS; index++ )
- {
- printf( " %d : %s\n", index, SoundCardNames[ index ] );
- }
- printf( "\n" );
- exit( 0 );
- }
- // Default is GenMidi
- card = 0;
- address = 0x330;
- ptr = getenv( "PM" );
- if ( ptr != NULL )
- {
- sscanf( ptr, "%d,%x", &card, &address );
- }
- ptr = GetUserText( "MPU" );
- if ( ptr != NULL )
- {
- sscanf( ptr, "%x", &address );
- }
- ptr = GetUserText( "TIMBRE" );
- if ( ptr != NULL )
- {
- sscanf( ptr, "%s", timbrefile );
- LoadTimbres( timbrefile );
- }
- ptr = GetUserText( "POSITION" );
- if ( ptr != NULL )
- {
- gotopos = 1;
- sscanf( ptr, "%d:%d:%d", &measure, &beat, &tick );
- }
- ptr = GetUserText( "TIME" );
- if ( ptr != NULL )
- {
- int minutes = 0;
- int seconds = 0;
- int milli = 0;
- gotopos = 2;
- sscanf( ptr, "%d:%d:%d", &minutes, &seconds, &milli );
- time = minutes * ( 60 * 1000 ) + seconds * 1000 + milli;
- }
- ptr = GetUserText( "CARD" );
- if ( ptr != NULL )
- {
- sscanf( ptr, "%d", &card );
- }
- if ( ( card < 0 ) || ( card >= NUMCARDS ) )
- {
- printf( "Value out of range for CARD: %d\n", card );
- }
- status = MUSIC_Init( SoundCardNums[ card ], address );
- if ( status != MUSIC_Ok )
- {
- printf( "Error - %s\n", MUSIC_ErrorString( status ) );
- exit( 1 );
- }
- strcpy( filename, argv[ 1 ] );
- DefaultExtension( filename, ".mid" );
- SongPtr = LoadMidi( filename );
- MUSIC_SetVolume( 255 );
- status = MUSIC_PlaySong( SongPtr, MUSIC_LoopSong );
- if ( status != MUSIC_Ok )
- {
- printf( "Error - %s\n", MUSIC_ErrorString( status ) );
- }
- else
- {
- char ch;
- songposition pos;
- if ( gotopos == 1 )
- {
- MUSIC_SetSongPosition( measure, beat, tick );
- }
- else if ( gotopos == 2 )
- {
- MUSIC_SetSongTime( time );
- }
- printf( "Playing file '%s'.\n\n", filename );
- printf( "Press F to advance one measure.\n"
- "Press R to rewind one measure.\n"
- "Press G to go to the selected position.\n"
- "Press ESCape to end.\n\n" );
- MUSIC_GetSongLength( &pos );
- printf( "Song length: time (m:s:ms) = %d:%d:%d, "
- "(measure:beat:tick) = %d:%d:%d\n\n",
- pos.milliseconds / (60*1000),
- ( pos.milliseconds / 1000 ) % 60,
- pos.milliseconds % 1000,
- pos.measure, pos.beat, pos.tick );
- ch = 0;
- TurnOffTextCursor();
- while( ch != 27 )
- {
- MUSIC_GetSongPosition( &pos );
- printf( "time (m:s:ms) = %d:%d:%d \t(measure:beat:tick) = %d:%d:%d \r",
- pos.milliseconds / (60*1000),
- ( pos.milliseconds / 1000 ) % 60,
- pos.milliseconds % 1000,
- pos.measure, pos.beat, pos.tick );
- fflush( stdout );
- if ( kbhit() )
- {
- ch = getch();
- if ( ( ch == 'G' ) || ( ch == 'g' ) )
- {
- if ( gotopos == 1 )
- {
- MUSIC_SetSongPosition( measure, beat, tick );
- }
- else
- {
- MUSIC_SetSongTime( time );
- }
- ch = 0;
- while( kbhit() )
- {
- getch();
- }
- }
- if ( ( ch == 'F' ) || ( ch == 'f' ) )
- {
- MUSIC_SetSongPosition( pos.measure + 1, 1, 0 );
- ch = 0;
- while( kbhit() )
- {
- getch();
- }
- }
- if ( ( ch == 'R' ) || ( ch == 'r' ) )
- {
- MUSIC_SetSongPosition( pos.measure - 1, 1, 0 );
- ch = 0;
- while( kbhit() )
- {
- getch();
- }
- }
- }
- }
- TurnOnTextCursor();
- MUSIC_StopSong();
- }
- free( SongPtr );
- MUSIC_Shutdown();
- printf( "\n" );
- }
- /*---------------------------------------------------------------------
- Function: LoadTimbres
- Loads the instruments from disk
- ---------------------------------------------------------------------*/
- void LoadTimbres
- (
- char *timbrefile
- )
- {
- FILE *in;
- long size;
- char *TimbrePtr;
- if ( ( in = fopen( timbrefile, "rb" ) ) == NULL )
- {
- printf( "Cannot open '%s' for read.\n", timbrefile );
- exit( 1 );
- }
- fseek( in, 0, SEEK_END );
- size = ftell( in );
- fseek( in, 0, SEEK_SET );
- TimbrePtr = ( char * )malloc( size );
- if ( TimbrePtr == NULL )
- {
- printf( "Out of memory while reading '%s'.\n", timbrefile );
- exit( 1 );
- }
- if ( fread( TimbrePtr, size, 1, in ) != 1 )
- {
- printf( "Unexpected end of file while reading '%s'.\n", timbrefile );
- exit(1);
- }
- fclose( in );
- MUSIC_RegisterTimbreBank( TimbrePtr );
- }
- /*---------------------------------------------------------------------
- Function: LoadMidi
- Loads the midi file from disk.
- ---------------------------------------------------------------------*/
- char *LoadMidi
- (
- char *filename
- )
- {
- FILE *in;
- long size;
- char *MidiPtr;
- if ( ( in = fopen( filename, "rb" ) ) == NULL )
- {
- printf( "Cannot open '%s' for read.\n", filename );
- exit( 1 );
- }
- fseek( in, 0, SEEK_END );
- size = ftell( in );
- fseek( in, 0, SEEK_SET );
- MidiPtr = ( char * )malloc( size );
- if ( MidiPtr == NULL )
- {
- printf( "Out of memory while reading '%s'.\n", filename );
- exit( 1 );
- }
- if ( fread( MidiPtr, size, 1, in ) != 1 )
- {
- printf( "Unexpected end of file while reading '%s'.\n", filename );
- exit(1);
- }
- fclose( in );
- return( MidiPtr );
- }
- /*---------------------------------------------------------------------
- Function: GetUserText
- Checks if the specified string is present in the command line
- and returns a pointer to the text following it.
- ---------------------------------------------------------------------*/
- char *GetUserText
- (
- const char *parameter
- )
- {
- int i;
- int length;
- char *text;
- char *ptr;
- extern int _argc;
- extern char **_argv;
- text = NULL;
- length = strlen( parameter );
- i = 1;
- while( i < _argc )
- {
- ptr = _argv[ i ];
- if ( ( strnicmp( parameter, ptr, length ) == 0 ) &&
- ( *( ptr + length ) == '=' ) )
- {
- text = ptr + length + 1;
- break;
- }
- i++;
- }
- return( text );
- }
- /*---------------------------------------------------------------------
- Function: CheckUserParm
- Checks if the specified string is present in the command line.
- ---------------------------------------------------------------------*/
- int CheckUserParm
- (
- const char *parameter
- )
- {
- int i;
- int found;
- char *ptr;
- extern int _argc;
- extern char **_argv;
- found = FALSE;
- i = 1;
- while( i < _argc )
- {
- ptr = _argv[ i ];
- // Only check parameters preceded by - or /
- if ( ( *ptr == '-' ) || ( *ptr == '/' ) )
- {
- ptr++;
- if ( stricmp( parameter, ptr ) == 0 )
- {
- found = TRUE;
- break;
- }
- }
- i++;
- }
- return( found );
- }
- /*---------------------------------------------------------------------
- Function: DefaultExtension
- Checks if the specified filename contains an extension and adds
- one if it doesn't.
- ---------------------------------------------------------------------*/
- void DefaultExtension
- (
- char *path,
- char *extension
- )
- {
- char *src;
- //
- // if path doesn't have a .EXT, append extension
- // (extension should include the .)
- //
- src = path + strlen( path ) - 1;
- while( ( *src != '\\' ) && ( src != path ) )
- {
- if ( *src == '.' )
- {
- // it has an extension
- return;
- }
- src--;
- }
- strcat( path, extension );
- }
- /*---------------------------------------------------------------------
- Function: TurnOffTextCursor
- Turns the cursor off.
- ---------------------------------------------------------------------*/
- void TurnOffTextCursor
- (
- void
- )
- {
- union REGS regs;
- regs.w.ax = 0x0100;
- regs.w.cx = 0x2000;
- #ifdef __FLAT__
- int386(0x10,®s,®s);
- #else
- int86(0x10,®s,®s);
- #endif
- }
- /*---------------------------------------------------------------------
- Function: TurnOnTextCursor
- Turns the cursor on.
- ---------------------------------------------------------------------*/
- void TurnOnTextCursor
- (
- void
- )
- {
- union REGS regs;
- regs.w.ax = 0x0100;
- regs.w.cx = 0x0708;
- #ifdef __FLAT__
- int386(0x10,®s,®s);
- #else
- int86(0x10,®s,®s);
- #endif
- }
|