ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SIDPlayer/src/main_sdl.cpp
(Generate patch)

Comparing SIDPlayer/src/main_sdl.cpp (file contents):
Revision 1.4 by cebix, 2001-01-16T14:12:54Z vs.
Revision 1.10 by cebix, 2001-05-22T20:01:34Z

# Line 1 | Line 1
1   /*
2   *  main_sdl.cpp - SIDPlayer SDL main program
3   *
4 < *  SIDPlayer (C) Copyright 1996-2000 Christian Bauer
4 > *  SIDPlayer (C) Copyright 1996-2001 Christian Bauer
5   *
6   *  This program is free software; you can redistribute it and/or modify
7   *  it under the terms of the GNU General Public License as published by
# Line 21 | Line 21
21   #include "sys.h"
22  
23   #include <SDL.h>
24 + #include <SDL/SDL_endian.h>
25 +
26   #include <stdio.h>
27   #include <stdlib.h>
28 + #include <errno.h>
29  
30   #include "main.h"
31   #include "prefs.h"
# Line 35 | Line 38
38  
39   static void usage(const char *prg_name)
40   {
41 <        printf("\nUsage: %s [options] FILE [song_number]]\n", prg_name);
41 >        printf("Usage: %s [OPTION...] FILE [song_number]\n", prg_name);
42 >        PrefsPrintUsage();
43          exit(0);
44   }
45  
# Line 50 | Line 54 | int main(int argc, char **argv)
54          // Print banner
55          printf(
56                  PACKAGE " Version " VERSION "\n\n"
57 <                "Copyright (C) 1996-2000 Christian Bauer\n"
57 >                "Copyright (C) 1996-2001 Christian Bauer\n"
58                  "E-mail: Christian.Bauer@uni-mainz.de\n"
59                  "http://www.uni-mainz.de/~bauec002/\n\n"
60                  "This is free software with ABSOLUTELY NO WARRANTY.\n"
# Line 58 | Line 62 | int main(int argc, char **argv)
62                  "For details, see the file COPYING.\n\n"
63          );
64  
61        // Parse arguments
62        if (argc < 2)
63                usage(argv[0]);
64        char *file_name = argv[argc - 1];
65        int song = 0;
66        if (argc >= 3) {
67                bool only_numbers = true;
68                for (int i=0; i<strlen(file_name); i++)
69                        if (!isdigit(file_name[i])) {
70                                only_numbers = false;
71                                break;
72                        }
73                if (only_numbers) {
74                        song = atoi(file_name);
75                        file_name = argv[argc - 2];
76                }
77        }
78
65          // Initialize everything
66          if (SDL_Init(SDL_INIT_AUDIO) < 0) {
67                  fprintf(stderr, "Couldn't initialize SDL (%s)\n", SDL_GetError());
# Line 83 | Line 69 | int main(int argc, char **argv)
69          }
70          atexit(quit);
71          InitAll(argc, argv);
72 +        int32 speed = PrefsFindInt32("speed");
73 +
74 +        // Parse non-option arguments
75 +        const char *file_name = NULL;
76 +        int song = 0;
77 +        for (int i=1; i<argc; i++) {
78 +                if (strcmp(argv[i], "--help") == 0)
79 +                        usage(argv[0]);
80 +                else if (argv[i][0] == '-') {
81 +                        fprintf(stderr, "Unrecognized option '%s'\n", argv[i]);
82 +                        usage(argv[0]);
83 +                } else {
84 +                        if (file_name == NULL)
85 +                                file_name = argv[i];  // First non-option argument is file name
86 +                        else
87 +                                song = atoi(argv[i]); // Second non-option argument is song number
88 +                }
89 +        }
90 +        if (file_name == NULL)
91 +                usage(argv[0]);
92  
93          // Load given PSID file
94          if (!LoadPSIDFile(file_name)) {
# Line 97 | Line 103 | int main(int argc, char **argv)
103                  SelectSong(song - 1);
104          }
105  
106 +        SIDAdjustSpeed(speed); // SelectSong and LoadPSIDFile() reset this to 100%
107 +
108          // Print file information
109          printf("Module Name: %s\n", module_name);
110          printf("Author     : %s\n", author_name);
111          printf("Copyright  : %s\n\n", copyright_info);
112          printf("Playing song %d/%d\n", current_song + 1, number_of_songs);
113  
114 <        // Start replay and enter main loop
115 <        SIDAdjustSpeed(PrefsFindInt32("speed"));
116 <        SDL_PauseAudio(false);
117 <        while (true) {
118 <                SDL_Event e;
119 <                if (SDL_WaitEvent(&e)) {
120 <                        if (e.type == SDL_QUIT)
121 <                                break;
114 >        // Replay or output to file?
115 >        const char *outfile = PrefsFindString("outfile");
116 >        if (outfile) {
117 >
118 >                // Open file
119 >                SDL_RWops *f = SDL_RWFromFile(outfile, "wb");
120 >                if (f == NULL) {
121 >                        fprintf(stderr, "Can't open '%s' for writing (%s)\n", outfile, strerror(errno));
122 >                        exit(1);
123 >                }
124 >
125 >                // Get format information
126 >                int32 channels = (PrefsFindBool("stereo") ? 2 : 1);
127 >                int32 bits = (PrefsFindBool("audio16bit") ? 16 : 8);
128 >                int32 rate = PrefsFindInt32("samplerate");
129 >                int32 time = PrefsFindInt32("time");
130 >                int32 bytes_per_frame = channels * (bits / 8);
131 >                int32 bytes_per_second = rate * bytes_per_frame;
132 >                int32 data_length = time * bytes_per_second;
133 >
134 >                // Write header
135 >                SDL_WriteLE32(f, 0x46464952); // "RIFF"
136 >                SDL_WriteLE32(f, 36 + data_length);
137 >                SDL_WriteLE32(f, 0x45564157); // "WAVE"
138 >                SDL_WriteLE32(f, 0x20746D66); // "fmt "
139 >                SDL_WriteLE32(f, 16);
140 >                SDL_WriteLE16(f, 1);
141 >                SDL_WriteLE16(f, channels);
142 >                SDL_WriteLE32(f, rate);
143 >                SDL_WriteLE32(f, bytes_per_second);
144 >                SDL_WriteLE16(f, bytes_per_frame);
145 >                SDL_WriteLE16(f, bits);
146 >                SDL_WriteLE32(f, 0x61746164); // "data"
147 >                SDL_WriteLE32(f, data_length);
148 >
149 >                // Calculate sound and write to file
150 >                uint8 *buf = new uint8[bytes_per_second];
151 >                for (int i=0; i<time; i++) {
152 >                        SIDCalcBuffer(buf, bytes_per_second);
153 > #if SDL_BYTEORDER == SDL_BIG_ENDIAN
154 >                        if (bits > 8) {
155 >                                // WAV file is little-endian, swap audio data
156 >                                for (int b=0; b<bytes_per_second; b+=2) {
157 >                                        uint8 tmp = buf[b];
158 >                                        buf[b] = buf[b+1];
159 >                                        buf[b+1] = tmp;
160 >                                }
161 >                        }
162 > #endif
163 >                        SDL_RWwrite(f, buf, bytes_per_second, 1);
164 >                }
165 >                delete[] buf;
166 >
167 >                // Close file
168 >                SDL_RWclose(f);
169 >                printf("Output written to '%s'\n", outfile);
170 >
171 >        } else {
172 >
173 >                // Start replay and enter main loop
174 >                SDL_PauseAudio(false);
175 >                while (true) {
176 >                        SDL_Event e;
177 >                        if (SDL_WaitEvent(&e)) {
178 >                                if (e.type == SDL_QUIT)
179 >                                        break;
180 >                        }
181                  }
182          }
183  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines