--- SIDPlayer/src/main_sdl.cpp 2001/04/01 12:13:49 1.8 +++ SIDPlayer/src/main_sdl.cpp 2001/04/01 17:04:43 1.9 @@ -21,8 +21,11 @@ #include "sys.h" #include +#include + #include #include +#include #include "main.h" #include "prefs.h" @@ -66,7 +69,6 @@ int main(int argc, char **argv) } atexit(quit); InitAll(argc, argv); - int32 selected_speed = PrefsFindInt32("speed"); // Parse non-option arguments const char *file_name = NULL; @@ -98,6 +100,7 @@ int main(int argc, char **argv) if (song > number_of_songs) song = number_of_songs; SelectSong(song - 1); + SIDAdjustSpeed(PrefsFindInt32("speed")); // SelectSong resets this to 100% } // Print file information @@ -106,14 +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 - SIDAdjustSpeed(selected_speed); // SelectSong resets this to 100% - 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