--- SIDPlayer/src/main_sdl.cpp 2000/10/10 21:47:47 1.2 +++ SIDPlayer/src/main_sdl.cpp 2001/04/01 17:04:43 1.9 @@ -1,7 +1,7 @@ /* * main_sdl.cpp - SIDPlayer SDL main program * - * SIDPlayer (C) Copyright 1996-2000 Christian Bauer + * SIDPlayer (C) Copyright 1996-2001 Christian Bauer * * 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 @@ -21,10 +21,15 @@ #include "sys.h" #include +#include + #include #include +#include #include "main.h" +#include "prefs.h" +#include "sid.h" /* @@ -33,7 +38,8 @@ static void usage(const char *prg_name) { - printf("\nUsage: %s FILE [song_number]]\n", prg_name); + printf("Usage: %s [OPTION...] FILE [song_number]\n", prg_name); + PrefsPrintUsage(); exit(0); } @@ -48,7 +54,7 @@ int main(int argc, char **argv) // Print banner printf( PACKAGE " Version " VERSION "\n\n" - "Copyright (C) 1996-2000 Christian Bauer\n" + "Copyright (C) 1996-2001 Christian Bauer\n" "E-mail: Christian.Bauer@uni-mainz.de\n" "http://www.uni-mainz.de/~bauec002/\n\n" "This is free software with ABSOLUTELY NO WARRANTY.\n" @@ -56,25 +62,36 @@ int main(int argc, char **argv) "For details, see the file COPYING.\n\n" ); - // Parse arguments - if (argc < 2) - usage(argv[0]); - char *file_name = argv[1]; - int song = 0; - if (argc == 3) - song = atoi(argv[2]); - // Initialize everything if (SDL_Init(SDL_INIT_AUDIO) < 0) { fprintf(stderr, "Couldn't initialize SDL (%s)\n", SDL_GetError()); exit(1); } atexit(quit); - InitAll(); + InitAll(argc, argv); + + // Parse non-option arguments + const char *file_name = NULL; + int song = 0; + for (int i=1; i number_of_songs) song = number_of_songs; SelectSong(song - 1); + SIDAdjustSpeed(PrefsFindInt32("speed")); // SelectSong resets this to 100% } // Print file information @@ -91,13 +109,73 @@ int main(int argc, char **argv) printf("Copyright : %s\n\n", copyright_info); printf("Playing song %d/%d\n", current_song + 1, number_of_songs); - // Start replay and enter main loop - SDL_PauseAudio(false); - while (true) { - SDL_Event e; - if (SDL_WaitEvent(&e)) { - if (e.type == SDL_QUIT) - break; + // Replay or output to file? + const char *outfile = PrefsFindString("outfile"); + if (outfile) { + + // Open file + SDL_RWops *f = SDL_RWFromFile(outfile, "wb"); + if (f == NULL) { + fprintf(stderr, "Can't open '%s' for writing (%s)\n", outfile, strerror(errno)); + exit(1); + } + + // Get format information + int32 channels = (PrefsFindBool("stereo") ? 2 : 1); + int32 bits = (PrefsFindBool("audio16bit") ? 16 : 8); + int32 rate = PrefsFindInt32("samplerate"); + int32 time = PrefsFindInt32("time"); + int32 bytes_per_frame = channels * (bits / 8); + int32 bytes_per_second = rate * bytes_per_frame; + int32 data_length = time * bytes_per_second; + + // Write header + SDL_WriteLE32(f, 0x46464952); // "RIFF" + SDL_WriteLE32(f, 36 + data_length); + SDL_WriteLE32(f, 0x45564157); // "WAVE" + SDL_WriteLE32(f, 0x20746D66); // "fmt " + SDL_WriteLE32(f, 16); + SDL_WriteLE16(f, 1); + SDL_WriteLE16(f, channels); + SDL_WriteLE32(f, rate); + SDL_WriteLE32(f, bytes_per_second); + SDL_WriteLE16(f, bytes_per_frame); + SDL_WriteLE16(f, bits); + SDL_WriteLE32(f, 0x61746164); // "data" + SDL_WriteLE32(f, data_length); + + // Calculate sound and write to file + uint8 *buf = new uint8[bytes_per_second]; + for (int i=0; i 8) { + // WAV file is little-endian, swap audio data + for (int b=0; b